blob: ce062aaa7d92f56a7694e4923a07fccc97ad5e37 [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
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700217 | Context.BIND_IMPORTANT_BACKGROUND
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700218 | Context.BIND_SHOWING_UI;
219
220 /**
221 * Binding flags used only while the {@link InputMethodService} is showing window.
222 */
223 private static final int IME_VISIBLE_BIND_FLAGS =
224 Context.BIND_AUTO_CREATE
225 | Context.BIND_TREAT_LIKE_ACTIVITY
226 | Context.BIND_FOREGROUND_SERVICE;
227
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;
satok2c93efc2012-04-02 19:33:47 +09002113 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002114 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002115 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002116 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2117 // When physical keyboard is attached, we show the ime switcher (or notification if
2118 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2119 // exists in the IME switcher dialog. Might be OK to remove this condition once
2120 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2121 return true;
2122 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002123 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2124 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002125 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002126
2127 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2128 final int N = imis.size();
2129 if (N > 2) return true;
2130 if (N < 1) return false;
2131 int nonAuxCount = 0;
2132 int auxCount = 0;
2133 InputMethodSubtype nonAuxSubtype = null;
2134 InputMethodSubtype auxSubtype = null;
2135 for(int i = 0; i < N; ++i) {
2136 final InputMethodInfo imi = imis.get(i);
2137 final List<InputMethodSubtype> subtypes =
2138 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2139 final int subtypeCount = subtypes.size();
2140 if (subtypeCount == 0) {
2141 ++nonAuxCount;
2142 } else {
2143 for (int j = 0; j < subtypeCount; ++j) {
2144 final InputMethodSubtype subtype = subtypes.get(j);
2145 if (!subtype.isAuxiliary()) {
2146 ++nonAuxCount;
2147 nonAuxSubtype = subtype;
2148 } else {
2149 ++auxCount;
2150 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002151 }
2152 }
satok7cfc0ed2011-06-20 21:29:36 +09002153 }
2154 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002155 if (nonAuxCount > 1 || auxCount > 1) {
2156 return true;
2157 } else if (nonAuxCount == 1 && auxCount == 1) {
2158 if (nonAuxSubtype != null && auxSubtype != null
2159 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2160 || auxSubtype.overridesImplicitlyEnabledSubtype()
2161 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2162 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2163 return false;
2164 }
2165 return true;
2166 }
2167 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002168 }
2169
John Spurlocke0980502013-10-25 11:59:29 -04002170 private boolean isKeyguardLocked() {
2171 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2172 }
2173
Yohei Yukawad6475a62017-04-17 10:35:27 -07002174 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002175 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002176 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002177 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2178 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002179 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002180 return;
2181 }
2182
Yohei Yukawa69e68022017-02-13 12:04:50 -08002183 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002184 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002185 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002186 mImeWindowVis = vis;
2187 mBackDisposition = backDisposition;
2188 updateSystemUiLocked(token, vis, backDisposition);
2189 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002190
2191 final boolean dismissImeOnBackKeyPressed;
2192 switch (backDisposition) {
2193 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2194 dismissImeOnBackKeyPressed = true;
2195 break;
2196 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2197 dismissImeOnBackKeyPressed = false;
2198 break;
2199 default:
2200 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2201 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2202 break;
2203 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002204 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2205 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002206 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002207 }
2208
2209 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2210 synchronized (mMethodMap) {
2211 updateSystemUiLocked(token, vis, backDisposition);
2212 }
2213 }
2214
2215 // Caution! This method is called in this class. Handle multi-user carefully
2216 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2217 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002218 return;
2219 }
2220
2221 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2222 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002223 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002224 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002225 // apply policy for binder calls
2226 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2227 vis = 0;
satok06487a52010-10-29 11:37:18 +09002228 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002229 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2230 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2231 if (mStatusBar != null) {
2232 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2233 needsToShowImeSwitcher);
2234 }
2235 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2236 if (imi != null && needsToShowImeSwitcher) {
2237 // Used to load label
2238 final CharSequence title = mRes.getText(
2239 com.android.internal.R.string.select_input_method);
2240 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2241 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002242 mImeSwitcherNotification.setContentTitle(title)
2243 .setContentText(summary)
2244 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002245 try {
2246 if ((mNotificationManager != null)
2247 && !mIWindowManager.hasNavigationBar()) {
2248 if (DEBUG) {
2249 Slog.d(TAG, "--- show notification: label = " + summary);
2250 }
2251 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002252 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002253 mImeSwitcherNotification.build(), UserHandle.ALL);
2254 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002255 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002256 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002257 }
2258 } else {
2259 if (mNotificationShown && mNotificationManager != null) {
2260 if (DEBUG) {
2261 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002262 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002263 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002264 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002265 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002266 }
satok06487a52010-10-29 11:37:18 +09002267 }
2268 } finally {
2269 Binder.restoreCallingIdentity(ident);
2270 }
2271 }
2272
satoke7c6998e2011-06-03 17:57:59 +09002273 @Override
satokf9f01002011-05-19 21:31:50 +09002274 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002275 if (!calledFromValidUser()) {
2276 return;
2277 }
satokf9f01002011-05-19 21:31:50 +09002278 synchronized (mMethodMap) {
2279 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2280 for (int i = 0; i < spans.length; ++i) {
2281 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002282 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002283 mSecureSuggestionSpans.put(ss, currentImi);
2284 }
2285 }
2286 }
2287 }
2288
satoke7c6998e2011-06-03 17:57:59 +09002289 @Override
satokf9f01002011-05-19 21:31:50 +09002290 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002291 if (!calledFromValidUser()) {
2292 return false;
2293 }
satokf9f01002011-05-19 21:31:50 +09002294 synchronized (mMethodMap) {
2295 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2296 // TODO: Do not send the intent if the process of the targetImi is already dead.
2297 if (targetImi != null) {
2298 final String[] suggestions = span.getSuggestions();
2299 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002300 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002301 final Intent intent = new Intent();
2302 // Ensures that only a class in the original IME package will receive the
2303 // notification.
satok42c5a162011-05-26 16:46:14 +09002304 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002305 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2306 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2307 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2308 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002309 final long ident = Binder.clearCallingIdentity();
2310 try {
2311 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2312 } finally {
2313 Binder.restoreCallingIdentity(ident);
2314 }
satokf9f01002011-05-19 21:31:50 +09002315 return true;
2316 }
2317 }
2318 return false;
2319 }
2320
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002321 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002322 updateInputMethodsFromSettingsLocked(enabledMayChange);
2323 updateKeyboardFromSettingsLocked();
2324 }
2325
2326 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002327 if (enabledMayChange) {
2328 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2329 for (int i=0; i<enabled.size(); i++) {
2330 // We allow the user to select "disabled until used" apps, so if they
2331 // are enabling one of those here we now need to make it enabled.
2332 InputMethodInfo imm = enabled.get(i);
2333 try {
2334 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2335 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2336 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002337 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002338 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002339 if (DEBUG) {
2340 Slog.d(TAG, "Update state(" + imm.getId()
2341 + "): DISABLED_UNTIL_USED -> DEFAULT");
2342 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002343 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2344 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002345 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2346 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002347 }
2348 } catch (RemoteException e) {
2349 }
2350 }
2351 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002352 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2353 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2354 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2355 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002356 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002357 // There is no input method selected, try to choose new applicable input method.
2358 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002359 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002360 }
2361 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002363 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002366 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
satokf3db1af2010-11-23 13:34:33 +09002368 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002369 } else {
2370 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002371 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002373 // Here is not the perfect place to reset the switching controller. Ideally
2374 // mSwitchingController and mSettings should be able to share the same state.
2375 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2376 // the same enabled IMEs list.
2377 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002378
2379 }
2380
2381 public void updateKeyboardFromSettingsLocked() {
2382 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2383 if (mSwitchingDialog != null
2384 && mSwitchingDialogTitleView != null
2385 && mSwitchingDialog.isShowing()) {
2386 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2387 com.android.internal.R.id.hard_keyboard_switch);
2388 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002391
Yohei Yukawab097b822015-12-01 10:43:08 -08002392 private void notifyInputMethodSubtypeChanged(final int userId,
2393 @Nullable final InputMethodInfo inputMethodInfo,
2394 @Nullable final InputMethodSubtype subtype) {
2395 final InputManagerInternal inputManagerInternal =
2396 LocalServices.getService(InputManagerInternal.class);
2397 if (inputManagerInternal != null) {
2398 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2399 }
2400 }
2401
satokab751aa2010-09-14 19:17:36 +09002402 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 InputMethodInfo info = mMethodMap.get(id);
2404 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002405 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002407
satokd81e9502012-05-21 12:58:45 +09002408 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002410 final int subtypeCount = info.getSubtypeCount();
2411 if (subtypeCount <= 0) {
2412 return;
satokcd7cd292010-11-20 15:46:23 +09002413 }
satokd81e9502012-05-21 12:58:45 +09002414 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2415 final InputMethodSubtype newSubtype;
2416 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2417 newSubtype = info.getSubtypeAt(subtypeId);
2418 } else {
2419 // If subtype is null, try to find the most applicable one from
2420 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002421 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002422 }
2423 if (newSubtype == null || oldSubtype == null) {
2424 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2425 + ", new subtype = " + newSubtype);
2426 return;
2427 }
2428 if (newSubtype != oldSubtype) {
2429 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2430 if (mCurMethod != null) {
2431 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002432 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002433 mCurMethod.changeInputMethodSubtype(newSubtype);
2434 } catch (RemoteException e) {
2435 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002436 return;
satokab751aa2010-09-14 19:17:36 +09002437 }
2438 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002439 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 return;
2442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002443
satokd81e9502012-05-21 12:58:45 +09002444 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 final long ident = Binder.clearCallingIdentity();
2446 try {
satokab751aa2010-09-14 19:17:36 +09002447 // Set a subtype to this input method.
2448 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002449 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2450 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2451 // because mCurMethodId is stored as a history in
2452 // setSelectedInputMethodAndSubtypeLocked().
2453 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002455 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002457 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002459 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002461 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 } finally {
2463 Binder.restoreCallingIdentity(ident);
2464 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002465
2466 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2467 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002469
satok42c5a162011-05-26 16:46:14 +09002470 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002471 public boolean showSoftInput(IInputMethodClient client, int flags,
2472 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002473 if (!calledFromValidUser()) {
2474 return false;
2475 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002476 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 long ident = Binder.clearCallingIdentity();
2478 try {
2479 synchronized (mMethodMap) {
2480 if (mCurClient == null || client == null
2481 || mCurClient.client.asBinder() != client.asBinder()) {
2482 try {
2483 // We need to check if this is the current client with
2484 // focus in the window manager, to allow this call to
2485 // be made before input is started in it.
2486 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002487 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002488 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 }
2490 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002491 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 }
2493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002494
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002495 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002496 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 }
2498 } finally {
2499 Binder.restoreCallingIdentity(ident);
2500 }
2501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002502
The Android Open Source Project4df24232009-03-05 14:34:35 -08002503 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002505 if (mAccessibilityRequestingNoSoftKeyboard) {
2506 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 }
Anna Galusza9b278112016-01-04 11:37:37 -08002508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2510 mShowExplicitlyRequested = true;
2511 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002512 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2513 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002515
Dianne Hackborncc278702009-09-02 23:07:23 -07002516 if (!mSystemReady) {
2517 return false;
2518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002519
The Android Open Source Project4df24232009-03-05 14:34:35 -08002520 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002522 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002523 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2524 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2525 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002527 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002528 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002529 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002530 mVisibleBound = true;
2531 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002532 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002533 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002534 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 // The client has asked to have the input method shown, but
2536 // we have been sitting here too long with a connection to the
2537 // service and no interface received, so let's disconnect/connect
2538 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002539 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002541 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002543 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002544 } else {
2545 if (DEBUG) {
2546 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2547 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002550
The Android Open Source Project4df24232009-03-05 14:34:35 -08002551 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002553
satok42c5a162011-05-26 16:46:14 +09002554 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002555 public boolean hideSoftInput(IInputMethodClient client, int flags,
2556 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002557 if (!calledFromValidUser()) {
2558 return false;
2559 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002560 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 long ident = Binder.clearCallingIdentity();
2562 try {
2563 synchronized (mMethodMap) {
2564 if (mCurClient == null || client == null
2565 || mCurClient.client.asBinder() != client.asBinder()) {
2566 try {
2567 // We need to check if this is the current client with
2568 // focus in the window manager, to allow this call to
2569 // be made before input is started in it.
2570 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002571 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2572 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002573 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 }
2575 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002576 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 }
2578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002579
Joe Onorato8a9b2202010-02-26 18:56:32 -08002580 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002581 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
2583 } finally {
2584 Binder.restoreCallingIdentity(ident);
2585 }
2586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002587
The Android Open Source Project4df24232009-03-05 14:34:35 -08002588 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2590 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002591 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 -08002592 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 }
2594 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002595 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 -08002596 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002598
2599 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2600 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2601 // to be updated with the new value sent from IME process. Even in such a transient state
2602 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2603 // application process as a valid request, and have even promised such a behavior with CTS
2604 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2605 // IMMS#InputShown indicates that the software keyboard is shown.
2606 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2607 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2608 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002609 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002610 if (shouldHideSoftInput) {
2611 // The IME will report its visible state again after the following message finally
2612 // delivered to the IME process as an IPC. Hence the inconsistency between
2613 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2614 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002615 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2616 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2617 res = true;
2618 } else {
2619 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002621 if (mHaveConnection && mVisibleBound) {
2622 mContext.unbindService(mVisibleConnection);
2623 mVisibleBound = false;
2624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 mInputShown = false;
2626 mShowRequested = false;
2627 mShowExplicitlyRequested = false;
2628 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002629 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002631
satok42c5a162011-05-26 16:46:14 +09002632 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002633 public InputBindResult startInputOrWindowGainedFocus(
2634 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2635 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002636 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002637 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002638 if (windowToken != null) {
2639 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002640 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002641 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002642 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2643 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002644 }
2645 }
2646
2647 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002648 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002649 IInputMethodClient client, IBinder windowToken, int controlFlags,
2650 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002651 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2652 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002653 // Needs to check the validity before clearing calling identity
2654 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002655 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 long ident = Binder.clearCallingIdentity();
2657 try {
2658 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002659 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2660 + InputMethodClient.getStartInputReason(startInputReason)
2661 + " client=" + client.asBinder()
2662 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002663 + " missingMethods="
2664 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002665 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002666 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002667 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002668 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002669
Dianne Hackborn7663d802012-02-24 13:08:49 -08002670 ClientState cs = mClients.get(client.asBinder());
2671 if (cs == null) {
2672 throw new IllegalArgumentException("unknown client "
2673 + client.asBinder());
2674 }
2675
2676 try {
2677 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2678 // Check with the window manager to make sure this client actually
2679 // has a window with focus. If not, reject. This is thread safe
2680 // because if the focus changes some time before or after, the
2681 // next client receiving focus that has any interest in input will
2682 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002683 if (DEBUG) {
2684 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2685 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2686 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002687 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002689 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002691
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002692 if (!calledFromValidUser) {
2693 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2694 Slog.w(TAG, "If you want to interect with IME, you need "
2695 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2696 hideCurrentInputLocked(0, null);
2697 return null;
2698 }
2699
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002700 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002701 if (DEBUG) {
2702 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2703 + " attribute=" + attribute + ", token = " + windowToken);
2704 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002705 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002706 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002707 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002708 }
2709 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002710 }
2711 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002712 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002713 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002714
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002715 // Should we auto-show the IME even if the caller has not
2716 // specified what should be done with it?
2717 // We only do this automatically if the window can resize
2718 // to accommodate the IME (so what the user sees will give
2719 // them good context without input information being obscured
2720 // by the IME) or if running on a large screen where there
2721 // is more room for the target window + IME.
2722 final boolean doAutoShow =
2723 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2724 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2725 || mRes.getConfiguration().isLayoutSizeAtLeast(
2726 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002727 final boolean isTextEditor =
2728 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2729
2730 // We want to start input before showing the IME, but after closing
2731 // it. We want to do this after closing it to help the IME disappear
2732 // more quickly (not get stuck behind it initializing itself for the
2733 // new focused input, even if its window wants to hide the IME).
2734 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2737 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002738 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2740 // There is no focus view, and this window will
2741 // be behind any soft input window, so hide the
2742 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002743 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002744 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002746 } else if (isTextEditor && doAutoShow && (softInputMode &
2747 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 // There is a focus view, and we are navigating forward
2749 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002750 // We only do this automatically if the window can resize
2751 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002752 // them good context without input information being obscured
2753 // by the IME) or if running on a large screen where there
2754 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002755 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002756 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002757 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002758 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002759 didStart = true;
2760 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002761 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 }
2763 break;
2764 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2765 // Do nothing.
2766 break;
2767 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2768 if ((softInputMode &
2769 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002770 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002771 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
2773 break;
2774 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002775 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002776 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 break;
2778 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2779 if ((softInputMode &
2780 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002781 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002782 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002783 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002784 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002785 didStart = true;
2786 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002787 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 }
2789 break;
2790 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002791 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002792 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002793 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002794 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002795 didStart = true;
2796 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002797 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 break;
2799 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002800
2801 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002802 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002803 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 }
2806 } finally {
2807 Binder.restoreCallingIdentity(ident);
2808 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002809
2810 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002812
Guliz Tuncay6908c152017-06-02 16:06:10 -07002813 private boolean canShowInputMethodPickerLocked(IInputMethodClient client) {
2814 final int uid = Binder.getCallingUid();
2815 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
2816 return true;
2817 } else if (mCurClient != null && client != null
2818 && mCurClient.client.asBinder() == client.asBinder()) {
2819 return true;
2820 } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid(
2821 mAppOpsManager,
2822 uid,
2823 mCurIntent.getComponent().getPackageName())) {
2824 return true;
2825 } else if (mContext.checkCallingPermission(
2826 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2827 == PackageManager.PERMISSION_GRANTED) {
2828 return true;
2829 }
2830
2831 return false;
2832 }
2833
satok42c5a162011-05-26 16:46:14 +09002834 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002835 public void showInputMethodPickerFromClient(
2836 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002837 if (!calledFromValidUser()) {
2838 return;
2839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 synchronized (mMethodMap) {
Guliz Tuncay6908c152017-06-02 16:06:10 -07002841 if(!canShowInputMethodPickerLocked(client)) {
satok47a44912010-10-06 16:03:58 +09002842 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002843 + Binder.getCallingUid() + ": " + client);
Guliz Tuncay6908c152017-06-02 16:06:10 -07002844 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 }
2846
satok440aab52010-11-25 09:43:11 +09002847 // Always call subtype picker, because subtype picker is a superset of input method
2848 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002849 mHandler.sendMessage(mCaller.obtainMessageI(
2850 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002851 }
2852 }
2853
satok42c5a162011-05-26 16:46:14 +09002854 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002856 if (!calledFromValidUser()) {
2857 return;
2858 }
satok28203512010-11-24 11:06:49 +09002859 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2860 }
2861
satok42c5a162011-05-26 16:46:14 +09002862 @Override
satok28203512010-11-24 11:06:49 +09002863 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002864 if (!calledFromValidUser()) {
2865 return;
2866 }
satok28203512010-11-24 11:06:49 +09002867 synchronized (mMethodMap) {
2868 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002869 setInputMethodWithSubtypeIdLocked(token, id,
2870 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2871 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002872 } else {
2873 setInputMethod(token, id);
2874 }
2875 }
satokab751aa2010-09-14 19:17:36 +09002876 }
2877
satok42c5a162011-05-26 16:46:14 +09002878 @Override
satokb416a712010-11-25 20:42:14 +09002879 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002880 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002881 if (!calledFromValidUser()) {
2882 return;
2883 }
satokb416a712010-11-25 20:42:14 +09002884 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002885 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2886 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002887 }
2888 }
2889
satok4fc87d62011-05-20 16:13:43 +09002890 @Override
satok735cf382010-11-11 20:40:09 +09002891 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002892 if (!calledFromValidUser()) {
2893 return false;
2894 }
satok735cf382010-11-11 20:40:09 +09002895 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002896 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002897 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002898 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002899 lastImi = mMethodMap.get(lastIme.first);
2900 } else {
2901 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002902 }
satok4fc87d62011-05-20 16:13:43 +09002903 String targetLastImiId = null;
2904 int subtypeId = NOT_A_SUBTYPE_ID;
2905 if (lastIme != null && lastImi != null) {
2906 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002907 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002908 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2909 : mCurrentSubtype.hashCode();
2910 // If the last IME is the same as the current IME and the last subtype is not
2911 // defined, there is no need to switch to the last IME.
2912 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2913 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002914 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002915 }
2916 }
2917
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002918 if (TextUtils.isEmpty(targetLastImiId)
2919 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002920 // This is a safety net. If the currentSubtype can't be added to the history
2921 // and the framework couldn't find the last ime, we will make the last ime be
2922 // the most applicable enabled keyboard subtype of the system imes.
2923 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2924 if (enabled != null) {
2925 final int N = enabled.size();
2926 final String locale = mCurrentSubtype == null
2927 ? mRes.getConfiguration().locale.toString()
2928 : mCurrentSubtype.getLocale();
2929 for (int i = 0; i < N; ++i) {
2930 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002931 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002932 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002933 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2934 InputMethodUtils.getSubtypes(imi),
2935 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002936 if (keyboardSubtype != null) {
2937 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002938 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002939 imi, keyboardSubtype.hashCode());
2940 if(keyboardSubtype.getLocale().equals(locale)) {
2941 break;
2942 }
2943 }
2944 }
2945 }
2946 }
2947 }
2948
2949 if (!TextUtils.isEmpty(targetLastImiId)) {
2950 if (DEBUG) {
2951 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2952 + ", from: " + mCurMethodId + ", " + subtypeId);
2953 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002954 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002955 return true;
2956 } else {
2957 return false;
2958 }
satok735cf382010-11-11 20:40:09 +09002959 }
2960 }
2961
satoke7c6998e2011-06-03 17:57:59 +09002962 @Override
satok688bd472012-02-09 20:09:17 +09002963 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002964 if (!calledFromValidUser()) {
2965 return false;
2966 }
satok688bd472012-02-09 20:09:17 +09002967 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002968 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002969 return false;
2970 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002971 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002972 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2973 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002974 if (nextSubtype == null) {
2975 return false;
2976 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002977 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2978 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002979 return true;
2980 }
2981 }
2982
2983 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002984 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2985 if (!calledFromValidUser()) {
2986 return false;
2987 }
2988 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002989 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002990 return false;
2991 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002992 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002993 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2994 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002995 if (nextSubtype == null) {
2996 return false;
2997 }
2998 return true;
2999 }
3000 }
3001
3002 @Override
satok68f1b782011-04-11 14:26:04 +09003003 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003004 if (!calledFromValidUser()) {
3005 return null;
3006 }
satok68f1b782011-04-11 14:26:04 +09003007 synchronized (mMethodMap) {
3008 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
3009 // TODO: Handle the case of the last IME with no subtypes
3010 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
3011 || TextUtils.isEmpty(lastIme.second)) return null;
3012 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
3013 if (lastImi == null) return null;
3014 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003015 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003016 final int lastSubtypeId =
3017 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09003018 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
3019 return null;
3020 }
3021 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09003022 } catch (NumberFormatException e) {
3023 return null;
3024 }
3025 }
3026 }
3027
satoke7c6998e2011-06-03 17:57:59 +09003028 @Override
satokee5e77c2011-09-02 18:50:15 +09003029 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003030 if (!calledFromValidUser()) {
3031 return;
3032 }
satok91e88122011-07-18 11:11:42 +09003033 // By this IPC call, only a process which shares the same uid with the IME can add
3034 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08003035 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09003036 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08003037 if (!mSystemReady) {
3038 return;
3039 }
satok91e88122011-07-18 11:11:42 +09003040 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09003041 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003042 final String[] packageInfos;
3043 try {
3044 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3045 } catch (RemoteException e) {
3046 Slog.e(TAG, "Failed to get package infos");
3047 return;
3048 }
satok91e88122011-07-18 11:11:42 +09003049 if (packageInfos != null) {
3050 final int packageNum = packageInfos.length;
3051 for (int i = 0; i < packageNum; ++i) {
3052 if (packageInfos[i].equals(imi.getPackageName())) {
3053 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003054 final long ident = Binder.clearCallingIdentity();
3055 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003056 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003057 } finally {
3058 Binder.restoreCallingIdentity(ident);
3059 }
satokee5e77c2011-09-02 18:50:15 +09003060 return;
satok91e88122011-07-18 11:11:42 +09003061 }
3062 }
3063 }
satoke7c6998e2011-06-03 17:57:59 +09003064 }
satokee5e77c2011-09-02 18:50:15 +09003065 return;
satoke7c6998e2011-06-03 17:57:59 +09003066 }
3067
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003068 @Override
3069 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003070 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003071 }
3072
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003073 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003074 public void clearLastInputMethodWindowForTransition(IBinder token) {
3075 if (!calledFromValidUser()) {
3076 return;
3077 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003078 synchronized (mMethodMap) {
3079 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003080 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003081 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003082 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003083 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003084 }
3085
3086 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003087 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003088 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003089 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003090 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003091 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003092 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3093 if (DEBUG) {
3094 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3095 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3096 + " actual: " + sequenceNumber);
3097 }
3098 return;
3099 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003100 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3101 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003102 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003103 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003104 }
3105 }
3106
satok28203512010-11-24 11:06:49 +09003107 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003109 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3110 }
3111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003113 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3114 if (token == null) {
3115 if (mContext.checkCallingOrSelfPermission(
3116 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3117 != PackageManager.PERMISSION_GRANTED) {
3118 throw new SecurityException(
3119 "Using null token requires permission "
3120 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003122 } else if (mCurToken != token) {
3123 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3124 + " token: " + token);
3125 return;
3126 }
3127
3128 final long ident = Binder.clearCallingIdentity();
3129 try {
3130 setInputMethodLocked(id, subtypeId);
3131 } finally {
3132 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 }
3134 }
3135
satok42c5a162011-05-26 16:46:14 +09003136 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003138 if (!calledFromValidUser()) {
3139 return;
3140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003142 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 return;
3144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 long ident = Binder.clearCallingIdentity();
3146 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003147 hideCurrentInputLocked(flags, null);
3148 } finally {
3149 Binder.restoreCallingIdentity(ident);
3150 }
3151 }
3152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003153
satok42c5a162011-05-26 16:46:14 +09003154 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003155 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003156 if (!calledFromValidUser()) {
3157 return;
3158 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003159 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003160 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003161 return;
3162 }
3163 long ident = Binder.clearCallingIdentity();
3164 try {
3165 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 } finally {
3167 Binder.restoreCallingIdentity(ident);
3168 }
3169 }
3170 }
3171
3172 void setEnabledSessionInMainThread(SessionState session) {
3173 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003174 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003176 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003177 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 } catch (RemoteException e) {
3179 }
3180 }
3181 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003182 if (mEnabledSession != null && mEnabledSession.session != null) {
3183 try {
3184 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3185 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3186 } catch (RemoteException e) {
3187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 }
3189 }
3190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003191
satok42c5a162011-05-26 16:46:14 +09003192 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003194 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003196 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003197 final boolean showAuxSubtypes;
3198 switch (msg.arg1) {
3199 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3200 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3201 // implemented so that auxiliary subtypes will be excluded when the soft
3202 // keyboard is invisible.
3203 showAuxSubtypes = mInputShown;
3204 break;
3205 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3206 showAuxSubtypes = true;
3207 break;
3208 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3209 showAuxSubtypes = false;
3210 break;
3211 default:
3212 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3213 return false;
3214 }
3215 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003216 return true;
3217
satok47a44912010-10-06 16:03:58 +09003218 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003219 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003220 return true;
3221
3222 case MSG_SHOW_IM_CONFIG:
3223 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003224 return true;
3225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 case MSG_UNBIND_INPUT:
3229 try {
3230 ((IInputMethod)msg.obj).unbindInput();
3231 } catch (RemoteException e) {
3232 // There is nothing interesting about the method dying.
3233 }
3234 return true;
3235 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003236 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 try {
3238 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3239 } catch (RemoteException e) {
3240 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003241 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 return true;
3243 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003244 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003246 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003247 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003248 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 } catch (RemoteException e) {
3250 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003251 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 return true;
3253 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003254 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003256 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003257 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003258 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 } catch (RemoteException e) {
3260 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003261 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003263 case MSG_HIDE_CURRENT_INPUT_METHOD:
3264 synchronized (mMethodMap) {
3265 hideCurrentInputLocked(0, null);
3266 }
3267 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003269 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003271 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3273 } catch (RemoteException e) {
3274 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003275 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003277 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003278 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003279 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003280 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003282 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003284 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003285 // Dispose the channel if the input method is not local to this process
3286 // because the remote proxy will get its own copy when unparceled.
3287 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003288 channel.dispose();
3289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003291 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003295
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003296 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003297 final int missingMethods = msg.arg1;
3298 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003299 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003300 final IBinder startInputToken = (IBinder) args.arg1;
3301 final SessionState session = (SessionState) args.arg2;
3302 final IInputContext inputContext = (IInputContext) args.arg3;
3303 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003306 session.method.startInput(startInputToken, inputContext, missingMethods,
3307 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 } catch (RemoteException e) {
3309 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003310 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003315
Yohei Yukawa33e81792015-11-17 21:14:42 -08003316 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003318 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 } catch (RemoteException e) {
3320 // There is nothing interesting about the last client dying.
3321 }
3322 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003323 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003324 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003325 IInputMethodClient client = (IInputMethodClient)args.arg1;
3326 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003328 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003330 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003331 } finally {
3332 // Dispose the channel if the input method is not local to this process
3333 // because the remote proxy will get its own copy when unparceled.
3334 if (res.channel != null && Binder.isProxy(client)) {
3335 res.channel.dispose();
3336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003338 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003340 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003341 case MSG_SET_ACTIVE:
3342 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003343 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003344 } catch (RemoteException e) {
3345 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3346 + ((ClientState)msg.obj).pid + " uid "
3347 + ((ClientState)msg.obj).uid);
3348 }
3349 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003350 case MSG_SET_INTERACTIVE:
3351 handleSetInteractive(msg.arg1 != 0);
3352 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003353 case MSG_SWITCH_IME:
3354 handleSwitchInputMethod(msg.arg1 != 0);
3355 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003356 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3357 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003358 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003359 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003360 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003361 } catch (RemoteException e) {
3362 Slog.w(TAG, "Got RemoteException sending "
3363 + "setUserActionNotificationSequenceNumber("
3364 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003365 + clientState.pid + " uid "
3366 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003367 }
3368 return true;
3369 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003370 case MSG_REPORT_FULLSCREEN_MODE: {
3371 final boolean fullscreen = msg.arg1 != 0;
3372 final ClientState clientState = (ClientState)msg.obj;
3373 try {
3374 clientState.client.reportFullscreenMode(fullscreen);
3375 } catch (RemoteException e) {
3376 Slog.w(TAG, "Got RemoteException sending "
3377 + "reportFullscreen(" + fullscreen + ") notification to pid="
3378 + clientState.pid + " uid=" + clientState.uid);
3379 }
3380 return true;
3381 }
satok01038492012-04-09 21:08:27 +09003382
3383 // --------------------------------------------------------------
3384 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003385 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003386 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003387 case MSG_SYSTEM_UNLOCK_USER:
3388 final int userId = msg.arg1;
3389 onUnlockUser(userId);
3390 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
3392 return false;
3393 }
3394
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003395 private void handleSetInteractive(final boolean interactive) {
3396 synchronized (mMethodMap) {
3397 mIsInteractive = interactive;
3398 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3399
3400 // Inform the current client of the change in active status
3401 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003402 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3403 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3404 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003405 }
3406 }
3407 }
3408
Yohei Yukawaae61f712015-12-09 13:00:10 -08003409 private void handleSwitchInputMethod(final boolean forwardDirection) {
3410 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003411 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003412 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003413 if (nextSubtype == null) {
3414 return;
3415 }
3416 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003417 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3418 if (newInputMethodInfo == null) {
3419 return;
3420 }
3421 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3422 newInputMethodInfo, mCurrentSubtype);
3423 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003424 if (mSubtypeSwitchedByShortCutToast == null) {
3425 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3426 Toast.LENGTH_SHORT);
3427 } else {
3428 mSubtypeSwitchedByShortCutToast.setText(toastText);
3429 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003430 mSubtypeSwitchedByShortCutToast.show();
3431 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003432 }
3433 }
3434
satokdc9ddae2011-10-06 12:22:36 +09003435 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003436 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3437 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003438 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003439 if (DEBUG) {
3440 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3441 }
satok723a27e2010-11-11 14:58:11 +09003442 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003443 return true;
3444 }
3445
3446 return false;
3447 }
3448
Yohei Yukawa94e33302016-02-12 19:37:03 -08003449 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003450 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003451 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003452 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003453 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003454 if (!mSystemReady) {
3455 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3456 return;
3457 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003458 mMethodList.clear();
3459 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003460 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003461 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003462
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003463 // Use for queryIntentServicesAsUser
3464 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003466 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3467 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3468 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003469 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003471 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3472 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003473
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003474 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003475 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 for (int i = 0; i < services.size(); ++i) {
3477 ResolveInfo ri = services.get(i);
3478 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003479 final String imeId = InputMethodInfo.computeId(ri);
3480 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3481 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 + ": it does not require the permission "
3483 + android.Manifest.permission.BIND_INPUT_METHOD);
3484 continue;
3485 }
3486
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003487 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003489 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 try {
satoke7c6998e2011-06-03 17:57:59 +09003491 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003492 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003493 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003494 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495
3496 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003497 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003499 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003500 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 }
3502 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003503
Yohei Yukawac4e44912017-02-09 19:30:22 -08003504 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3505 // negatives when the package state remains to be the same but only the component state is
3506 // changed.
3507 {
3508 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3509 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3510 // conservative, but it seems we cannot use it for now (Issue 35176630).
3511 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3512 new Intent(InputMethod.SERVICE_INTERFACE),
3513 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3514 final int N = allInputMethodServices.size();
3515 for (int i = 0; i < N; ++i) {
3516 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003517 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3518 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003519 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003520 }
3521 }
3522
Yohei Yukawa859df052016-02-17 07:56:46 -08003523 // TODO: The following code should find better place to live.
3524 if (!resetDefaultEnabledIme) {
3525 boolean enabledImeFound = false;
3526 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3527 final int N = enabledImes.size();
3528 for (int i = 0; i < N; ++i) {
3529 final InputMethodInfo imi = enabledImes.get(i);
3530 if (mMethodList.contains(imi)) {
3531 enabledImeFound = true;
3532 break;
3533 }
3534 }
3535 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003536 if (DEBUG) {
3537 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3538 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003539 resetDefaultEnabledIme = true;
3540 resetSelectedInputMethodAndSubtypeLocked("");
3541 }
3542 }
3543
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003544 if (resetDefaultEnabledIme) {
3545 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003546 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003547 final int N = defaultEnabledIme.size();
3548 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003549 final InputMethodInfo imi = defaultEnabledIme.get(i);
3550 if (DEBUG) {
3551 Slog.d(TAG, "--- enable ime = " + imi);
3552 }
3553 setInputMethodEnabledLocked(imi.getId(), true);
3554 }
3555 }
3556
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003557 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003558 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003559 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003560 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3561 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003562 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003563 }
3564 } else {
3565 // Double check that the default IME is certainly enabled.
3566 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003567 }
3568 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003569 // Here is not the perfect place to reset the switching controller. Ideally
3570 // mSwitchingController and mSettings should be able to share the same state.
3571 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3572 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003573 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003577
satok217f5482010-12-15 05:19:19 +09003578 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003579 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003580 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003581 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3582 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003583 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003584 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003585 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003586 final int userId;
3587 synchronized (mMethodMap) {
3588 userId = mSettings.getCurrentUserId();
3589 }
3590 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003591 }
3592
3593 private void showConfigureInputMethods() {
3594 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3595 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3596 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3597 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003598 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003599 }
3600
satok2c93efc2012-04-02 19:33:47 +09003601 private boolean isScreenLocked() {
3602 return mKeyguardManager != null
3603 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3604 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003605
Seigo Nonaka14e13912015-05-06 21:04:13 -07003606 private void showInputMethodMenu(boolean showAuxSubtypes) {
3607 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608
satok2c93efc2012-04-02 19:33:47 +09003609 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003610
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003611 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003612 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003613 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003614
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003615 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003616 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003617 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3618 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003619 if (immis == null || immis.size() == 0) {
3620 return;
3621 }
3622
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003623 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624
satok688bd472012-02-09 20:09:17 +09003625 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003626 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003627 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003628
satokc3690562012-01-10 20:14:43 +09003629 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003630 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003631 if (currentSubtype != null) {
3632 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003633 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3634 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003635 }
3636 }
3637
Ken Wakasa761eb372011-03-04 19:06:18 +09003638 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003639 mIms = new InputMethodInfo[N];
3640 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003641 int checkedItem = 0;
3642 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003643 final ImeSubtypeListItem item = imList.get(i);
3644 mIms[i] = item.mImi;
3645 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003646 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003647 int subtypeId = mSubtypeIds[i];
3648 if ((subtypeId == NOT_A_SUBTYPE_ID)
3649 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3650 || (subtypeId == lastInputMethodSubtypeId)) {
3651 checkedItem = i;
3652 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003655
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003656 final Context settingsContext = new ContextThemeWrapper(
3657 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003658 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3659
3660 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003661 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3662 @Override
3663 public void onCancel(DialogInterface dialog) {
3664 hideInputMethodMenu();
3665 }
3666 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003667
3668 final Context dialogContext = mDialogBuilder.getContext();
3669 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3670 com.android.internal.R.styleable.DialogPreference,
3671 com.android.internal.R.attr.alertDialogStyle, 0);
3672 final Drawable dialogIcon = a.getDrawable(
3673 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3674 a.recycle();
3675
3676 mDialogBuilder.setIcon(dialogIcon);
3677
Yohei Yukawad34e1482016-02-11 08:03:52 -08003678 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003679 final View tv = inflater.inflate(
3680 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3681 mDialogBuilder.setCustomTitle(tv);
3682
3683 // Setup layout for a toggle switch of the hardware keyboard
3684 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003685 mSwitchingDialogTitleView
3686 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003687 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003688 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003689 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003690 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003691 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003692 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3693 @Override
3694 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003695 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003696 // Ensure that the input method dialog is dismissed when changing
3697 // the hardware keyboard state.
3698 hideInputMethodMenu();
3699 }
3700 });
3701
Alan Viverette505e3ab2014-11-24 15:22:11 -08003702 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003703 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3704 final OnClickListener choiceListener = new OnClickListener() {
3705 @Override
3706 public void onClick(final DialogInterface dialog, final int which) {
3707 synchronized (mMethodMap) {
3708 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3709 || mSubtypeIds.length <= which) {
3710 return;
satok01038492012-04-09 21:08:27 +09003711 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003712 final InputMethodInfo im = mIms[which];
3713 int subtypeId = mSubtypeIds[which];
3714 adapter.mCheckedItem = which;
3715 adapter.notifyDataSetChanged();
3716 hideInputMethodMenu();
3717 if (im != null) {
3718 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3719 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003720 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003721 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003723 }
3724 }
3725 };
3726 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003729 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003730 final Window w = mSwitchingDialog.getWindow();
3731 final WindowManager.LayoutParams attrs = w.getAttributes();
3732 w.setType(TYPE_INPUT_METHOD_DIALOG);
3733 // Use an alternate token for the dialog for that window manager can group the token
3734 // with other IME windows based on type vs. grouping based on whichever token happens
3735 // to get selected by the system later on.
3736 attrs.token = mSwitchingDialogToken;
3737 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3738 attrs.setTitle("Select input method");
3739 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003740 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 mSwitchingDialog.show();
3742 }
3743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003744
Ken Wakasa05dbb652011-08-22 15:22:43 +09003745 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3746 private final LayoutInflater mInflater;
3747 private final int mTextViewResourceId;
3748 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003749 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003750 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3751 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3752 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003753
Ken Wakasa05dbb652011-08-22 15:22:43 +09003754 mTextViewResourceId = textViewResourceId;
3755 mItemsList = itemsList;
3756 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003757 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003758 }
3759
3760 @Override
3761 public View getView(int position, View convertView, ViewGroup parent) {
3762 final View view = convertView != null ? convertView
3763 : mInflater.inflate(mTextViewResourceId, null);
3764 if (position < 0 || position >= mItemsList.size()) return view;
3765 final ImeSubtypeListItem item = mItemsList.get(position);
3766 final CharSequence imeName = item.mImeName;
3767 final CharSequence subtypeName = item.mSubtypeName;
3768 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3769 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3770 if (TextUtils.isEmpty(subtypeName)) {
3771 firstTextView.setText(imeName);
3772 secondTextView.setVisibility(View.GONE);
3773 } else {
3774 firstTextView.setText(subtypeName);
3775 secondTextView.setText(imeName);
3776 secondTextView.setVisibility(View.VISIBLE);
3777 }
3778 final RadioButton radioButton =
3779 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3780 radioButton.setChecked(position == mCheckedItem);
3781 return view;
3782 }
3783 }
3784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003786 synchronized (mMethodMap) {
3787 hideInputMethodMenuLocked();
3788 }
3789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003790
The Android Open Source Project10592532009-03-18 17:39:46 -07003791 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003792 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793
The Android Open Source Project10592532009-03-18 17:39:46 -07003794 if (mSwitchingDialog != null) {
3795 mSwitchingDialog.dismiss();
3796 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003798
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003799 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003800 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003801 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003805
satok42c5a162011-05-26 16:46:14 +09003806 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003808 // TODO: Make this work even for non-current users?
3809 if (!calledFromValidUser()) {
3810 return false;
3811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 synchronized (mMethodMap) {
3813 if (mContext.checkCallingOrSelfPermission(
3814 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3815 != PackageManager.PERMISSION_GRANTED) {
3816 throw new SecurityException(
3817 "Requires permission "
3818 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3819 }
Anna Galusza9b278112016-01-04 11:37:37 -08003820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 long ident = Binder.clearCallingIdentity();
3822 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003823 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 } finally {
3825 Binder.restoreCallingIdentity(ident);
3826 }
3827 }
3828 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003829
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003830 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3831 // Make sure this is a valid input method.
3832 InputMethodInfo imm = mMethodMap.get(id);
3833 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003834 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003835 }
3836
satokd87c2592010-09-29 11:52:06 +09003837 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3838 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003839
satokd87c2592010-09-29 11:52:06 +09003840 if (enabled) {
3841 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3842 if (pair.first.equals(id)) {
3843 // We are enabling this input method, but it is already enabled.
3844 // Nothing to do. The previous state was enabled.
3845 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003846 }
3847 }
satokd87c2592010-09-29 11:52:06 +09003848 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3849 // Previous state was disabled.
3850 return false;
3851 } else {
3852 StringBuilder builder = new StringBuilder();
3853 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3854 builder, enabledInputMethodsList, id)) {
3855 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003856 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003857 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3858 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3859 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003860 }
3861 // Previous state was enabled.
3862 return true;
3863 } else {
3864 // We are disabling the input method but it is already disabled.
3865 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003866 return false;
3867 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003868 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003869 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003870
satok723a27e2010-11-11 14:58:11 +09003871 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3872 boolean setSubtypeOnly) {
3873 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003874 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003875
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003876 mCurUserActionNotificationSequenceNumber =
3877 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3878 if (DEBUG) {
3879 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3880 + mCurUserActionNotificationSequenceNumber);
3881 }
3882
3883 if (mCurClient != null && mCurClient.client != null) {
3884 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3885 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003886 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003887 }
3888
satok723a27e2010-11-11 14:58:11 +09003889 // Set Subtype here
3890 if (imi == null || subtypeId < 0) {
3891 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003892 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003893 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003894 if (subtypeId < imi.getSubtypeCount()) {
3895 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3896 mSettings.putSelectedSubtype(subtype.hashCode());
3897 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003898 } else {
3899 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003900 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003901 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003902 }
satokab751aa2010-09-14 19:17:36 +09003903 }
satok723a27e2010-11-11 14:58:11 +09003904
Yohei Yukawa68645a62016-02-17 07:54:20 -08003905 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003906 // Set InputMethod here
3907 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3908 }
3909 }
3910
3911 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3912 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3913 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3914 // newDefaultIme is empty when there is no candidate for the selected IME.
3915 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3916 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3917 if (subtypeHashCode != null) {
3918 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003919 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003920 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003921 } catch (NumberFormatException e) {
3922 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3923 }
3924 }
3925 }
3926 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003927 }
3928
satok4e4569d2010-11-19 18:45:53 +09003929 // If there are no selected shortcuts, tries finding the most applicable ones.
3930 private Pair<InputMethodInfo, InputMethodSubtype>
3931 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3932 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3933 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003934 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003935 boolean foundInSystemIME = false;
3936
3937 // Search applicable subtype for each InputMethodInfo
3938 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003939 final String imiId = imi.getId();
3940 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3941 continue;
3942 }
satokcd7cd292010-11-20 15:46:23 +09003943 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003944 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003945 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003946 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003947 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003948 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003949 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003950 }
satokdf31ae62011-01-15 06:19:44 +09003951 // 2. Search by the system locale from enabledSubtypes.
3952 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003953 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003954 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003955 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003956 }
satoka86f5e42011-09-02 17:12:42 +09003957 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003958 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003959 final ArrayList<InputMethodSubtype> subtypesForSearch =
3960 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003961 ? InputMethodUtils.getSubtypes(imi)
3962 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003963 // 4. Search by the current subtype's locale from all subtypes.
3964 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003965 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003966 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003967 }
3968 // 5. Search by the system locale from all subtypes.
3969 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003970 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003971 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003972 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003973 }
satokcd7cd292010-11-20 15:46:23 +09003974 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003975 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003976 // The current input method is the most applicable IME.
3977 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003978 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003979 break;
satok7599a7f2010-12-22 13:45:23 +09003980 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003981 // The system input method is 2nd applicable IME.
3982 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003983 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003984 if ((imi.getServiceInfo().applicationInfo.flags
3985 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3986 foundInSystemIME = true;
3987 }
satok4e4569d2010-11-19 18:45:53 +09003988 }
3989 }
3990 }
3991 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003992 if (mostApplicableIMI != null) {
3993 Slog.w(TAG, "Most applicable shortcut input method was:"
3994 + mostApplicableIMI.getId());
3995 if (mostApplicableSubtype != null) {
3996 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3997 + "," + mostApplicableSubtype.getMode() + ","
3998 + mostApplicableSubtype.getLocale());
3999 }
4000 }
satok4e4569d2010-11-19 18:45:53 +09004001 }
satokcd7cd292010-11-20 15:46:23 +09004002 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004003 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09004004 } else {
4005 return null;
4006 }
4007 }
4008
satokab751aa2010-09-14 19:17:36 +09004009 /**
4010 * @return Return the current subtype of this input method.
4011 */
satok42c5a162011-05-26 16:46:14 +09004012 @Override
satokab751aa2010-09-14 19:17:36 +09004013 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004014 // TODO: Make this work even for non-current users?
4015 if (!calledFromValidUser()) {
4016 return null;
4017 }
4018 synchronized (mMethodMap) {
4019 return getCurrentInputMethodSubtypeLocked();
4020 }
4021 }
4022
4023 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09004024 if (mCurMethodId == null) {
4025 return null;
4026 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004027 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004028 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
4029 if (imi == null || imi.getSubtypeCount() == 0) {
4030 return null;
satok4e4569d2010-11-19 18:45:53 +09004031 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004032 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004033 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
4034 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004035 if (subtypeId == NOT_A_SUBTYPE_ID) {
4036 // If there are no selected subtypes, the framework will try to find
4037 // the most applicable subtype from explicitly or implicitly enabled
4038 // subtypes.
4039 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004040 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004041 // If there is only one explicitly or implicitly enabled subtype,
4042 // just returns it.
4043 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4044 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4045 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004046 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004047 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004048 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004049 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004050 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004051 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4052 true);
satok4e4569d2010-11-19 18:45:53 +09004053 }
satok3ef8b292010-11-23 06:06:29 +09004054 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004055 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004056 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004057 }
4058 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004059 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004060 }
4061
satok4e4569d2010-11-19 18:45:53 +09004062 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004063 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004064 @Override
satok4e4569d2010-11-19 18:45:53 +09004065 public List getShortcutInputMethodsAndSubtypes() {
4066 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004067 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004068 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004069 // If there are no selected shortcut subtypes, the framework will try to find
4070 // the most applicable subtype from all subtypes whose mode is
4071 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004072 Pair<InputMethodInfo, InputMethodSubtype> info =
4073 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004074 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004075 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004076 ret.add(info.first);
4077 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004078 }
satok3da92232011-01-11 22:46:30 +09004079 return ret;
satokf3db1af2010-11-23 13:34:33 +09004080 }
satokf3db1af2010-11-23 13:34:33 +09004081 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4082 ret.add(imi);
4083 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4084 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004085 }
4086 }
satokf3db1af2010-11-23 13:34:33 +09004087 return ret;
satok4e4569d2010-11-19 18:45:53 +09004088 }
4089 }
4090
satok42c5a162011-05-26 16:46:14 +09004091 @Override
satokb66d2872010-11-10 01:04:04 +09004092 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004093 // TODO: Make this work even for non-current users?
4094 if (!calledFromValidUser()) {
4095 return false;
4096 }
satokb66d2872010-11-10 01:04:04 +09004097 synchronized (mMethodMap) {
4098 if (subtype != null && mCurMethodId != null) {
4099 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004100 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004101 if (subtypeId != NOT_A_SUBTYPE_ID) {
4102 setInputMethodLocked(mCurMethodId, subtypeId);
4103 return true;
4104 }
4105 }
4106 return false;
4107 }
4108 }
4109
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004110 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004111 private static class InputMethodFileManager {
4112 private static final String SYSTEM_PATH = "system";
4113 private static final String INPUT_METHOD_PATH = "inputmethod";
4114 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4115 private static final String NODE_SUBTYPES = "subtypes";
4116 private static final String NODE_SUBTYPE = "subtype";
4117 private static final String NODE_IMI = "imi";
4118 private static final String ATTR_ID = "id";
4119 private static final String ATTR_LABEL = "label";
4120 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004121 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004122 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004123 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004124 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4125 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4126 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004127 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004128 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4129 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004130 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004131 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004132 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004133 if (methodMap == null) {
4134 throw new NullPointerException("methodMap is null");
4135 }
4136 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004137 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004138 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4139 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004140 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004141 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004142 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4143 }
4144 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4145 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4146 if (!subtypeFile.exists()) {
4147 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004148 writeAdditionalInputMethodSubtypes(
4149 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004150 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004151 readAdditionalInputMethodSubtypes(
4152 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004153 }
4154 }
4155
4156 private void deleteAllInputMethodSubtypes(String imiId) {
4157 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004158 mAdditionalSubtypesMap.remove(imiId);
4159 writeAdditionalInputMethodSubtypes(
4160 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004161 }
4162 }
4163
4164 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004165 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004166 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004167 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004168 final int N = additionalSubtypes.length;
4169 for (int i = 0; i < N; ++i) {
4170 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004171 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004172 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004173 } else {
4174 Slog.w(TAG, "Duplicated subtype definition found: "
4175 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004176 }
4177 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004178 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4179 writeAdditionalInputMethodSubtypes(
4180 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004181 }
4182 }
4183
4184 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4185 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004186 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004187 }
4188 }
4189
4190 private static void writeAdditionalInputMethodSubtypes(
4191 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4192 HashMap<String, InputMethodInfo> methodMap) {
4193 // Safety net for the case that this function is called before methodMap is set.
4194 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4195 FileOutputStream fos = null;
4196 try {
4197 fos = subtypesFile.startWrite();
4198 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004199 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004200 out.startDocument(null, true);
4201 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4202 out.startTag(null, NODE_SUBTYPES);
4203 for (String imiId : allSubtypes.keySet()) {
4204 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4205 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4206 continue;
4207 }
4208 out.startTag(null, NODE_IMI);
4209 out.attribute(null, ATTR_ID, imiId);
4210 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4211 final int N = subtypesList.size();
4212 for (int i = 0; i < N; ++i) {
4213 final InputMethodSubtype subtype = subtypesList.get(i);
4214 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004215 if (subtype.hasSubtypeId()) {
4216 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4217 String.valueOf(subtype.getSubtypeId()));
4218 }
satoke7c6998e2011-06-03 17:57:59 +09004219 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4220 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4221 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004222 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4223 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004224 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4225 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4226 out.attribute(null, ATTR_IS_AUXILIARY,
4227 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004228 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4229 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004230 out.endTag(null, NODE_SUBTYPE);
4231 }
4232 out.endTag(null, NODE_IMI);
4233 }
4234 out.endTag(null, NODE_SUBTYPES);
4235 out.endDocument();
4236 subtypesFile.finishWrite(fos);
4237 } catch (java.io.IOException e) {
4238 Slog.w(TAG, "Error writing subtypes", e);
4239 if (fos != null) {
4240 subtypesFile.failWrite(fos);
4241 }
4242 }
4243 }
4244
4245 private static void readAdditionalInputMethodSubtypes(
4246 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4247 if (allSubtypes == null || subtypesFile == null) return;
4248 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004249 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004250 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004251 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004252 int type = parser.getEventType();
4253 // Skip parsing until START_TAG
4254 while ((type = parser.next()) != XmlPullParser.START_TAG
4255 && type != XmlPullParser.END_DOCUMENT) {}
4256 String firstNodeName = parser.getName();
4257 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4258 throw new XmlPullParserException("Xml doesn't start with subtypes");
4259 }
4260 final int depth =parser.getDepth();
4261 String currentImiId = null;
4262 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4263 while (((type = parser.next()) != XmlPullParser.END_TAG
4264 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4265 if (type != XmlPullParser.START_TAG)
4266 continue;
4267 final String nodeName = parser.getName();
4268 if (NODE_IMI.equals(nodeName)) {
4269 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4270 if (TextUtils.isEmpty(currentImiId)) {
4271 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4272 continue;
4273 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004274 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004275 allSubtypes.put(currentImiId, tempSubtypesArray);
4276 } else if (NODE_SUBTYPE.equals(nodeName)) {
4277 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4278 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4279 continue;
4280 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004281 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004282 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004283 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004284 parser.getAttributeValue(null, ATTR_LABEL));
4285 final String imeSubtypeLocale =
4286 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004287 final String languageTag =
4288 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004289 final String imeSubtypeMode =
4290 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4291 final String imeSubtypeExtraValue =
4292 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004293 final boolean isAuxiliary = "1".equals(String.valueOf(
4294 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004295 final boolean isAsciiCapable = "1".equals(String.valueOf(
4296 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004297 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004298 .setSubtypeNameResId(label)
4299 .setSubtypeIconResId(icon)
4300 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004301 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004302 .setSubtypeMode(imeSubtypeMode)
4303 .setSubtypeExtraValue(imeSubtypeExtraValue)
4304 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004305 .setIsAsciiCapable(isAsciiCapable);
4306 final String subtypeIdString =
4307 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4308 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004309 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004310 }
4311 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004312 }
4313 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004314 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4315 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004316 return;
satoke7c6998e2011-06-03 17:57:59 +09004317 }
4318 }
4319 }
4320
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004321 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4322 @NonNull
4323 private final Handler mHandler;
4324
4325 LocalServiceImpl(@NonNull final Handler handler) {
4326 mHandler = handler;
4327 }
4328
4329 @Override
4330 public void setInteractive(boolean interactive) {
4331 // Do everything in handler so as not to block the caller.
4332 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4333 interactive ? 1 : 0, 0));
4334 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004335
4336 @Override
4337 public void switchInputMethod(boolean forwardDirection) {
4338 // Do everything in handler so as not to block the caller.
4339 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4340 forwardDirection ? 1 : 0, 0));
4341 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004342
4343 @Override
4344 public void hideCurrentInputMethod() {
4345 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4346 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4347 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004348 }
4349
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004350 private static String imeWindowStatusToString(final int imeWindowVis) {
4351 final StringBuilder sb = new StringBuilder();
4352 boolean first = true;
4353 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4354 sb.append("Active");
4355 first = false;
4356 }
4357 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4358 if (!first) {
4359 sb.append("|");
4360 }
4361 sb.append("Visible");
4362 }
4363 return sb.toString();
4364 }
4365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004367 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4368 @Nullable Uri contentUri, @Nullable String packageName) {
4369 if (!calledFromValidUser()) {
4370 return null;
4371 }
4372
4373 if (token == null) {
4374 throw new NullPointerException("token");
4375 }
4376 if (packageName == null) {
4377 throw new NullPointerException("packageName");
4378 }
4379 if (contentUri == null) {
4380 throw new NullPointerException("contentUri");
4381 }
4382 final String contentUriScheme = contentUri.getScheme();
4383 if (!"content".equals(contentUriScheme)) {
4384 throw new InvalidParameterException("contentUri must have content scheme");
4385 }
4386
4387 synchronized (mMethodMap) {
4388 final int uid = Binder.getCallingUid();
4389 if (mCurMethodId == null) {
4390 return null;
4391 }
4392 if (mCurToken != token) {
4393 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4394 + " token=" + token);
4395 return null;
4396 }
4397 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4398 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4399 // nature of our system. Let's compare it with our internal record.
4400 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4401 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4402 + mCurAttribute.packageName + " packageName=" + packageName);
4403 return null;
4404 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004405 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004406 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004407 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004408 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004409 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4410 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4411 imeUserId);
4412 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4413 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4414 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4415 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4416 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4417 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4418 // actually allowed to "uid", which is guaranteed to be the IME's one.
4419 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4420 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004421 }
4422 }
4423
4424 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004425 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4426 if (!calledFromValidUser()) {
4427 return;
4428 }
4429 synchronized (mMethodMap) {
4430 if (!calledWithValidToken(token)) {
4431 return;
4432 }
4433 if (mCurClient != null && mCurClient.client != null) {
4434 mInFullscreenMode = fullscreen;
4435 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4436 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4437 }
4438 }
4439 }
4440
4441 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004443 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444
4445 IInputMethod method;
4446 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004447 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 synchronized (mMethodMap) {
4452 p.println("Current Input Method Manager state:");
4453 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004454 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 for (int i=0; i<N; i++) {
4456 InputMethodInfo info = mMethodList.get(i);
4457 p.println(" InputMethod #" + i + ":");
4458 info.dump(p, " ");
4459 }
4460 p.println(" Clients:");
4461 for (ClientState ci : mClients.values()) {
4462 p.println(" Client " + ci + ":");
4463 p.println(" client=" + ci.client);
4464 p.println(" inputContext=" + ci.inputContext);
4465 p.println(" sessionRequested=" + ci.sessionRequested);
4466 p.println(" curSession=" + ci.curSession);
4467 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004468 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004470 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004471 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4472 + " softInputMode=" +
4473 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4474 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004475 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4477 + " mBoundToMethod=" + mBoundToMethod);
4478 p.println(" mCurToken=" + mCurToken);
4479 p.println(" mCurIntent=" + mCurIntent);
4480 method = mCurMethod;
4481 p.println(" mCurMethod=" + mCurMethod);
4482 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004483 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 p.println(" mShowRequested=" + mShowRequested
4485 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4486 + " mShowForced=" + mShowForced
4487 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004488 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004489 p.println(" mCurUserActionNotificationSequenceNumber="
4490 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004491 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004492 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004493 p.println(" mSwitchingController:");
4494 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004495 p.println(" mSettings:");
4496 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004497
4498 p.println(" mStartInputHistory:");
4499 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004501
Jeff Brownb88102f2010-09-08 11:49:43 -07004502 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 pw.flush();
4505 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004506 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4507 } catch (IOException | RemoteException e) {
4508 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004510 } else {
4511 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004513
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004514 if (focusedWindowClient != null && client != focusedWindowClient) {
4515 p.println(" ");
4516 p.println("Warning: Current input method client doesn't match the last focused. "
4517 + "window.");
4518 p.println("Dumping input method client in the last focused window just in case.");
4519 p.println(" ");
4520 pw.flush();
4521 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004522 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4523 } catch (IOException | RemoteException e) {
4524 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004525 }
4526 }
4527
Jeff Brownb88102f2010-09-08 11:49:43 -07004528 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530 pw.flush();
4531 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004532 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4533 } catch (IOException | RemoteException e) {
4534 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004536 } else {
4537 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 }
4539 }
4540}