blob: 8ad3d23648bf1cbbd1c30289741ba51a401d1f08 [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;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070053import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070054import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070055import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080056import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080057import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070058import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070060import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090061import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090062import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090063import android.app.Notification;
64import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070065import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090066import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080068import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.ContentResolver;
70import android.content.Context;
71import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090073import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090075import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070077import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090078import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.content.pm.PackageManager;
80import android.content.pm.ResolveInfo;
81import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070082import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.content.res.Resources;
84import android.content.res.TypedArray;
85import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080086import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080087import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080088import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070089import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040091import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070092import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090093import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Handler;
95import android.os.IBinder;
96import android.os.IInterface;
97import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070098import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -0700100import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800102import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.os.ServiceManager;
104import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900105import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800106import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.provider.Settings;
108import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900109import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700110import android.util.ArrayMap;
111import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700112import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900114import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900115import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.util.PrintWriterPrinter;
117import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900118import android.util.Slog;
119import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900120import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700122import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900123import android.view.LayoutInflater;
124import android.view.View;
125import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700126import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700128import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900129import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800131import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700132import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import android.view.inputmethod.InputMethod;
134import android.view.inputmethod.InputMethodInfo;
135import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700136import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900137import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900138import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900139import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900140import android.widget.CompoundButton;
141import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900142import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900143import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900144import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700145import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
satoke7c6998e2011-06-03 17:57:59 +0900147import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900149import java.io.FileInputStream;
150import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151import java.io.IOException;
152import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700153import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100154import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700155import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800156import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900158import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800159import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160import java.util.HashMap;
161import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800162import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800163import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800164import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166/**
167 * This class provides a system service that manages input methods.
168 */
169public class InputMethodManagerService extends IInputMethodManager.Stub
170 implements ServiceConnection, Handler.Callback {
171 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700172 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700173 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700175 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
176 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
177 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 static final int MSG_UNBIND_INPUT = 1000;
180 static final int MSG_BIND_INPUT = 1010;
181 static final int MSG_SHOW_SOFT_INPUT = 1020;
182 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700183 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 static final int MSG_ATTACH_TOKEN = 1040;
185 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800188
Yohei Yukawa33e81792015-11-17 21:14:42 -0800189 static final int MSG_UNBIND_CLIENT = 3000;
190 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700191 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700192 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900193 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800194 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800195 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
satok01038492012-04-09 21:08:27 +0900197 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
198
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700199 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
200
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700201 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
satokf9f01002011-05-19 21:31:50 +0900203 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
204
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900205 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900206 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900207
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700208 /**
209 * Binding flags for establishing connection to the {@link InputMethodService}.
210 */
211 private static final int IME_CONNECTION_BIND_FLAGS =
212 Context.BIND_AUTO_CREATE
213 | Context.BIND_NOT_VISIBLE
214 | Context.BIND_NOT_FOREGROUND
215 | Context.BIND_SHOWING_UI;
216
217 /**
218 * Binding flags used only while the {@link InputMethodService} is showing window.
219 */
220 private static final int IME_VISIBLE_BIND_FLAGS =
221 Context.BIND_AUTO_CREATE
222 | Context.BIND_TREAT_LIKE_ACTIVITY
223 | Context.BIND_FOREGROUND_SERVICE;
224
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700225 @Retention(SOURCE)
226 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
227 private @interface HardKeyboardBehavior {
228 int WIRELESS_AFFORDANCE = 0;
229 int WIRED_AFFORDANCE = 1;
230 }
satok4e4569d2010-11-19 18:45:53 +0900231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800233 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900235 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700238 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700240 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900241 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900242 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900243 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800244 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900246 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 // All known input methods. mMethodMap also serves as the global
249 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700250 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
251 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900252 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700253 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900254 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
Yohei Yukawae0733062017-02-09 22:49:35 -0800256 /**
257 * Tracks how many times {@link #mMethodMap} was updated.
258 */
259 @GuardedBy("mMethodMap")
260 private int mMethodMapUpdateCount = 0;
261
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700262 // Used to bring IME service up to visible adjustment while it is being shown.
263 final ServiceConnection mVisibleConnection = new ServiceConnection() {
264 @Override public void onServiceConnected(ComponentName name, IBinder service) {
265 }
266
267 @Override public void onServiceDisconnected(ComponentName name) {
268 }
269 };
270 boolean mVisibleBound = false;
271
satok7cfc0ed2011-06-20 21:29:36 +0900272 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700273 private NotificationManager mNotificationManager;
274 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700275 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400276 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700277 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900278 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900279 private boolean mNotificationShown;
280
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900281 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 final ClientState client;
283 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700284
285 IInputMethodSession session;
286 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 @Override
289 public String toString() {
290 return "SessionState{uid " + client.uid + " pid " + client.pid
291 + " method " + Integer.toHexString(
292 System.identityHashCode(method))
293 + " session " + Integer.toHexString(
294 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700295 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 + "}";
297 }
298
299 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700300 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 client = _client;
302 method = _method;
303 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700304 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
Jeff Brownc28867a2013-03-26 15:42:39 -0700308 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 final IInputMethodClient client;
310 final IInputContext inputContext;
311 final int uid;
312 final int pid;
313 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 boolean sessionRequested;
316 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 @Override
319 public String toString() {
320 return "ClientState{" + Integer.toHexString(
321 System.identityHashCode(this)) + " uid " + uid
322 + " pid " + pid + "}";
323 }
324
325 ClientState(IInputMethodClient _client, IInputContext _inputContext,
326 int _uid, int _pid) {
327 client = _client;
328 inputContext = _inputContext;
329 uid = _uid;
330 pid = _pid;
331 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
332 }
333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700335 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700338 * Set once the system is ready to run third party code.
339 */
340 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700342 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700343 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
344 * method. This is to be synchronized with the secure settings keyed with
345 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
346 *
347 * <p>This can be transiently {@code null} when the system is re-initializing input method
348 * settings, e.g., the system locale is just changed.</p>
349 *
350 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
351 * {@link InputMethodManagerService}.</p>
352 *
353 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700355 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
359 * The current binding sequence number, incremented every time there is
360 * a new bind performed.
361 */
362 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
365 * The client that is currently bound to an input method.
366 */
367 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800370 * The last window token that we confirmed to be focused. This is always updated upon reports
371 * from the input method client. If the window state is already changed before the report is
372 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700373 */
374 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700376 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800377 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
378 *
379 * @see #mCurFocusedWindow
380 */
381 int mCurFocusedWindowSoftInputMode;
382
383 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800384 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
385 */
386 ClientState mCurFocusedWindowClient;
387
388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * The input context last provided by the current client.
390 */
391 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700394 * The missing method flags for the input context last provided by the current client.
395 *
396 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
397 */
398 int mCurInputContextMissingMethods;
399
400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 * The attributes last provided by the current client.
402 */
403 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700406 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700408 *
409 * <p>This can be {@code null} when no input method is connected.</p>
410 *
411 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700413 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 /**
satokab751aa2010-09-14 19:17:36 +0900417 * The current subtype of the current input method.
418 */
419 private InputMethodSubtype mCurrentSubtype;
420
satok4e4569d2010-11-19 18:45:53 +0900421 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900422 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700423 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900424
John Spurlocke0980502013-10-25 11:59:29 -0400425 // Was the keyguard locked when this client became current?
426 private boolean mCurClientInKeyguard;
427
satokab751aa2010-09-14 19:17:36 +0900428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 * Set to true if our ServiceConnection is currently actively bound to
430 * a service (whether or not we have gotten its IBinder back yet).
431 */
432 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
435 * Set if the client has asked for the input method to be shown.
436 */
437 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
440 * Set if we were explicitly told to show the input method.
441 */
442 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
445 * Set if we were forced to be shown.
446 */
447 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 /**
450 * Set if we last told the input method to show itself.
451 */
452 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800455 * {@code true} if the current input method is in fullscreen mode.
456 */
457 boolean mInFullscreenMode;
458
459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 * The Intent used to connect to the current input method.
461 */
462 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 /**
465 * The token we have made for the currently active input method, to
466 * identify it in the future.
467 */
468 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 /**
471 * If non-null, this is the input method service we are currently connected
472 * to.
473 */
474 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 /**
477 * Time that we last initiated a bind to the input method, to determine
478 * if we should try to disconnect and reconnect to it.
479 */
480 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 /**
483 * Have we called mCurMethod.bindInput()?
484 */
485 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 /**
488 * Currently enabled session. Only touched by service thread, not
489 * protected by a lock.
490 */
491 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700494 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700496 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900498 int mCurUserActionNotificationSequenceNumber = 0;
499
Joe Onorato857fd9b2011-01-27 15:08:35 -0800500 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900501
502 /**
503 * A set of status bits regarding the active IME.
504 *
505 * <p>This value is a combination of following two bits:</p>
506 * <dl>
507 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
508 * <dd>
509 * If this bit is ON, connected IME is ready to accept touch/key events.
510 * </dd>
511 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
512 * <dd>
513 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
514 * </dd>
515 * </dl>
516 * <em>Do not update this value outside of setImeWindowStatus.</em>
517 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800518 int mImeWindowVis;
519
Ken Wakasa05dbb652011-08-22 15:22:43 +0900520 private AlertDialog.Builder mDialogBuilder;
521 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700522 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900523 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700524 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900525 private InputMethodInfo[] mIms;
526 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800527 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700528 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800529 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900530 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
531 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500532 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700533 @HardKeyboardBehavior
534 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800535
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800536 /**
537 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
538 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
539 * will not affect those tasks that are already posted.
540 *
541 * <p>Posting {@link #MSG_START_INPUT} message basically means that
542 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
543 * back in the current IME process shortly, which will also affect what the current IME starts
544 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
545 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
546 * logical input session between the client application and the current IME.</p>
547 *
548 * <p>Be careful to not keep strong references to this object forever, which can prevent
549 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
550 * </p>
551 */
552 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800553 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
554
555 final int mSequenceNumber;
556 final long mTimestamp;
557 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800558 @NonNull
559 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800560 @NonNull
561 final String mImeId;
562 // @InputMethodClient.StartInputReason
563 final int mStartInputReason;
564 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800565 @Nullable
566 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800567 @NonNull
568 final EditorInfo mEditorInfo;
569 final int mTargetWindowSoftInputMode;
570 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800571
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800572 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
573 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
574 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
575 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
576 mSequenceNumber = sSequenceNumber.getAndIncrement();
577 mTimestamp = SystemClock.uptimeMillis();
578 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800579 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800580 mImeId = imeId;
581 mStartInputReason = startInputReason;
582 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800583 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800584 mEditorInfo = editorInfo;
585 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
586 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800587 }
588 }
589
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800590 @GuardedBy("mMethodMap")
591 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800592
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800593 /**
594 * A ring buffer to store the history of {@link StartInputInfo}.
595 */
596 private static final class StartInputHistory {
597 /**
598 * Entry size for non low-RAM devices.
599 *
600 * <p>TODO: Consider to follow what other system services have been doing to manage
601 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
602 */
603 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
604
605 /**
606 * Entry size for non low-RAM devices.
607 *
608 * <p>TODO: Consider to follow what other system services have been doing to manage
609 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
610 */
611 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
612
613 private static int getEntrySize() {
614 if (ActivityManager.isLowRamDeviceStatic()) {
615 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
616 } else {
617 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
618 }
619 }
620
621 /**
622 * Backing store for the ring bugger.
623 */
624 private final Entry[] mEntries = new Entry[getEntrySize()];
625
626 /**
627 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
628 * write.
629 */
630 private int mNextIndex = 0;
631
632 /**
633 * Recyclable entry to store the information in {@link StartInputInfo}.
634 */
635 private static final class Entry {
636 int mSequenceNumber;
637 long mTimestamp;
638 long mWallTime;
639 @NonNull
640 String mImeTokenString;
641 @NonNull
642 String mImeId;
643 /* @InputMethodClient.StartInputReason */
644 int mStartInputReason;
645 boolean mRestarting;
646 @NonNull
647 String mTargetWindowString;
648 @NonNull
649 EditorInfo mEditorInfo;
650 int mTargetWindowSoftInputMode;
651 int mClientBindSequenceNumber;
652
653 Entry(@NonNull StartInputInfo original) {
654 set(original);
655 }
656
657 void set(@NonNull StartInputInfo original) {
658 mSequenceNumber = original.mSequenceNumber;
659 mTimestamp = original.mTimestamp;
660 mWallTime = original.mWallTime;
661 // Intentionally convert to String so as not to keep a strong reference to a Binder
662 // object.
663 mImeTokenString = String.valueOf(original.mImeToken);
664 mImeId = original.mImeId;
665 mStartInputReason = original.mStartInputReason;
666 mRestarting = original.mRestarting;
667 // Intentionally convert to String so as not to keep a strong reference to a Binder
668 // object.
669 mTargetWindowString = String.valueOf(original.mTargetWindow);
670 mEditorInfo = original.mEditorInfo;
671 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
672 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
673 }
674 }
675
676 /**
677 * Add a new entry and discard the oldest entry as needed.
678 * @param info {@lin StartInputInfo} to be added.
679 */
680 void addEntry(@NonNull StartInputInfo info) {
681 final int index = mNextIndex;
682 if (mEntries[index] == null) {
683 mEntries[index] = new Entry(info);
684 } else {
685 mEntries[index].set(info);
686 }
687 mNextIndex = (mNextIndex + 1) % mEntries.length;
688 }
689
690 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
691 final SimpleDateFormat dataFormat =
692 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
693
694 for (int i = 0; i < mEntries.length; ++i) {
695 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
696 if (entry == null) {
697 continue;
698 }
699 pw.print(prefix);
700 pw.println("StartInput #" + entry.mSequenceNumber + ":");
701
702 pw.print(prefix);
703 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
704 + " (timestamp=" + entry.mTimestamp + ")"
705 + " reason="
706 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
707 + " restarting=" + entry.mRestarting);
708
709 pw.print(prefix);
710 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
711
712 pw.print(prefix);
713 pw.println(" targetWin=" + entry.mTargetWindowString
714 + " [" + entry.mEditorInfo.packageName + "]"
715 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
716
717 pw.print(prefix);
718 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
719 entry.mTargetWindowSoftInputMode));
720
721 pw.print(prefix);
722 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
723 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
724 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
725 + " fieldName=" + entry.mEditorInfo.fieldName
726 + " actionId=" + entry.mEditorInfo.actionId
727 + " actionLabel=" + entry.mEditorInfo.actionLabel);
728 }
729 }
730 }
731
732 @GuardedBy("mMethodMap")
733 @NonNull
734 private final StartInputHistory mStartInputHistory = new StartInputHistory();
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700737 int mUserId;
738 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700739 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800740 String mLastEnabled = "";
741
Yohei Yukawa81482972015-06-04 00:58:59 -0700742 /**
743 * <em>This constructor must be called within the lock.</em>
744 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 SettingsObserver(Handler handler) {
746 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700747 }
748
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800749 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700750 if (mRegistered && mUserId == userId) {
751 return;
752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700754 if (mRegistered) {
755 mContext.getContentResolver().unregisterContentObserver(this);
756 mRegistered = false;
757 }
758 if (mUserId != userId) {
759 mLastEnabled = "";
760 mUserId = userId;
761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700763 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900764 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700765 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900766 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700767 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700768 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700769 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800770 resolver.registerContentObserver(Settings.Secure.getUriFor(
771 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700772 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
Michael Wright7b5a96b2014-08-09 19:28:42 -0700775 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800776 final Uri showImeUri = Settings.Secure.getUriFor(
777 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
778 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
779 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700781 if (showImeUri.equals(uri)) {
782 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800783 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
784 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
785 mContext.getContentResolver(),
786 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
787 0, mUserId) == 1;
788 if (mAccessibilityRequestingNoSoftKeyboard) {
789 final boolean showRequested = mShowRequested;
790 hideCurrentInputLocked(0, null);
791 mShowRequested = showRequested;
792 } else if (mShowRequested) {
793 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
794 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700795 } else {
796 boolean enabledChanged = false;
797 String newEnabled = mSettings.getEnabledInputMethodsStr();
798 if (!mLastEnabled.equals(newEnabled)) {
799 mLastEnabled = newEnabled;
800 enabledChanged = true;
801 }
802 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700806
807 @Override
808 public String toString() {
809 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
810 + " mLastEnabled=" + mLastEnabled + "}";
811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
Yohei Yukawa79247822017-01-23 15:26:15 -0800814 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900815 @Override
816 public void onReceive(Context context, Intent intent) {
817 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700818 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900819 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700820 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900821 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800822 } else if (Intent.ACTION_USER_ADDED.equals(action)
823 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100824 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800825 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700826 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
827 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
828 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
829 final String prevValue = intent.getStringExtra(
830 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
831 final String newValue = intent.getStringExtra(
832 Intent.EXTRA_SETTING_NEW_VALUE);
833 restoreEnabledInputMethods(mContext, prevValue, newValue);
834 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800835 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800836 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900837 } else {
838 Slog.w(TAG, "Unexpected intent " + intent);
839 }
840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800842
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800843 /**
844 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
845 *
846 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
847 * the users. We should ignore this event if this is about any background user's locale.</p>
848 *
849 * <p>Caution: This method must not be called when system is not ready.</p>
850 */
851 void onActionLocaleChanged() {
852 synchronized (mMethodMap) {
853 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
854 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
855 return;
856 }
857 buildInputMethodListLocked(true);
858 // If the locale is changed, needs to reset the default ime
859 resetDefaultImeLocked(mContext);
860 updateFromSettingsLocked(true);
861 mLastSystemLocales = possibleNewLocale;
862 }
863 }
864
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700865 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
866 // does not attempt to validate on the fly with any installed device policy, so must only
867 // be run in the context of initial device setup.
868 //
869 // TODO: Move this method to InputMethodUtils with adding unit tests.
870 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
871 if (DEBUG_RESTORE) {
872 Slog.i(TAG, "Restoring enabled input methods:");
873 Slog.i(TAG, "prev=" + prevValue);
874 Slog.i(TAG, " new=" + newValue);
875 }
876 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900877 ArrayMap<String, ArraySet<String>> prevMap =
878 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
879 ArrayMap<String, ArraySet<String>> newMap =
880 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700881
882 // Merge the restored ime+subtype enabled states into the live state
883 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
884 final String imeId = entry.getKey();
885 ArraySet<String> prevSubtypes = prevMap.get(imeId);
886 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700887 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700888 prevMap.put(imeId, prevSubtypes);
889 }
890 prevSubtypes.addAll(entry.getValue());
891 }
892
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700893 final String mergedImesAndSubtypesString =
894 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700895 if (DEBUG_RESTORE) {
896 Slog.i(TAG, "Merged IME string:");
897 Slog.i(TAG, " " + mergedImesAndSubtypesString);
898 }
899 Settings.Secure.putString(context.getContentResolver(),
900 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
901 }
902
Yohei Yukawac4e44912017-02-09 19:30:22 -0800903 final class MyPackageMonitor extends PackageMonitor {
904 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800905 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800906 *
907 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
908 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
909 * dynamically unless the entire package is updated, which also always triggers package
910 * rescanning.</p>
911 */
912 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800913 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
914
915 /**
916 * Packages that are appeared, disappeared, or modified for whatever reason.
917 *
918 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
919 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
920 * duplicate elements for our use case.</p>
921 *
922 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
923 * which should be bound to {@link #getRegisteredHandler()}.</p>
924 */
925 private final ArrayList<String> mChangedPackages = new ArrayList<>();
926
927 /**
928 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
929 *
930 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
931 * which should be bound to {@link #getRegisteredHandler()}.</p>
932 */
933 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800934
935 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800936 void clearKnownImePackageNamesLocked() {
937 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800938 }
939
940 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800941 final void addKnownImePackageNameLocked(@NonNull String packageName) {
942 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800943 }
944
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800945 @GuardedBy("mMethodMap")
946 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900947 final int userId = getChangingUserId();
948 final boolean retval = userId == mSettings.getCurrentUserId();
949 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900950 if (!retval) {
951 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
952 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900953 }
954 return retval;
955 }
956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800958 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800960 if (!isChangingPackagesOfCurrentUserLocked()) {
961 return false;
962 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900963 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 final int N = mMethodList.size();
965 if (curInputMethodId != null) {
966 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800967 InputMethodInfo imi = mMethodList.get(i);
968 if (imi.getId().equals(curInputMethodId)) {
969 for (String pkg : packages) {
970 if (imi.getPackageName().equals(pkg)) {
971 if (!doit) {
972 return true;
973 }
satok723a27e2010-11-11 14:58:11 +0900974 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800975 chooseNewDefaultIMELocked();
976 return true;
977 }
978 }
979 }
980 }
981 }
982 }
983 return false;
984 }
985
986 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800987 public void onBeginPackageChanges() {
988 clearPackageChangeState();
989 }
990
991 @Override
992 public void onPackageAppeared(String packageName, int reason) {
993 if (!mImePackageAppeared) {
994 final PackageManager pm = mContext.getPackageManager();
995 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
996 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
997 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
998 // No need to lock this because we access it only on getRegisteredHandler().
999 if (!services.isEmpty()) {
1000 mImePackageAppeared = true;
1001 }
1002 }
1003 // No need to lock this because we access it only on getRegisteredHandler().
1004 mChangedPackages.add(packageName);
1005 }
1006
1007 @Override
1008 public void onPackageDisappeared(String packageName, int reason) {
1009 // No need to lock this because we access it only on getRegisteredHandler().
1010 mChangedPackages.add(packageName);
1011 }
1012
1013 @Override
1014 public void onPackageModified(String packageName) {
1015 // No need to lock this because we access it only on getRegisteredHandler().
1016 mChangedPackages.add(packageName);
1017 }
1018
1019 @Override
1020 public void onPackagesSuspended(String[] packages) {
1021 // No need to lock this because we access it only on getRegisteredHandler().
1022 for (String packageName : packages) {
1023 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001024 }
1025 }
1026
1027 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001028 public void onPackagesUnsuspended(String[] packages) {
1029 // No need to lock this because we access it only on getRegisteredHandler().
1030 for (String packageName : packages) {
1031 mChangedPackages.add(packageName);
1032 }
1033 }
1034
1035 @Override
1036 public void onFinishPackageChanges() {
1037 onFinishPackageChangesInternal();
1038 clearPackageChangeState();
1039 }
1040
1041 private void clearPackageChangeState() {
1042 // No need to lock them because we access these fields only on getRegisteredHandler().
1043 mChangedPackages.clear();
1044 mImePackageAppeared = false;
1045 }
1046
1047 private boolean shouldRebuildInputMethodListLocked() {
1048 // This method is guaranteed to be called only by getRegisteredHandler().
1049
1050 // If there is any new package that contains at least one IME, then rebuilt the list
1051 // of IMEs.
1052 if (mImePackageAppeared) {
1053 return true;
1054 }
1055
1056 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1057 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1058 // is an option, but it may still do some extra operations that we do not need here.
1059 final int N = mChangedPackages.size();
1060 for (int i = 0; i < N; ++i) {
1061 final String packageName = mChangedPackages.get(i);
1062 if (mKnownImePackageNames.contains(packageName)) {
1063 return true;
1064 }
1065 }
1066 return false;
1067 }
1068
1069 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001070 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001071 if (!isChangingPackagesOfCurrentUserLocked()) {
1072 return;
1073 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001074 if (!shouldRebuildInputMethodListLocked()) {
1075 return;
1076 }
1077
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001078 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001079 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001080 final int N = mMethodList.size();
1081 if (curInputMethodId != null) {
1082 for (int i=0; i<N; i++) {
1083 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001084 final String imiId = imi.getId();
1085 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001086 curIm = imi;
1087 }
satoke7c6998e2011-06-03 17:57:59 +09001088
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001089 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001090 if (isPackageModified(imi.getPackageName())) {
1091 mFileManager.deleteAllInputMethodSubtypes(imiId);
1092 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001093 if (change == PACKAGE_TEMPORARY_CHANGE
1094 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001096 + imi.getComponent());
1097 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
1099 }
1100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
Yohei Yukawa94e33302016-02-12 19:37:03 -08001102 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001106 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001107 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001108 if (change == PACKAGE_TEMPORARY_CHANGE
1109 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001110 ServiceInfo si = null;
1111 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 si = mIPackageManager.getServiceInfo(
1113 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1114 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001115 }
1116 if (si == null) {
1117 // Uh oh, current input method is no longer around!
1118 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001119 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001120 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001121 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001122 changed = true;
1123 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001124 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001125 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001126 }
1127 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001128 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001129 }
satokab751aa2010-09-14 19:17:36 +09001130
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001131 if (curIm == null) {
1132 // We currently don't have a default input method... is
1133 // one now available?
1134 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001135 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1136 // Even if the current input method is still available, mCurrentSubtype could
1137 // be obsolete when the package is modified in practice.
1138 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001141 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001142 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
1144 }
1145 }
1146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
Jeff Brownc28867a2013-03-26 15:42:39 -07001148 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001149 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001150 private final IInputMethod mMethod;
1151 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001152
Jeff Brownc28867a2013-03-26 15:42:39 -07001153 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1154 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001155 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001156 mMethod = method;
1157 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
satoke7c6998e2011-06-03 17:57:59 +09001160 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001161 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001162 long ident = Binder.clearCallingIdentity();
1163 try {
1164 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1165 } finally {
1166 Binder.restoreCallingIdentity(ident);
1167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001170
satok01038492012-04-09 21:08:27 +09001171 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001172 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001173 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001174 public void onHardKeyboardStatusChange(boolean available) {
1175 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1176 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001177 }
1178
Michael Wright7b5a96b2014-08-09 19:28:42 -07001179 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001180 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001181 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001182 }
1183 synchronized(mMethodMap) {
1184 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1185 && mSwitchingDialog.isShowing()) {
1186 mSwitchingDialogTitleView.findViewById(
1187 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1188 available ? View.VISIBLE : View.GONE);
1189 }
1190 }
1191 }
1192 }
1193
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001194 public static final class Lifecycle extends SystemService {
1195 private InputMethodManagerService mService;
1196
1197 public Lifecycle(Context context) {
1198 super(context);
1199 mService = new InputMethodManagerService(context);
1200 }
1201
1202 @Override
1203 public void onStart() {
1204 LocalServices.addService(InputMethodManagerInternal.class,
1205 new LocalServiceImpl(mService.mHandler));
1206 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1207 }
1208
1209 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001210 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001211 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001212 // TODO: Dispatch this to a worker thread as needed.
1213 mService.onSwitchUser(userHandle);
1214 }
1215
1216 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001217 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001218 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001219 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001220 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1221 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1222 .getService(Context.STATUS_BAR_SERVICE);
1223 mService.systemRunning(statusBarService);
1224 }
1225 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001226
1227 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001228 public void onUnlockUser(final @UserIdInt int userHandle) {
1229 // Called on ActivityManager thread.
1230 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001231 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001232 }
1233 }
1234
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001235 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001236 synchronized(mMethodMap) {
1237 final int currentUserId = mSettings.getCurrentUserId();
1238 if (DEBUG) {
1239 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1240 }
1241 if (userId != currentUserId) {
1242 return;
1243 }
1244 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001245 if (mSystemReady) {
1246 // We need to rebuild IMEs.
1247 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1248 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1249 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001250 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001251 }
1252
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001253 void onSwitchUser(@UserIdInt int userId) {
1254 synchronized (mMethodMap) {
1255 switchUserLocked(userId);
1256 }
1257 }
1258
Seigo Nonaka7309b122015-08-17 18:34:13 -07001259 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001260 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001262 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001264 // Note: SettingsObserver doesn't register observers in its constructor.
1265 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 mIWindowManager = IWindowManager.Stub.asInterface(
1267 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001268 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001269 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001270 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 public void executeMessage(Message msg) {
1272 handleMessage(msg);
1273 }
Mita Yuned218c72012-12-06 17:18:25 -08001274 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001275 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001276 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001277 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001278 mHasFeature = context.getPackageManager().hasSystemFeature(
1279 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001280 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001281 mHardKeyboardBehavior = mContext.getResources().getInteger(
1282 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001283
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001284 Bundle extras = new Bundle();
1285 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001286 mImeSwitcherNotification =
1287 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1288 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1289 .setWhen(0)
1290 .setOngoing(true)
1291 .addExtras(extras)
1292 .setCategory(Notification.CATEGORY_SYSTEM)
1293 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001294
satok7cfc0ed2011-06-20 21:29:36 +09001295 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001296 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001297
1298 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001299
satok7cfc0ed2011-06-20 21:29:36 +09001300 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001301 int userId = 0;
1302 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001303 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001304 } catch (RemoteException e) {
1305 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1306 }
satok913a8922010-08-26 21:53:41 +09001307
satokd87c2592010-09-29 11:52:06 +09001308 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001309 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001310 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001311
Kenny Guy2a764942014-04-02 13:29:20 +01001312 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001313 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001314 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1315 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001316 }
1317
satok5b927c432012-05-01 20:09:34 +09001318 private void resetDefaultImeLocked(Context context) {
1319 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001320 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001321 return;
1322 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001323 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001324 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001325 if (suitableImes.isEmpty()) {
1326 Slog.i(TAG, "No default found");
1327 return;
satok5b927c432012-05-01 20:09:34 +09001328 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001329 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001330 if (DEBUG) {
1331 Slog.i(TAG, "Default found, using " + defIm.getId());
1332 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001333 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001334 }
1335
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001336 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001337 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1338 + " currentUserId=" + mSettings.getCurrentUserId());
1339
Yohei Yukawa81482972015-06-04 00:58:59 -07001340 // ContentObserver should be registered again when the user is changed
1341 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001342
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001343 // If the system is not ready or the device is not yed unlocked by the user, then we use
1344 // copy-on-write settings.
1345 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001346 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001347 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001348 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001349 // InputMethodFileManager should be reset when the user is changed
1350 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001351 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001352
1353 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1354 + " defaultImiId=" + defaultImiId);
1355
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001356 // For secondary users, the list of enabled IMEs may not have been updated since the
1357 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1358 // not be empty even if the IME has been uninstalled by the primary user.
1359 // Even in such cases, IMMS works fine because it will find the most applicable
1360 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001361 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001362 mLastSystemLocales = mRes.getConfiguration().getLocales();
1363
1364 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1365 if (mSystemReady) {
1366 hideCurrentInputLocked(0, null);
1367 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1368 buildInputMethodListLocked(initialUserSwitch);
1369 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1370 // This is the first time of the user switch and
1371 // set the current ime to the proper one.
1372 resetDefaultImeLocked(mContext);
1373 }
1374 updateFromSettingsLocked(true);
1375 try {
1376 startInputInnerLocked();
1377 } catch (RuntimeException e) {
1378 Slog.w(TAG, "Unexpected exception", e);
1379 }
1380 }
1381
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001382 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001383 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1384 mSettings.getEnabledInputMethodListLocked(), newUserId,
1385 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001386 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001387
1388 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1389 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001390 }
1391
Kenny Guy2a764942014-04-02 13:29:20 +01001392 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001393 mSettings.setCurrentProfileIds(
1394 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001395 }
1396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 @Override
1398 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1399 throws RemoteException {
1400 try {
1401 return super.onTransact(code, data, reply, flags);
1402 } catch (RuntimeException e) {
1403 // The input method manager only throws security exceptions, so let's
1404 // log all others.
1405 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001406 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
1408 throw e;
1409 }
1410 }
1411
Svetoslav Ganova0027152013-06-25 14:59:53 -07001412 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001413 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001414 if (DEBUG) {
1415 Slog.d(TAG, "--- systemReady");
1416 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001417 if (!mSystemReady) {
1418 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001419 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001420 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001421 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001422 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001423 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1424 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001425 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001426 if (mStatusBar != null) {
1427 mStatusBar.setIconVisibility(mSlotIme, false);
1428 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001429 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001430 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1431 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001432 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001433 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001434 mHardKeyboardListener);
1435 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001436
1437 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1438 mSettingsObserver.registerContentObserverLocked(currentUserId);
1439
1440 final IntentFilter broadcastFilter = new IntentFilter();
1441 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1442 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1443 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1444 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1445 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1446 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1447
1448 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1449 resetDefaultImeLocked(mContext);
1450 updateFromSettingsLocked(true);
1451 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1452 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1453 mContext.getBasePackageName());
1454
Dianne Hackborncc278702009-09-02 23:07:23 -07001455 try {
1456 startInputInnerLocked();
1457 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001458 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001459 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001460 }
1461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001464 // ---------------------------------------------------------------------------------------
1465 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1466 // 1) it comes from the system process
1467 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1468 private boolean calledFromValidUser() {
1469 final int uid = Binder.getCallingUid();
1470 final int userId = UserHandle.getUserId(uid);
1471 if (DEBUG) {
1472 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1473 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1474 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001475 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1476 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001477 }
Kenny Guy2a764942014-04-02 13:29:20 +01001478 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001479 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001480 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001481
1482 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1483 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1484 // must not manage background users' states in any functions.
1485 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1486 // by a token.
1487 if (mContext.checkCallingOrSelfPermission(
1488 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1489 == PackageManager.PERMISSION_GRANTED) {
1490 if (DEBUG) {
1491 Slog.d(TAG, "--- Access granted because the calling process has "
1492 + "the INTERACT_ACROSS_USERS_FULL permission");
1493 }
1494 return true;
1495 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001496 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001497 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1498 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001499 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001500 }
1501
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001502
1503 /**
1504 * Returns true iff the caller is identified to be the current input method with the token.
1505 * @param token The window token given to the input method when it was started.
1506 * @return true if and only if non-null valid token is specified.
1507 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001508 private boolean calledWithValidToken(@Nullable IBinder token) {
1509 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1510 if (DEBUG) {
1511 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1512 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1513 }
1514 return false;
1515 }
1516 if (token == null || token != mCurToken) {
1517 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1518 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1519 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001520 return false;
1521 }
1522 return true;
1523 }
1524
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001525 private boolean bindCurrentInputMethodService(
1526 Intent service, ServiceConnection conn, int flags) {
1527 if (service == null || conn == null) {
1528 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1529 return false;
1530 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001531 return mContext.bindServiceAsUser(service, conn, flags,
1532 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001533 }
1534
satoke7c6998e2011-06-03 17:57:59 +09001535 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001537 // TODO: Make this work even for non-current users?
1538 if (!calledFromValidUser()) {
1539 return Collections.emptyList();
1540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001542 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
1544 }
1545
satoke7c6998e2011-06-03 17:57:59 +09001546 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001548 // TODO: Make this work even for non-current users?
1549 if (!calledFromValidUser()) {
1550 return Collections.emptyList();
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001553 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
1555 }
1556
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001557 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001558 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001559 * @return enabled subtypes of the specified imi
1560 */
satoke7c6998e2011-06-03 17:57:59 +09001561 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001562 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001563 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001564 // TODO: Make this work even for non-current users?
1565 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001566 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001567 }
satok67ddf9c2010-11-17 09:45:54 +09001568 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001569 final InputMethodInfo imi;
1570 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001571 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001572 } else {
1573 imi = mMethodMap.get(imiId);
1574 }
1575 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001576 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001577 }
1578 return mSettings.getEnabledInputMethodSubtypeListLocked(
1579 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001580 }
1581 }
1582
satoke7c6998e2011-06-03 17:57:59 +09001583 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 public void addClient(IInputMethodClient client,
1585 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001586 if (!calledFromValidUser()) {
1587 return;
1588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 synchronized (mMethodMap) {
1590 mClients.put(client.asBinder(), new ClientState(client,
1591 inputContext, uid, pid));
1592 }
1593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001594
satoke7c6998e2011-06-03 17:57:59 +09001595 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001597 if (!calledFromValidUser()) {
1598 return;
1599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001601 ClientState cs = mClients.remove(client.asBinder());
1602 if (cs != null) {
1603 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001604 if (mCurClient == cs) {
1605 mCurClient = null;
1606 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001607 if (mCurFocusedWindowClient == cs) {
1608 mCurFocusedWindowClient = null;
1609 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 void executeOrSendMessage(IInterface target, Message msg) {
1615 if (target.asBinder() instanceof Binder) {
1616 mCaller.sendMessage(msg);
1617 } else {
1618 handleMessage(msg);
1619 msg.recycle();
1620 }
1621 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001622
Yohei Yukawa33e81792015-11-17 21:14:42 -08001623 void unbindCurrentClientLocked(
1624 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001626 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 + mCurClient.client.asBinder());
1628 if (mBoundToMethod) {
1629 mBoundToMethod = false;
1630 if (mCurMethod != null) {
1631 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1632 MSG_UNBIND_INPUT, mCurMethod));
1633 }
1634 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001635
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001636 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1637 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001638 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1639 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001642
The Android Open Source Project10592532009-03-18 17:39:46 -07001643 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 private int getImeShowFlags() {
1648 int flags = 0;
1649 if (mShowForced) {
1650 flags |= InputMethod.SHOW_FORCED
1651 | InputMethod.SHOW_EXPLICIT;
1652 } else if (mShowExplicitlyRequested) {
1653 flags |= InputMethod.SHOW_EXPLICIT;
1654 }
1655 return flags;
1656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 private int getAppShowFlags() {
1659 int flags = 0;
1660 if (mShowForced) {
1661 flags |= InputMethodManager.SHOW_FORCED;
1662 } else if (!mShowExplicitlyRequested) {
1663 flags |= InputMethodManager.SHOW_IMPLICIT;
1664 }
1665 return flags;
1666 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001668 InputBindResult attachNewInputLocked(
1669 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 if (!mBoundToMethod) {
1671 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1672 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1673 mBoundToMethod = true;
1674 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001675
1676 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001677 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1678 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1679 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001680 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001681 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001684 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001685 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001686 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001688 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001689 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001691 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001692 (session.channel != null ? session.channel.dup() : null),
1693 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001696 InputBindResult startInputLocked(
1697 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001698 IInputMethodClient client, IInputContext inputContext,
1699 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001700 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 // If no method is currently selected, do nothing.
1702 if (mCurMethodId == null) {
1703 return mNoBinding;
1704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 ClientState cs = mClients.get(client.asBinder());
1707 if (cs == null) {
1708 throw new IllegalArgumentException("unknown client "
1709 + client.asBinder());
1710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001711
Yohei Yukawa74750f22016-03-22 12:54:22 -07001712 if (attribute == null) {
1713 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1714 + " uid=" + cs.uid + " pid=" + cs.pid);
1715 return null;
1716 }
1717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 try {
1719 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1720 // Check with the window manager to make sure this client actually
1721 // has a window with focus. If not, reject. This is thread safe
1722 // because if the focus changes some time before or after, the
1723 // next client receiving focus that has any interest in input will
1724 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001725 if (DEBUG) {
1726 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1727 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 return null;
1730 }
1731 } catch (RemoteException e) {
1732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001734 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001735 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001736 }
1737
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001738 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001739 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001740 @NonNull EditorInfo attribute, int controlFlags,
1741 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001742 // If no method is currently selected, do nothing.
1743 if (mCurMethodId == null) {
1744 return mNoBinding;
1745 }
1746
Yohei Yukawad57ba672015-06-08 16:39:46 -07001747 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1748 attribute.packageName)) {
1749 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1750 + " uid=" + cs.uid + " package=" + attribute.packageName);
1751 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001752 }
1753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001755 // Was the keyguard locked when switching over to the new client?
1756 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 // If the client is changing, we need to switch over to the new
1758 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001759 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001760 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001761 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762
1763 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001764 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001765 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001766 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
1768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 // Bump up the sequence for this client and attach it.
1771 mCurSeq++;
1772 if (mCurSeq <= 0) mCurSeq = 1;
1773 mCurClient = cs;
1774 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001775 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 // Check if the input method is changing.
1779 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1780 if (cs.curSession != null) {
1781 // Fast case: if we are already connected to the input method,
1782 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001783 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001784 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
1786 if (mHaveConnection) {
1787 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 // Return to client, and we will get back with it when
1789 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001790 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001791 return new InputBindResult(null, null, mCurId, mCurSeq,
1792 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 } else if (SystemClock.uptimeMillis()
1794 < (mLastBindTime+TIME_TO_RECONNECT)) {
1795 // In this case we have connected to the service, but
1796 // don't yet have its interface. If it hasn't been too
1797 // long since we did the connection, we'll return to
1798 // the client and wait to get the service interface so
1799 // we can report back. If it has been too long, we want
1800 // to fall through so we can try a disconnect/reconnect
1801 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001802 return new InputBindResult(null, null, mCurId, mCurSeq,
1803 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001805 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1806 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808 }
1809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001810
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001811 return startInputInnerLocked();
1812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001813
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001814 InputBindResult startInputInnerLocked() {
1815 if (mCurMethodId == null) {
1816 return mNoBinding;
1817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001818
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001819 if (!mSystemReady) {
1820 // If the system is not yet ready, we shouldn't be running third
1821 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001822 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1823 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1827 if (info == null) {
1828 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1829 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001830
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001831 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1834 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001835 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1836 com.android.internal.R.string.input_method_binding_label);
1837 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1838 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001839 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 mLastBindTime = SystemClock.uptimeMillis();
1841 mHaveConnection = true;
1842 mCurId = info.getId();
1843 mCurToken = new Binder();
1844 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001845 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001846 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 } catch (RemoteException e) {
1848 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001849 return new InputBindResult(null, null, mCurId, mCurSeq,
1850 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 } else {
1852 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001853 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 + mCurIntent);
1855 }
1856 return null;
1857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001859 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001860 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001861 IInputMethodClient client, IInputContext inputContext,
1862 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001863 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001864 if (!calledFromValidUser()) {
1865 return null;
1866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001868 if (DEBUG) {
1869 Slog.v(TAG, "startInput: reason="
1870 + InputMethodClient.getStartInputReason(startInputReason)
1871 + " client = " + client.asBinder()
1872 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001873 + " missingMethods="
1874 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001875 + " attribute=" + attribute
1876 + " controlFlags=#" + Integer.toHexString(controlFlags));
1877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 final long ident = Binder.clearCallingIdentity();
1879 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001880 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1881 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 } finally {
1883 Binder.restoreCallingIdentity(ident);
1884 }
1885 }
1886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001887
satoke7c6998e2011-06-03 17:57:59 +09001888 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 public void finishInput(IInputMethodClient client) {
1890 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001891
satoke7c6998e2011-06-03 17:57:59 +09001892 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 public void onServiceConnected(ComponentName name, IBinder service) {
1894 synchronized (mMethodMap) {
1895 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1896 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001897 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001898 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001899 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001900 return;
1901 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001902 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001903 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1904 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001906 clearClientSessionLocked(mCurClient);
1907 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
1909 }
1910 }
1911 }
1912
Jeff Brownc28867a2013-03-26 15:42:39 -07001913 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1914 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 synchronized (mMethodMap) {
1916 if (mCurMethod != null && method != null
1917 && mCurMethod.asBinder() == method.asBinder()) {
1918 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001919 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001921 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001922 InputBindResult res = attachNewInputLocked(
1923 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 if (res.method != null) {
1925 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001926 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001928 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 }
1930 }
1931 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001932
1933 // Session abandoned. Close its associated input channel.
1934 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001936
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001937 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001938 if (mVisibleBound) {
1939 mContext.unbindService(mVisibleConnection);
1940 mVisibleBound = false;
1941 }
1942
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001943 if (mHaveConnection) {
1944 mContext.unbindService(this);
1945 mHaveConnection = false;
1946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001947
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001948 if (mCurToken != null) {
1949 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001950 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001951 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001952 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001953 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001954 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001955 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001956 } catch (RemoteException e) {
1957 }
1958 mCurToken = null;
1959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001960
The Android Open Source Project10592532009-03-18 17:39:46 -07001961 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001962 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001963 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001964
Yohei Yukawa33e81792015-11-17 21:14:42 -08001965 void resetCurrentMethodAndClient(
1966 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001967 mCurMethodId = null;
1968 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001969 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001970 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001971
1972 void requestClientSessionLocked(ClientState cs) {
1973 if (!cs.sessionRequested) {
1974 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1975 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1976 cs.sessionRequested = true;
1977 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1978 MSG_CREATE_SESSION, mCurMethod, channels[1],
1979 new MethodCallback(this, mCurMethod, channels[0])));
1980 }
1981 }
1982
1983 void clearClientSessionLocked(ClientState cs) {
1984 finishSessionLocked(cs.curSession);
1985 cs.curSession = null;
1986 cs.sessionRequested = false;
1987 }
1988
1989 private void finishSessionLocked(SessionState sessionState) {
1990 if (sessionState != null) {
1991 if (sessionState.session != null) {
1992 try {
1993 sessionState.session.finishSession();
1994 } catch (RemoteException e) {
1995 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001996 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001997 }
1998 sessionState.session = null;
1999 }
2000 if (sessionState.channel != null) {
2001 sessionState.channel.dispose();
2002 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002003 }
2004 }
2005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002007 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 if (mCurMethod != null) {
2009 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002010 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002012
Jeff Brownc28867a2013-03-26 15:42:39 -07002013 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002014 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 mCurMethod = null;
2016 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002017 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002018 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002019 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002020 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002022
satoke7c6998e2011-06-03 17:57:59 +09002023 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002025 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2026 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2027 // which is irregular but can eventually happen for everyone just by continuing using the
2028 // device. Thus it is important to make sure that all the internal states are properly
2029 // refreshed when this method is called back. Running
2030 // adb install -r <APK that implements the current IME>
2031 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002033 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 + " mCurIntent=" + mCurIntent);
2035 if (mCurMethod != null && mCurIntent != null
2036 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002037 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 // We consider this to be a new bind attempt, since the system
2039 // should now try to restart the service for us.
2040 mLastBindTime = SystemClock.uptimeMillis();
2041 mShowRequested = mInputShown;
2042 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002043 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045 }
2046 }
2047
satokf9f01002011-05-19 21:31:50 +09002048 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002050 synchronized (mMethodMap) {
2051 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002052 return;
2053 }
2054 final long ident = Binder.clearCallingIdentity();
2055 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002057 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002058 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002059 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002062 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002063 CharSequence contentDescription = null;
2064 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002065 // Use PackageManager to load label
2066 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002067 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002068 mIPackageManager.getApplicationInfo(packageName, 0,
2069 mSettings.getCurrentUserId()));
2070 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002071 /* ignore */
2072 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002073 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002074 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002075 contentDescription != null
2076 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002077 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002080 } finally {
2081 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084 }
2085
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002086 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002087 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002088 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09002089 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002090 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002091 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002092 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2093 // When physical keyboard is attached, we show the ime switcher (or notification if
2094 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2095 // exists in the IME switcher dialog. Might be OK to remove this condition once
2096 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2097 return true;
2098 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002099 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2100 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002101 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002102
2103 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2104 final int N = imis.size();
2105 if (N > 2) return true;
2106 if (N < 1) return false;
2107 int nonAuxCount = 0;
2108 int auxCount = 0;
2109 InputMethodSubtype nonAuxSubtype = null;
2110 InputMethodSubtype auxSubtype = null;
2111 for(int i = 0; i < N; ++i) {
2112 final InputMethodInfo imi = imis.get(i);
2113 final List<InputMethodSubtype> subtypes =
2114 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2115 final int subtypeCount = subtypes.size();
2116 if (subtypeCount == 0) {
2117 ++nonAuxCount;
2118 } else {
2119 for (int j = 0; j < subtypeCount; ++j) {
2120 final InputMethodSubtype subtype = subtypes.get(j);
2121 if (!subtype.isAuxiliary()) {
2122 ++nonAuxCount;
2123 nonAuxSubtype = subtype;
2124 } else {
2125 ++auxCount;
2126 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002127 }
2128 }
satok7cfc0ed2011-06-20 21:29:36 +09002129 }
2130 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002131 if (nonAuxCount > 1 || auxCount > 1) {
2132 return true;
2133 } else if (nonAuxCount == 1 && auxCount == 1) {
2134 if (nonAuxSubtype != null && auxSubtype != null
2135 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2136 || auxSubtype.overridesImplicitlyEnabledSubtype()
2137 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2138 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2139 return false;
2140 }
2141 return true;
2142 }
2143 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002144 }
2145
John Spurlocke0980502013-10-25 11:59:29 -04002146 private boolean isKeyguardLocked() {
2147 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2148 }
2149
Yohei Yukawad6475a62017-04-17 10:35:27 -07002150 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002151 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002152 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002153 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2154 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002155 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002156 return;
2157 }
2158
Yohei Yukawa69e68022017-02-13 12:04:50 -08002159 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002160 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002161 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002162 mImeWindowVis = vis;
2163 mBackDisposition = backDisposition;
2164 updateSystemUiLocked(token, vis, backDisposition);
2165 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002166
2167 final boolean dismissImeOnBackKeyPressed;
2168 switch (backDisposition) {
2169 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2170 dismissImeOnBackKeyPressed = true;
2171 break;
2172 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2173 dismissImeOnBackKeyPressed = false;
2174 break;
2175 default:
2176 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2177 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2178 break;
2179 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002180 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2181 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002182 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002183 }
2184
2185 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2186 synchronized (mMethodMap) {
2187 updateSystemUiLocked(token, vis, backDisposition);
2188 }
2189 }
2190
2191 // Caution! This method is called in this class. Handle multi-user carefully
2192 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2193 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002194 return;
2195 }
2196
2197 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2198 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002199 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002200 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002201 // apply policy for binder calls
2202 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2203 vis = 0;
satok06487a52010-10-29 11:37:18 +09002204 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002205 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2206 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2207 if (mStatusBar != null) {
2208 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2209 needsToShowImeSwitcher);
2210 }
2211 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2212 if (imi != null && needsToShowImeSwitcher) {
2213 // Used to load label
2214 final CharSequence title = mRes.getText(
2215 com.android.internal.R.string.select_input_method);
2216 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2217 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002218 mImeSwitcherNotification.setContentTitle(title)
2219 .setContentText(summary)
2220 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002221 try {
2222 if ((mNotificationManager != null)
2223 && !mIWindowManager.hasNavigationBar()) {
2224 if (DEBUG) {
2225 Slog.d(TAG, "--- show notification: label = " + summary);
2226 }
2227 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002228 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002229 mImeSwitcherNotification.build(), UserHandle.ALL);
2230 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002231 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002232 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002233 }
2234 } else {
2235 if (mNotificationShown && mNotificationManager != null) {
2236 if (DEBUG) {
2237 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002238 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002239 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002240 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002241 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002242 }
satok06487a52010-10-29 11:37:18 +09002243 }
2244 } finally {
2245 Binder.restoreCallingIdentity(ident);
2246 }
2247 }
2248
satoke7c6998e2011-06-03 17:57:59 +09002249 @Override
satokf9f01002011-05-19 21:31:50 +09002250 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002251 if (!calledFromValidUser()) {
2252 return;
2253 }
satokf9f01002011-05-19 21:31:50 +09002254 synchronized (mMethodMap) {
2255 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2256 for (int i = 0; i < spans.length; ++i) {
2257 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002258 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002259 mSecureSuggestionSpans.put(ss, currentImi);
2260 }
2261 }
2262 }
2263 }
2264
satoke7c6998e2011-06-03 17:57:59 +09002265 @Override
satokf9f01002011-05-19 21:31:50 +09002266 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002267 if (!calledFromValidUser()) {
2268 return false;
2269 }
satokf9f01002011-05-19 21:31:50 +09002270 synchronized (mMethodMap) {
2271 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2272 // TODO: Do not send the intent if the process of the targetImi is already dead.
2273 if (targetImi != null) {
2274 final String[] suggestions = span.getSuggestions();
2275 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002276 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002277 final Intent intent = new Intent();
2278 // Ensures that only a class in the original IME package will receive the
2279 // notification.
satok42c5a162011-05-26 16:46:14 +09002280 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002281 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2282 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2283 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2284 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002285 final long ident = Binder.clearCallingIdentity();
2286 try {
2287 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2288 } finally {
2289 Binder.restoreCallingIdentity(ident);
2290 }
satokf9f01002011-05-19 21:31:50 +09002291 return true;
2292 }
2293 }
2294 return false;
2295 }
2296
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002297 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002298 updateInputMethodsFromSettingsLocked(enabledMayChange);
2299 updateKeyboardFromSettingsLocked();
2300 }
2301
2302 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002303 if (enabledMayChange) {
2304 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2305 for (int i=0; i<enabled.size(); i++) {
2306 // We allow the user to select "disabled until used" apps, so if they
2307 // are enabling one of those here we now need to make it enabled.
2308 InputMethodInfo imm = enabled.get(i);
2309 try {
2310 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2311 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2312 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002313 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002314 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002315 if (DEBUG) {
2316 Slog.d(TAG, "Update state(" + imm.getId()
2317 + "): DISABLED_UNTIL_USED -> DEFAULT");
2318 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002319 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2320 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002321 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2322 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002323 }
2324 } catch (RemoteException e) {
2325 }
2326 }
2327 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002328 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2329 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2330 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2331 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002332 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002333 // There is no input method selected, try to choose new applicable input method.
2334 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002335 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002336 }
2337 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002339 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002341 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002342 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
satokf3db1af2010-11-23 13:34:33 +09002344 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002345 } else {
2346 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002347 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002349 // Here is not the perfect place to reset the switching controller. Ideally
2350 // mSwitchingController and mSettings should be able to share the same state.
2351 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2352 // the same enabled IMEs list.
2353 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002354
2355 }
2356
2357 public void updateKeyboardFromSettingsLocked() {
2358 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2359 if (mSwitchingDialog != null
2360 && mSwitchingDialogTitleView != null
2361 && mSwitchingDialog.isShowing()) {
2362 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2363 com.android.internal.R.id.hard_keyboard_switch);
2364 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
Yohei Yukawab097b822015-12-01 10:43:08 -08002368 private void notifyInputMethodSubtypeChanged(final int userId,
2369 @Nullable final InputMethodInfo inputMethodInfo,
2370 @Nullable final InputMethodSubtype subtype) {
2371 final InputManagerInternal inputManagerInternal =
2372 LocalServices.getService(InputManagerInternal.class);
2373 if (inputManagerInternal != null) {
2374 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2375 }
2376 }
2377
satokab751aa2010-09-14 19:17:36 +09002378 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 InputMethodInfo info = mMethodMap.get(id);
2380 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002381 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002383
satokd81e9502012-05-21 12:58:45 +09002384 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002386 final int subtypeCount = info.getSubtypeCount();
2387 if (subtypeCount <= 0) {
2388 return;
satokcd7cd292010-11-20 15:46:23 +09002389 }
satokd81e9502012-05-21 12:58:45 +09002390 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2391 final InputMethodSubtype newSubtype;
2392 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2393 newSubtype = info.getSubtypeAt(subtypeId);
2394 } else {
2395 // If subtype is null, try to find the most applicable one from
2396 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002397 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002398 }
2399 if (newSubtype == null || oldSubtype == null) {
2400 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2401 + ", new subtype = " + newSubtype);
2402 return;
2403 }
2404 if (newSubtype != oldSubtype) {
2405 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2406 if (mCurMethod != null) {
2407 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002408 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002409 mCurMethod.changeInputMethodSubtype(newSubtype);
2410 } catch (RemoteException e) {
2411 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002412 return;
satokab751aa2010-09-14 19:17:36 +09002413 }
2414 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002415 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 return;
2418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002419
satokd81e9502012-05-21 12:58:45 +09002420 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 final long ident = Binder.clearCallingIdentity();
2422 try {
satokab751aa2010-09-14 19:17:36 +09002423 // Set a subtype to this input method.
2424 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002425 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2426 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2427 // because mCurMethodId is stored as a history in
2428 // setSelectedInputMethodAndSubtypeLocked().
2429 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002431 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002433 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002435 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002437 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 } finally {
2439 Binder.restoreCallingIdentity(ident);
2440 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002441
2442 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2443 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002445
satok42c5a162011-05-26 16:46:14 +09002446 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002447 public boolean showSoftInput(IInputMethodClient client, int flags,
2448 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002449 if (!calledFromValidUser()) {
2450 return false;
2451 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002452 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 long ident = Binder.clearCallingIdentity();
2454 try {
2455 synchronized (mMethodMap) {
2456 if (mCurClient == null || client == null
2457 || mCurClient.client.asBinder() != client.asBinder()) {
2458 try {
2459 // We need to check if this is the current client with
2460 // focus in the window manager, to allow this call to
2461 // be made before input is started in it.
2462 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002463 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002464 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 }
2466 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002467 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
2469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002470
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002471 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002472 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 }
2474 } finally {
2475 Binder.restoreCallingIdentity(ident);
2476 }
2477 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002478
The Android Open Source Project4df24232009-03-05 14:34:35 -08002479 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002481 if (mAccessibilityRequestingNoSoftKeyboard) {
2482 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 }
Anna Galusza9b278112016-01-04 11:37:37 -08002484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2486 mShowExplicitlyRequested = true;
2487 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002488 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2489 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002491
Dianne Hackborncc278702009-09-02 23:07:23 -07002492 if (!mSystemReady) {
2493 return false;
2494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002495
The Android Open Source Project4df24232009-03-05 14:34:35 -08002496 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002498 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002499 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2500 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2501 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002503 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002504 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002505 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002506 mVisibleBound = true;
2507 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002508 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002510 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 // The client has asked to have the input method shown, but
2512 // we have been sitting here too long with a connection to the
2513 // service and no interface received, so let's disconnect/connect
2514 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002515 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002517 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002519 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002520 } else {
2521 if (DEBUG) {
2522 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2523 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002526
The Android Open Source Project4df24232009-03-05 14:34:35 -08002527 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002529
satok42c5a162011-05-26 16:46:14 +09002530 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002531 public boolean hideSoftInput(IInputMethodClient client, int flags,
2532 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002533 if (!calledFromValidUser()) {
2534 return false;
2535 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002536 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 long ident = Binder.clearCallingIdentity();
2538 try {
2539 synchronized (mMethodMap) {
2540 if (mCurClient == null || client == null
2541 || mCurClient.client.asBinder() != client.asBinder()) {
2542 try {
2543 // We need to check if this is the current client with
2544 // focus in the window manager, to allow this call to
2545 // be made before input is started in it.
2546 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002547 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2548 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002549 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
2551 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002552 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 }
2554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002555
Joe Onorato8a9b2202010-02-26 18:56:32 -08002556 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002557 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 }
2559 } finally {
2560 Binder.restoreCallingIdentity(ident);
2561 }
2562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002563
The Android Open Source Project4df24232009-03-05 14:34:35 -08002564 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2566 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002567 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 -08002568 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
2570 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002571 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 -08002572 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002574
2575 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2576 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2577 // to be updated with the new value sent from IME process. Even in such a transient state
2578 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2579 // application process as a valid request, and have even promised such a behavior with CTS
2580 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2581 // IMMS#InputShown indicates that the software keyboard is shown.
2582 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2583 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2584 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002585 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002586 if (shouldHideSoftInput) {
2587 // The IME will report its visible state again after the following message finally
2588 // delivered to the IME process as an IPC. Hence the inconsistency between
2589 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2590 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002591 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2592 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2593 res = true;
2594 } else {
2595 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002597 if (mHaveConnection && mVisibleBound) {
2598 mContext.unbindService(mVisibleConnection);
2599 mVisibleBound = false;
2600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 mInputShown = false;
2602 mShowRequested = false;
2603 mShowExplicitlyRequested = false;
2604 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002605 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002607
satok42c5a162011-05-26 16:46:14 +09002608 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002609 public InputBindResult startInputOrWindowGainedFocus(
2610 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2611 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002612 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002613 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002614 if (windowToken != null) {
2615 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002616 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002617 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002618 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2619 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002620 }
2621 }
2622
2623 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002624 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002625 IInputMethodClient client, IBinder windowToken, int controlFlags,
2626 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002627 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2628 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002629 // Needs to check the validity before clearing calling identity
2630 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002631 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 long ident = Binder.clearCallingIdentity();
2633 try {
2634 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002635 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2636 + InputMethodClient.getStartInputReason(startInputReason)
2637 + " client=" + client.asBinder()
2638 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002639 + " missingMethods="
2640 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002641 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002642 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002643 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002644 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002645
Dianne Hackborn7663d802012-02-24 13:08:49 -08002646 ClientState cs = mClients.get(client.asBinder());
2647 if (cs == null) {
2648 throw new IllegalArgumentException("unknown client "
2649 + client.asBinder());
2650 }
2651
2652 try {
2653 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2654 // Check with the window manager to make sure this client actually
2655 // has a window with focus. If not, reject. This is thread safe
2656 // because if the focus changes some time before or after, the
2657 // next client receiving focus that has any interest in input will
2658 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002659 if (DEBUG) {
2660 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2661 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2662 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002663 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002665 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002667
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002668 if (!calledFromValidUser) {
2669 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2670 Slog.w(TAG, "If you want to interect with IME, you need "
2671 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2672 hideCurrentInputLocked(0, null);
2673 return null;
2674 }
2675
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002676 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002677 if (DEBUG) {
2678 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2679 + " attribute=" + attribute + ", token = " + windowToken);
2680 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002681 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002682 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002683 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002684 }
2685 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002686 }
2687 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002688 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002689 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002690
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002691 // Should we auto-show the IME even if the caller has not
2692 // specified what should be done with it?
2693 // We only do this automatically if the window can resize
2694 // to accommodate the IME (so what the user sees will give
2695 // them good context without input information being obscured
2696 // by the IME) or if running on a large screen where there
2697 // is more room for the target window + IME.
2698 final boolean doAutoShow =
2699 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2700 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2701 || mRes.getConfiguration().isLayoutSizeAtLeast(
2702 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002703 final boolean isTextEditor =
2704 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2705
2706 // We want to start input before showing the IME, but after closing
2707 // it. We want to do this after closing it to help the IME disappear
2708 // more quickly (not get stuck behind it initializing itself for the
2709 // new focused input, even if its window wants to hide the IME).
2710 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2713 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002714 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2716 // There is no focus view, and this window will
2717 // be behind any soft input window, so hide the
2718 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002719 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002720 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002722 } else if (isTextEditor && doAutoShow && (softInputMode &
2723 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 // There is a focus view, and we are navigating forward
2725 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002726 // We only do this automatically if the window can resize
2727 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002728 // them good context without input information being obscured
2729 // by the IME) or if running on a large screen where there
2730 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002731 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002732 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002733 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002734 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002735 didStart = true;
2736 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002737 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 }
2739 break;
2740 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2741 // Do nothing.
2742 break;
2743 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2744 if ((softInputMode &
2745 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002746 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002747 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
2749 break;
2750 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002751 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002752 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 break;
2754 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2755 if ((softInputMode &
2756 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002757 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002758 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002759 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002760 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002761 didStart = true;
2762 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002763 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 }
2765 break;
2766 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002767 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002768 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002769 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002770 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002771 didStart = true;
2772 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002773 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 break;
2775 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002776
2777 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002778 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002779 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 }
2782 } finally {
2783 Binder.restoreCallingIdentity(ident);
2784 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002785
2786 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002788
satok42c5a162011-05-26 16:46:14 +09002789 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002790 public void showInputMethodPickerFromClient(
2791 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002792 if (!calledFromValidUser()) {
2793 return;
2794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 synchronized (mMethodMap) {
2796 if (mCurClient == null || client == null
2797 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002798 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002799 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 }
2801
satok440aab52010-11-25 09:43:11 +09002802 // Always call subtype picker, because subtype picker is a superset of input method
2803 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002804 mHandler.sendMessage(mCaller.obtainMessageI(
2805 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002806 }
2807 }
2808
satok42c5a162011-05-26 16:46:14 +09002809 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002811 if (!calledFromValidUser()) {
2812 return;
2813 }
satok28203512010-11-24 11:06:49 +09002814 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2815 }
2816
satok42c5a162011-05-26 16:46:14 +09002817 @Override
satok28203512010-11-24 11:06:49 +09002818 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002819 if (!calledFromValidUser()) {
2820 return;
2821 }
satok28203512010-11-24 11:06:49 +09002822 synchronized (mMethodMap) {
2823 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002824 setInputMethodWithSubtypeIdLocked(token, id,
2825 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2826 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002827 } else {
2828 setInputMethod(token, id);
2829 }
2830 }
satokab751aa2010-09-14 19:17:36 +09002831 }
2832
satok42c5a162011-05-26 16:46:14 +09002833 @Override
satokb416a712010-11-25 20:42:14 +09002834 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002835 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002836 if (!calledFromValidUser()) {
2837 return;
2838 }
satokb416a712010-11-25 20:42:14 +09002839 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002840 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2841 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002842 }
2843 }
2844
satok4fc87d62011-05-20 16:13:43 +09002845 @Override
satok735cf382010-11-11 20:40:09 +09002846 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002847 if (!calledFromValidUser()) {
2848 return false;
2849 }
satok735cf382010-11-11 20:40:09 +09002850 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002851 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002852 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002853 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002854 lastImi = mMethodMap.get(lastIme.first);
2855 } else {
2856 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002857 }
satok4fc87d62011-05-20 16:13:43 +09002858 String targetLastImiId = null;
2859 int subtypeId = NOT_A_SUBTYPE_ID;
2860 if (lastIme != null && lastImi != null) {
2861 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002862 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002863 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2864 : mCurrentSubtype.hashCode();
2865 // If the last IME is the same as the current IME and the last subtype is not
2866 // defined, there is no need to switch to the last IME.
2867 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2868 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002869 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002870 }
2871 }
2872
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002873 if (TextUtils.isEmpty(targetLastImiId)
2874 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002875 // This is a safety net. If the currentSubtype can't be added to the history
2876 // and the framework couldn't find the last ime, we will make the last ime be
2877 // the most applicable enabled keyboard subtype of the system imes.
2878 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2879 if (enabled != null) {
2880 final int N = enabled.size();
2881 final String locale = mCurrentSubtype == null
2882 ? mRes.getConfiguration().locale.toString()
2883 : mCurrentSubtype.getLocale();
2884 for (int i = 0; i < N; ++i) {
2885 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002886 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002887 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002888 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2889 InputMethodUtils.getSubtypes(imi),
2890 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002891 if (keyboardSubtype != null) {
2892 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002893 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002894 imi, keyboardSubtype.hashCode());
2895 if(keyboardSubtype.getLocale().equals(locale)) {
2896 break;
2897 }
2898 }
2899 }
2900 }
2901 }
2902 }
2903
2904 if (!TextUtils.isEmpty(targetLastImiId)) {
2905 if (DEBUG) {
2906 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2907 + ", from: " + mCurMethodId + ", " + subtypeId);
2908 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002909 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002910 return true;
2911 } else {
2912 return false;
2913 }
satok735cf382010-11-11 20:40:09 +09002914 }
2915 }
2916
satoke7c6998e2011-06-03 17:57:59 +09002917 @Override
satok688bd472012-02-09 20:09:17 +09002918 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002919 if (!calledFromValidUser()) {
2920 return false;
2921 }
satok688bd472012-02-09 20:09:17 +09002922 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002923 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002924 return false;
2925 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002926 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002927 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2928 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002929 if (nextSubtype == null) {
2930 return false;
2931 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002932 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2933 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002934 return true;
2935 }
2936 }
2937
2938 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002939 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2940 if (!calledFromValidUser()) {
2941 return false;
2942 }
2943 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002944 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002945 return false;
2946 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002947 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002948 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2949 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002950 if (nextSubtype == null) {
2951 return false;
2952 }
2953 return true;
2954 }
2955 }
2956
2957 @Override
satok68f1b782011-04-11 14:26:04 +09002958 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002959 if (!calledFromValidUser()) {
2960 return null;
2961 }
satok68f1b782011-04-11 14:26:04 +09002962 synchronized (mMethodMap) {
2963 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2964 // TODO: Handle the case of the last IME with no subtypes
2965 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2966 || TextUtils.isEmpty(lastIme.second)) return null;
2967 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2968 if (lastImi == null) return null;
2969 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002970 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002971 final int lastSubtypeId =
2972 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002973 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2974 return null;
2975 }
2976 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002977 } catch (NumberFormatException e) {
2978 return null;
2979 }
2980 }
2981 }
2982
satoke7c6998e2011-06-03 17:57:59 +09002983 @Override
satokee5e77c2011-09-02 18:50:15 +09002984 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002985 if (!calledFromValidUser()) {
2986 return;
2987 }
satok91e88122011-07-18 11:11:42 +09002988 // By this IPC call, only a process which shares the same uid with the IME can add
2989 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002990 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002991 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002992 if (!mSystemReady) {
2993 return;
2994 }
satok91e88122011-07-18 11:11:42 +09002995 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002996 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002997 final String[] packageInfos;
2998 try {
2999 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3000 } catch (RemoteException e) {
3001 Slog.e(TAG, "Failed to get package infos");
3002 return;
3003 }
satok91e88122011-07-18 11:11:42 +09003004 if (packageInfos != null) {
3005 final int packageNum = packageInfos.length;
3006 for (int i = 0; i < packageNum; ++i) {
3007 if (packageInfos[i].equals(imi.getPackageName())) {
3008 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003009 final long ident = Binder.clearCallingIdentity();
3010 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003011 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003012 } finally {
3013 Binder.restoreCallingIdentity(ident);
3014 }
satokee5e77c2011-09-02 18:50:15 +09003015 return;
satok91e88122011-07-18 11:11:42 +09003016 }
3017 }
3018 }
satoke7c6998e2011-06-03 17:57:59 +09003019 }
satokee5e77c2011-09-02 18:50:15 +09003020 return;
satoke7c6998e2011-06-03 17:57:59 +09003021 }
3022
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003023 @Override
3024 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003025 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003026 }
3027
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003028 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003029 public void clearLastInputMethodWindowForTransition(IBinder token) {
3030 if (!calledFromValidUser()) {
3031 return;
3032 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003033 synchronized (mMethodMap) {
3034 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003035 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003036 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003037 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003038 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003039 }
3040
3041 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003042 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003043 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003044 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003045 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003046 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003047 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3048 if (DEBUG) {
3049 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3050 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3051 + " actual: " + sequenceNumber);
3052 }
3053 return;
3054 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003055 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3056 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003057 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003058 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003059 }
3060 }
3061
satok28203512010-11-24 11:06:49 +09003062 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003064 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3065 }
3066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003068 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3069 if (token == null) {
3070 if (mContext.checkCallingOrSelfPermission(
3071 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3072 != PackageManager.PERMISSION_GRANTED) {
3073 throw new SecurityException(
3074 "Using null token requires permission "
3075 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003077 } else if (mCurToken != token) {
3078 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3079 + " token: " + token);
3080 return;
3081 }
3082
3083 final long ident = Binder.clearCallingIdentity();
3084 try {
3085 setInputMethodLocked(id, subtypeId);
3086 } finally {
3087 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 }
3089 }
3090
satok42c5a162011-05-26 16:46:14 +09003091 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003093 if (!calledFromValidUser()) {
3094 return;
3095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003097 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 return;
3099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 long ident = Binder.clearCallingIdentity();
3101 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003102 hideCurrentInputLocked(flags, null);
3103 } finally {
3104 Binder.restoreCallingIdentity(ident);
3105 }
3106 }
3107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003108
satok42c5a162011-05-26 16:46:14 +09003109 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003110 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003111 if (!calledFromValidUser()) {
3112 return;
3113 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003114 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003115 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003116 return;
3117 }
3118 long ident = Binder.clearCallingIdentity();
3119 try {
3120 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 } finally {
3122 Binder.restoreCallingIdentity(ident);
3123 }
3124 }
3125 }
3126
3127 void setEnabledSessionInMainThread(SessionState session) {
3128 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003129 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003131 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003132 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 } catch (RemoteException e) {
3134 }
3135 }
3136 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003137 if (mEnabledSession != null && mEnabledSession.session != null) {
3138 try {
3139 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3140 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3141 } catch (RemoteException e) {
3142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 }
3144 }
3145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003146
satok42c5a162011-05-26 16:46:14 +09003147 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003149 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003151 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003152 final boolean showAuxSubtypes;
3153 switch (msg.arg1) {
3154 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3155 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3156 // implemented so that auxiliary subtypes will be excluded when the soft
3157 // keyboard is invisible.
3158 showAuxSubtypes = mInputShown;
3159 break;
3160 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3161 showAuxSubtypes = true;
3162 break;
3163 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3164 showAuxSubtypes = false;
3165 break;
3166 default:
3167 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3168 return false;
3169 }
3170 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003171 return true;
3172
satok47a44912010-10-06 16:03:58 +09003173 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003174 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003175 return true;
3176
3177 case MSG_SHOW_IM_CONFIG:
3178 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003179 return true;
3180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 case MSG_UNBIND_INPUT:
3184 try {
3185 ((IInputMethod)msg.obj).unbindInput();
3186 } catch (RemoteException e) {
3187 // There is nothing interesting about the method dying.
3188 }
3189 return true;
3190 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003191 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 try {
3193 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3194 } catch (RemoteException e) {
3195 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003196 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 return true;
3198 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003199 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003201 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003202 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003203 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 } catch (RemoteException e) {
3205 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003206 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 return true;
3208 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003209 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003211 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003212 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003213 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 } catch (RemoteException e) {
3215 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003216 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003218 case MSG_HIDE_CURRENT_INPUT_METHOD:
3219 synchronized (mMethodMap) {
3220 hideCurrentInputLocked(0, null);
3221 }
3222 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003224 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003226 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3228 } catch (RemoteException e) {
3229 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003230 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003232 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003233 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003234 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003235 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003237 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003239 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003240 // Dispose the channel if the input method is not local to this process
3241 // because the remote proxy will get its own copy when unparceled.
3242 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003243 channel.dispose();
3244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003246 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003250
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003251 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003252 final int missingMethods = msg.arg1;
3253 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003254 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003255 final IBinder startInputToken = (IBinder) args.arg1;
3256 final SessionState session = (SessionState) args.arg2;
3257 final IInputContext inputContext = (IInputContext) args.arg3;
3258 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003261 session.method.startInput(startInputToken, inputContext, missingMethods,
3262 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 } catch (RemoteException e) {
3264 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003265 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003270
Yohei Yukawa33e81792015-11-17 21:14:42 -08003271 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003273 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 } catch (RemoteException e) {
3275 // There is nothing interesting about the last client dying.
3276 }
3277 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003278 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003279 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003280 IInputMethodClient client = (IInputMethodClient)args.arg1;
3281 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003283 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003285 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003286 } finally {
3287 // Dispose the channel if the input method is not local to this process
3288 // because the remote proxy will get its own copy when unparceled.
3289 if (res.channel != null && Binder.isProxy(client)) {
3290 res.channel.dispose();
3291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003293 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003295 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003296 case MSG_SET_ACTIVE:
3297 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003298 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003299 } catch (RemoteException e) {
3300 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3301 + ((ClientState)msg.obj).pid + " uid "
3302 + ((ClientState)msg.obj).uid);
3303 }
3304 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003305 case MSG_SET_INTERACTIVE:
3306 handleSetInteractive(msg.arg1 != 0);
3307 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003308 case MSG_SWITCH_IME:
3309 handleSwitchInputMethod(msg.arg1 != 0);
3310 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003311 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3312 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003313 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003314 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003315 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003316 } catch (RemoteException e) {
3317 Slog.w(TAG, "Got RemoteException sending "
3318 + "setUserActionNotificationSequenceNumber("
3319 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003320 + clientState.pid + " uid "
3321 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003322 }
3323 return true;
3324 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003325 case MSG_REPORT_FULLSCREEN_MODE: {
3326 final boolean fullscreen = msg.arg1 != 0;
3327 final ClientState clientState = (ClientState)msg.obj;
3328 try {
3329 clientState.client.reportFullscreenMode(fullscreen);
3330 } catch (RemoteException e) {
3331 Slog.w(TAG, "Got RemoteException sending "
3332 + "reportFullscreen(" + fullscreen + ") notification to pid="
3333 + clientState.pid + " uid=" + clientState.uid);
3334 }
3335 return true;
3336 }
satok01038492012-04-09 21:08:27 +09003337
3338 // --------------------------------------------------------------
3339 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003340 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003341 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003342 case MSG_SYSTEM_UNLOCK_USER:
3343 final int userId = msg.arg1;
3344 onUnlockUser(userId);
3345 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
3347 return false;
3348 }
3349
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003350 private void handleSetInteractive(final boolean interactive) {
3351 synchronized (mMethodMap) {
3352 mIsInteractive = interactive;
3353 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3354
3355 // Inform the current client of the change in active status
3356 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003357 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3358 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3359 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003360 }
3361 }
3362 }
3363
Yohei Yukawaae61f712015-12-09 13:00:10 -08003364 private void handleSwitchInputMethod(final boolean forwardDirection) {
3365 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003366 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003367 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003368 if (nextSubtype == null) {
3369 return;
3370 }
3371 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003372 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3373 if (newInputMethodInfo == null) {
3374 return;
3375 }
3376 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3377 newInputMethodInfo, mCurrentSubtype);
3378 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003379 if (mSubtypeSwitchedByShortCutToast == null) {
3380 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3381 Toast.LENGTH_SHORT);
3382 } else {
3383 mSubtypeSwitchedByShortCutToast.setText(toastText);
3384 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003385 mSubtypeSwitchedByShortCutToast.show();
3386 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003387 }
3388 }
3389
satokdc9ddae2011-10-06 12:22:36 +09003390 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003391 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3392 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003393 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003394 if (DEBUG) {
3395 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3396 }
satok723a27e2010-11-11 14:58:11 +09003397 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003398 return true;
3399 }
3400
3401 return false;
3402 }
3403
Yohei Yukawa94e33302016-02-12 19:37:03 -08003404 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003405 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003406 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003407 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003408 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003409 if (!mSystemReady) {
3410 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3411 return;
3412 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003413 mMethodList.clear();
3414 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003415 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003416 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003417
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003418 // Use for queryIntentServicesAsUser
3419 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003421 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3422 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3423 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003424 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003426 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3427 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003428
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003429 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003430 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 for (int i = 0; i < services.size(); ++i) {
3432 ResolveInfo ri = services.get(i);
3433 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003434 final String imeId = InputMethodInfo.computeId(ri);
3435 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3436 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 + ": it does not require the permission "
3438 + android.Manifest.permission.BIND_INPUT_METHOD);
3439 continue;
3440 }
3441
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003442 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003444 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 try {
satoke7c6998e2011-06-03 17:57:59 +09003446 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003447 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003448 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003449 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450
3451 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003452 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003454 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003455 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 }
3457 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003458
Yohei Yukawac4e44912017-02-09 19:30:22 -08003459 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3460 // negatives when the package state remains to be the same but only the component state is
3461 // changed.
3462 {
3463 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3464 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3465 // conservative, but it seems we cannot use it for now (Issue 35176630).
3466 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3467 new Intent(InputMethod.SERVICE_INTERFACE),
3468 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3469 final int N = allInputMethodServices.size();
3470 for (int i = 0; i < N; ++i) {
3471 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003472 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3473 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003474 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003475 }
3476 }
3477
Yohei Yukawa859df052016-02-17 07:56:46 -08003478 // TODO: The following code should find better place to live.
3479 if (!resetDefaultEnabledIme) {
3480 boolean enabledImeFound = false;
3481 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3482 final int N = enabledImes.size();
3483 for (int i = 0; i < N; ++i) {
3484 final InputMethodInfo imi = enabledImes.get(i);
3485 if (mMethodList.contains(imi)) {
3486 enabledImeFound = true;
3487 break;
3488 }
3489 }
3490 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003491 if (DEBUG) {
3492 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3493 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003494 resetDefaultEnabledIme = true;
3495 resetSelectedInputMethodAndSubtypeLocked("");
3496 }
3497 }
3498
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003499 if (resetDefaultEnabledIme) {
3500 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003501 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003502 final int N = defaultEnabledIme.size();
3503 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003504 final InputMethodInfo imi = defaultEnabledIme.get(i);
3505 if (DEBUG) {
3506 Slog.d(TAG, "--- enable ime = " + imi);
3507 }
3508 setInputMethodEnabledLocked(imi.getId(), true);
3509 }
3510 }
3511
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003512 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003513 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003514 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003515 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3516 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003517 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003518 }
3519 } else {
3520 // Double check that the default IME is certainly enabled.
3521 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003522 }
3523 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003524 // Here is not the perfect place to reset the switching controller. Ideally
3525 // mSwitchingController and mSettings should be able to share the same state.
3526 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3527 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003528 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003532
satok217f5482010-12-15 05:19:19 +09003533 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003534 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003535 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003536 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3537 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003538 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003539 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003540 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003541 final int userId;
3542 synchronized (mMethodMap) {
3543 userId = mSettings.getCurrentUserId();
3544 }
3545 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003546 }
3547
3548 private void showConfigureInputMethods() {
3549 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3550 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3551 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3552 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003553 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003554 }
3555
satok2c93efc2012-04-02 19:33:47 +09003556 private boolean isScreenLocked() {
3557 return mKeyguardManager != null
3558 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3559 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003560
Seigo Nonaka14e13912015-05-06 21:04:13 -07003561 private void showInputMethodMenu(boolean showAuxSubtypes) {
3562 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003565 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003566
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003567 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003568 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003569 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003570
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003571 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003572 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003573 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3574 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003575 if (immis == null || immis.size() == 0) {
3576 return;
3577 }
3578
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003579 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580
satok688bd472012-02-09 20:09:17 +09003581 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003582 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003583 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003584
satokc3690562012-01-10 20:14:43 +09003585 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003586 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003587 if (currentSubtype != null) {
3588 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003589 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3590 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003591 }
3592 }
3593
Ken Wakasa761eb372011-03-04 19:06:18 +09003594 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003595 mIms = new InputMethodInfo[N];
3596 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003597 int checkedItem = 0;
3598 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003599 final ImeSubtypeListItem item = imList.get(i);
3600 mIms[i] = item.mImi;
3601 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003602 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003603 int subtypeId = mSubtypeIds[i];
3604 if ((subtypeId == NOT_A_SUBTYPE_ID)
3605 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3606 || (subtypeId == lastInputMethodSubtypeId)) {
3607 checkedItem = i;
3608 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003611
3612 final Context settingsContext = new ContextThemeWrapper(context,
3613 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3614
3615 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003616 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3617 @Override
3618 public void onCancel(DialogInterface dialog) {
3619 hideInputMethodMenu();
3620 }
3621 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003622
3623 final Context dialogContext = mDialogBuilder.getContext();
3624 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3625 com.android.internal.R.styleable.DialogPreference,
3626 com.android.internal.R.attr.alertDialogStyle, 0);
3627 final Drawable dialogIcon = a.getDrawable(
3628 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3629 a.recycle();
3630
3631 mDialogBuilder.setIcon(dialogIcon);
3632
Yohei Yukawad34e1482016-02-11 08:03:52 -08003633 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003634 final View tv = inflater.inflate(
3635 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3636 mDialogBuilder.setCustomTitle(tv);
3637
3638 // Setup layout for a toggle switch of the hardware keyboard
3639 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003640 mSwitchingDialogTitleView
3641 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003642 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003643 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003644 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003645 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003646 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003647 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3648 @Override
3649 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003650 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003651 // Ensure that the input method dialog is dismissed when changing
3652 // the hardware keyboard state.
3653 hideInputMethodMenu();
3654 }
3655 });
3656
Alan Viverette505e3ab2014-11-24 15:22:11 -08003657 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003658 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3659 final OnClickListener choiceListener = new OnClickListener() {
3660 @Override
3661 public void onClick(final DialogInterface dialog, final int which) {
3662 synchronized (mMethodMap) {
3663 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3664 || mSubtypeIds.length <= which) {
3665 return;
satok01038492012-04-09 21:08:27 +09003666 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003667 final InputMethodInfo im = mIms[which];
3668 int subtypeId = mSubtypeIds[which];
3669 adapter.mCheckedItem = which;
3670 adapter.notifyDataSetChanged();
3671 hideInputMethodMenu();
3672 if (im != null) {
3673 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3674 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003675 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003676 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003678 }
3679 }
3680 };
3681 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003684 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003685 final Window w = mSwitchingDialog.getWindow();
3686 final WindowManager.LayoutParams attrs = w.getAttributes();
3687 w.setType(TYPE_INPUT_METHOD_DIALOG);
3688 // Use an alternate token for the dialog for that window manager can group the token
3689 // with other IME windows based on type vs. grouping based on whichever token happens
3690 // to get selected by the system later on.
3691 attrs.token = mSwitchingDialogToken;
3692 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3693 attrs.setTitle("Select input method");
3694 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003695 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 mSwitchingDialog.show();
3697 }
3698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003699
Ken Wakasa05dbb652011-08-22 15:22:43 +09003700 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3701 private final LayoutInflater mInflater;
3702 private final int mTextViewResourceId;
3703 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003704 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003705 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3706 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3707 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003708
Ken Wakasa05dbb652011-08-22 15:22:43 +09003709 mTextViewResourceId = textViewResourceId;
3710 mItemsList = itemsList;
3711 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003712 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003713 }
3714
3715 @Override
3716 public View getView(int position, View convertView, ViewGroup parent) {
3717 final View view = convertView != null ? convertView
3718 : mInflater.inflate(mTextViewResourceId, null);
3719 if (position < 0 || position >= mItemsList.size()) return view;
3720 final ImeSubtypeListItem item = mItemsList.get(position);
3721 final CharSequence imeName = item.mImeName;
3722 final CharSequence subtypeName = item.mSubtypeName;
3723 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3724 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3725 if (TextUtils.isEmpty(subtypeName)) {
3726 firstTextView.setText(imeName);
3727 secondTextView.setVisibility(View.GONE);
3728 } else {
3729 firstTextView.setText(subtypeName);
3730 secondTextView.setText(imeName);
3731 secondTextView.setVisibility(View.VISIBLE);
3732 }
3733 final RadioButton radioButton =
3734 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3735 radioButton.setChecked(position == mCheckedItem);
3736 return view;
3737 }
3738 }
3739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003741 synchronized (mMethodMap) {
3742 hideInputMethodMenuLocked();
3743 }
3744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003745
The Android Open Source Project10592532009-03-18 17:39:46 -07003746 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003747 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748
The Android Open Source Project10592532009-03-18 17:39:46 -07003749 if (mSwitchingDialog != null) {
3750 mSwitchingDialog.dismiss();
3751 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003753
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003754 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003755 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003756 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003760
satok42c5a162011-05-26 16:46:14 +09003761 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003763 // TODO: Make this work even for non-current users?
3764 if (!calledFromValidUser()) {
3765 return false;
3766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 synchronized (mMethodMap) {
3768 if (mContext.checkCallingOrSelfPermission(
3769 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3770 != PackageManager.PERMISSION_GRANTED) {
3771 throw new SecurityException(
3772 "Requires permission "
3773 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3774 }
Anna Galusza9b278112016-01-04 11:37:37 -08003775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 long ident = Binder.clearCallingIdentity();
3777 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003778 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 } finally {
3780 Binder.restoreCallingIdentity(ident);
3781 }
3782 }
3783 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003784
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003785 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3786 // Make sure this is a valid input method.
3787 InputMethodInfo imm = mMethodMap.get(id);
3788 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003789 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003790 }
3791
satokd87c2592010-09-29 11:52:06 +09003792 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3793 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003794
satokd87c2592010-09-29 11:52:06 +09003795 if (enabled) {
3796 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3797 if (pair.first.equals(id)) {
3798 // We are enabling this input method, but it is already enabled.
3799 // Nothing to do. The previous state was enabled.
3800 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003801 }
3802 }
satokd87c2592010-09-29 11:52:06 +09003803 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3804 // Previous state was disabled.
3805 return false;
3806 } else {
3807 StringBuilder builder = new StringBuilder();
3808 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3809 builder, enabledInputMethodsList, id)) {
3810 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003811 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003812 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3813 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3814 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003815 }
3816 // Previous state was enabled.
3817 return true;
3818 } else {
3819 // We are disabling the input method but it is already disabled.
3820 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003821 return false;
3822 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003823 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003824 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003825
satok723a27e2010-11-11 14:58:11 +09003826 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3827 boolean setSubtypeOnly) {
3828 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003829 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003830
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003831 mCurUserActionNotificationSequenceNumber =
3832 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3833 if (DEBUG) {
3834 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3835 + mCurUserActionNotificationSequenceNumber);
3836 }
3837
3838 if (mCurClient != null && mCurClient.client != null) {
3839 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3840 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003841 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003842 }
3843
satok723a27e2010-11-11 14:58:11 +09003844 // Set Subtype here
3845 if (imi == null || subtypeId < 0) {
3846 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003847 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003848 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003849 if (subtypeId < imi.getSubtypeCount()) {
3850 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3851 mSettings.putSelectedSubtype(subtype.hashCode());
3852 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003853 } else {
3854 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003855 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003856 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003857 }
satokab751aa2010-09-14 19:17:36 +09003858 }
satok723a27e2010-11-11 14:58:11 +09003859
Yohei Yukawa68645a62016-02-17 07:54:20 -08003860 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003861 // Set InputMethod here
3862 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3863 }
3864 }
3865
3866 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3867 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3868 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3869 // newDefaultIme is empty when there is no candidate for the selected IME.
3870 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3871 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3872 if (subtypeHashCode != null) {
3873 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003874 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003875 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003876 } catch (NumberFormatException e) {
3877 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3878 }
3879 }
3880 }
3881 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003882 }
3883
satok4e4569d2010-11-19 18:45:53 +09003884 // If there are no selected shortcuts, tries finding the most applicable ones.
3885 private Pair<InputMethodInfo, InputMethodSubtype>
3886 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3887 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3888 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003889 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003890 boolean foundInSystemIME = false;
3891
3892 // Search applicable subtype for each InputMethodInfo
3893 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003894 final String imiId = imi.getId();
3895 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3896 continue;
3897 }
satokcd7cd292010-11-20 15:46:23 +09003898 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003899 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003900 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003901 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003902 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003903 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003904 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003905 }
satokdf31ae62011-01-15 06:19:44 +09003906 // 2. Search by the system locale from enabledSubtypes.
3907 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003908 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003909 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003910 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003911 }
satoka86f5e42011-09-02 17:12:42 +09003912 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003913 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003914 final ArrayList<InputMethodSubtype> subtypesForSearch =
3915 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003916 ? InputMethodUtils.getSubtypes(imi)
3917 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003918 // 4. Search by the current subtype's locale from all subtypes.
3919 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003920 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003921 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003922 }
3923 // 5. Search by the system locale from all subtypes.
3924 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003925 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003926 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003927 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003928 }
satokcd7cd292010-11-20 15:46:23 +09003929 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003930 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003931 // The current input method is the most applicable IME.
3932 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003933 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003934 break;
satok7599a7f2010-12-22 13:45:23 +09003935 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003936 // The system input method is 2nd applicable IME.
3937 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003938 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003939 if ((imi.getServiceInfo().applicationInfo.flags
3940 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3941 foundInSystemIME = true;
3942 }
satok4e4569d2010-11-19 18:45:53 +09003943 }
3944 }
3945 }
3946 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003947 if (mostApplicableIMI != null) {
3948 Slog.w(TAG, "Most applicable shortcut input method was:"
3949 + mostApplicableIMI.getId());
3950 if (mostApplicableSubtype != null) {
3951 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3952 + "," + mostApplicableSubtype.getMode() + ","
3953 + mostApplicableSubtype.getLocale());
3954 }
3955 }
satok4e4569d2010-11-19 18:45:53 +09003956 }
satokcd7cd292010-11-20 15:46:23 +09003957 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003958 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003959 } else {
3960 return null;
3961 }
3962 }
3963
satokab751aa2010-09-14 19:17:36 +09003964 /**
3965 * @return Return the current subtype of this input method.
3966 */
satok42c5a162011-05-26 16:46:14 +09003967 @Override
satokab751aa2010-09-14 19:17:36 +09003968 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003969 // TODO: Make this work even for non-current users?
3970 if (!calledFromValidUser()) {
3971 return null;
3972 }
3973 synchronized (mMethodMap) {
3974 return getCurrentInputMethodSubtypeLocked();
3975 }
3976 }
3977
3978 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003979 if (mCurMethodId == null) {
3980 return null;
3981 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003982 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003983 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3984 if (imi == null || imi.getSubtypeCount() == 0) {
3985 return null;
satok4e4569d2010-11-19 18:45:53 +09003986 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003987 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003988 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3989 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003990 if (subtypeId == NOT_A_SUBTYPE_ID) {
3991 // If there are no selected subtypes, the framework will try to find
3992 // the most applicable subtype from explicitly or implicitly enabled
3993 // subtypes.
3994 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003995 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003996 // If there is only one explicitly or implicitly enabled subtype,
3997 // just returns it.
3998 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3999 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4000 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004001 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004002 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004003 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004004 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004005 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004006 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4007 true);
satok4e4569d2010-11-19 18:45:53 +09004008 }
satok3ef8b292010-11-23 06:06:29 +09004009 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004010 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004011 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004012 }
4013 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004014 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004015 }
4016
satok4e4569d2010-11-19 18:45:53 +09004017 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004018 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004019 @Override
satok4e4569d2010-11-19 18:45:53 +09004020 public List getShortcutInputMethodsAndSubtypes() {
4021 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004022 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004023 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004024 // If there are no selected shortcut subtypes, the framework will try to find
4025 // the most applicable subtype from all subtypes whose mode is
4026 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004027 Pair<InputMethodInfo, InputMethodSubtype> info =
4028 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004029 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004030 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004031 ret.add(info.first);
4032 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004033 }
satok3da92232011-01-11 22:46:30 +09004034 return ret;
satokf3db1af2010-11-23 13:34:33 +09004035 }
satokf3db1af2010-11-23 13:34:33 +09004036 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4037 ret.add(imi);
4038 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4039 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004040 }
4041 }
satokf3db1af2010-11-23 13:34:33 +09004042 return ret;
satok4e4569d2010-11-19 18:45:53 +09004043 }
4044 }
4045
satok42c5a162011-05-26 16:46:14 +09004046 @Override
satokb66d2872010-11-10 01:04:04 +09004047 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004048 // TODO: Make this work even for non-current users?
4049 if (!calledFromValidUser()) {
4050 return false;
4051 }
satokb66d2872010-11-10 01:04:04 +09004052 synchronized (mMethodMap) {
4053 if (subtype != null && mCurMethodId != null) {
4054 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004055 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004056 if (subtypeId != NOT_A_SUBTYPE_ID) {
4057 setInputMethodLocked(mCurMethodId, subtypeId);
4058 return true;
4059 }
4060 }
4061 return false;
4062 }
4063 }
4064
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004065 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004066 private static class InputMethodFileManager {
4067 private static final String SYSTEM_PATH = "system";
4068 private static final String INPUT_METHOD_PATH = "inputmethod";
4069 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4070 private static final String NODE_SUBTYPES = "subtypes";
4071 private static final String NODE_SUBTYPE = "subtype";
4072 private static final String NODE_IMI = "imi";
4073 private static final String ATTR_ID = "id";
4074 private static final String ATTR_LABEL = "label";
4075 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004076 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004077 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004078 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004079 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4080 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4081 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004082 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004083 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4084 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004085 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004086 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004087 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004088 if (methodMap == null) {
4089 throw new NullPointerException("methodMap is null");
4090 }
4091 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004092 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004093 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4094 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004095 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004096 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004097 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4098 }
4099 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4100 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4101 if (!subtypeFile.exists()) {
4102 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004103 writeAdditionalInputMethodSubtypes(
4104 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004105 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004106 readAdditionalInputMethodSubtypes(
4107 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004108 }
4109 }
4110
4111 private void deleteAllInputMethodSubtypes(String imiId) {
4112 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004113 mAdditionalSubtypesMap.remove(imiId);
4114 writeAdditionalInputMethodSubtypes(
4115 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004116 }
4117 }
4118
4119 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004120 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004121 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004122 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004123 final int N = additionalSubtypes.length;
4124 for (int i = 0; i < N; ++i) {
4125 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004126 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004127 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004128 } else {
4129 Slog.w(TAG, "Duplicated subtype definition found: "
4130 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004131 }
4132 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004133 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4134 writeAdditionalInputMethodSubtypes(
4135 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004136 }
4137 }
4138
4139 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4140 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004141 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004142 }
4143 }
4144
4145 private static void writeAdditionalInputMethodSubtypes(
4146 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4147 HashMap<String, InputMethodInfo> methodMap) {
4148 // Safety net for the case that this function is called before methodMap is set.
4149 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4150 FileOutputStream fos = null;
4151 try {
4152 fos = subtypesFile.startWrite();
4153 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004154 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004155 out.startDocument(null, true);
4156 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4157 out.startTag(null, NODE_SUBTYPES);
4158 for (String imiId : allSubtypes.keySet()) {
4159 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4160 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4161 continue;
4162 }
4163 out.startTag(null, NODE_IMI);
4164 out.attribute(null, ATTR_ID, imiId);
4165 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4166 final int N = subtypesList.size();
4167 for (int i = 0; i < N; ++i) {
4168 final InputMethodSubtype subtype = subtypesList.get(i);
4169 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004170 if (subtype.hasSubtypeId()) {
4171 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4172 String.valueOf(subtype.getSubtypeId()));
4173 }
satoke7c6998e2011-06-03 17:57:59 +09004174 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4175 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4176 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004177 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4178 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004179 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4180 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4181 out.attribute(null, ATTR_IS_AUXILIARY,
4182 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004183 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4184 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004185 out.endTag(null, NODE_SUBTYPE);
4186 }
4187 out.endTag(null, NODE_IMI);
4188 }
4189 out.endTag(null, NODE_SUBTYPES);
4190 out.endDocument();
4191 subtypesFile.finishWrite(fos);
4192 } catch (java.io.IOException e) {
4193 Slog.w(TAG, "Error writing subtypes", e);
4194 if (fos != null) {
4195 subtypesFile.failWrite(fos);
4196 }
4197 }
4198 }
4199
4200 private static void readAdditionalInputMethodSubtypes(
4201 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4202 if (allSubtypes == null || subtypesFile == null) return;
4203 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004204 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004205 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004206 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004207 int type = parser.getEventType();
4208 // Skip parsing until START_TAG
4209 while ((type = parser.next()) != XmlPullParser.START_TAG
4210 && type != XmlPullParser.END_DOCUMENT) {}
4211 String firstNodeName = parser.getName();
4212 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4213 throw new XmlPullParserException("Xml doesn't start with subtypes");
4214 }
4215 final int depth =parser.getDepth();
4216 String currentImiId = null;
4217 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4218 while (((type = parser.next()) != XmlPullParser.END_TAG
4219 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4220 if (type != XmlPullParser.START_TAG)
4221 continue;
4222 final String nodeName = parser.getName();
4223 if (NODE_IMI.equals(nodeName)) {
4224 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4225 if (TextUtils.isEmpty(currentImiId)) {
4226 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4227 continue;
4228 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004229 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004230 allSubtypes.put(currentImiId, tempSubtypesArray);
4231 } else if (NODE_SUBTYPE.equals(nodeName)) {
4232 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4233 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4234 continue;
4235 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004236 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004237 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004238 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004239 parser.getAttributeValue(null, ATTR_LABEL));
4240 final String imeSubtypeLocale =
4241 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004242 final String languageTag =
4243 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004244 final String imeSubtypeMode =
4245 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4246 final String imeSubtypeExtraValue =
4247 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004248 final boolean isAuxiliary = "1".equals(String.valueOf(
4249 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004250 final boolean isAsciiCapable = "1".equals(String.valueOf(
4251 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004252 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004253 .setSubtypeNameResId(label)
4254 .setSubtypeIconResId(icon)
4255 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004256 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004257 .setSubtypeMode(imeSubtypeMode)
4258 .setSubtypeExtraValue(imeSubtypeExtraValue)
4259 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004260 .setIsAsciiCapable(isAsciiCapable);
4261 final String subtypeIdString =
4262 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4263 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004264 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004265 }
4266 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004267 }
4268 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004269 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4270 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004271 return;
satoke7c6998e2011-06-03 17:57:59 +09004272 }
4273 }
4274 }
4275
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004276 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4277 @NonNull
4278 private final Handler mHandler;
4279
4280 LocalServiceImpl(@NonNull final Handler handler) {
4281 mHandler = handler;
4282 }
4283
4284 @Override
4285 public void setInteractive(boolean interactive) {
4286 // Do everything in handler so as not to block the caller.
4287 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4288 interactive ? 1 : 0, 0));
4289 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004290
4291 @Override
4292 public void switchInputMethod(boolean forwardDirection) {
4293 // Do everything in handler so as not to block the caller.
4294 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4295 forwardDirection ? 1 : 0, 0));
4296 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004297
4298 @Override
4299 public void hideCurrentInputMethod() {
4300 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4301 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4302 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004303 }
4304
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004305 private static String imeWindowStatusToString(final int imeWindowVis) {
4306 final StringBuilder sb = new StringBuilder();
4307 boolean first = true;
4308 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4309 sb.append("Active");
4310 first = false;
4311 }
4312 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4313 if (!first) {
4314 sb.append("|");
4315 }
4316 sb.append("Visible");
4317 }
4318 return sb.toString();
4319 }
4320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004322 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4323 @Nullable Uri contentUri, @Nullable String packageName) {
4324 if (!calledFromValidUser()) {
4325 return null;
4326 }
4327
4328 if (token == null) {
4329 throw new NullPointerException("token");
4330 }
4331 if (packageName == null) {
4332 throw new NullPointerException("packageName");
4333 }
4334 if (contentUri == null) {
4335 throw new NullPointerException("contentUri");
4336 }
4337 final String contentUriScheme = contentUri.getScheme();
4338 if (!"content".equals(contentUriScheme)) {
4339 throw new InvalidParameterException("contentUri must have content scheme");
4340 }
4341
4342 synchronized (mMethodMap) {
4343 final int uid = Binder.getCallingUid();
4344 if (mCurMethodId == null) {
4345 return null;
4346 }
4347 if (mCurToken != token) {
4348 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4349 + " token=" + token);
4350 return null;
4351 }
4352 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4353 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4354 // nature of our system. Let's compare it with our internal record.
4355 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4356 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4357 + mCurAttribute.packageName + " packageName=" + packageName);
4358 return null;
4359 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004360 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004361 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004362 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004363 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004364 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4365 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4366 imeUserId);
4367 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4368 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4369 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4370 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4371 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4372 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4373 // actually allowed to "uid", which is guaranteed to be the IME's one.
4374 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4375 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004376 }
4377 }
4378
4379 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004380 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4381 if (!calledFromValidUser()) {
4382 return;
4383 }
4384 synchronized (mMethodMap) {
4385 if (!calledWithValidToken(token)) {
4386 return;
4387 }
4388 if (mCurClient != null && mCurClient.client != null) {
4389 mInFullscreenMode = fullscreen;
4390 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4391 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4392 }
4393 }
4394 }
4395
4396 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004398 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399
4400 IInputMethod method;
4401 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004402 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 synchronized (mMethodMap) {
4407 p.println("Current Input Method Manager state:");
4408 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004409 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 for (int i=0; i<N; i++) {
4411 InputMethodInfo info = mMethodList.get(i);
4412 p.println(" InputMethod #" + i + ":");
4413 info.dump(p, " ");
4414 }
4415 p.println(" Clients:");
4416 for (ClientState ci : mClients.values()) {
4417 p.println(" Client " + ci + ":");
4418 p.println(" client=" + ci.client);
4419 p.println(" inputContext=" + ci.inputContext);
4420 p.println(" sessionRequested=" + ci.sessionRequested);
4421 p.println(" curSession=" + ci.curSession);
4422 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004423 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004425 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004426 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4427 + " softInputMode=" +
4428 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4429 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004430 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4432 + " mBoundToMethod=" + mBoundToMethod);
4433 p.println(" mCurToken=" + mCurToken);
4434 p.println(" mCurIntent=" + mCurIntent);
4435 method = mCurMethod;
4436 p.println(" mCurMethod=" + mCurMethod);
4437 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004438 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 p.println(" mShowRequested=" + mShowRequested
4440 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4441 + " mShowForced=" + mShowForced
4442 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004443 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004444 p.println(" mCurUserActionNotificationSequenceNumber="
4445 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004446 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004447 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004448 p.println(" mSwitchingController:");
4449 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004450 p.println(" mSettings:");
4451 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004452
4453 p.println(" mStartInputHistory:");
4454 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004456
Jeff Brownb88102f2010-09-08 11:49:43 -07004457 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 pw.flush();
4460 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004461 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4462 } catch (IOException | RemoteException e) {
4463 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004465 } else {
4466 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004468
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004469 if (focusedWindowClient != null && client != focusedWindowClient) {
4470 p.println(" ");
4471 p.println("Warning: Current input method client doesn't match the last focused. "
4472 + "window.");
4473 p.println("Dumping input method client in the last focused window just in case.");
4474 p.println(" ");
4475 pw.flush();
4476 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004477 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4478 } catch (IOException | RemoteException e) {
4479 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004480 }
4481 }
4482
Jeff Brownb88102f2010-09-08 11:49:43 -07004483 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 pw.flush();
4486 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004487 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4488 } catch (IOException | RemoteException e) {
4489 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004491 } else {
4492 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 }
4494 }
4495}