blob: c23757fca381e28167ffca86bbe1de950621e961 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
Chris Wren282cfef2017-03-27 15:01:44 -040031import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050032import com.android.internal.notification.SystemNotificationChannels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070034import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070035import com.android.internal.os.TransferPipe;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060036import com.android.internal.util.DumpUtils;
satoke7c6998e2011-06-03 17:57:59 +090037import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import com.android.internal.view.IInputContext;
39import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.IInputMethodClient;
41import com.android.internal.view.IInputMethodManager;
42import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070043import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080045import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080046import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
satoke7c6998e2011-06-03 17:57:59 +090048import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090050import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Yohei Yukawad6475a62017-04-17 10:35:27 -070052import android.annotation.BinderThread;
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +090053import android.annotation.ColorInt;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070054import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070055import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070056import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080057import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080058import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070059import android.app.ActivityManagerInternal;
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -070060import android.app.ActivityThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070062import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090063import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090064import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090065import android.app.Notification;
66import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070067import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090068import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080070import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.ContentResolver;
72import android.content.Context;
73import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090075import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090077import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070079import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090080import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.content.pm.PackageManager;
82import android.content.pm.ResolveInfo;
83import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070084import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.content.res.Resources;
86import android.content.res.TypedArray;
87import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080088import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080089import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080090import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070091import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040093import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070094import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090095import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.Handler;
97import android.os.IBinder;
98import android.os.IInterface;
99import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -0700100import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -0700102import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800104import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.os.ServiceManager;
106import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900107import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800108import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.provider.Settings;
110import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900111import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700112import android.util.ArrayMap;
113import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700114import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900116import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900117import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.util.PrintWriterPrinter;
119import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900120import android.util.Slog;
121import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900122import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700124import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.view.LayoutInflater;
126import android.view.View;
127import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700128import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700130import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900131import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800133import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700134import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import android.view.inputmethod.InputMethod;
136import android.view.inputmethod.InputMethodInfo;
137import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700138import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900139import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900140import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900141import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900142import android.widget.CompoundButton;
143import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900144import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900145import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900146import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700147import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
satoke7c6998e2011-06-03 17:57:59 +0900149import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900151import java.io.FileInputStream;
152import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153import java.io.IOException;
154import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700155import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100156import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700157import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800158import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900160import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800161import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162import java.util.HashMap;
163import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800164import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800165import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800166import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168/**
169 * This class provides a system service that manages input methods.
170 */
171public class InputMethodManagerService extends IInputMethodManager.Stub
172 implements ServiceConnection, Handler.Callback {
173 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700174 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700175 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700177 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
178 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
179 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 static final int MSG_UNBIND_INPUT = 1000;
182 static final int MSG_BIND_INPUT = 1010;
183 static final int MSG_SHOW_SOFT_INPUT = 1020;
184 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700185 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 static final int MSG_ATTACH_TOKEN = 1040;
187 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
Yohei Yukawa33e81792015-11-17 21:14:42 -0800191 static final int MSG_UNBIND_CLIENT = 3000;
192 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700193 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700194 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900195 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800196 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800197 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800198
satok01038492012-04-09 21:08:27 +0900199 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
200
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700201 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
202
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700203 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
satokf9f01002011-05-19 21:31:50 +0900205 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
206
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900207 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900208 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900209
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700210 /**
211 * Binding flags for establishing connection to the {@link InputMethodService}.
212 */
213 private static final int IME_CONNECTION_BIND_FLAGS =
214 Context.BIND_AUTO_CREATE
215 | Context.BIND_NOT_VISIBLE
216 | Context.BIND_NOT_FOREGROUND
Yohei Yukawaad78a612017-08-04 01:57:27 -0700217 | Context.BIND_IMPORTANT_BACKGROUND;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700218
219 /**
220 * Binding flags used only while the {@link InputMethodService} is showing window.
221 */
222 private static final int IME_VISIBLE_BIND_FLAGS =
223 Context.BIND_AUTO_CREATE
224 | Context.BIND_TREAT_LIKE_ACTIVITY
Yohei Yukawaad78a612017-08-04 01:57:27 -0700225 | Context.BIND_FOREGROUND_SERVICE
226 | Context.BIND_SHOWING_UI;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700227
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700228 @Retention(SOURCE)
229 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
230 private @interface HardKeyboardBehavior {
231 int WIRELESS_AFFORDANCE = 0;
232 int WIRED_AFFORDANCE = 1;
233 }
satok4e4569d2010-11-19 18:45:53 +0900234
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900235 /**
236 * A protected broadcast intent action for internal use for {@link PendingIntent} in
237 * the notification.
238 */
239 private static final String ACTION_SHOW_INPUT_METHOD_PICKER =
240 "com.android.server.InputMethodManagerService.SHOW_INPUT_METHOD_PICKER";
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800243 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900245 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700248 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700250 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900251 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900252 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900253 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800254 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900256 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 // All known input methods. mMethodMap also serves as the global
259 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700260 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
261 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900262 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700263 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900264 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
Yohei Yukawae0733062017-02-09 22:49:35 -0800266 /**
267 * Tracks how many times {@link #mMethodMap} was updated.
268 */
269 @GuardedBy("mMethodMap")
270 private int mMethodMapUpdateCount = 0;
271
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700272 // Used to bring IME service up to visible adjustment while it is being shown.
273 final ServiceConnection mVisibleConnection = new ServiceConnection() {
274 @Override public void onServiceConnected(ComponentName name, IBinder service) {
275 }
276
277 @Override public void onServiceDisconnected(ComponentName name) {
278 }
279 };
280 boolean mVisibleBound = false;
281
satok7cfc0ed2011-06-20 21:29:36 +0900282 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700283 private NotificationManager mNotificationManager;
284 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700285 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400286 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700287 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900288 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900289 private boolean mNotificationShown;
290
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900291 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 final ClientState client;
293 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700294
295 IInputMethodSession session;
296 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 @Override
299 public String toString() {
300 return "SessionState{uid " + client.uid + " pid " + client.pid
301 + " method " + Integer.toHexString(
302 System.identityHashCode(method))
303 + " session " + Integer.toHexString(
304 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700305 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 + "}";
307 }
308
309 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700310 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 client = _client;
312 method = _method;
313 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700314 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
Jeff Brownc28867a2013-03-26 15:42:39 -0700318 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 final IInputMethodClient client;
320 final IInputContext inputContext;
321 final int uid;
322 final int pid;
323 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 boolean sessionRequested;
326 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 @Override
329 public String toString() {
330 return "ClientState{" + Integer.toHexString(
331 System.identityHashCode(this)) + " uid " + uid
332 + " pid " + pid + "}";
333 }
334
335 ClientState(IInputMethodClient _client, IInputContext _inputContext,
336 int _uid, int _pid) {
337 client = _client;
338 inputContext = _inputContext;
339 uid = _uid;
340 pid = _pid;
341 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
342 }
343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700345 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700348 * Set once the system is ready to run third party code.
349 */
350 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700352 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700353 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
354 * method. This is to be synchronized with the secure settings keyed with
355 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
356 *
357 * <p>This can be transiently {@code null} when the system is re-initializing input method
358 * settings, e.g., the system locale is just changed.</p>
359 *
360 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
361 * {@link InputMethodManagerService}.</p>
362 *
363 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700365 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * The current binding sequence number, incremented every time there is
370 * a new bind performed.
371 */
372 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
375 * The client that is currently bound to an input method.
376 */
377 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800380 * The last window token that we confirmed to be focused. This is always updated upon reports
381 * from the input method client. If the window state is already changed before the report is
382 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700383 */
384 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700386 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800387 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
388 *
389 * @see #mCurFocusedWindow
390 */
391 int mCurFocusedWindowSoftInputMode;
392
393 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800394 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
395 */
396 ClientState mCurFocusedWindowClient;
397
398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 * The input context last provided by the current client.
400 */
401 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700404 * The missing method flags for the input context last provided by the current client.
405 *
406 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
407 */
408 int mCurInputContextMissingMethods;
409
410 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 * The attributes last provided by the current client.
412 */
413 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700416 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700418 *
419 * <p>This can be {@code null} when no input method is connected.</p>
420 *
421 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700423 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 /**
satokab751aa2010-09-14 19:17:36 +0900427 * The current subtype of the current input method.
428 */
429 private InputMethodSubtype mCurrentSubtype;
430
satok4e4569d2010-11-19 18:45:53 +0900431 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900432 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700433 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900434
John Spurlocke0980502013-10-25 11:59:29 -0400435 // Was the keyguard locked when this client became current?
436 private boolean mCurClientInKeyguard;
437
satokab751aa2010-09-14 19:17:36 +0900438 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 * Set to true if our ServiceConnection is currently actively bound to
440 * a service (whether or not we have gotten its IBinder back yet).
441 */
442 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
445 * Set if the client has asked for the input method to be shown.
446 */
447 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 /**
450 * Set if we were explicitly told to show the input method.
451 */
452 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 /**
455 * Set if we were forced to be shown.
456 */
457 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 /**
460 * Set if we last told the input method to show itself.
461 */
462 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800465 * {@code true} if the current input method is in fullscreen mode.
466 */
467 boolean mInFullscreenMode;
468
469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 * The Intent used to connect to the current input method.
471 */
472 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 /**
475 * The token we have made for the currently active input method, to
476 * identify it in the future.
477 */
478 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 /**
481 * If non-null, this is the input method service we are currently connected
482 * to.
483 */
484 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 /**
487 * Time that we last initiated a bind to the input method, to determine
488 * if we should try to disconnect and reconnect to it.
489 */
490 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /**
493 * Have we called mCurMethod.bindInput()?
494 */
495 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 /**
498 * Currently enabled session. Only touched by service thread, not
499 * protected by a lock.
500 */
501 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700504 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700506 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800507
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900508 int mCurUserActionNotificationSequenceNumber = 0;
509
Joe Onorato857fd9b2011-01-27 15:08:35 -0800510 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900511
512 /**
513 * A set of status bits regarding the active IME.
514 *
515 * <p>This value is a combination of following two bits:</p>
516 * <dl>
517 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
518 * <dd>
519 * If this bit is ON, connected IME is ready to accept touch/key events.
520 * </dd>
521 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
522 * <dd>
523 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
524 * </dd>
525 * </dl>
526 * <em>Do not update this value outside of setImeWindowStatus.</em>
527 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800528 int mImeWindowVis;
529
Ken Wakasa05dbb652011-08-22 15:22:43 +0900530 private AlertDialog.Builder mDialogBuilder;
531 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700532 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900533 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700534 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900535 private InputMethodInfo[] mIms;
536 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800537 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700538 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800539 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900540 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
541 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500542 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700543 @HardKeyboardBehavior
544 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800546 /**
547 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
548 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
549 * will not affect those tasks that are already posted.
550 *
551 * <p>Posting {@link #MSG_START_INPUT} message basically means that
552 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
553 * back in the current IME process shortly, which will also affect what the current IME starts
554 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
555 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
556 * logical input session between the client application and the current IME.</p>
557 *
558 * <p>Be careful to not keep strong references to this object forever, which can prevent
559 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
560 * </p>
561 */
562 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800563 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
564
565 final int mSequenceNumber;
566 final long mTimestamp;
567 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800568 @NonNull
569 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800570 @NonNull
571 final String mImeId;
572 // @InputMethodClient.StartInputReason
573 final int mStartInputReason;
574 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800575 @Nullable
576 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800577 @NonNull
578 final EditorInfo mEditorInfo;
579 final int mTargetWindowSoftInputMode;
580 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800581
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800582 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
583 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
584 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
585 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
586 mSequenceNumber = sSequenceNumber.getAndIncrement();
587 mTimestamp = SystemClock.uptimeMillis();
588 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800589 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800590 mImeId = imeId;
591 mStartInputReason = startInputReason;
592 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800593 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800594 mEditorInfo = editorInfo;
595 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
596 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800597 }
598 }
599
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800600 @GuardedBy("mMethodMap")
601 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800602
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800603 /**
604 * A ring buffer to store the history of {@link StartInputInfo}.
605 */
606 private static final class StartInputHistory {
607 /**
608 * Entry size for non low-RAM devices.
609 *
610 * <p>TODO: Consider to follow what other system services have been doing to manage
611 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
612 */
613 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
614
615 /**
616 * Entry size for non low-RAM devices.
617 *
618 * <p>TODO: Consider to follow what other system services have been doing to manage
619 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
620 */
621 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
622
623 private static int getEntrySize() {
624 if (ActivityManager.isLowRamDeviceStatic()) {
625 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
626 } else {
627 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
628 }
629 }
630
631 /**
632 * Backing store for the ring bugger.
633 */
634 private final Entry[] mEntries = new Entry[getEntrySize()];
635
636 /**
637 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
638 * write.
639 */
640 private int mNextIndex = 0;
641
642 /**
643 * Recyclable entry to store the information in {@link StartInputInfo}.
644 */
645 private static final class Entry {
646 int mSequenceNumber;
647 long mTimestamp;
648 long mWallTime;
649 @NonNull
650 String mImeTokenString;
651 @NonNull
652 String mImeId;
653 /* @InputMethodClient.StartInputReason */
654 int mStartInputReason;
655 boolean mRestarting;
656 @NonNull
657 String mTargetWindowString;
658 @NonNull
659 EditorInfo mEditorInfo;
660 int mTargetWindowSoftInputMode;
661 int mClientBindSequenceNumber;
662
663 Entry(@NonNull StartInputInfo original) {
664 set(original);
665 }
666
667 void set(@NonNull StartInputInfo original) {
668 mSequenceNumber = original.mSequenceNumber;
669 mTimestamp = original.mTimestamp;
670 mWallTime = original.mWallTime;
671 // Intentionally convert to String so as not to keep a strong reference to a Binder
672 // object.
673 mImeTokenString = String.valueOf(original.mImeToken);
674 mImeId = original.mImeId;
675 mStartInputReason = original.mStartInputReason;
676 mRestarting = original.mRestarting;
677 // Intentionally convert to String so as not to keep a strong reference to a Binder
678 // object.
679 mTargetWindowString = String.valueOf(original.mTargetWindow);
680 mEditorInfo = original.mEditorInfo;
681 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
682 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
683 }
684 }
685
686 /**
687 * Add a new entry and discard the oldest entry as needed.
688 * @param info {@lin StartInputInfo} to be added.
689 */
690 void addEntry(@NonNull StartInputInfo info) {
691 final int index = mNextIndex;
692 if (mEntries[index] == null) {
693 mEntries[index] = new Entry(info);
694 } else {
695 mEntries[index].set(info);
696 }
697 mNextIndex = (mNextIndex + 1) % mEntries.length;
698 }
699
700 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
701 final SimpleDateFormat dataFormat =
702 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
703
704 for (int i = 0; i < mEntries.length; ++i) {
705 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
706 if (entry == null) {
707 continue;
708 }
709 pw.print(prefix);
710 pw.println("StartInput #" + entry.mSequenceNumber + ":");
711
712 pw.print(prefix);
713 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
714 + " (timestamp=" + entry.mTimestamp + ")"
715 + " reason="
716 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
717 + " restarting=" + entry.mRestarting);
718
719 pw.print(prefix);
720 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
721
722 pw.print(prefix);
723 pw.println(" targetWin=" + entry.mTargetWindowString
724 + " [" + entry.mEditorInfo.packageName + "]"
725 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
726
727 pw.print(prefix);
728 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
729 entry.mTargetWindowSoftInputMode));
730
731 pw.print(prefix);
732 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
733 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
734 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
735 + " fieldName=" + entry.mEditorInfo.fieldName
736 + " actionId=" + entry.mEditorInfo.actionId
737 + " actionLabel=" + entry.mEditorInfo.actionLabel);
738 }
739 }
740 }
741
742 @GuardedBy("mMethodMap")
743 @NonNull
744 private final StartInputHistory mStartInputHistory = new StartInputHistory();
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700747 int mUserId;
748 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700749 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800750 String mLastEnabled = "";
751
Yohei Yukawa81482972015-06-04 00:58:59 -0700752 /**
753 * <em>This constructor must be called within the lock.</em>
754 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 SettingsObserver(Handler handler) {
756 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700757 }
758
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800759 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700760 if (mRegistered && mUserId == userId) {
761 return;
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700764 if (mRegistered) {
765 mContext.getContentResolver().unregisterContentObserver(this);
766 mRegistered = false;
767 }
768 if (mUserId != userId) {
769 mLastEnabled = "";
770 mUserId = userId;
771 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700773 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900774 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700775 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900776 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700777 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700778 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700779 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800780 resolver.registerContentObserver(Settings.Secure.getUriFor(
781 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700782 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800784
Michael Wright7b5a96b2014-08-09 19:28:42 -0700785 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800786 final Uri showImeUri = Settings.Secure.getUriFor(
787 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
788 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
789 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700791 if (showImeUri.equals(uri)) {
792 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800793 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
794 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
795 mContext.getContentResolver(),
796 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
797 0, mUserId) == 1;
798 if (mAccessibilityRequestingNoSoftKeyboard) {
799 final boolean showRequested = mShowRequested;
800 hideCurrentInputLocked(0, null);
801 mShowRequested = showRequested;
802 } else if (mShowRequested) {
803 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
804 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700805 } else {
806 boolean enabledChanged = false;
807 String newEnabled = mSettings.getEnabledInputMethodsStr();
808 if (!mLastEnabled.equals(newEnabled)) {
809 mLastEnabled = newEnabled;
810 enabledChanged = true;
811 }
812 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
815 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700816
817 @Override
818 public String toString() {
819 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
820 + " mLastEnabled=" + mLastEnabled + "}";
821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800823
Yohei Yukawa79247822017-01-23 15:26:15 -0800824 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900825 @Override
826 public void onReceive(Context context, Intent intent) {
827 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700828 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900829 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700830 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900831 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800832 } else if (Intent.ACTION_USER_ADDED.equals(action)
833 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100834 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800835 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700836 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
837 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
838 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
839 final String prevValue = intent.getStringExtra(
840 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
841 final String newValue = intent.getStringExtra(
842 Intent.EXTRA_SETTING_NEW_VALUE);
843 restoreEnabledInputMethods(mContext, prevValue, newValue);
844 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800845 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800846 onActionLocaleChanged();
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900847 } else if (ACTION_SHOW_INPUT_METHOD_PICKER.equals(action)) {
848 // ACTION_SHOW_INPUT_METHOD_PICKER action is a protected-broadcast and it is
849 // guaranteed to be send only from the system, so that there is no need for extra
850 // security check such as
851 // {@link #canShowInputMethodPickerLocked(IInputMethodClient)}.
852 mHandler.obtainMessage(
853 MSG_SHOW_IM_SUBTYPE_PICKER,
854 InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES,
855 0 /* arg2 */)
856 .sendToTarget();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900857 } else {
858 Slog.w(TAG, "Unexpected intent " + intent);
859 }
860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800862
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800863 /**
864 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
865 *
866 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
867 * the users. We should ignore this event if this is about any background user's locale.</p>
868 *
869 * <p>Caution: This method must not be called when system is not ready.</p>
870 */
871 void onActionLocaleChanged() {
872 synchronized (mMethodMap) {
873 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
874 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
875 return;
876 }
877 buildInputMethodListLocked(true);
878 // If the locale is changed, needs to reset the default ime
879 resetDefaultImeLocked(mContext);
880 updateFromSettingsLocked(true);
881 mLastSystemLocales = possibleNewLocale;
882 }
883 }
884
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700885 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
886 // does not attempt to validate on the fly with any installed device policy, so must only
887 // be run in the context of initial device setup.
888 //
889 // TODO: Move this method to InputMethodUtils with adding unit tests.
890 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
891 if (DEBUG_RESTORE) {
892 Slog.i(TAG, "Restoring enabled input methods:");
893 Slog.i(TAG, "prev=" + prevValue);
894 Slog.i(TAG, " new=" + newValue);
895 }
896 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900897 ArrayMap<String, ArraySet<String>> prevMap =
898 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
899 ArrayMap<String, ArraySet<String>> newMap =
900 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700901
902 // Merge the restored ime+subtype enabled states into the live state
903 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
904 final String imeId = entry.getKey();
905 ArraySet<String> prevSubtypes = prevMap.get(imeId);
906 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700907 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700908 prevMap.put(imeId, prevSubtypes);
909 }
910 prevSubtypes.addAll(entry.getValue());
911 }
912
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700913 final String mergedImesAndSubtypesString =
914 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700915 if (DEBUG_RESTORE) {
916 Slog.i(TAG, "Merged IME string:");
917 Slog.i(TAG, " " + mergedImesAndSubtypesString);
918 }
919 Settings.Secure.putString(context.getContentResolver(),
920 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
921 }
922
Yohei Yukawac4e44912017-02-09 19:30:22 -0800923 final class MyPackageMonitor extends PackageMonitor {
924 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800925 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800926 *
927 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
928 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
929 * dynamically unless the entire package is updated, which also always triggers package
930 * rescanning.</p>
931 */
932 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800933 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
934
935 /**
936 * Packages that are appeared, disappeared, or modified for whatever reason.
937 *
938 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
939 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
940 * duplicate elements for our use case.</p>
941 *
942 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
943 * which should be bound to {@link #getRegisteredHandler()}.</p>
944 */
945 private final ArrayList<String> mChangedPackages = new ArrayList<>();
946
947 /**
948 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
949 *
950 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
951 * which should be bound to {@link #getRegisteredHandler()}.</p>
952 */
953 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800954
955 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800956 void clearKnownImePackageNamesLocked() {
957 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800958 }
959
960 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800961 final void addKnownImePackageNameLocked(@NonNull String packageName) {
962 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800963 }
964
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800965 @GuardedBy("mMethodMap")
966 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900967 final int userId = getChangingUserId();
968 final boolean retval = userId == mSettings.getCurrentUserId();
969 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900970 if (!retval) {
971 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
972 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900973 }
974 return retval;
975 }
976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800978 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800980 if (!isChangingPackagesOfCurrentUserLocked()) {
981 return false;
982 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 final int N = mMethodList.size();
985 if (curInputMethodId != null) {
986 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800987 InputMethodInfo imi = mMethodList.get(i);
988 if (imi.getId().equals(curInputMethodId)) {
989 for (String pkg : packages) {
990 if (imi.getPackageName().equals(pkg)) {
991 if (!doit) {
992 return true;
993 }
satok723a27e2010-11-11 14:58:11 +0900994 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800995 chooseNewDefaultIMELocked();
996 return true;
997 }
998 }
999 }
1000 }
1001 }
1002 }
1003 return false;
1004 }
1005
1006 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001007 public void onBeginPackageChanges() {
1008 clearPackageChangeState();
1009 }
1010
1011 @Override
1012 public void onPackageAppeared(String packageName, int reason) {
1013 if (!mImePackageAppeared) {
1014 final PackageManager pm = mContext.getPackageManager();
1015 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
1016 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
1017 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
1018 // No need to lock this because we access it only on getRegisteredHandler().
1019 if (!services.isEmpty()) {
1020 mImePackageAppeared = true;
1021 }
1022 }
1023 // No need to lock this because we access it only on getRegisteredHandler().
1024 mChangedPackages.add(packageName);
1025 }
1026
1027 @Override
1028 public void onPackageDisappeared(String packageName, int reason) {
1029 // No need to lock this because we access it only on getRegisteredHandler().
1030 mChangedPackages.add(packageName);
1031 }
1032
1033 @Override
1034 public void onPackageModified(String packageName) {
1035 // No need to lock this because we access it only on getRegisteredHandler().
1036 mChangedPackages.add(packageName);
1037 }
1038
1039 @Override
1040 public void onPackagesSuspended(String[] packages) {
1041 // No need to lock this because we access it only on getRegisteredHandler().
1042 for (String packageName : packages) {
1043 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001044 }
1045 }
1046
1047 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001048 public void onPackagesUnsuspended(String[] packages) {
1049 // No need to lock this because we access it only on getRegisteredHandler().
1050 for (String packageName : packages) {
1051 mChangedPackages.add(packageName);
1052 }
1053 }
1054
1055 @Override
1056 public void onFinishPackageChanges() {
1057 onFinishPackageChangesInternal();
1058 clearPackageChangeState();
1059 }
1060
1061 private void clearPackageChangeState() {
1062 // No need to lock them because we access these fields only on getRegisteredHandler().
1063 mChangedPackages.clear();
1064 mImePackageAppeared = false;
1065 }
1066
1067 private boolean shouldRebuildInputMethodListLocked() {
1068 // This method is guaranteed to be called only by getRegisteredHandler().
1069
1070 // If there is any new package that contains at least one IME, then rebuilt the list
1071 // of IMEs.
1072 if (mImePackageAppeared) {
1073 return true;
1074 }
1075
1076 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1077 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1078 // is an option, but it may still do some extra operations that we do not need here.
1079 final int N = mChangedPackages.size();
1080 for (int i = 0; i < N; ++i) {
1081 final String packageName = mChangedPackages.get(i);
1082 if (mKnownImePackageNames.contains(packageName)) {
1083 return true;
1084 }
1085 }
1086 return false;
1087 }
1088
1089 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001090 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001091 if (!isChangingPackagesOfCurrentUserLocked()) {
1092 return;
1093 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001094 if (!shouldRebuildInputMethodListLocked()) {
1095 return;
1096 }
1097
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001098 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001099 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001100 final int N = mMethodList.size();
1101 if (curInputMethodId != null) {
1102 for (int i=0; i<N; i++) {
1103 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001104 final String imiId = imi.getId();
1105 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001106 curIm = imi;
1107 }
satoke7c6998e2011-06-03 17:57:59 +09001108
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001109 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001110 if (isPackageModified(imi.getPackageName())) {
1111 mFileManager.deleteAllInputMethodSubtypes(imiId);
1112 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001113 if (change == PACKAGE_TEMPORARY_CHANGE
1114 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001116 + imi.getComponent());
1117 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 }
1119 }
1120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121
Yohei Yukawa94e33302016-02-12 19:37:03 -08001122 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001126 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001127 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001128 if (change == PACKAGE_TEMPORARY_CHANGE
1129 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001130 ServiceInfo si = null;
1131 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 si = mIPackageManager.getServiceInfo(
1133 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1134 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001135 }
1136 if (si == null) {
1137 // Uh oh, current input method is no longer around!
1138 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001139 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001140 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001141 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001142 changed = true;
1143 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001145 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001146 }
1147 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001148 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001149 }
satokab751aa2010-09-14 19:17:36 +09001150
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001151 if (curIm == null) {
1152 // We currently don't have a default input method... is
1153 // one now available?
1154 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001155 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1156 // Even if the current input method is still available, mCurrentSubtype could
1157 // be obsolete when the package is modified in practice.
1158 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001161 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001162 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 }
1165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
Jeff Brownc28867a2013-03-26 15:42:39 -07001168 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001169 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001170 private final IInputMethod mMethod;
1171 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
Jeff Brownc28867a2013-03-26 15:42:39 -07001173 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1174 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001175 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001176 mMethod = method;
1177 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
satoke7c6998e2011-06-03 17:57:59 +09001180 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001181 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001182 long ident = Binder.clearCallingIdentity();
1183 try {
1184 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1185 } finally {
1186 Binder.restoreCallingIdentity(ident);
1187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
satok01038492012-04-09 21:08:27 +09001191 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001192 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001193 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001194 public void onHardKeyboardStatusChange(boolean available) {
1195 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1196 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001197 }
1198
Michael Wright7b5a96b2014-08-09 19:28:42 -07001199 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001200 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001201 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001202 }
1203 synchronized(mMethodMap) {
1204 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1205 && mSwitchingDialog.isShowing()) {
1206 mSwitchingDialogTitleView.findViewById(
1207 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1208 available ? View.VISIBLE : View.GONE);
1209 }
1210 }
1211 }
1212 }
1213
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001214 public static final class Lifecycle extends SystemService {
1215 private InputMethodManagerService mService;
1216
1217 public Lifecycle(Context context) {
1218 super(context);
1219 mService = new InputMethodManagerService(context);
1220 }
1221
1222 @Override
1223 public void onStart() {
1224 LocalServices.addService(InputMethodManagerInternal.class,
1225 new LocalServiceImpl(mService.mHandler));
1226 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1227 }
1228
1229 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001230 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001231 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001232 // TODO: Dispatch this to a worker thread as needed.
1233 mService.onSwitchUser(userHandle);
1234 }
1235
1236 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001237 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001238 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001239 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001240 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1241 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1242 .getService(Context.STATUS_BAR_SERVICE);
1243 mService.systemRunning(statusBarService);
1244 }
1245 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001246
1247 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001248 public void onUnlockUser(final @UserIdInt int userHandle) {
1249 // Called on ActivityManager thread.
1250 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001251 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001252 }
1253 }
1254
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001255 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001256 synchronized(mMethodMap) {
1257 final int currentUserId = mSettings.getCurrentUserId();
1258 if (DEBUG) {
1259 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1260 }
1261 if (userId != currentUserId) {
1262 return;
1263 }
1264 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001265 if (mSystemReady) {
1266 // We need to rebuild IMEs.
1267 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1268 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1269 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001270 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001271 }
1272
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001273 void onSwitchUser(@UserIdInt int userId) {
1274 synchronized (mMethodMap) {
1275 switchUserLocked(userId);
1276 }
1277 }
1278
Seigo Nonaka7309b122015-08-17 18:34:13 -07001279 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001280 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001282 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001284 // Note: SettingsObserver doesn't register observers in its constructor.
1285 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 mIWindowManager = IWindowManager.Stub.asInterface(
1287 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001288 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001289 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001290 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 public void executeMessage(Message msg) {
1292 handleMessage(msg);
1293 }
Mita Yuned218c72012-12-06 17:18:25 -08001294 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001295 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001296 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001297 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001298 mHasFeature = context.getPackageManager().hasSystemFeature(
1299 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001300 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001301 mHardKeyboardBehavior = mContext.getResources().getInteger(
1302 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001303
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001304 Bundle extras = new Bundle();
1305 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001306 @ColorInt final int accentColor = mContext.getColor(
1307 com.android.internal.R.color.system_notification_accent_color);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001308 mImeSwitcherNotification =
1309 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1310 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1311 .setWhen(0)
1312 .setOngoing(true)
1313 .addExtras(extras)
1314 .setCategory(Notification.CATEGORY_SYSTEM)
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001315 .setColor(accentColor);
Daniel Sandler590d5152012-06-14 16:10:13 -04001316
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001317 Intent intent = new Intent(ACTION_SHOW_INPUT_METHOD_PICKER)
1318 .setPackage(mContext.getPackageName());
satok683e2382011-07-12 08:28:52 +09001319 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001320
1321 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001322
satok7cfc0ed2011-06-20 21:29:36 +09001323 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001324 int userId = 0;
1325 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001326 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001327 } catch (RemoteException e) {
1328 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1329 }
satok913a8922010-08-26 21:53:41 +09001330
satokd87c2592010-09-29 11:52:06 +09001331 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001332 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001333 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001334
Kenny Guy2a764942014-04-02 13:29:20 +01001335 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001336 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001337 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1338 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001339 }
1340
satok5b927c432012-05-01 20:09:34 +09001341 private void resetDefaultImeLocked(Context context) {
1342 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001343 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001344 return;
1345 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001346 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001347 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001348 if (suitableImes.isEmpty()) {
1349 Slog.i(TAG, "No default found");
1350 return;
satok5b927c432012-05-01 20:09:34 +09001351 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001352 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001353 if (DEBUG) {
1354 Slog.i(TAG, "Default found, using " + defIm.getId());
1355 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001356 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001357 }
1358
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001359 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001360 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1361 + " currentUserId=" + mSettings.getCurrentUserId());
1362
Yohei Yukawa81482972015-06-04 00:58:59 -07001363 // ContentObserver should be registered again when the user is changed
1364 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001365
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001366 // If the system is not ready or the device is not yed unlocked by the user, then we use
1367 // copy-on-write settings.
1368 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001369 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001370 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001371 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001372 // InputMethodFileManager should be reset when the user is changed
1373 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001374 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001375
1376 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1377 + " defaultImiId=" + defaultImiId);
1378
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001379 // For secondary users, the list of enabled IMEs may not have been updated since the
1380 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1381 // not be empty even if the IME has been uninstalled by the primary user.
1382 // Even in such cases, IMMS works fine because it will find the most applicable
1383 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001384 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001385 mLastSystemLocales = mRes.getConfiguration().getLocales();
1386
1387 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1388 if (mSystemReady) {
1389 hideCurrentInputLocked(0, null);
1390 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1391 buildInputMethodListLocked(initialUserSwitch);
1392 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1393 // This is the first time of the user switch and
1394 // set the current ime to the proper one.
1395 resetDefaultImeLocked(mContext);
1396 }
1397 updateFromSettingsLocked(true);
1398 try {
1399 startInputInnerLocked();
1400 } catch (RuntimeException e) {
1401 Slog.w(TAG, "Unexpected exception", e);
1402 }
1403 }
1404
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001405 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001406 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1407 mSettings.getEnabledInputMethodListLocked(), newUserId,
1408 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001409 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001410
1411 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1412 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001413 }
1414
Kenny Guy2a764942014-04-02 13:29:20 +01001415 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001416 mSettings.setCurrentProfileIds(
1417 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001418 }
1419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 @Override
1421 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1422 throws RemoteException {
1423 try {
1424 return super.onTransact(code, data, reply, flags);
1425 } catch (RuntimeException e) {
1426 // The input method manager only throws security exceptions, so let's
1427 // log all others.
1428 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001429 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431 throw e;
1432 }
1433 }
1434
Svetoslav Ganova0027152013-06-25 14:59:53 -07001435 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001436 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001437 if (DEBUG) {
1438 Slog.d(TAG, "--- systemReady");
1439 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001440 if (!mSystemReady) {
1441 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001442 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001443 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001444 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001445 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001446 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1447 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001448 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001449 if (mStatusBar != null) {
1450 mStatusBar.setIconVisibility(mSlotIme, false);
1451 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001452 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001453 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1454 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001455 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001456 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001457 mHardKeyboardListener);
1458 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001459
1460 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1461 mSettingsObserver.registerContentObserverLocked(currentUserId);
1462
1463 final IntentFilter broadcastFilter = new IntentFilter();
1464 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1465 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1466 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1467 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1468 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001469 broadcastFilter.addAction(ACTION_SHOW_INPUT_METHOD_PICKER);
Yohei Yukawa79247822017-01-23 15:26:15 -08001470 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1471
1472 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1473 resetDefaultImeLocked(mContext);
1474 updateFromSettingsLocked(true);
1475 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1476 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1477 mContext.getBasePackageName());
1478
Dianne Hackborncc278702009-09-02 23:07:23 -07001479 try {
1480 startInputInnerLocked();
1481 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001483 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001484 }
1485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001487
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001488 // ---------------------------------------------------------------------------------------
1489 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1490 // 1) it comes from the system process
1491 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1492 private boolean calledFromValidUser() {
1493 final int uid = Binder.getCallingUid();
1494 final int userId = UserHandle.getUserId(uid);
1495 if (DEBUG) {
1496 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1497 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1498 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001499 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1500 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001501 }
Kenny Guy2a764942014-04-02 13:29:20 +01001502 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001503 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001504 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001505
1506 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1507 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1508 // must not manage background users' states in any functions.
1509 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1510 // by a token.
1511 if (mContext.checkCallingOrSelfPermission(
1512 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1513 == PackageManager.PERMISSION_GRANTED) {
1514 if (DEBUG) {
1515 Slog.d(TAG, "--- Access granted because the calling process has "
1516 + "the INTERACT_ACROSS_USERS_FULL permission");
1517 }
1518 return true;
1519 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001520 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001521 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1522 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001523 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001524 }
1525
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001526
1527 /**
1528 * Returns true iff the caller is identified to be the current input method with the token.
1529 * @param token The window token given to the input method when it was started.
1530 * @return true if and only if non-null valid token is specified.
1531 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001532 private boolean calledWithValidToken(@Nullable IBinder token) {
1533 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1534 if (DEBUG) {
1535 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1536 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1537 }
1538 return false;
1539 }
1540 if (token == null || token != mCurToken) {
1541 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1542 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1543 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001544 return false;
1545 }
1546 return true;
1547 }
1548
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001549 private boolean bindCurrentInputMethodService(
1550 Intent service, ServiceConnection conn, int flags) {
1551 if (service == null || conn == null) {
1552 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1553 return false;
1554 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001555 return mContext.bindServiceAsUser(service, conn, flags,
1556 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001557 }
1558
satoke7c6998e2011-06-03 17:57:59 +09001559 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001561 // TODO: Make this work even for non-current users?
1562 if (!calledFromValidUser()) {
1563 return Collections.emptyList();
1564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001566 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568 }
1569
satoke7c6998e2011-06-03 17:57:59 +09001570 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001572 // TODO: Make this work even for non-current users?
1573 if (!calledFromValidUser()) {
1574 return Collections.emptyList();
1575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001577 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 }
1579 }
1580
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001581 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001582 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001583 * @return enabled subtypes of the specified imi
1584 */
satoke7c6998e2011-06-03 17:57:59 +09001585 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001586 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001587 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001588 // TODO: Make this work even for non-current users?
1589 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001590 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001591 }
satok67ddf9c2010-11-17 09:45:54 +09001592 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001593 final InputMethodInfo imi;
1594 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001595 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001596 } else {
1597 imi = mMethodMap.get(imiId);
1598 }
1599 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001600 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001601 }
1602 return mSettings.getEnabledInputMethodSubtypeListLocked(
1603 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001604 }
1605 }
1606
satoke7c6998e2011-06-03 17:57:59 +09001607 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 public void addClient(IInputMethodClient client,
1609 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001610 if (!calledFromValidUser()) {
1611 return;
1612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 synchronized (mMethodMap) {
1614 mClients.put(client.asBinder(), new ClientState(client,
1615 inputContext, uid, pid));
1616 }
1617 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001618
satoke7c6998e2011-06-03 17:57:59 +09001619 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001621 if (!calledFromValidUser()) {
1622 return;
1623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001625 ClientState cs = mClients.remove(client.asBinder());
1626 if (cs != null) {
1627 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001628 if (mCurClient == cs) {
1629 mCurClient = null;
1630 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001631 if (mCurFocusedWindowClient == cs) {
1632 mCurFocusedWindowClient = null;
1633 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
1636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 void executeOrSendMessage(IInterface target, Message msg) {
1639 if (target.asBinder() instanceof Binder) {
1640 mCaller.sendMessage(msg);
1641 } else {
1642 handleMessage(msg);
1643 msg.recycle();
1644 }
1645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
Yohei Yukawa33e81792015-11-17 21:14:42 -08001647 void unbindCurrentClientLocked(
1648 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001650 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 + mCurClient.client.asBinder());
1652 if (mBoundToMethod) {
1653 mBoundToMethod = false;
1654 if (mCurMethod != null) {
1655 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1656 MSG_UNBIND_INPUT, mCurMethod));
1657 }
1658 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001659
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001660 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1661 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001662 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1663 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
The Android Open Source Project10592532009-03-18 17:39:46 -07001667 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
1669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 private int getImeShowFlags() {
1672 int flags = 0;
1673 if (mShowForced) {
1674 flags |= InputMethod.SHOW_FORCED
1675 | InputMethod.SHOW_EXPLICIT;
1676 } else if (mShowExplicitlyRequested) {
1677 flags |= InputMethod.SHOW_EXPLICIT;
1678 }
1679 return flags;
1680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 private int getAppShowFlags() {
1683 int flags = 0;
1684 if (mShowForced) {
1685 flags |= InputMethodManager.SHOW_FORCED;
1686 } else if (!mShowExplicitlyRequested) {
1687 flags |= InputMethodManager.SHOW_IMPLICIT;
1688 }
1689 return flags;
1690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001691
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001692 InputBindResult attachNewInputLocked(
1693 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 if (!mBoundToMethod) {
1695 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1696 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1697 mBoundToMethod = true;
1698 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001699
1700 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001701 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1702 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1703 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001704 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001705 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001708 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001709 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001710 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001712 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001713 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001715 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001716 (session.channel != null ? session.channel.dup() : null),
1717 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001719
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001720 InputBindResult startInputLocked(
1721 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001722 IInputMethodClient client, IInputContext inputContext,
1723 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001724 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 // If no method is currently selected, do nothing.
1726 if (mCurMethodId == null) {
1727 return mNoBinding;
1728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 ClientState cs = mClients.get(client.asBinder());
1731 if (cs == null) {
1732 throw new IllegalArgumentException("unknown client "
1733 + client.asBinder());
1734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001735
Yohei Yukawa74750f22016-03-22 12:54:22 -07001736 if (attribute == null) {
1737 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1738 + " uid=" + cs.uid + " pid=" + cs.pid);
1739 return null;
1740 }
1741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 try {
1743 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1744 // Check with the window manager to make sure this client actually
1745 // has a window with focus. If not, reject. This is thread safe
1746 // because if the focus changes some time before or after, the
1747 // next client receiving focus that has any interest in input will
1748 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001749 if (DEBUG) {
1750 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1751 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 return null;
1754 }
1755 } catch (RemoteException e) {
1756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001757
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001758 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001759 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001760 }
1761
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001762 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001763 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001764 @NonNull EditorInfo attribute, int controlFlags,
1765 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001766 // If no method is currently selected, do nothing.
1767 if (mCurMethodId == null) {
1768 return mNoBinding;
1769 }
1770
Yohei Yukawad57ba672015-06-08 16:39:46 -07001771 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1772 attribute.packageName)) {
1773 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1774 + " uid=" + cs.uid + " package=" + attribute.packageName);
1775 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001776 }
1777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001779 // Was the keyguard locked when switching over to the new client?
1780 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 // If the client is changing, we need to switch over to the new
1782 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001783 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001784 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001785 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786
1787 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001788 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001789 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001790 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 }
1792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 // Bump up the sequence for this client and attach it.
1795 mCurSeq++;
1796 if (mCurSeq <= 0) mCurSeq = 1;
1797 mCurClient = cs;
1798 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001799 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 // Check if the input method is changing.
1803 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1804 if (cs.curSession != null) {
1805 // Fast case: if we are already connected to the input method,
1806 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001807 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001808 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810 if (mHaveConnection) {
1811 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 // Return to client, and we will get back with it when
1813 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001814 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001815 return new InputBindResult(null, null, mCurId, mCurSeq,
1816 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 } else if (SystemClock.uptimeMillis()
1818 < (mLastBindTime+TIME_TO_RECONNECT)) {
1819 // In this case we have connected to the service, but
1820 // don't yet have its interface. If it hasn't been too
1821 // long since we did the connection, we'll return to
1822 // the client and wait to get the service interface so
1823 // we can report back. If it has been too long, we want
1824 // to fall through so we can try a disconnect/reconnect
1825 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001826 return new InputBindResult(null, null, mCurId, mCurSeq,
1827 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001829 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1830 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
1832 }
1833 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001834
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001835 return startInputInnerLocked();
1836 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001837
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001838 InputBindResult startInputInnerLocked() {
1839 if (mCurMethodId == null) {
1840 return mNoBinding;
1841 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001842
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001843 if (!mSystemReady) {
1844 // If the system is not yet ready, we shouldn't be running third
1845 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001846 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1847 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001848 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1851 if (info == null) {
1852 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001854
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001855 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1858 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001859 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1860 com.android.internal.R.string.input_method_binding_label);
1861 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1862 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001863 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 mLastBindTime = SystemClock.uptimeMillis();
1865 mHaveConnection = true;
1866 mCurId = info.getId();
1867 mCurToken = new Binder();
1868 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001869 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001870 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 } catch (RemoteException e) {
1872 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001873 return new InputBindResult(null, null, mCurId, mCurSeq,
1874 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 } else {
1876 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001877 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 + mCurIntent);
1879 }
1880 return null;
1881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001882
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001883 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001884 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001885 IInputMethodClient client, IInputContext inputContext,
1886 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001887 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001888 if (!calledFromValidUser()) {
1889 return null;
1890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001892 if (DEBUG) {
1893 Slog.v(TAG, "startInput: reason="
1894 + InputMethodClient.getStartInputReason(startInputReason)
1895 + " client = " + client.asBinder()
1896 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001897 + " missingMethods="
1898 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001899 + " attribute=" + attribute
1900 + " controlFlags=#" + Integer.toHexString(controlFlags));
1901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 final long ident = Binder.clearCallingIdentity();
1903 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001904 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1905 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 } finally {
1907 Binder.restoreCallingIdentity(ident);
1908 }
1909 }
1910 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001911
satoke7c6998e2011-06-03 17:57:59 +09001912 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 public void finishInput(IInputMethodClient client) {
1914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001915
satoke7c6998e2011-06-03 17:57:59 +09001916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 public void onServiceConnected(ComponentName name, IBinder service) {
1918 synchronized (mMethodMap) {
1919 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1920 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001921 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001922 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001923 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001924 return;
1925 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001926 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001927 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1928 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001930 clearClientSessionLocked(mCurClient);
1931 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 }
1934 }
1935 }
1936
Jeff Brownc28867a2013-03-26 15:42:39 -07001937 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1938 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 synchronized (mMethodMap) {
1940 if (mCurMethod != null && method != null
1941 && mCurMethod.asBinder() == method.asBinder()) {
1942 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001943 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001945 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001946 InputBindResult res = attachNewInputLocked(
1947 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 if (res.method != null) {
1949 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001950 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001952 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 }
1955 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001956
1957 // Session abandoned. Close its associated input channel.
1958 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001960
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001961 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001962 if (mVisibleBound) {
1963 mContext.unbindService(mVisibleConnection);
1964 mVisibleBound = false;
1965 }
1966
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001967 if (mHaveConnection) {
1968 mContext.unbindService(this);
1969 mHaveConnection = false;
1970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001971
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001972 if (mCurToken != null) {
1973 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001974 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001975 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001976 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001977 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001978 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001979 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001980 } catch (RemoteException e) {
1981 }
1982 mCurToken = null;
1983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001984
The Android Open Source Project10592532009-03-18 17:39:46 -07001985 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001986 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001988
Yohei Yukawa33e81792015-11-17 21:14:42 -08001989 void resetCurrentMethodAndClient(
1990 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001991 mCurMethodId = null;
1992 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001993 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001994 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001995
1996 void requestClientSessionLocked(ClientState cs) {
1997 if (!cs.sessionRequested) {
1998 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1999 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
2000 cs.sessionRequested = true;
2001 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
2002 MSG_CREATE_SESSION, mCurMethod, channels[1],
2003 new MethodCallback(this, mCurMethod, channels[0])));
2004 }
2005 }
2006
2007 void clearClientSessionLocked(ClientState cs) {
2008 finishSessionLocked(cs.curSession);
2009 cs.curSession = null;
2010 cs.sessionRequested = false;
2011 }
2012
2013 private void finishSessionLocked(SessionState sessionState) {
2014 if (sessionState != null) {
2015 if (sessionState.session != null) {
2016 try {
2017 sessionState.session.finishSession();
2018 } catch (RemoteException e) {
2019 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002020 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07002021 }
2022 sessionState.session = null;
2023 }
2024 if (sessionState.channel != null) {
2025 sessionState.channel.dispose();
2026 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002027 }
2028 }
2029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002030
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002031 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 if (mCurMethod != null) {
2033 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002034 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002036
Jeff Brownc28867a2013-03-26 15:42:39 -07002037 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002038 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 mCurMethod = null;
2040 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002041 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002042 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002043 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002044 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002046
satoke7c6998e2011-06-03 17:57:59 +09002047 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002049 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2050 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2051 // which is irregular but can eventually happen for everyone just by continuing using the
2052 // device. Thus it is important to make sure that all the internal states are properly
2053 // refreshed when this method is called back. Running
2054 // adb install -r <APK that implements the current IME>
2055 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002057 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 + " mCurIntent=" + mCurIntent);
2059 if (mCurMethod != null && mCurIntent != null
2060 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002061 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 // We consider this to be a new bind attempt, since the system
2063 // should now try to restart the service for us.
2064 mLastBindTime = SystemClock.uptimeMillis();
2065 mShowRequested = mInputShown;
2066 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002067 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
2069 }
2070 }
2071
satokf9f01002011-05-19 21:31:50 +09002072 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002074 synchronized (mMethodMap) {
2075 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002076 return;
2077 }
2078 final long ident = Binder.clearCallingIdentity();
2079 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002081 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002082 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002083 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002086 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002087 CharSequence contentDescription = null;
2088 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002089 // Use PackageManager to load label
2090 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002091 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002092 mIPackageManager.getApplicationInfo(packageName, 0,
2093 mSettings.getCurrentUserId()));
2094 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002095 /* ignore */
2096 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002097 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002098 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002099 contentDescription != null
2100 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002101 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002104 } finally {
2105 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
2108 }
2109
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002110 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002111 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002112 if (mSwitchingDialog != null) return false;
Yohei Yukawad2bc3092017-07-31 15:37:14 -07002113 if (mWindowManagerInternal.isKeyguardShowingAndNotOccluded()
2114 && mKeyguardManager != null && mKeyguardManager.isKeyguardSecure()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002115 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002116 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002117 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2118 // When physical keyboard is attached, we show the ime switcher (or notification if
2119 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2120 // exists in the IME switcher dialog. Might be OK to remove this condition once
2121 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2122 return true;
2123 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002124 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2125 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002126 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002127
2128 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2129 final int N = imis.size();
2130 if (N > 2) return true;
2131 if (N < 1) return false;
2132 int nonAuxCount = 0;
2133 int auxCount = 0;
2134 InputMethodSubtype nonAuxSubtype = null;
2135 InputMethodSubtype auxSubtype = null;
2136 for(int i = 0; i < N; ++i) {
2137 final InputMethodInfo imi = imis.get(i);
2138 final List<InputMethodSubtype> subtypes =
2139 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2140 final int subtypeCount = subtypes.size();
2141 if (subtypeCount == 0) {
2142 ++nonAuxCount;
2143 } else {
2144 for (int j = 0; j < subtypeCount; ++j) {
2145 final InputMethodSubtype subtype = subtypes.get(j);
2146 if (!subtype.isAuxiliary()) {
2147 ++nonAuxCount;
2148 nonAuxSubtype = subtype;
2149 } else {
2150 ++auxCount;
2151 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002152 }
2153 }
satok7cfc0ed2011-06-20 21:29:36 +09002154 }
2155 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002156 if (nonAuxCount > 1 || auxCount > 1) {
2157 return true;
2158 } else if (nonAuxCount == 1 && auxCount == 1) {
2159 if (nonAuxSubtype != null && auxSubtype != null
2160 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2161 || auxSubtype.overridesImplicitlyEnabledSubtype()
2162 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2163 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2164 return false;
2165 }
2166 return true;
2167 }
2168 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002169 }
2170
John Spurlocke0980502013-10-25 11:59:29 -04002171 private boolean isKeyguardLocked() {
2172 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2173 }
2174
Yohei Yukawad6475a62017-04-17 10:35:27 -07002175 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002176 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002177 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002178 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2179 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002180 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002181 return;
2182 }
2183
Yohei Yukawa69e68022017-02-13 12:04:50 -08002184 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002185 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002186 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002187 mImeWindowVis = vis;
2188 mBackDisposition = backDisposition;
2189 updateSystemUiLocked(token, vis, backDisposition);
2190 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002191
2192 final boolean dismissImeOnBackKeyPressed;
2193 switch (backDisposition) {
2194 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2195 dismissImeOnBackKeyPressed = true;
2196 break;
2197 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2198 dismissImeOnBackKeyPressed = false;
2199 break;
2200 default:
2201 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2202 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2203 break;
2204 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002205 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2206 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002207 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002208 }
2209
2210 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2211 synchronized (mMethodMap) {
2212 updateSystemUiLocked(token, vis, backDisposition);
2213 }
2214 }
2215
2216 // Caution! This method is called in this class. Handle multi-user carefully
2217 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2218 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002219 return;
2220 }
2221
2222 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2223 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002224 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002225 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002226 // apply policy for binder calls
2227 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2228 vis = 0;
satok06487a52010-10-29 11:37:18 +09002229 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002230 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2231 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2232 if (mStatusBar != null) {
2233 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2234 needsToShowImeSwitcher);
2235 }
2236 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2237 if (imi != null && needsToShowImeSwitcher) {
2238 // Used to load label
2239 final CharSequence title = mRes.getText(
2240 com.android.internal.R.string.select_input_method);
2241 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2242 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002243 mImeSwitcherNotification.setContentTitle(title)
2244 .setContentText(summary)
2245 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002246 try {
2247 if ((mNotificationManager != null)
2248 && !mIWindowManager.hasNavigationBar()) {
2249 if (DEBUG) {
2250 Slog.d(TAG, "--- show notification: label = " + summary);
2251 }
2252 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002253 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002254 mImeSwitcherNotification.build(), UserHandle.ALL);
2255 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002256 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002257 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002258 }
2259 } else {
2260 if (mNotificationShown && mNotificationManager != null) {
2261 if (DEBUG) {
2262 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002263 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002264 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002265 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002266 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002267 }
satok06487a52010-10-29 11:37:18 +09002268 }
2269 } finally {
2270 Binder.restoreCallingIdentity(ident);
2271 }
2272 }
2273
satoke7c6998e2011-06-03 17:57:59 +09002274 @Override
satokf9f01002011-05-19 21:31:50 +09002275 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002276 if (!calledFromValidUser()) {
2277 return;
2278 }
satokf9f01002011-05-19 21:31:50 +09002279 synchronized (mMethodMap) {
2280 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2281 for (int i = 0; i < spans.length; ++i) {
2282 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002283 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002284 mSecureSuggestionSpans.put(ss, currentImi);
2285 }
2286 }
2287 }
2288 }
2289
satoke7c6998e2011-06-03 17:57:59 +09002290 @Override
satokf9f01002011-05-19 21:31:50 +09002291 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002292 if (!calledFromValidUser()) {
2293 return false;
2294 }
satokf9f01002011-05-19 21:31:50 +09002295 synchronized (mMethodMap) {
2296 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2297 // TODO: Do not send the intent if the process of the targetImi is already dead.
2298 if (targetImi != null) {
2299 final String[] suggestions = span.getSuggestions();
2300 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002301 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002302 final Intent intent = new Intent();
2303 // Ensures that only a class in the original IME package will receive the
2304 // notification.
satok42c5a162011-05-26 16:46:14 +09002305 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002306 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2307 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2308 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2309 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002310 final long ident = Binder.clearCallingIdentity();
2311 try {
2312 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2313 } finally {
2314 Binder.restoreCallingIdentity(ident);
2315 }
satokf9f01002011-05-19 21:31:50 +09002316 return true;
2317 }
2318 }
2319 return false;
2320 }
2321
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002322 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002323 updateInputMethodsFromSettingsLocked(enabledMayChange);
2324 updateKeyboardFromSettingsLocked();
2325 }
2326
2327 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002328 if (enabledMayChange) {
2329 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2330 for (int i=0; i<enabled.size(); i++) {
2331 // We allow the user to select "disabled until used" apps, so if they
2332 // are enabling one of those here we now need to make it enabled.
2333 InputMethodInfo imm = enabled.get(i);
2334 try {
2335 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2336 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2337 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002338 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002339 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002340 if (DEBUG) {
2341 Slog.d(TAG, "Update state(" + imm.getId()
2342 + "): DISABLED_UNTIL_USED -> DEFAULT");
2343 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002344 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2345 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002346 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2347 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002348 }
2349 } catch (RemoteException e) {
2350 }
2351 }
2352 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002353 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2354 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2355 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2356 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002357 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002358 // There is no input method selected, try to choose new applicable input method.
2359 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002360 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002361 }
2362 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002364 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002366 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002367 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
satokf3db1af2010-11-23 13:34:33 +09002369 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002370 } else {
2371 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002372 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002374 // Here is not the perfect place to reset the switching controller. Ideally
2375 // mSwitchingController and mSettings should be able to share the same state.
2376 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2377 // the same enabled IMEs list.
2378 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002379
2380 }
2381
2382 public void updateKeyboardFromSettingsLocked() {
2383 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2384 if (mSwitchingDialog != null
2385 && mSwitchingDialogTitleView != null
2386 && mSwitchingDialog.isShowing()) {
2387 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2388 com.android.internal.R.id.hard_keyboard_switch);
2389 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002392
Yohei Yukawab097b822015-12-01 10:43:08 -08002393 private void notifyInputMethodSubtypeChanged(final int userId,
2394 @Nullable final InputMethodInfo inputMethodInfo,
2395 @Nullable final InputMethodSubtype subtype) {
2396 final InputManagerInternal inputManagerInternal =
2397 LocalServices.getService(InputManagerInternal.class);
2398 if (inputManagerInternal != null) {
2399 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2400 }
2401 }
2402
satokab751aa2010-09-14 19:17:36 +09002403 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 InputMethodInfo info = mMethodMap.get(id);
2405 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002406 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002408
satokd81e9502012-05-21 12:58:45 +09002409 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002411 final int subtypeCount = info.getSubtypeCount();
2412 if (subtypeCount <= 0) {
2413 return;
satokcd7cd292010-11-20 15:46:23 +09002414 }
satokd81e9502012-05-21 12:58:45 +09002415 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2416 final InputMethodSubtype newSubtype;
2417 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2418 newSubtype = info.getSubtypeAt(subtypeId);
2419 } else {
2420 // If subtype is null, try to find the most applicable one from
2421 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002422 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002423 }
2424 if (newSubtype == null || oldSubtype == null) {
2425 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2426 + ", new subtype = " + newSubtype);
2427 return;
2428 }
2429 if (newSubtype != oldSubtype) {
2430 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2431 if (mCurMethod != null) {
2432 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002433 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002434 mCurMethod.changeInputMethodSubtype(newSubtype);
2435 } catch (RemoteException e) {
2436 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002437 return;
satokab751aa2010-09-14 19:17:36 +09002438 }
2439 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002440 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 return;
2443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002444
satokd81e9502012-05-21 12:58:45 +09002445 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 final long ident = Binder.clearCallingIdentity();
2447 try {
satokab751aa2010-09-14 19:17:36 +09002448 // Set a subtype to this input method.
2449 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002450 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2451 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2452 // because mCurMethodId is stored as a history in
2453 // setSelectedInputMethodAndSubtypeLocked().
2454 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002456 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002458 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002460 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002462 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 } finally {
2464 Binder.restoreCallingIdentity(ident);
2465 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002466
2467 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2468 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002470
satok42c5a162011-05-26 16:46:14 +09002471 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002472 public boolean showSoftInput(IInputMethodClient client, int flags,
2473 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002474 if (!calledFromValidUser()) {
2475 return false;
2476 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002477 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 long ident = Binder.clearCallingIdentity();
2479 try {
2480 synchronized (mMethodMap) {
2481 if (mCurClient == null || client == null
2482 || mCurClient.client.asBinder() != client.asBinder()) {
2483 try {
2484 // We need to check if this is the current client with
2485 // focus in the window manager, to allow this call to
2486 // be made before input is started in it.
2487 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002488 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002489 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
2491 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002492 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
2494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002495
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002496 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002497 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 }
2499 } finally {
2500 Binder.restoreCallingIdentity(ident);
2501 }
2502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002503
The Android Open Source Project4df24232009-03-05 14:34:35 -08002504 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002506 if (mAccessibilityRequestingNoSoftKeyboard) {
2507 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 }
Anna Galusza9b278112016-01-04 11:37:37 -08002509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2511 mShowExplicitlyRequested = true;
2512 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002513 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2514 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002516
Dianne Hackborncc278702009-09-02 23:07:23 -07002517 if (!mSystemReady) {
2518 return false;
2519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002520
The Android Open Source Project4df24232009-03-05 14:34:35 -08002521 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002523 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002524 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2525 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2526 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002528 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002529 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002530 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002531 mVisibleBound = true;
2532 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002533 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002535 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 // The client has asked to have the input method shown, but
2537 // we have been sitting here too long with a connection to the
2538 // service and no interface received, so let's disconnect/connect
2539 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002540 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002542 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002544 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002545 } else {
2546 if (DEBUG) {
2547 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2548 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002551
The Android Open Source Project4df24232009-03-05 14:34:35 -08002552 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002554
satok42c5a162011-05-26 16:46:14 +09002555 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002556 public boolean hideSoftInput(IInputMethodClient client, int flags,
2557 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002558 if (!calledFromValidUser()) {
2559 return false;
2560 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002561 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 long ident = Binder.clearCallingIdentity();
2563 try {
2564 synchronized (mMethodMap) {
2565 if (mCurClient == null || client == null
2566 || mCurClient.client.asBinder() != client.asBinder()) {
2567 try {
2568 // We need to check if this is the current client with
2569 // focus in the window manager, to allow this call to
2570 // be made before input is started in it.
2571 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002572 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2573 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002574 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 }
2576 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002577 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002580
Joe Onorato8a9b2202010-02-26 18:56:32 -08002581 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002582 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 }
2584 } finally {
2585 Binder.restoreCallingIdentity(ident);
2586 }
2587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002588
The Android Open Source Project4df24232009-03-05 14:34:35 -08002589 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2591 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002592 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 -08002593 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
2595 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002596 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 -08002597 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002599
2600 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2601 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2602 // to be updated with the new value sent from IME process. Even in such a transient state
2603 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2604 // application process as a valid request, and have even promised such a behavior with CTS
2605 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2606 // IMMS#InputShown indicates that the software keyboard is shown.
2607 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2608 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2609 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002610 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002611 if (shouldHideSoftInput) {
2612 // The IME will report its visible state again after the following message finally
2613 // delivered to the IME process as an IPC. Hence the inconsistency between
2614 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2615 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002616 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2617 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2618 res = true;
2619 } else {
2620 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002622 if (mHaveConnection && mVisibleBound) {
2623 mContext.unbindService(mVisibleConnection);
2624 mVisibleBound = false;
2625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 mInputShown = false;
2627 mShowRequested = false;
2628 mShowExplicitlyRequested = false;
2629 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002630 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002632
satok42c5a162011-05-26 16:46:14 +09002633 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002634 public InputBindResult startInputOrWindowGainedFocus(
2635 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2636 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002637 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002638 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002639 if (windowToken != null) {
2640 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002641 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002642 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002643 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2644 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002645 }
2646 }
2647
2648 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002649 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002650 IInputMethodClient client, IBinder windowToken, int controlFlags,
2651 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002652 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2653 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002654 // Needs to check the validity before clearing calling identity
2655 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002656 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 long ident = Binder.clearCallingIdentity();
2658 try {
2659 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002660 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2661 + InputMethodClient.getStartInputReason(startInputReason)
2662 + " client=" + client.asBinder()
2663 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002664 + " missingMethods="
2665 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002666 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002667 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002668 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002669 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002670
Dianne Hackborn7663d802012-02-24 13:08:49 -08002671 ClientState cs = mClients.get(client.asBinder());
2672 if (cs == null) {
2673 throw new IllegalArgumentException("unknown client "
2674 + client.asBinder());
2675 }
2676
2677 try {
2678 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2679 // Check with the window manager to make sure this client actually
2680 // has a window with focus. If not, reject. This is thread safe
2681 // because if the focus changes some time before or after, the
2682 // next client receiving focus that has any interest in input will
2683 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002684 if (DEBUG) {
2685 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2686 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2687 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002688 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002690 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002692
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002693 if (!calledFromValidUser) {
2694 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2695 Slog.w(TAG, "If you want to interect with IME, you need "
2696 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2697 hideCurrentInputLocked(0, null);
2698 return null;
2699 }
2700
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002701 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002702 if (DEBUG) {
2703 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2704 + " attribute=" + attribute + ", token = " + windowToken);
2705 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002706 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002707 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002708 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002709 }
2710 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002711 }
2712 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002713 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002714 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002715
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002716 // Should we auto-show the IME even if the caller has not
2717 // specified what should be done with it?
2718 // We only do this automatically if the window can resize
2719 // to accommodate the IME (so what the user sees will give
2720 // them good context without input information being obscured
2721 // by the IME) or if running on a large screen where there
2722 // is more room for the target window + IME.
2723 final boolean doAutoShow =
2724 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2725 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2726 || mRes.getConfiguration().isLayoutSizeAtLeast(
2727 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002728 final boolean isTextEditor =
2729 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2730
2731 // We want to start input before showing the IME, but after closing
2732 // it. We want to do this after closing it to help the IME disappear
2733 // more quickly (not get stuck behind it initializing itself for the
2734 // new focused input, even if its window wants to hide the IME).
2735 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2738 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002739 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2741 // There is no focus view, and this window will
2742 // be behind any soft input window, so hide the
2743 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002744 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002745 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002747 } else if (isTextEditor && doAutoShow && (softInputMode &
2748 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 // There is a focus view, and we are navigating forward
2750 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002751 // We only do this automatically if the window can resize
2752 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002753 // them good context without input information being obscured
2754 // by the IME) or if running on a large screen where there
2755 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002756 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002757 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002758 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002759 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002760 didStart = true;
2761 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002762 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 }
2764 break;
2765 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2766 // Do nothing.
2767 break;
2768 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2769 if ((softInputMode &
2770 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002771 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002772 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 }
2774 break;
2775 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002776 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002777 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 break;
2779 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2780 if ((softInputMode &
2781 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002782 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002783 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002784 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002785 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002786 didStart = true;
2787 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002788 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 }
2790 break;
2791 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002792 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002793 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002794 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002795 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002796 didStart = true;
2797 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002798 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 break;
2800 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002801
2802 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002803 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002804 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 }
2807 } finally {
2808 Binder.restoreCallingIdentity(ident);
2809 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002810
2811 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002813
Guliz Tuncay6908c152017-06-02 16:06:10 -07002814 private boolean canShowInputMethodPickerLocked(IInputMethodClient client) {
2815 final int uid = Binder.getCallingUid();
2816 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
2817 return true;
2818 } else if (mCurClient != null && client != null
2819 && mCurClient.client.asBinder() == client.asBinder()) {
2820 return true;
2821 } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid(
2822 mAppOpsManager,
2823 uid,
2824 mCurIntent.getComponent().getPackageName())) {
2825 return true;
2826 } else if (mContext.checkCallingPermission(
2827 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2828 == PackageManager.PERMISSION_GRANTED) {
2829 return true;
2830 }
2831
2832 return false;
2833 }
2834
satok42c5a162011-05-26 16:46:14 +09002835 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002836 public void showInputMethodPickerFromClient(
2837 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002838 if (!calledFromValidUser()) {
2839 return;
2840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 synchronized (mMethodMap) {
Guliz Tuncay6908c152017-06-02 16:06:10 -07002842 if(!canShowInputMethodPickerLocked(client)) {
satok47a44912010-10-06 16:03:58 +09002843 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002844 + Binder.getCallingUid() + ": " + client);
Guliz Tuncay6908c152017-06-02 16:06:10 -07002845 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 }
2847
satok440aab52010-11-25 09:43:11 +09002848 // Always call subtype picker, because subtype picker is a superset of input method
2849 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002850 mHandler.sendMessage(mCaller.obtainMessageI(
2851 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002852 }
2853 }
2854
satok42c5a162011-05-26 16:46:14 +09002855 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002857 if (!calledFromValidUser()) {
2858 return;
2859 }
satok28203512010-11-24 11:06:49 +09002860 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2861 }
2862
satok42c5a162011-05-26 16:46:14 +09002863 @Override
satok28203512010-11-24 11:06:49 +09002864 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002865 if (!calledFromValidUser()) {
2866 return;
2867 }
satok28203512010-11-24 11:06:49 +09002868 synchronized (mMethodMap) {
2869 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002870 setInputMethodWithSubtypeIdLocked(token, id,
2871 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2872 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002873 } else {
2874 setInputMethod(token, id);
2875 }
2876 }
satokab751aa2010-09-14 19:17:36 +09002877 }
2878
satok42c5a162011-05-26 16:46:14 +09002879 @Override
satokb416a712010-11-25 20:42:14 +09002880 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002881 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002882 if (!calledFromValidUser()) {
2883 return;
2884 }
satokb416a712010-11-25 20:42:14 +09002885 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002886 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2887 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002888 }
2889 }
2890
satok4fc87d62011-05-20 16:13:43 +09002891 @Override
satok735cf382010-11-11 20:40:09 +09002892 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002893 if (!calledFromValidUser()) {
2894 return false;
2895 }
satok735cf382010-11-11 20:40:09 +09002896 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002897 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002898 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002899 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002900 lastImi = mMethodMap.get(lastIme.first);
2901 } else {
2902 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002903 }
satok4fc87d62011-05-20 16:13:43 +09002904 String targetLastImiId = null;
2905 int subtypeId = NOT_A_SUBTYPE_ID;
2906 if (lastIme != null && lastImi != null) {
2907 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002908 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002909 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2910 : mCurrentSubtype.hashCode();
2911 // If the last IME is the same as the current IME and the last subtype is not
2912 // defined, there is no need to switch to the last IME.
2913 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2914 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002915 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002916 }
2917 }
2918
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002919 if (TextUtils.isEmpty(targetLastImiId)
2920 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002921 // This is a safety net. If the currentSubtype can't be added to the history
2922 // and the framework couldn't find the last ime, we will make the last ime be
2923 // the most applicable enabled keyboard subtype of the system imes.
2924 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2925 if (enabled != null) {
2926 final int N = enabled.size();
2927 final String locale = mCurrentSubtype == null
2928 ? mRes.getConfiguration().locale.toString()
2929 : mCurrentSubtype.getLocale();
2930 for (int i = 0; i < N; ++i) {
2931 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002932 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002933 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002934 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2935 InputMethodUtils.getSubtypes(imi),
2936 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002937 if (keyboardSubtype != null) {
2938 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002939 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002940 imi, keyboardSubtype.hashCode());
2941 if(keyboardSubtype.getLocale().equals(locale)) {
2942 break;
2943 }
2944 }
2945 }
2946 }
2947 }
2948 }
2949
2950 if (!TextUtils.isEmpty(targetLastImiId)) {
2951 if (DEBUG) {
2952 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2953 + ", from: " + mCurMethodId + ", " + subtypeId);
2954 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002955 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002956 return true;
2957 } else {
2958 return false;
2959 }
satok735cf382010-11-11 20:40:09 +09002960 }
2961 }
2962
satoke7c6998e2011-06-03 17:57:59 +09002963 @Override
satok688bd472012-02-09 20:09:17 +09002964 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002965 if (!calledFromValidUser()) {
2966 return false;
2967 }
satok688bd472012-02-09 20:09:17 +09002968 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002969 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002970 return false;
2971 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002972 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002973 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2974 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002975 if (nextSubtype == null) {
2976 return false;
2977 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002978 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2979 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002980 return true;
2981 }
2982 }
2983
2984 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002985 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2986 if (!calledFromValidUser()) {
2987 return false;
2988 }
2989 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002990 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002991 return false;
2992 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002993 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002994 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2995 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002996 if (nextSubtype == null) {
2997 return false;
2998 }
2999 return true;
3000 }
3001 }
3002
3003 @Override
satok68f1b782011-04-11 14:26:04 +09003004 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003005 if (!calledFromValidUser()) {
3006 return null;
3007 }
satok68f1b782011-04-11 14:26:04 +09003008 synchronized (mMethodMap) {
3009 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
3010 // TODO: Handle the case of the last IME with no subtypes
3011 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
3012 || TextUtils.isEmpty(lastIme.second)) return null;
3013 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
3014 if (lastImi == null) return null;
3015 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003016 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003017 final int lastSubtypeId =
3018 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09003019 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
3020 return null;
3021 }
3022 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09003023 } catch (NumberFormatException e) {
3024 return null;
3025 }
3026 }
3027 }
3028
satoke7c6998e2011-06-03 17:57:59 +09003029 @Override
satokee5e77c2011-09-02 18:50:15 +09003030 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003031 if (!calledFromValidUser()) {
3032 return;
3033 }
satok91e88122011-07-18 11:11:42 +09003034 // By this IPC call, only a process which shares the same uid with the IME can add
3035 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08003036 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09003037 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08003038 if (!mSystemReady) {
3039 return;
3040 }
satok91e88122011-07-18 11:11:42 +09003041 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09003042 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003043 final String[] packageInfos;
3044 try {
3045 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3046 } catch (RemoteException e) {
3047 Slog.e(TAG, "Failed to get package infos");
3048 return;
3049 }
satok91e88122011-07-18 11:11:42 +09003050 if (packageInfos != null) {
3051 final int packageNum = packageInfos.length;
3052 for (int i = 0; i < packageNum; ++i) {
3053 if (packageInfos[i].equals(imi.getPackageName())) {
3054 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003055 final long ident = Binder.clearCallingIdentity();
3056 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003057 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003058 } finally {
3059 Binder.restoreCallingIdentity(ident);
3060 }
satokee5e77c2011-09-02 18:50:15 +09003061 return;
satok91e88122011-07-18 11:11:42 +09003062 }
3063 }
3064 }
satoke7c6998e2011-06-03 17:57:59 +09003065 }
satokee5e77c2011-09-02 18:50:15 +09003066 return;
satoke7c6998e2011-06-03 17:57:59 +09003067 }
3068
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003069 @Override
3070 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003071 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003072 }
3073
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003074 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003075 public void clearLastInputMethodWindowForTransition(IBinder token) {
3076 if (!calledFromValidUser()) {
3077 return;
3078 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003079 synchronized (mMethodMap) {
3080 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003081 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003082 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003083 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003084 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003085 }
3086
3087 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003088 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003089 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003090 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003091 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003092 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003093 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3094 if (DEBUG) {
3095 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3096 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3097 + " actual: " + sequenceNumber);
3098 }
3099 return;
3100 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003101 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3102 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003103 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003104 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003105 }
3106 }
3107
satok28203512010-11-24 11:06:49 +09003108 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003110 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3111 }
3112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003114 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3115 if (token == null) {
3116 if (mContext.checkCallingOrSelfPermission(
3117 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3118 != PackageManager.PERMISSION_GRANTED) {
3119 throw new SecurityException(
3120 "Using null token requires permission "
3121 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003123 } else if (mCurToken != token) {
3124 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3125 + " token: " + token);
3126 return;
3127 }
3128
3129 final long ident = Binder.clearCallingIdentity();
3130 try {
3131 setInputMethodLocked(id, subtypeId);
3132 } finally {
3133 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 }
3135 }
3136
satok42c5a162011-05-26 16:46:14 +09003137 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003139 if (!calledFromValidUser()) {
3140 return;
3141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003143 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 return;
3145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 long ident = Binder.clearCallingIdentity();
3147 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003148 hideCurrentInputLocked(flags, null);
3149 } finally {
3150 Binder.restoreCallingIdentity(ident);
3151 }
3152 }
3153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003154
satok42c5a162011-05-26 16:46:14 +09003155 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003156 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003157 if (!calledFromValidUser()) {
3158 return;
3159 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003160 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003161 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003162 return;
3163 }
3164 long ident = Binder.clearCallingIdentity();
3165 try {
3166 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 } finally {
3168 Binder.restoreCallingIdentity(ident);
3169 }
3170 }
3171 }
3172
3173 void setEnabledSessionInMainThread(SessionState session) {
3174 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003175 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003177 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003178 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 } catch (RemoteException e) {
3180 }
3181 }
3182 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003183 if (mEnabledSession != null && mEnabledSession.session != null) {
3184 try {
3185 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3186 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3187 } catch (RemoteException e) {
3188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 }
3190 }
3191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003192
satok42c5a162011-05-26 16:46:14 +09003193 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003195 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003197 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003198 final boolean showAuxSubtypes;
3199 switch (msg.arg1) {
3200 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3201 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3202 // implemented so that auxiliary subtypes will be excluded when the soft
3203 // keyboard is invisible.
3204 showAuxSubtypes = mInputShown;
3205 break;
3206 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3207 showAuxSubtypes = true;
3208 break;
3209 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3210 showAuxSubtypes = false;
3211 break;
3212 default:
3213 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3214 return false;
3215 }
3216 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003217 return true;
3218
satok47a44912010-10-06 16:03:58 +09003219 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003220 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003221 return true;
3222
3223 case MSG_SHOW_IM_CONFIG:
3224 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003225 return true;
3226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 case MSG_UNBIND_INPUT:
3230 try {
3231 ((IInputMethod)msg.obj).unbindInput();
3232 } catch (RemoteException e) {
3233 // There is nothing interesting about the method dying.
3234 }
3235 return true;
3236 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003237 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 try {
3239 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3240 } catch (RemoteException e) {
3241 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003242 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 return true;
3244 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003245 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003247 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003248 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003249 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 } catch (RemoteException e) {
3251 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003252 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 return true;
3254 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003255 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003257 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003258 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003259 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 } catch (RemoteException e) {
3261 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003262 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003264 case MSG_HIDE_CURRENT_INPUT_METHOD:
3265 synchronized (mMethodMap) {
3266 hideCurrentInputLocked(0, null);
3267 }
3268 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003270 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003272 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3274 } catch (RemoteException e) {
3275 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003276 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003278 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003279 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003280 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003281 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003283 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003285 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003286 // Dispose the channel if the input method is not local to this process
3287 // because the remote proxy will get its own copy when unparceled.
3288 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003289 channel.dispose();
3290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003292 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003296
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003297 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003298 final int missingMethods = msg.arg1;
3299 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003300 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003301 final IBinder startInputToken = (IBinder) args.arg1;
3302 final SessionState session = (SessionState) args.arg2;
3303 final IInputContext inputContext = (IInputContext) args.arg3;
3304 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003307 session.method.startInput(startInputToken, inputContext, missingMethods,
3308 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 } catch (RemoteException e) {
3310 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003311 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003316
Yohei Yukawa33e81792015-11-17 21:14:42 -08003317 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003319 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 } catch (RemoteException e) {
3321 // There is nothing interesting about the last client dying.
3322 }
3323 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003324 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003325 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003326 IInputMethodClient client = (IInputMethodClient)args.arg1;
3327 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003329 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003331 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003332 } finally {
3333 // Dispose the channel if the input method is not local to this process
3334 // because the remote proxy will get its own copy when unparceled.
3335 if (res.channel != null && Binder.isProxy(client)) {
3336 res.channel.dispose();
3337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003339 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003341 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003342 case MSG_SET_ACTIVE:
3343 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003344 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003345 } catch (RemoteException e) {
3346 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3347 + ((ClientState)msg.obj).pid + " uid "
3348 + ((ClientState)msg.obj).uid);
3349 }
3350 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003351 case MSG_SET_INTERACTIVE:
3352 handleSetInteractive(msg.arg1 != 0);
3353 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003354 case MSG_SWITCH_IME:
3355 handleSwitchInputMethod(msg.arg1 != 0);
3356 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003357 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3358 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003359 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003360 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003361 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003362 } catch (RemoteException e) {
3363 Slog.w(TAG, "Got RemoteException sending "
3364 + "setUserActionNotificationSequenceNumber("
3365 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003366 + clientState.pid + " uid "
3367 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003368 }
3369 return true;
3370 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003371 case MSG_REPORT_FULLSCREEN_MODE: {
3372 final boolean fullscreen = msg.arg1 != 0;
3373 final ClientState clientState = (ClientState)msg.obj;
3374 try {
3375 clientState.client.reportFullscreenMode(fullscreen);
3376 } catch (RemoteException e) {
3377 Slog.w(TAG, "Got RemoteException sending "
3378 + "reportFullscreen(" + fullscreen + ") notification to pid="
3379 + clientState.pid + " uid=" + clientState.uid);
3380 }
3381 return true;
3382 }
satok01038492012-04-09 21:08:27 +09003383
3384 // --------------------------------------------------------------
3385 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003386 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003387 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003388 case MSG_SYSTEM_UNLOCK_USER:
3389 final int userId = msg.arg1;
3390 onUnlockUser(userId);
3391 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 }
3393 return false;
3394 }
3395
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003396 private void handleSetInteractive(final boolean interactive) {
3397 synchronized (mMethodMap) {
3398 mIsInteractive = interactive;
3399 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3400
3401 // Inform the current client of the change in active status
3402 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003403 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3404 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3405 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003406 }
3407 }
3408 }
3409
Yohei Yukawaae61f712015-12-09 13:00:10 -08003410 private void handleSwitchInputMethod(final boolean forwardDirection) {
3411 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003412 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003413 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003414 if (nextSubtype == null) {
3415 return;
3416 }
3417 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003418 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3419 if (newInputMethodInfo == null) {
3420 return;
3421 }
3422 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3423 newInputMethodInfo, mCurrentSubtype);
3424 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003425 if (mSubtypeSwitchedByShortCutToast == null) {
3426 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3427 Toast.LENGTH_SHORT);
3428 } else {
3429 mSubtypeSwitchedByShortCutToast.setText(toastText);
3430 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003431 mSubtypeSwitchedByShortCutToast.show();
3432 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003433 }
3434 }
3435
satokdc9ddae2011-10-06 12:22:36 +09003436 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003437 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3438 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003439 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003440 if (DEBUG) {
3441 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3442 }
satok723a27e2010-11-11 14:58:11 +09003443 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003444 return true;
3445 }
3446
3447 return false;
3448 }
3449
Yohei Yukawa94e33302016-02-12 19:37:03 -08003450 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003451 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003452 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003453 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003454 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003455 if (!mSystemReady) {
3456 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3457 return;
3458 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003459 mMethodList.clear();
3460 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003461 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003462 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003463
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003464 // Use for queryIntentServicesAsUser
3465 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003467 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3468 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3469 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003470 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003472 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3473 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003474
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003475 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003476 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 for (int i = 0; i < services.size(); ++i) {
3478 ResolveInfo ri = services.get(i);
3479 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003480 final String imeId = InputMethodInfo.computeId(ri);
3481 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3482 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 + ": it does not require the permission "
3484 + android.Manifest.permission.BIND_INPUT_METHOD);
3485 continue;
3486 }
3487
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003488 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003490 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 try {
satoke7c6998e2011-06-03 17:57:59 +09003492 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003493 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003494 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003495 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496
3497 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003498 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003500 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003501 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 }
3503 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003504
Yohei Yukawac4e44912017-02-09 19:30:22 -08003505 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3506 // negatives when the package state remains to be the same but only the component state is
3507 // changed.
3508 {
3509 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3510 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3511 // conservative, but it seems we cannot use it for now (Issue 35176630).
3512 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3513 new Intent(InputMethod.SERVICE_INTERFACE),
3514 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3515 final int N = allInputMethodServices.size();
3516 for (int i = 0; i < N; ++i) {
3517 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003518 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3519 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003520 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003521 }
3522 }
3523
Yohei Yukawa859df052016-02-17 07:56:46 -08003524 // TODO: The following code should find better place to live.
3525 if (!resetDefaultEnabledIme) {
3526 boolean enabledImeFound = false;
3527 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3528 final int N = enabledImes.size();
3529 for (int i = 0; i < N; ++i) {
3530 final InputMethodInfo imi = enabledImes.get(i);
3531 if (mMethodList.contains(imi)) {
3532 enabledImeFound = true;
3533 break;
3534 }
3535 }
3536 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003537 if (DEBUG) {
3538 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3539 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003540 resetDefaultEnabledIme = true;
3541 resetSelectedInputMethodAndSubtypeLocked("");
3542 }
3543 }
3544
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003545 if (resetDefaultEnabledIme) {
3546 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003547 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003548 final int N = defaultEnabledIme.size();
3549 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003550 final InputMethodInfo imi = defaultEnabledIme.get(i);
3551 if (DEBUG) {
3552 Slog.d(TAG, "--- enable ime = " + imi);
3553 }
3554 setInputMethodEnabledLocked(imi.getId(), true);
3555 }
3556 }
3557
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003558 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003559 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003560 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003561 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3562 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003563 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003564 }
3565 } else {
3566 // Double check that the default IME is certainly enabled.
3567 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003568 }
3569 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003570 // Here is not the perfect place to reset the switching controller. Ideally
3571 // mSwitchingController and mSettings should be able to share the same state.
3572 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3573 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003574 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003578
satok217f5482010-12-15 05:19:19 +09003579 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003580 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003581 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003582 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3583 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003584 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003585 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003586 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003587 final int userId;
3588 synchronized (mMethodMap) {
3589 userId = mSettings.getCurrentUserId();
3590 }
3591 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003592 }
3593
3594 private void showConfigureInputMethods() {
3595 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3596 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3597 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3598 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003599 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003600 }
3601
satok2c93efc2012-04-02 19:33:47 +09003602 private boolean isScreenLocked() {
3603 return mKeyguardManager != null
3604 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3605 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003606
Seigo Nonaka14e13912015-05-06 21:04:13 -07003607 private void showInputMethodMenu(boolean showAuxSubtypes) {
3608 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609
satok2c93efc2012-04-02 19:33:47 +09003610 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003611
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003612 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003613 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003614 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003615
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003616 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003617 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003618 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3619 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003620 if (immis == null || immis.size() == 0) {
3621 return;
3622 }
3623
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003624 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625
satok688bd472012-02-09 20:09:17 +09003626 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003627 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003628 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003629
satokc3690562012-01-10 20:14:43 +09003630 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003631 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003632 if (currentSubtype != null) {
3633 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003634 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3635 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003636 }
3637 }
3638
Ken Wakasa761eb372011-03-04 19:06:18 +09003639 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003640 mIms = new InputMethodInfo[N];
3641 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003642 int checkedItem = 0;
3643 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003644 final ImeSubtypeListItem item = imList.get(i);
3645 mIms[i] = item.mImi;
3646 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003647 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003648 int subtypeId = mSubtypeIds[i];
3649 if ((subtypeId == NOT_A_SUBTYPE_ID)
3650 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3651 || (subtypeId == lastInputMethodSubtypeId)) {
3652 checkedItem = i;
3653 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003656
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003657 final Context settingsContext = new ContextThemeWrapper(
3658 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003659 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3660
3661 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003662 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3663 @Override
3664 public void onCancel(DialogInterface dialog) {
3665 hideInputMethodMenu();
3666 }
3667 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003668
3669 final Context dialogContext = mDialogBuilder.getContext();
3670 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3671 com.android.internal.R.styleable.DialogPreference,
3672 com.android.internal.R.attr.alertDialogStyle, 0);
3673 final Drawable dialogIcon = a.getDrawable(
3674 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3675 a.recycle();
3676
3677 mDialogBuilder.setIcon(dialogIcon);
3678
Yohei Yukawad34e1482016-02-11 08:03:52 -08003679 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003680 final View tv = inflater.inflate(
3681 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3682 mDialogBuilder.setCustomTitle(tv);
3683
3684 // Setup layout for a toggle switch of the hardware keyboard
3685 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003686 mSwitchingDialogTitleView
3687 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003688 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003689 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003690 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003691 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003692 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003693 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3694 @Override
3695 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003696 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003697 // Ensure that the input method dialog is dismissed when changing
3698 // the hardware keyboard state.
3699 hideInputMethodMenu();
3700 }
3701 });
3702
Alan Viverette505e3ab2014-11-24 15:22:11 -08003703 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003704 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3705 final OnClickListener choiceListener = new OnClickListener() {
3706 @Override
3707 public void onClick(final DialogInterface dialog, final int which) {
3708 synchronized (mMethodMap) {
3709 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3710 || mSubtypeIds.length <= which) {
3711 return;
satok01038492012-04-09 21:08:27 +09003712 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003713 final InputMethodInfo im = mIms[which];
3714 int subtypeId = mSubtypeIds[which];
3715 adapter.mCheckedItem = which;
3716 adapter.notifyDataSetChanged();
3717 hideInputMethodMenu();
3718 if (im != null) {
3719 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3720 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003721 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003722 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003724 }
3725 }
3726 };
3727 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003730 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003731 final Window w = mSwitchingDialog.getWindow();
3732 final WindowManager.LayoutParams attrs = w.getAttributes();
3733 w.setType(TYPE_INPUT_METHOD_DIALOG);
3734 // Use an alternate token for the dialog for that window manager can group the token
3735 // with other IME windows based on type vs. grouping based on whichever token happens
3736 // to get selected by the system later on.
3737 attrs.token = mSwitchingDialogToken;
3738 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3739 attrs.setTitle("Select input method");
3740 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003741 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742 mSwitchingDialog.show();
3743 }
3744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003745
Ken Wakasa05dbb652011-08-22 15:22:43 +09003746 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3747 private final LayoutInflater mInflater;
3748 private final int mTextViewResourceId;
3749 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003750 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003751 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3752 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3753 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003754
Ken Wakasa05dbb652011-08-22 15:22:43 +09003755 mTextViewResourceId = textViewResourceId;
3756 mItemsList = itemsList;
3757 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003758 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003759 }
3760
3761 @Override
3762 public View getView(int position, View convertView, ViewGroup parent) {
3763 final View view = convertView != null ? convertView
3764 : mInflater.inflate(mTextViewResourceId, null);
3765 if (position < 0 || position >= mItemsList.size()) return view;
3766 final ImeSubtypeListItem item = mItemsList.get(position);
3767 final CharSequence imeName = item.mImeName;
3768 final CharSequence subtypeName = item.mSubtypeName;
3769 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3770 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3771 if (TextUtils.isEmpty(subtypeName)) {
3772 firstTextView.setText(imeName);
3773 secondTextView.setVisibility(View.GONE);
3774 } else {
3775 firstTextView.setText(subtypeName);
3776 secondTextView.setText(imeName);
3777 secondTextView.setVisibility(View.VISIBLE);
3778 }
3779 final RadioButton radioButton =
3780 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3781 radioButton.setChecked(position == mCheckedItem);
3782 return view;
3783 }
3784 }
3785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003787 synchronized (mMethodMap) {
3788 hideInputMethodMenuLocked();
3789 }
3790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003791
The Android Open Source Project10592532009-03-18 17:39:46 -07003792 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003793 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794
The Android Open Source Project10592532009-03-18 17:39:46 -07003795 if (mSwitchingDialog != null) {
3796 mSwitchingDialog.dismiss();
3797 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003799
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003800 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003801 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003802 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003806
satok42c5a162011-05-26 16:46:14 +09003807 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003809 // TODO: Make this work even for non-current users?
3810 if (!calledFromValidUser()) {
3811 return false;
3812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 synchronized (mMethodMap) {
3814 if (mContext.checkCallingOrSelfPermission(
3815 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3816 != PackageManager.PERMISSION_GRANTED) {
3817 throw new SecurityException(
3818 "Requires permission "
3819 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3820 }
Anna Galusza9b278112016-01-04 11:37:37 -08003821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 long ident = Binder.clearCallingIdentity();
3823 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003824 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 } finally {
3826 Binder.restoreCallingIdentity(ident);
3827 }
3828 }
3829 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003830
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003831 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3832 // Make sure this is a valid input method.
3833 InputMethodInfo imm = mMethodMap.get(id);
3834 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003835 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003836 }
3837
satokd87c2592010-09-29 11:52:06 +09003838 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3839 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003840
satokd87c2592010-09-29 11:52:06 +09003841 if (enabled) {
3842 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3843 if (pair.first.equals(id)) {
3844 // We are enabling this input method, but it is already enabled.
3845 // Nothing to do. The previous state was enabled.
3846 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003847 }
3848 }
satokd87c2592010-09-29 11:52:06 +09003849 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3850 // Previous state was disabled.
3851 return false;
3852 } else {
3853 StringBuilder builder = new StringBuilder();
3854 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3855 builder, enabledInputMethodsList, id)) {
3856 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003857 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003858 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3859 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3860 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003861 }
3862 // Previous state was enabled.
3863 return true;
3864 } else {
3865 // We are disabling the input method but it is already disabled.
3866 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003867 return false;
3868 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003869 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003870 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003871
satok723a27e2010-11-11 14:58:11 +09003872 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3873 boolean setSubtypeOnly) {
3874 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003875 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003876
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003877 mCurUserActionNotificationSequenceNumber =
3878 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3879 if (DEBUG) {
3880 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3881 + mCurUserActionNotificationSequenceNumber);
3882 }
3883
3884 if (mCurClient != null && mCurClient.client != null) {
3885 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3886 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003887 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003888 }
3889
satok723a27e2010-11-11 14:58:11 +09003890 // Set Subtype here
3891 if (imi == null || subtypeId < 0) {
3892 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003893 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003894 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003895 if (subtypeId < imi.getSubtypeCount()) {
3896 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3897 mSettings.putSelectedSubtype(subtype.hashCode());
3898 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003899 } else {
3900 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003901 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003902 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003903 }
satokab751aa2010-09-14 19:17:36 +09003904 }
satok723a27e2010-11-11 14:58:11 +09003905
Yohei Yukawa68645a62016-02-17 07:54:20 -08003906 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003907 // Set InputMethod here
3908 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3909 }
3910 }
3911
3912 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3913 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3914 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3915 // newDefaultIme is empty when there is no candidate for the selected IME.
3916 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3917 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3918 if (subtypeHashCode != null) {
3919 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003920 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003921 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003922 } catch (NumberFormatException e) {
3923 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3924 }
3925 }
3926 }
3927 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003928 }
3929
satok4e4569d2010-11-19 18:45:53 +09003930 // If there are no selected shortcuts, tries finding the most applicable ones.
3931 private Pair<InputMethodInfo, InputMethodSubtype>
3932 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3933 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3934 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003935 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003936 boolean foundInSystemIME = false;
3937
3938 // Search applicable subtype for each InputMethodInfo
3939 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003940 final String imiId = imi.getId();
3941 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3942 continue;
3943 }
satokcd7cd292010-11-20 15:46:23 +09003944 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003945 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003946 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003947 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003948 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003949 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003950 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003951 }
satokdf31ae62011-01-15 06:19:44 +09003952 // 2. Search by the system locale from enabledSubtypes.
3953 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003954 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003955 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003956 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003957 }
satoka86f5e42011-09-02 17:12:42 +09003958 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003959 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003960 final ArrayList<InputMethodSubtype> subtypesForSearch =
3961 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003962 ? InputMethodUtils.getSubtypes(imi)
3963 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003964 // 4. Search by the current subtype's locale from all subtypes.
3965 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003966 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003967 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003968 }
3969 // 5. Search by the system locale from all subtypes.
3970 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003971 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003972 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003973 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003974 }
satokcd7cd292010-11-20 15:46:23 +09003975 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003976 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003977 // The current input method is the most applicable IME.
3978 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003979 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003980 break;
satok7599a7f2010-12-22 13:45:23 +09003981 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003982 // The system input method is 2nd applicable IME.
3983 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003984 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003985 if ((imi.getServiceInfo().applicationInfo.flags
3986 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3987 foundInSystemIME = true;
3988 }
satok4e4569d2010-11-19 18:45:53 +09003989 }
3990 }
3991 }
3992 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003993 if (mostApplicableIMI != null) {
3994 Slog.w(TAG, "Most applicable shortcut input method was:"
3995 + mostApplicableIMI.getId());
3996 if (mostApplicableSubtype != null) {
3997 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3998 + "," + mostApplicableSubtype.getMode() + ","
3999 + mostApplicableSubtype.getLocale());
4000 }
4001 }
satok4e4569d2010-11-19 18:45:53 +09004002 }
satokcd7cd292010-11-20 15:46:23 +09004003 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004004 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09004005 } else {
4006 return null;
4007 }
4008 }
4009
satokab751aa2010-09-14 19:17:36 +09004010 /**
4011 * @return Return the current subtype of this input method.
4012 */
satok42c5a162011-05-26 16:46:14 +09004013 @Override
satokab751aa2010-09-14 19:17:36 +09004014 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004015 // TODO: Make this work even for non-current users?
4016 if (!calledFromValidUser()) {
4017 return null;
4018 }
4019 synchronized (mMethodMap) {
4020 return getCurrentInputMethodSubtypeLocked();
4021 }
4022 }
4023
4024 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09004025 if (mCurMethodId == null) {
4026 return null;
4027 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004028 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004029 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
4030 if (imi == null || imi.getSubtypeCount() == 0) {
4031 return null;
satok4e4569d2010-11-19 18:45:53 +09004032 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004033 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004034 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
4035 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004036 if (subtypeId == NOT_A_SUBTYPE_ID) {
4037 // If there are no selected subtypes, the framework will try to find
4038 // the most applicable subtype from explicitly or implicitly enabled
4039 // subtypes.
4040 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004041 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004042 // If there is only one explicitly or implicitly enabled subtype,
4043 // just returns it.
4044 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4045 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4046 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004047 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004048 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004049 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004050 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004051 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004052 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4053 true);
satok4e4569d2010-11-19 18:45:53 +09004054 }
satok3ef8b292010-11-23 06:06:29 +09004055 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004056 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004057 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004058 }
4059 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004060 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004061 }
4062
satok4e4569d2010-11-19 18:45:53 +09004063 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004064 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004065 @Override
satok4e4569d2010-11-19 18:45:53 +09004066 public List getShortcutInputMethodsAndSubtypes() {
4067 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004068 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004069 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004070 // If there are no selected shortcut subtypes, the framework will try to find
4071 // the most applicable subtype from all subtypes whose mode is
4072 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004073 Pair<InputMethodInfo, InputMethodSubtype> info =
4074 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004075 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004076 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004077 ret.add(info.first);
4078 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004079 }
satok3da92232011-01-11 22:46:30 +09004080 return ret;
satokf3db1af2010-11-23 13:34:33 +09004081 }
satokf3db1af2010-11-23 13:34:33 +09004082 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4083 ret.add(imi);
4084 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4085 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004086 }
4087 }
satokf3db1af2010-11-23 13:34:33 +09004088 return ret;
satok4e4569d2010-11-19 18:45:53 +09004089 }
4090 }
4091
satok42c5a162011-05-26 16:46:14 +09004092 @Override
satokb66d2872010-11-10 01:04:04 +09004093 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004094 // TODO: Make this work even for non-current users?
4095 if (!calledFromValidUser()) {
4096 return false;
4097 }
satokb66d2872010-11-10 01:04:04 +09004098 synchronized (mMethodMap) {
4099 if (subtype != null && mCurMethodId != null) {
4100 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004101 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004102 if (subtypeId != NOT_A_SUBTYPE_ID) {
4103 setInputMethodLocked(mCurMethodId, subtypeId);
4104 return true;
4105 }
4106 }
4107 return false;
4108 }
4109 }
4110
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004111 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004112 private static class InputMethodFileManager {
4113 private static final String SYSTEM_PATH = "system";
4114 private static final String INPUT_METHOD_PATH = "inputmethod";
4115 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4116 private static final String NODE_SUBTYPES = "subtypes";
4117 private static final String NODE_SUBTYPE = "subtype";
4118 private static final String NODE_IMI = "imi";
4119 private static final String ATTR_ID = "id";
4120 private static final String ATTR_LABEL = "label";
4121 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004122 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004123 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004124 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004125 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4126 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4127 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004128 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004129 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4130 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004131 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004132 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004133 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004134 if (methodMap == null) {
4135 throw new NullPointerException("methodMap is null");
4136 }
4137 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004138 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004139 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4140 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004141 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004142 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004143 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4144 }
4145 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4146 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4147 if (!subtypeFile.exists()) {
4148 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004149 writeAdditionalInputMethodSubtypes(
4150 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004151 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004152 readAdditionalInputMethodSubtypes(
4153 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004154 }
4155 }
4156
4157 private void deleteAllInputMethodSubtypes(String imiId) {
4158 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004159 mAdditionalSubtypesMap.remove(imiId);
4160 writeAdditionalInputMethodSubtypes(
4161 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004162 }
4163 }
4164
4165 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004166 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004167 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004168 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004169 final int N = additionalSubtypes.length;
4170 for (int i = 0; i < N; ++i) {
4171 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004172 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004173 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004174 } else {
4175 Slog.w(TAG, "Duplicated subtype definition found: "
4176 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004177 }
4178 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004179 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4180 writeAdditionalInputMethodSubtypes(
4181 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004182 }
4183 }
4184
4185 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4186 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004187 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004188 }
4189 }
4190
4191 private static void writeAdditionalInputMethodSubtypes(
4192 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4193 HashMap<String, InputMethodInfo> methodMap) {
4194 // Safety net for the case that this function is called before methodMap is set.
4195 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4196 FileOutputStream fos = null;
4197 try {
4198 fos = subtypesFile.startWrite();
4199 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004200 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004201 out.startDocument(null, true);
4202 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4203 out.startTag(null, NODE_SUBTYPES);
4204 for (String imiId : allSubtypes.keySet()) {
4205 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4206 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4207 continue;
4208 }
4209 out.startTag(null, NODE_IMI);
4210 out.attribute(null, ATTR_ID, imiId);
4211 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4212 final int N = subtypesList.size();
4213 for (int i = 0; i < N; ++i) {
4214 final InputMethodSubtype subtype = subtypesList.get(i);
4215 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004216 if (subtype.hasSubtypeId()) {
4217 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4218 String.valueOf(subtype.getSubtypeId()));
4219 }
satoke7c6998e2011-06-03 17:57:59 +09004220 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4221 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4222 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004223 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4224 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004225 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4226 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4227 out.attribute(null, ATTR_IS_AUXILIARY,
4228 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004229 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4230 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004231 out.endTag(null, NODE_SUBTYPE);
4232 }
4233 out.endTag(null, NODE_IMI);
4234 }
4235 out.endTag(null, NODE_SUBTYPES);
4236 out.endDocument();
4237 subtypesFile.finishWrite(fos);
4238 } catch (java.io.IOException e) {
4239 Slog.w(TAG, "Error writing subtypes", e);
4240 if (fos != null) {
4241 subtypesFile.failWrite(fos);
4242 }
4243 }
4244 }
4245
4246 private static void readAdditionalInputMethodSubtypes(
4247 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4248 if (allSubtypes == null || subtypesFile == null) return;
4249 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004250 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004251 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004252 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004253 int type = parser.getEventType();
4254 // Skip parsing until START_TAG
4255 while ((type = parser.next()) != XmlPullParser.START_TAG
4256 && type != XmlPullParser.END_DOCUMENT) {}
4257 String firstNodeName = parser.getName();
4258 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4259 throw new XmlPullParserException("Xml doesn't start with subtypes");
4260 }
4261 final int depth =parser.getDepth();
4262 String currentImiId = null;
4263 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4264 while (((type = parser.next()) != XmlPullParser.END_TAG
4265 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4266 if (type != XmlPullParser.START_TAG)
4267 continue;
4268 final String nodeName = parser.getName();
4269 if (NODE_IMI.equals(nodeName)) {
4270 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4271 if (TextUtils.isEmpty(currentImiId)) {
4272 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4273 continue;
4274 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004275 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004276 allSubtypes.put(currentImiId, tempSubtypesArray);
4277 } else if (NODE_SUBTYPE.equals(nodeName)) {
4278 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4279 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4280 continue;
4281 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004282 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004283 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004284 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004285 parser.getAttributeValue(null, ATTR_LABEL));
4286 final String imeSubtypeLocale =
4287 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004288 final String languageTag =
4289 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004290 final String imeSubtypeMode =
4291 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4292 final String imeSubtypeExtraValue =
4293 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004294 final boolean isAuxiliary = "1".equals(String.valueOf(
4295 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004296 final boolean isAsciiCapable = "1".equals(String.valueOf(
4297 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004298 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004299 .setSubtypeNameResId(label)
4300 .setSubtypeIconResId(icon)
4301 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004302 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004303 .setSubtypeMode(imeSubtypeMode)
4304 .setSubtypeExtraValue(imeSubtypeExtraValue)
4305 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004306 .setIsAsciiCapable(isAsciiCapable);
4307 final String subtypeIdString =
4308 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4309 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004310 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004311 }
4312 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004313 }
4314 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004315 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4316 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004317 return;
satoke7c6998e2011-06-03 17:57:59 +09004318 }
4319 }
4320 }
4321
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004322 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4323 @NonNull
4324 private final Handler mHandler;
4325
4326 LocalServiceImpl(@NonNull final Handler handler) {
4327 mHandler = handler;
4328 }
4329
4330 @Override
4331 public void setInteractive(boolean interactive) {
4332 // Do everything in handler so as not to block the caller.
4333 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4334 interactive ? 1 : 0, 0));
4335 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004336
4337 @Override
4338 public void switchInputMethod(boolean forwardDirection) {
4339 // Do everything in handler so as not to block the caller.
4340 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4341 forwardDirection ? 1 : 0, 0));
4342 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004343
4344 @Override
4345 public void hideCurrentInputMethod() {
4346 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4347 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4348 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004349 }
4350
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004351 private static String imeWindowStatusToString(final int imeWindowVis) {
4352 final StringBuilder sb = new StringBuilder();
4353 boolean first = true;
4354 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4355 sb.append("Active");
4356 first = false;
4357 }
4358 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4359 if (!first) {
4360 sb.append("|");
4361 }
4362 sb.append("Visible");
4363 }
4364 return sb.toString();
4365 }
4366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004368 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4369 @Nullable Uri contentUri, @Nullable String packageName) {
4370 if (!calledFromValidUser()) {
4371 return null;
4372 }
4373
4374 if (token == null) {
4375 throw new NullPointerException("token");
4376 }
4377 if (packageName == null) {
4378 throw new NullPointerException("packageName");
4379 }
4380 if (contentUri == null) {
4381 throw new NullPointerException("contentUri");
4382 }
4383 final String contentUriScheme = contentUri.getScheme();
4384 if (!"content".equals(contentUriScheme)) {
4385 throw new InvalidParameterException("contentUri must have content scheme");
4386 }
4387
4388 synchronized (mMethodMap) {
4389 final int uid = Binder.getCallingUid();
4390 if (mCurMethodId == null) {
4391 return null;
4392 }
4393 if (mCurToken != token) {
4394 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4395 + " token=" + token);
4396 return null;
4397 }
4398 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4399 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4400 // nature of our system. Let's compare it with our internal record.
4401 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4402 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4403 + mCurAttribute.packageName + " packageName=" + packageName);
4404 return null;
4405 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004406 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004407 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004408 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004409 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004410 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4411 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4412 imeUserId);
4413 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4414 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4415 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4416 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4417 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4418 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4419 // actually allowed to "uid", which is guaranteed to be the IME's one.
4420 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4421 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004422 }
4423 }
4424
4425 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004426 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4427 if (!calledFromValidUser()) {
4428 return;
4429 }
4430 synchronized (mMethodMap) {
4431 if (!calledWithValidToken(token)) {
4432 return;
4433 }
4434 if (mCurClient != null && mCurClient.client != null) {
4435 mInFullscreenMode = fullscreen;
4436 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4437 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4438 }
4439 }
4440 }
4441
4442 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004444 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445
4446 IInputMethod method;
4447 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004448 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 synchronized (mMethodMap) {
4453 p.println("Current Input Method Manager state:");
4454 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004455 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 for (int i=0; i<N; i++) {
4457 InputMethodInfo info = mMethodList.get(i);
4458 p.println(" InputMethod #" + i + ":");
4459 info.dump(p, " ");
4460 }
4461 p.println(" Clients:");
4462 for (ClientState ci : mClients.values()) {
4463 p.println(" Client " + ci + ":");
4464 p.println(" client=" + ci.client);
4465 p.println(" inputContext=" + ci.inputContext);
4466 p.println(" sessionRequested=" + ci.sessionRequested);
4467 p.println(" curSession=" + ci.curSession);
4468 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004469 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004471 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004472 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4473 + " softInputMode=" +
4474 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4475 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004476 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4478 + " mBoundToMethod=" + mBoundToMethod);
4479 p.println(" mCurToken=" + mCurToken);
4480 p.println(" mCurIntent=" + mCurIntent);
4481 method = mCurMethod;
4482 p.println(" mCurMethod=" + mCurMethod);
4483 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004484 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 p.println(" mShowRequested=" + mShowRequested
4486 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4487 + " mShowForced=" + mShowForced
4488 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004489 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004490 p.println(" mCurUserActionNotificationSequenceNumber="
4491 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004492 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004493 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004494 p.println(" mSwitchingController:");
4495 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004496 p.println(" mSettings:");
4497 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004498
4499 p.println(" mStartInputHistory:");
4500 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004502
Jeff Brownb88102f2010-09-08 11:49:43 -07004503 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 pw.flush();
4506 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004507 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4508 } catch (IOException | RemoteException e) {
4509 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004511 } else {
4512 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004514
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004515 if (focusedWindowClient != null && client != focusedWindowClient) {
4516 p.println(" ");
4517 p.println("Warning: Current input method client doesn't match the last focused. "
4518 + "window.");
4519 p.println("Dumping input method client in the last focused window just in case.");
4520 p.println(" ");
4521 pw.flush();
4522 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004523 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4524 } catch (IOException | RemoteException e) {
4525 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004526 }
4527 }
4528
Jeff Brownb88102f2010-09-08 11:49:43 -07004529 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 pw.flush();
4532 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004533 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4534 } catch (IOException | RemoteException e) {
4535 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004537 } else {
4538 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 }
4540 }
4541}