blob: fc57a0d584002f7e3f032e3ba8ba9e46fdd91eae [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
Chris Wren282cfef2017-03-27 15:01:44 -040031import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050032import com.android.internal.notification.SystemNotificationChannels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070034import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070035import com.android.internal.os.TransferPipe;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060036import com.android.internal.util.DumpUtils;
satoke7c6998e2011-06-03 17:57:59 +090037import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import com.android.internal.view.IInputContext;
39import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.IInputMethodClient;
41import com.android.internal.view.IInputMethodManager;
42import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070043import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080045import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080046import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
satoke7c6998e2011-06-03 17:57:59 +090048import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090050import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Yohei Yukawad6475a62017-04-17 10:35:27 -070052import android.annotation.BinderThread;
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +090053import android.annotation.ColorInt;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070054import android.annotation.IntDef;
Yohei Yukawa930328c2017-10-18 20:19:53 -070055import android.annotation.MainThread;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070056import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070057import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080058import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080059import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070060import android.app.ActivityManagerInternal;
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -070061import android.app.ActivityThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070063import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090064import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090065import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090066import android.app.Notification;
67import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070068import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090069import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080071import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.ContentResolver;
73import android.content.Context;
74import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090076import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090078import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070080import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090081import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.content.pm.PackageManager;
83import android.content.pm.ResolveInfo;
84import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070085import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.content.res.Resources;
87import android.content.res.TypedArray;
88import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080089import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080090import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080091import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070092import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040094import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070095import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090096import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.Handler;
98import android.os.IBinder;
99import android.os.IInterface;
100import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -0700101import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -0700103import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800105import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.os.ServiceManager;
107import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900108import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800109import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.provider.Settings;
111import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900112import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700113import android.util.ArrayMap;
114import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700115import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900117import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900118import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.util.PrintWriterPrinter;
120import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900121import android.util.Slog;
122import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900123import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700125import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900126import android.view.LayoutInflater;
127import android.view.View;
128import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700129import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +0900131import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800133import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700134import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import android.view.inputmethod.InputMethod;
136import android.view.inputmethod.InputMethodInfo;
137import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700138import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900139import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900140import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900141import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900142import android.widget.CompoundButton;
143import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900144import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900145import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900146import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700147import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Adrian Roose99bc052017-11-20 17:55:31 +0100149import com.android.server.wm.WindowManagerInternal;
150
satoke7c6998e2011-06-03 17:57:59 +0900151import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900153import java.io.FileInputStream;
154import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155import java.io.IOException;
156import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700157import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100158import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700159import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800160import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900162import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800163import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164import java.util.HashMap;
165import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800166import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800167import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800168import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
170/**
171 * This class provides a system service that manages input methods.
172 */
173public class InputMethodManagerService extends IInputMethodManager.Stub
174 implements ServiceConnection, Handler.Callback {
175 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700176 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700177 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700179 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
180 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
181 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 static final int MSG_UNBIND_INPUT = 1000;
184 static final int MSG_BIND_INPUT = 1010;
185 static final int MSG_SHOW_SOFT_INPUT = 1020;
186 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700187 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 static final int MSG_ATTACH_TOKEN = 1040;
189 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
Yohei Yukawa33e81792015-11-17 21:14:42 -0800193 static final int MSG_UNBIND_CLIENT = 3000;
194 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700195 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700196 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900197 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800198 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800199 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
satok01038492012-04-09 21:08:27 +0900201 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
202
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700203 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
204
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700205 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800206
satokf9f01002011-05-19 21:31:50 +0900207 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
208
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900209 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900210 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900211
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700212 /**
213 * Binding flags for establishing connection to the {@link InputMethodService}.
214 */
215 private static final int IME_CONNECTION_BIND_FLAGS =
216 Context.BIND_AUTO_CREATE
217 | Context.BIND_NOT_VISIBLE
218 | Context.BIND_NOT_FOREGROUND
Yohei Yukawaad78a612017-08-04 01:57:27 -0700219 | Context.BIND_IMPORTANT_BACKGROUND;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700220
221 /**
222 * Binding flags used only while the {@link InputMethodService} is showing window.
223 */
224 private static final int IME_VISIBLE_BIND_FLAGS =
225 Context.BIND_AUTO_CREATE
226 | Context.BIND_TREAT_LIKE_ACTIVITY
Yohei Yukawaad78a612017-08-04 01:57:27 -0700227 | Context.BIND_FOREGROUND_SERVICE
228 | Context.BIND_SHOWING_UI;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700229
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700230 @Retention(SOURCE)
231 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
232 private @interface HardKeyboardBehavior {
233 int WIRELESS_AFFORDANCE = 0;
234 int WIRED_AFFORDANCE = 1;
235 }
satok4e4569d2010-11-19 18:45:53 +0900236
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900237 /**
238 * A protected broadcast intent action for internal use for {@link PendingIntent} in
239 * the notification.
240 */
241 private static final String ACTION_SHOW_INPUT_METHOD_PICKER =
242 "com.android.server.InputMethodManagerService.SHOW_INPUT_METHOD_PICKER";
243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800245 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900247 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700250 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700252 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900253 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900254 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900255 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800256 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900258 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 // All known input methods. mMethodMap also serves as the global
261 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700262 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
263 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900264 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700265 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900266 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
Yohei Yukawae0733062017-02-09 22:49:35 -0800268 /**
269 * Tracks how many times {@link #mMethodMap} was updated.
270 */
271 @GuardedBy("mMethodMap")
272 private int mMethodMapUpdateCount = 0;
273
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700274 // Used to bring IME service up to visible adjustment while it is being shown.
275 final ServiceConnection mVisibleConnection = new ServiceConnection() {
276 @Override public void onServiceConnected(ComponentName name, IBinder service) {
277 }
278
279 @Override public void onServiceDisconnected(ComponentName name) {
280 }
281 };
282 boolean mVisibleBound = false;
283
satok7cfc0ed2011-06-20 21:29:36 +0900284 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700285 private NotificationManager mNotificationManager;
286 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700287 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400288 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700289 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900290 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900291 private boolean mNotificationShown;
292
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900293 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 final ClientState client;
295 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700296
297 IInputMethodSession session;
298 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 @Override
301 public String toString() {
302 return "SessionState{uid " + client.uid + " pid " + client.pid
303 + " method " + Integer.toHexString(
304 System.identityHashCode(method))
305 + " session " + Integer.toHexString(
306 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700307 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 + "}";
309 }
310
311 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700312 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 client = _client;
314 method = _method;
315 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700316 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
Jeff Brownc28867a2013-03-26 15:42:39 -0700320 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 final IInputMethodClient client;
322 final IInputContext inputContext;
323 final int uid;
324 final int pid;
325 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 boolean sessionRequested;
328 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 @Override
331 public String toString() {
332 return "ClientState{" + Integer.toHexString(
333 System.identityHashCode(this)) + " uid " + uid
334 + " pid " + pid + "}";
335 }
336
337 ClientState(IInputMethodClient _client, IInputContext _inputContext,
338 int _uid, int _pid) {
339 client = _client;
340 inputContext = _inputContext;
341 uid = _uid;
342 pid = _pid;
343 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
344 }
345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700347 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700350 * Set once the system is ready to run third party code.
351 */
352 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700354 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700355 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
356 * method. This is to be synchronized with the secure settings keyed with
357 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
358 *
359 * <p>This can be transiently {@code null} when the system is re-initializing input method
360 * settings, e.g., the system locale is just changed.</p>
361 *
362 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
363 * {@link InputMethodManagerService}.</p>
364 *
365 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700367 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
371 * The current binding sequence number, incremented every time there is
372 * a new bind performed.
373 */
374 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * The client that is currently bound to an input method.
378 */
379 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800382 * The last window token that we confirmed to be focused. This is always updated upon reports
383 * from the input method client. If the window state is already changed before the report is
384 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700385 */
386 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700388 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800389 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
390 *
391 * @see #mCurFocusedWindow
392 */
393 int mCurFocusedWindowSoftInputMode;
394
395 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800396 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
397 */
398 ClientState mCurFocusedWindowClient;
399
400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 * The input context last provided by the current client.
402 */
403 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700406 * The missing method flags for the input context last provided by the current client.
407 *
408 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
409 */
410 int mCurInputContextMissingMethods;
411
412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 * The attributes last provided by the current client.
414 */
415 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700418 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700420 *
421 * <p>This can be {@code null} when no input method is connected.</p>
422 *
423 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700425 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 /**
satokab751aa2010-09-14 19:17:36 +0900429 * The current subtype of the current input method.
430 */
431 private InputMethodSubtype mCurrentSubtype;
432
satok4e4569d2010-11-19 18:45:53 +0900433 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900434 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700435 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900436
John Spurlocke0980502013-10-25 11:59:29 -0400437 // Was the keyguard locked when this client became current?
438 private boolean mCurClientInKeyguard;
439
satokab751aa2010-09-14 19:17:36 +0900440 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 * Set to true if our ServiceConnection is currently actively bound to
442 * a service (whether or not we have gotten its IBinder back yet).
443 */
444 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 /**
447 * Set if the client has asked for the input method to be shown.
448 */
449 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
452 * Set if we were explicitly told to show the input method.
453 */
454 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 /**
457 * Set if we were forced to be shown.
458 */
459 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 /**
462 * Set if we last told the input method to show itself.
463 */
464 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800467 * {@code true} if the current input method is in fullscreen mode.
468 */
469 boolean mInFullscreenMode;
470
471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 * The Intent used to connect to the current input method.
473 */
474 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 /**
477 * The token we have made for the currently active input method, to
478 * identify it in the future.
479 */
480 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 /**
483 * If non-null, this is the input method service we are currently connected
484 * to.
485 */
486 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 /**
489 * Time that we last initiated a bind to the input method, to determine
490 * if we should try to disconnect and reconnect to it.
491 */
492 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 /**
495 * Have we called mCurMethod.bindInput()?
496 */
497 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 /**
500 * Currently enabled session. Only touched by service thread, not
501 * protected by a lock.
502 */
503 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700506 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700508 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900510 int mCurUserActionNotificationSequenceNumber = 0;
511
Joe Onorato857fd9b2011-01-27 15:08:35 -0800512 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900513
514 /**
515 * A set of status bits regarding the active IME.
516 *
517 * <p>This value is a combination of following two bits:</p>
518 * <dl>
519 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
520 * <dd>
521 * If this bit is ON, connected IME is ready to accept touch/key events.
522 * </dd>
523 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
524 * <dd>
525 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
526 * </dd>
527 * </dl>
528 * <em>Do not update this value outside of setImeWindowStatus.</em>
529 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800530 int mImeWindowVis;
531
Ken Wakasa05dbb652011-08-22 15:22:43 +0900532 private AlertDialog.Builder mDialogBuilder;
533 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700534 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900535 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700536 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900537 private InputMethodInfo[] mIms;
538 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800539 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700540 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800541 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900542 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
543 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500544 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700545 @HardKeyboardBehavior
546 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800548 /**
549 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
550 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
551 * will not affect those tasks that are already posted.
552 *
553 * <p>Posting {@link #MSG_START_INPUT} message basically means that
554 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
555 * back in the current IME process shortly, which will also affect what the current IME starts
556 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
557 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
558 * logical input session between the client application and the current IME.</p>
559 *
560 * <p>Be careful to not keep strong references to this object forever, which can prevent
561 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
562 * </p>
563 */
564 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800565 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
566
567 final int mSequenceNumber;
568 final long mTimestamp;
569 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800570 @NonNull
571 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800572 @NonNull
573 final String mImeId;
574 // @InputMethodClient.StartInputReason
575 final int mStartInputReason;
576 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800577 @Nullable
578 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800579 @NonNull
580 final EditorInfo mEditorInfo;
581 final int mTargetWindowSoftInputMode;
582 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800583
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800584 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
585 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
586 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
587 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
588 mSequenceNumber = sSequenceNumber.getAndIncrement();
589 mTimestamp = SystemClock.uptimeMillis();
590 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800591 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800592 mImeId = imeId;
593 mStartInputReason = startInputReason;
594 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800595 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800596 mEditorInfo = editorInfo;
597 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
598 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800599 }
600 }
601
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800602 @GuardedBy("mMethodMap")
603 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800604
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800605 /**
606 * A ring buffer to store the history of {@link StartInputInfo}.
607 */
608 private static final class StartInputHistory {
609 /**
610 * Entry size for non low-RAM devices.
611 *
612 * <p>TODO: Consider to follow what other system services have been doing to manage
613 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
614 */
615 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
616
617 /**
618 * Entry size for non low-RAM devices.
619 *
620 * <p>TODO: Consider to follow what other system services have been doing to manage
621 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
622 */
623 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
624
625 private static int getEntrySize() {
626 if (ActivityManager.isLowRamDeviceStatic()) {
627 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
628 } else {
629 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
630 }
631 }
632
633 /**
634 * Backing store for the ring bugger.
635 */
636 private final Entry[] mEntries = new Entry[getEntrySize()];
637
638 /**
639 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
640 * write.
641 */
642 private int mNextIndex = 0;
643
644 /**
645 * Recyclable entry to store the information in {@link StartInputInfo}.
646 */
647 private static final class Entry {
648 int mSequenceNumber;
649 long mTimestamp;
650 long mWallTime;
651 @NonNull
652 String mImeTokenString;
653 @NonNull
654 String mImeId;
655 /* @InputMethodClient.StartInputReason */
656 int mStartInputReason;
657 boolean mRestarting;
658 @NonNull
659 String mTargetWindowString;
660 @NonNull
661 EditorInfo mEditorInfo;
662 int mTargetWindowSoftInputMode;
663 int mClientBindSequenceNumber;
664
665 Entry(@NonNull StartInputInfo original) {
666 set(original);
667 }
668
669 void set(@NonNull StartInputInfo original) {
670 mSequenceNumber = original.mSequenceNumber;
671 mTimestamp = original.mTimestamp;
672 mWallTime = original.mWallTime;
673 // Intentionally convert to String so as not to keep a strong reference to a Binder
674 // object.
675 mImeTokenString = String.valueOf(original.mImeToken);
676 mImeId = original.mImeId;
677 mStartInputReason = original.mStartInputReason;
678 mRestarting = original.mRestarting;
679 // Intentionally convert to String so as not to keep a strong reference to a Binder
680 // object.
681 mTargetWindowString = String.valueOf(original.mTargetWindow);
682 mEditorInfo = original.mEditorInfo;
683 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
684 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
685 }
686 }
687
688 /**
689 * Add a new entry and discard the oldest entry as needed.
690 * @param info {@lin StartInputInfo} to be added.
691 */
692 void addEntry(@NonNull StartInputInfo info) {
693 final int index = mNextIndex;
694 if (mEntries[index] == null) {
695 mEntries[index] = new Entry(info);
696 } else {
697 mEntries[index].set(info);
698 }
699 mNextIndex = (mNextIndex + 1) % mEntries.length;
700 }
701
702 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
703 final SimpleDateFormat dataFormat =
704 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
705
706 for (int i = 0; i < mEntries.length; ++i) {
707 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
708 if (entry == null) {
709 continue;
710 }
711 pw.print(prefix);
712 pw.println("StartInput #" + entry.mSequenceNumber + ":");
713
714 pw.print(prefix);
715 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
716 + " (timestamp=" + entry.mTimestamp + ")"
717 + " reason="
718 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
719 + " restarting=" + entry.mRestarting);
720
721 pw.print(prefix);
722 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
723
724 pw.print(prefix);
725 pw.println(" targetWin=" + entry.mTargetWindowString
726 + " [" + entry.mEditorInfo.packageName + "]"
727 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
728
729 pw.print(prefix);
730 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
731 entry.mTargetWindowSoftInputMode));
732
733 pw.print(prefix);
734 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
735 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
736 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
737 + " fieldName=" + entry.mEditorInfo.fieldName
738 + " actionId=" + entry.mEditorInfo.actionId
739 + " actionLabel=" + entry.mEditorInfo.actionLabel);
740 }
741 }
742 }
743
744 @GuardedBy("mMethodMap")
745 @NonNull
746 private final StartInputHistory mStartInputHistory = new StartInputHistory();
747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700749 int mUserId;
750 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700751 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800752 String mLastEnabled = "";
753
Yohei Yukawa81482972015-06-04 00:58:59 -0700754 /**
755 * <em>This constructor must be called within the lock.</em>
756 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 SettingsObserver(Handler handler) {
758 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700759 }
760
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800761 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700762 if (mRegistered && mUserId == userId) {
763 return;
764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700766 if (mRegistered) {
767 mContext.getContentResolver().unregisterContentObserver(this);
768 mRegistered = false;
769 }
770 if (mUserId != userId) {
771 mLastEnabled = "";
772 mUserId = userId;
773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700775 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900776 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700777 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900778 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700779 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700780 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700781 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800782 resolver.registerContentObserver(Settings.Secure.getUriFor(
783 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700784 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
Michael Wright7b5a96b2014-08-09 19:28:42 -0700787 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800788 final Uri showImeUri = Settings.Secure.getUriFor(
789 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
790 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
791 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700793 if (showImeUri.equals(uri)) {
794 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800795 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
796 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
797 mContext.getContentResolver(),
798 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
799 0, mUserId) == 1;
800 if (mAccessibilityRequestingNoSoftKeyboard) {
801 final boolean showRequested = mShowRequested;
802 hideCurrentInputLocked(0, null);
803 mShowRequested = showRequested;
804 } else if (mShowRequested) {
805 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
806 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700807 } else {
808 boolean enabledChanged = false;
809 String newEnabled = mSettings.getEnabledInputMethodsStr();
810 if (!mLastEnabled.equals(newEnabled)) {
811 mLastEnabled = newEnabled;
812 enabledChanged = true;
813 }
814 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700818
819 @Override
820 public String toString() {
821 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
822 + " mLastEnabled=" + mLastEnabled + "}";
823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825
Yohei Yukawa79247822017-01-23 15:26:15 -0800826 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900827 @Override
828 public void onReceive(Context context, Intent intent) {
829 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700830 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900831 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700832 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900833 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800834 } else if (Intent.ACTION_USER_ADDED.equals(action)
835 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100836 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800837 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700838 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
839 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
840 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
841 final String prevValue = intent.getStringExtra(
842 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
843 final String newValue = intent.getStringExtra(
844 Intent.EXTRA_SETTING_NEW_VALUE);
845 restoreEnabledInputMethods(mContext, prevValue, newValue);
846 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800847 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800848 onActionLocaleChanged();
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900849 } else if (ACTION_SHOW_INPUT_METHOD_PICKER.equals(action)) {
850 // ACTION_SHOW_INPUT_METHOD_PICKER action is a protected-broadcast and it is
851 // guaranteed to be send only from the system, so that there is no need for extra
852 // security check such as
853 // {@link #canShowInputMethodPickerLocked(IInputMethodClient)}.
854 mHandler.obtainMessage(
855 MSG_SHOW_IM_SUBTYPE_PICKER,
856 InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES,
857 0 /* arg2 */)
858 .sendToTarget();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900859 } else {
860 Slog.w(TAG, "Unexpected intent " + intent);
861 }
862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800865 /**
866 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
867 *
868 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
869 * the users. We should ignore this event if this is about any background user's locale.</p>
870 *
871 * <p>Caution: This method must not be called when system is not ready.</p>
872 */
873 void onActionLocaleChanged() {
874 synchronized (mMethodMap) {
875 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
876 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
877 return;
878 }
879 buildInputMethodListLocked(true);
880 // If the locale is changed, needs to reset the default ime
881 resetDefaultImeLocked(mContext);
882 updateFromSettingsLocked(true);
883 mLastSystemLocales = possibleNewLocale;
884 }
885 }
886
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700887 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
888 // does not attempt to validate on the fly with any installed device policy, so must only
889 // be run in the context of initial device setup.
890 //
891 // TODO: Move this method to InputMethodUtils with adding unit tests.
892 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
893 if (DEBUG_RESTORE) {
894 Slog.i(TAG, "Restoring enabled input methods:");
895 Slog.i(TAG, "prev=" + prevValue);
896 Slog.i(TAG, " new=" + newValue);
897 }
898 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900899 ArrayMap<String, ArraySet<String>> prevMap =
900 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
901 ArrayMap<String, ArraySet<String>> newMap =
902 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700903
904 // Merge the restored ime+subtype enabled states into the live state
905 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
906 final String imeId = entry.getKey();
907 ArraySet<String> prevSubtypes = prevMap.get(imeId);
908 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700909 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700910 prevMap.put(imeId, prevSubtypes);
911 }
912 prevSubtypes.addAll(entry.getValue());
913 }
914
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700915 final String mergedImesAndSubtypesString =
916 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700917 if (DEBUG_RESTORE) {
918 Slog.i(TAG, "Merged IME string:");
919 Slog.i(TAG, " " + mergedImesAndSubtypesString);
920 }
921 Settings.Secure.putString(context.getContentResolver(),
922 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
923 }
924
Yohei Yukawac4e44912017-02-09 19:30:22 -0800925 final class MyPackageMonitor extends PackageMonitor {
926 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800927 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800928 *
929 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
930 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
931 * dynamically unless the entire package is updated, which also always triggers package
932 * rescanning.</p>
933 */
934 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800935 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
936
937 /**
938 * Packages that are appeared, disappeared, or modified for whatever reason.
939 *
940 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
941 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
942 * duplicate elements for our use case.</p>
943 *
944 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
945 * which should be bound to {@link #getRegisteredHandler()}.</p>
946 */
947 private final ArrayList<String> mChangedPackages = new ArrayList<>();
948
949 /**
950 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
951 *
952 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
953 * which should be bound to {@link #getRegisteredHandler()}.</p>
954 */
955 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800956
957 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800958 void clearKnownImePackageNamesLocked() {
959 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800960 }
961
962 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800963 final void addKnownImePackageNameLocked(@NonNull String packageName) {
964 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800965 }
966
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800967 @GuardedBy("mMethodMap")
968 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900969 final int userId = getChangingUserId();
970 final boolean retval = userId == mSettings.getCurrentUserId();
971 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900972 if (!retval) {
973 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
974 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900975 }
976 return retval;
977 }
978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800980 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800982 if (!isChangingPackagesOfCurrentUserLocked()) {
983 return false;
984 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 final int N = mMethodList.size();
987 if (curInputMethodId != null) {
988 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800989 InputMethodInfo imi = mMethodList.get(i);
990 if (imi.getId().equals(curInputMethodId)) {
991 for (String pkg : packages) {
992 if (imi.getPackageName().equals(pkg)) {
993 if (!doit) {
994 return true;
995 }
satok723a27e2010-11-11 14:58:11 +0900996 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800997 chooseNewDefaultIMELocked();
998 return true;
999 }
1000 }
1001 }
1002 }
1003 }
1004 }
1005 return false;
1006 }
1007
1008 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001009 public void onBeginPackageChanges() {
1010 clearPackageChangeState();
1011 }
1012
1013 @Override
1014 public void onPackageAppeared(String packageName, int reason) {
1015 if (!mImePackageAppeared) {
1016 final PackageManager pm = mContext.getPackageManager();
1017 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
1018 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
1019 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
1020 // No need to lock this because we access it only on getRegisteredHandler().
1021 if (!services.isEmpty()) {
1022 mImePackageAppeared = true;
1023 }
1024 }
1025 // No need to lock this because we access it only on getRegisteredHandler().
1026 mChangedPackages.add(packageName);
1027 }
1028
1029 @Override
1030 public void onPackageDisappeared(String packageName, int reason) {
1031 // No need to lock this because we access it only on getRegisteredHandler().
1032 mChangedPackages.add(packageName);
1033 }
1034
1035 @Override
1036 public void onPackageModified(String packageName) {
1037 // No need to lock this because we access it only on getRegisteredHandler().
1038 mChangedPackages.add(packageName);
1039 }
1040
1041 @Override
1042 public void onPackagesSuspended(String[] packages) {
1043 // No need to lock this because we access it only on getRegisteredHandler().
1044 for (String packageName : packages) {
1045 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001046 }
1047 }
1048
1049 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001050 public void onPackagesUnsuspended(String[] packages) {
1051 // No need to lock this because we access it only on getRegisteredHandler().
1052 for (String packageName : packages) {
1053 mChangedPackages.add(packageName);
1054 }
1055 }
1056
1057 @Override
1058 public void onFinishPackageChanges() {
1059 onFinishPackageChangesInternal();
1060 clearPackageChangeState();
1061 }
1062
1063 private void clearPackageChangeState() {
1064 // No need to lock them because we access these fields only on getRegisteredHandler().
1065 mChangedPackages.clear();
1066 mImePackageAppeared = false;
1067 }
1068
1069 private boolean shouldRebuildInputMethodListLocked() {
1070 // This method is guaranteed to be called only by getRegisteredHandler().
1071
1072 // If there is any new package that contains at least one IME, then rebuilt the list
1073 // of IMEs.
1074 if (mImePackageAppeared) {
1075 return true;
1076 }
1077
1078 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1079 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1080 // is an option, but it may still do some extra operations that we do not need here.
1081 final int N = mChangedPackages.size();
1082 for (int i = 0; i < N; ++i) {
1083 final String packageName = mChangedPackages.get(i);
1084 if (mKnownImePackageNames.contains(packageName)) {
1085 return true;
1086 }
1087 }
1088 return false;
1089 }
1090
1091 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001092 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001093 if (!isChangingPackagesOfCurrentUserLocked()) {
1094 return;
1095 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001096 if (!shouldRebuildInputMethodListLocked()) {
1097 return;
1098 }
1099
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001100 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001101 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001102 final int N = mMethodList.size();
1103 if (curInputMethodId != null) {
1104 for (int i=0; i<N; i++) {
1105 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001106 final String imiId = imi.getId();
1107 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001108 curIm = imi;
1109 }
satoke7c6998e2011-06-03 17:57:59 +09001110
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001111 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001112 if (isPackageModified(imi.getPackageName())) {
1113 mFileManager.deleteAllInputMethodSubtypes(imiId);
1114 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001115 if (change == PACKAGE_TEMPORARY_CHANGE
1116 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001117 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001118 + imi.getComponent());
1119 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 }
1122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001123
Yohei Yukawa94e33302016-02-12 19:37:03 -08001124 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001127
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001128 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001129 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001130 if (change == PACKAGE_TEMPORARY_CHANGE
1131 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001132 ServiceInfo si = null;
1133 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001134 si = mIPackageManager.getServiceInfo(
1135 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1136 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001137 }
1138 if (si == null) {
1139 // Uh oh, current input method is no longer around!
1140 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001141 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001142 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001143 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001144 changed = true;
1145 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001146 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001147 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001148 }
1149 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001150 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001151 }
satokab751aa2010-09-14 19:17:36 +09001152
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001153 if (curIm == null) {
1154 // We currently don't have a default input method... is
1155 // one now available?
1156 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001157 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1158 // Even if the current input method is still available, mCurrentSubtype could
1159 // be obsolete when the package is modified in practice.
1160 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001162
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001163 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001164 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 }
1167 }
1168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001169
Jeff Brownc28867a2013-03-26 15:42:39 -07001170 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001171 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001172 private final IInputMethod mMethod;
1173 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
Jeff Brownc28867a2013-03-26 15:42:39 -07001175 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1176 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001177 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001178 mMethod = method;
1179 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001181
satoke7c6998e2011-06-03 17:57:59 +09001182 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001183 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001184 long ident = Binder.clearCallingIdentity();
1185 try {
1186 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1187 } finally {
1188 Binder.restoreCallingIdentity(ident);
1189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
satok01038492012-04-09 21:08:27 +09001193 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001194 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001195 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001196 public void onHardKeyboardStatusChange(boolean available) {
1197 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1198 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001199 }
1200
Michael Wright7b5a96b2014-08-09 19:28:42 -07001201 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001202 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001203 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001204 }
1205 synchronized(mMethodMap) {
1206 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1207 && mSwitchingDialog.isShowing()) {
1208 mSwitchingDialogTitleView.findViewById(
1209 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1210 available ? View.VISIBLE : View.GONE);
1211 }
1212 }
1213 }
1214 }
1215
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001216 public static final class Lifecycle extends SystemService {
1217 private InputMethodManagerService mService;
1218
1219 public Lifecycle(Context context) {
1220 super(context);
1221 mService = new InputMethodManagerService(context);
1222 }
1223
1224 @Override
1225 public void onStart() {
1226 LocalServices.addService(InputMethodManagerInternal.class,
1227 new LocalServiceImpl(mService.mHandler));
1228 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1229 }
1230
1231 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001232 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001233 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001234 // TODO: Dispatch this to a worker thread as needed.
1235 mService.onSwitchUser(userHandle);
1236 }
1237
1238 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001239 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001240 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001241 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001242 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1243 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1244 .getService(Context.STATUS_BAR_SERVICE);
1245 mService.systemRunning(statusBarService);
1246 }
1247 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001248
1249 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001250 public void onUnlockUser(final @UserIdInt int userHandle) {
1251 // Called on ActivityManager thread.
1252 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001253 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001254 }
1255 }
1256
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001257 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001258 synchronized(mMethodMap) {
1259 final int currentUserId = mSettings.getCurrentUserId();
1260 if (DEBUG) {
1261 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1262 }
1263 if (userId != currentUserId) {
1264 return;
1265 }
1266 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001267 if (mSystemReady) {
1268 // We need to rebuild IMEs.
1269 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1270 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1271 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001272 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001273 }
1274
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001275 void onSwitchUser(@UserIdInt int userId) {
1276 synchronized (mMethodMap) {
1277 switchUserLocked(userId);
1278 }
1279 }
1280
Seigo Nonaka7309b122015-08-17 18:34:13 -07001281 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001282 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001284 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001286 // Note: SettingsObserver doesn't register observers in its constructor.
1287 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 mIWindowManager = IWindowManager.Stub.asInterface(
1289 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001290 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001291 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001292 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 public void executeMessage(Message msg) {
1294 handleMessage(msg);
1295 }
Mita Yuned218c72012-12-06 17:18:25 -08001296 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001297 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001298 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001299 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001300 mHasFeature = context.getPackageManager().hasSystemFeature(
1301 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001302 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001303 mHardKeyboardBehavior = mContext.getResources().getInteger(
1304 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001305
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001306 Bundle extras = new Bundle();
1307 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001308 @ColorInt final int accentColor = mContext.getColor(
1309 com.android.internal.R.color.system_notification_accent_color);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001310 mImeSwitcherNotification =
1311 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1312 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1313 .setWhen(0)
1314 .setOngoing(true)
1315 .addExtras(extras)
1316 .setCategory(Notification.CATEGORY_SYSTEM)
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001317 .setColor(accentColor);
Daniel Sandler590d5152012-06-14 16:10:13 -04001318
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001319 Intent intent = new Intent(ACTION_SHOW_INPUT_METHOD_PICKER)
1320 .setPackage(mContext.getPackageName());
satok683e2382011-07-12 08:28:52 +09001321 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001322
1323 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001324
satok7cfc0ed2011-06-20 21:29:36 +09001325 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001326 int userId = 0;
1327 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001328 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001329 } catch (RemoteException e) {
1330 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1331 }
satok913a8922010-08-26 21:53:41 +09001332
satokd87c2592010-09-29 11:52:06 +09001333 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001334 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001335 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001336
Kenny Guy2a764942014-04-02 13:29:20 +01001337 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001338 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001339 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1340 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001341 }
1342
satok5b927c432012-05-01 20:09:34 +09001343 private void resetDefaultImeLocked(Context context) {
1344 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001345 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001346 return;
1347 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001348 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001349 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001350 if (suitableImes.isEmpty()) {
1351 Slog.i(TAG, "No default found");
1352 return;
satok5b927c432012-05-01 20:09:34 +09001353 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001354 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001355 if (DEBUG) {
1356 Slog.i(TAG, "Default found, using " + defIm.getId());
1357 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001358 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001359 }
1360
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001361 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001362 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1363 + " currentUserId=" + mSettings.getCurrentUserId());
1364
Yohei Yukawa81482972015-06-04 00:58:59 -07001365 // ContentObserver should be registered again when the user is changed
1366 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001367
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001368 // If the system is not ready or the device is not yed unlocked by the user, then we use
1369 // copy-on-write settings.
1370 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001371 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001372 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001373 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001374 // InputMethodFileManager should be reset when the user is changed
1375 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001376 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001377
1378 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1379 + " defaultImiId=" + defaultImiId);
1380
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001381 // For secondary users, the list of enabled IMEs may not have been updated since the
1382 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1383 // not be empty even if the IME has been uninstalled by the primary user.
1384 // Even in such cases, IMMS works fine because it will find the most applicable
1385 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001386 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001387 mLastSystemLocales = mRes.getConfiguration().getLocales();
1388
1389 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1390 if (mSystemReady) {
1391 hideCurrentInputLocked(0, null);
1392 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1393 buildInputMethodListLocked(initialUserSwitch);
1394 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1395 // This is the first time of the user switch and
1396 // set the current ime to the proper one.
1397 resetDefaultImeLocked(mContext);
1398 }
1399 updateFromSettingsLocked(true);
1400 try {
1401 startInputInnerLocked();
1402 } catch (RuntimeException e) {
1403 Slog.w(TAG, "Unexpected exception", e);
1404 }
1405 }
1406
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001407 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001408 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1409 mSettings.getEnabledInputMethodListLocked(), newUserId,
1410 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001411 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001412
1413 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1414 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001415 }
1416
Kenny Guy2a764942014-04-02 13:29:20 +01001417 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001418 mSettings.setCurrentProfileIds(
1419 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001420 }
1421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 @Override
1423 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1424 throws RemoteException {
1425 try {
1426 return super.onTransact(code, data, reply, flags);
1427 } catch (RuntimeException e) {
1428 // The input method manager only throws security exceptions, so let's
1429 // log all others.
1430 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001431 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 }
1433 throw e;
1434 }
1435 }
1436
Svetoslav Ganova0027152013-06-25 14:59:53 -07001437 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001438 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001439 if (DEBUG) {
1440 Slog.d(TAG, "--- systemReady");
1441 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001442 if (!mSystemReady) {
1443 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001444 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001445 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001446 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001447 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001448 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1449 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001450 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001451 if (mStatusBar != null) {
1452 mStatusBar.setIconVisibility(mSlotIme, false);
1453 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001454 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001455 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1456 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001457 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001458 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001459 mHardKeyboardListener);
1460 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001461
1462 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1463 mSettingsObserver.registerContentObserverLocked(currentUserId);
1464
1465 final IntentFilter broadcastFilter = new IntentFilter();
1466 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1467 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1468 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1469 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1470 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001471 broadcastFilter.addAction(ACTION_SHOW_INPUT_METHOD_PICKER);
Yohei Yukawa79247822017-01-23 15:26:15 -08001472 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1473
Yohei Yukawa1f9a3cb2017-10-26 15:00:59 -07001474 final String defaultImiId = mSettings.getSelectedInputMethod();
1475 final boolean imeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
1476 buildInputMethodListLocked(!imeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa79247822017-01-23 15:26:15 -08001477 resetDefaultImeLocked(mContext);
1478 updateFromSettingsLocked(true);
1479 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1480 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1481 mContext.getBasePackageName());
1482
Dianne Hackborncc278702009-09-02 23:07:23 -07001483 try {
1484 startInputInnerLocked();
1485 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001486 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001487 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001488 }
1489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001492 // ---------------------------------------------------------------------------------------
1493 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1494 // 1) it comes from the system process
1495 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1496 private boolean calledFromValidUser() {
1497 final int uid = Binder.getCallingUid();
1498 final int userId = UserHandle.getUserId(uid);
1499 if (DEBUG) {
1500 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1501 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1502 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001503 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1504 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 }
Kenny Guy2a764942014-04-02 13:29:20 +01001506 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001507 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001508 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001509
1510 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1511 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1512 // must not manage background users' states in any functions.
1513 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1514 // by a token.
1515 if (mContext.checkCallingOrSelfPermission(
1516 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1517 == PackageManager.PERMISSION_GRANTED) {
1518 if (DEBUG) {
1519 Slog.d(TAG, "--- Access granted because the calling process has "
1520 + "the INTERACT_ACROSS_USERS_FULL permission");
1521 }
1522 return true;
1523 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001524 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001525 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1526 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001527 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001528 }
1529
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001530
1531 /**
1532 * Returns true iff the caller is identified to be the current input method with the token.
1533 * @param token The window token given to the input method when it was started.
1534 * @return true if and only if non-null valid token is specified.
1535 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001536 private boolean calledWithValidToken(@Nullable IBinder token) {
1537 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1538 if (DEBUG) {
1539 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1540 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1541 }
1542 return false;
1543 }
1544 if (token == null || token != mCurToken) {
1545 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1546 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1547 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001548 return false;
1549 }
1550 return true;
1551 }
1552
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001553 private boolean bindCurrentInputMethodService(
1554 Intent service, ServiceConnection conn, int flags) {
1555 if (service == null || conn == null) {
1556 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1557 return false;
1558 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001559 return mContext.bindServiceAsUser(service, conn, flags,
1560 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001561 }
1562
satoke7c6998e2011-06-03 17:57:59 +09001563 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001565 // TODO: Make this work even for non-current users?
1566 if (!calledFromValidUser()) {
1567 return Collections.emptyList();
1568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001570 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
1572 }
1573
satoke7c6998e2011-06-03 17:57:59 +09001574 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001576 // TODO: Make this work even for non-current users?
1577 if (!calledFromValidUser()) {
1578 return Collections.emptyList();
1579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001581 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 }
1584
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001585 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001586 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001587 * @return enabled subtypes of the specified imi
1588 */
satoke7c6998e2011-06-03 17:57:59 +09001589 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001590 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001591 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001592 // TODO: Make this work even for non-current users?
1593 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001594 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001595 }
satok67ddf9c2010-11-17 09:45:54 +09001596 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001597 final InputMethodInfo imi;
1598 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001599 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001600 } else {
1601 imi = mMethodMap.get(imiId);
1602 }
1603 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001604 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001605 }
1606 return mSettings.getEnabledInputMethodSubtypeListLocked(
1607 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001608 }
1609 }
1610
satoke7c6998e2011-06-03 17:57:59 +09001611 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 public void addClient(IInputMethodClient client,
1613 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001614 if (!calledFromValidUser()) {
1615 return;
1616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 synchronized (mMethodMap) {
1618 mClients.put(client.asBinder(), new ClientState(client,
1619 inputContext, uid, pid));
1620 }
1621 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001622
satoke7c6998e2011-06-03 17:57:59 +09001623 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001625 if (!calledFromValidUser()) {
1626 return;
1627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001629 ClientState cs = mClients.remove(client.asBinder());
1630 if (cs != null) {
1631 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001632 if (mCurClient == cs) {
1633 mCurClient = null;
1634 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001635 if (mCurFocusedWindowClient == cs) {
1636 mCurFocusedWindowClient = null;
1637 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 void executeOrSendMessage(IInterface target, Message msg) {
1643 if (target.asBinder() instanceof Binder) {
1644 mCaller.sendMessage(msg);
1645 } else {
1646 handleMessage(msg);
1647 msg.recycle();
1648 }
1649 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001650
Yohei Yukawa33e81792015-11-17 21:14:42 -08001651 void unbindCurrentClientLocked(
1652 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001654 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 + mCurClient.client.asBinder());
1656 if (mBoundToMethod) {
1657 mBoundToMethod = false;
1658 if (mCurMethod != null) {
1659 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1660 MSG_UNBIND_INPUT, mCurMethod));
1661 }
1662 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001663
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001664 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1665 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001666 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1667 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
The Android Open Source Project10592532009-03-18 17:39:46 -07001671 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 private int getImeShowFlags() {
1676 int flags = 0;
1677 if (mShowForced) {
1678 flags |= InputMethod.SHOW_FORCED
1679 | InputMethod.SHOW_EXPLICIT;
1680 } else if (mShowExplicitlyRequested) {
1681 flags |= InputMethod.SHOW_EXPLICIT;
1682 }
1683 return flags;
1684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 private int getAppShowFlags() {
1687 int flags = 0;
1688 if (mShowForced) {
1689 flags |= InputMethodManager.SHOW_FORCED;
1690 } else if (!mShowExplicitlyRequested) {
1691 flags |= InputMethodManager.SHOW_IMPLICIT;
1692 }
1693 return flags;
1694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001696 InputBindResult attachNewInputLocked(
1697 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 if (!mBoundToMethod) {
1699 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1700 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1701 mBoundToMethod = true;
1702 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001703
1704 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001705 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1706 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1707 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001708 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001709 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001712 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001713 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001714 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001716 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001717 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001719 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001720 (session.channel != null ? session.channel.dup() : null),
1721 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001724 InputBindResult startInputLocked(
1725 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001726 IInputMethodClient client, IInputContext inputContext,
1727 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001728 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 // If no method is currently selected, do nothing.
1730 if (mCurMethodId == null) {
1731 return mNoBinding;
1732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 ClientState cs = mClients.get(client.asBinder());
1735 if (cs == null) {
1736 throw new IllegalArgumentException("unknown client "
1737 + client.asBinder());
1738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
Yohei Yukawa74750f22016-03-22 12:54:22 -07001740 if (attribute == null) {
1741 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1742 + " uid=" + cs.uid + " pid=" + cs.pid);
1743 return null;
1744 }
1745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 try {
1747 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1748 // Check with the window manager to make sure this client actually
1749 // has a window with focus. If not, reject. This is thread safe
1750 // because if the focus changes some time before or after, the
1751 // next client receiving focus that has any interest in input will
1752 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001753 if (DEBUG) {
1754 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1755 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 return null;
1758 }
1759 } catch (RemoteException e) {
1760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001762 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001763 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001764 }
1765
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001766 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001767 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001768 @NonNull EditorInfo attribute, int controlFlags,
1769 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001770 // If no method is currently selected, do nothing.
1771 if (mCurMethodId == null) {
1772 return mNoBinding;
1773 }
1774
Yohei Yukawad57ba672015-06-08 16:39:46 -07001775 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1776 attribute.packageName)) {
1777 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1778 + " uid=" + cs.uid + " package=" + attribute.packageName);
1779 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001780 }
1781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001783 // Was the keyguard locked when switching over to the new client?
1784 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 // If the client is changing, we need to switch over to the new
1786 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001787 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001788 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001789 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790
1791 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001792 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001793 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001794 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 // Bump up the sequence for this client and attach it.
1799 mCurSeq++;
1800 if (mCurSeq <= 0) mCurSeq = 1;
1801 mCurClient = cs;
1802 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001803 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 // Check if the input method is changing.
1807 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1808 if (cs.curSession != null) {
1809 // Fast case: if we are already connected to the input method,
1810 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001811 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001812 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814 if (mHaveConnection) {
1815 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 // Return to client, and we will get back with it when
1817 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001818 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001819 return new InputBindResult(null, null, mCurId, mCurSeq,
1820 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 } else if (SystemClock.uptimeMillis()
1822 < (mLastBindTime+TIME_TO_RECONNECT)) {
1823 // In this case we have connected to the service, but
1824 // don't yet have its interface. If it hasn't been too
1825 // long since we did the connection, we'll return to
1826 // the client and wait to get the service interface so
1827 // we can report back. If it has been too long, we want
1828 // to fall through so we can try a disconnect/reconnect
1829 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001830 return new InputBindResult(null, null, mCurId, mCurSeq,
1831 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1834 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
1836 }
1837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001839 return startInputInnerLocked();
1840 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001841
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001842 InputBindResult startInputInnerLocked() {
1843 if (mCurMethodId == null) {
1844 return mNoBinding;
1845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001846
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001847 if (!mSystemReady) {
1848 // If the system is not yet ready, we shouldn't be running third
1849 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001850 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1851 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001852 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1855 if (info == null) {
1856 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001859 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1862 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001863 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1864 com.android.internal.R.string.input_method_binding_label);
1865 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1866 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001867 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 mLastBindTime = SystemClock.uptimeMillis();
1869 mHaveConnection = true;
1870 mCurId = info.getId();
1871 mCurToken = new Binder();
1872 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001873 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001874 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 } catch (RemoteException e) {
1876 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001877 return new InputBindResult(null, null, mCurId, mCurSeq,
1878 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 } else {
1880 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001881 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 + mCurIntent);
1883 }
1884 return null;
1885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001886
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001887 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001888 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001889 IInputMethodClient client, IInputContext inputContext,
1890 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001891 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001892 if (!calledFromValidUser()) {
1893 return null;
1894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001896 if (DEBUG) {
1897 Slog.v(TAG, "startInput: reason="
1898 + InputMethodClient.getStartInputReason(startInputReason)
1899 + " client = " + client.asBinder()
1900 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001901 + " missingMethods="
1902 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001903 + " attribute=" + attribute
1904 + " controlFlags=#" + Integer.toHexString(controlFlags));
1905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 final long ident = Binder.clearCallingIdentity();
1907 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001908 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1909 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 } finally {
1911 Binder.restoreCallingIdentity(ident);
1912 }
1913 }
1914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001915
satoke7c6998e2011-06-03 17:57:59 +09001916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 public void finishInput(IInputMethodClient client) {
1918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001919
satoke7c6998e2011-06-03 17:57:59 +09001920 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 public void onServiceConnected(ComponentName name, IBinder service) {
1922 synchronized (mMethodMap) {
1923 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1924 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001925 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001926 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001927 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001928 return;
1929 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001930 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001931 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1932 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001934 clearClientSessionLocked(mCurClient);
1935 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937 }
1938 }
1939 }
1940
Jeff Brownc28867a2013-03-26 15:42:39 -07001941 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1942 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 synchronized (mMethodMap) {
1944 if (mCurMethod != null && method != null
1945 && mCurMethod.asBinder() == method.asBinder()) {
1946 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001947 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001949 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001950 InputBindResult res = attachNewInputLocked(
1951 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 if (res.method != null) {
1953 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001954 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001956 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958 }
1959 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001960
1961 // Session abandoned. Close its associated input channel.
1962 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001964
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001965 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001966 if (mVisibleBound) {
1967 mContext.unbindService(mVisibleConnection);
1968 mVisibleBound = false;
1969 }
1970
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001971 if (mHaveConnection) {
1972 mContext.unbindService(this);
1973 mHaveConnection = false;
1974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001975
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001976 if (mCurToken != null) {
1977 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001978 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001979 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001980 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001981 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001982 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001983 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001984 } catch (RemoteException e) {
1985 }
1986 mCurToken = null;
1987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001988
The Android Open Source Project10592532009-03-18 17:39:46 -07001989 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001990 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001991 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001992
Yohei Yukawa33e81792015-11-17 21:14:42 -08001993 void resetCurrentMethodAndClient(
1994 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001995 mCurMethodId = null;
1996 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001997 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001998 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001999
2000 void requestClientSessionLocked(ClientState cs) {
2001 if (!cs.sessionRequested) {
2002 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
2003 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
2004 cs.sessionRequested = true;
2005 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
2006 MSG_CREATE_SESSION, mCurMethod, channels[1],
2007 new MethodCallback(this, mCurMethod, channels[0])));
2008 }
2009 }
2010
2011 void clearClientSessionLocked(ClientState cs) {
2012 finishSessionLocked(cs.curSession);
2013 cs.curSession = null;
2014 cs.sessionRequested = false;
2015 }
2016
2017 private void finishSessionLocked(SessionState sessionState) {
2018 if (sessionState != null) {
2019 if (sessionState.session != null) {
2020 try {
2021 sessionState.session.finishSession();
2022 } catch (RemoteException e) {
2023 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002024 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07002025 }
2026 sessionState.session = null;
2027 }
2028 if (sessionState.channel != null) {
2029 sessionState.channel.dispose();
2030 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002031 }
2032 }
2033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002034
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002035 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 if (mCurMethod != null) {
2037 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002038 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002040
Jeff Brownc28867a2013-03-26 15:42:39 -07002041 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002042 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 mCurMethod = null;
2044 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002045 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002046 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002047 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002048 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002050
satoke7c6998e2011-06-03 17:57:59 +09002051 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002053 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2054 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2055 // which is irregular but can eventually happen for everyone just by continuing using the
2056 // device. Thus it is important to make sure that all the internal states are properly
2057 // refreshed when this method is called back. Running
2058 // adb install -r <APK that implements the current IME>
2059 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002061 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 + " mCurIntent=" + mCurIntent);
2063 if (mCurMethod != null && mCurIntent != null
2064 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002065 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 // We consider this to be a new bind attempt, since the system
2067 // should now try to restart the service for us.
2068 mLastBindTime = SystemClock.uptimeMillis();
2069 mShowRequested = mInputShown;
2070 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002071 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
2073 }
2074 }
2075
satokf9f01002011-05-19 21:31:50 +09002076 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002078 synchronized (mMethodMap) {
2079 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002080 return;
2081 }
2082 final long ident = Binder.clearCallingIdentity();
2083 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002085 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002086 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002087 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002090 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002091 CharSequence contentDescription = null;
2092 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002093 // Use PackageManager to load label
2094 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002095 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002096 mIPackageManager.getApplicationInfo(packageName, 0,
2097 mSettings.getCurrentUserId()));
2098 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002099 /* ignore */
2100 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002101 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002102 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002103 contentDescription != null
2104 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002105 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002108 } finally {
2109 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112 }
2113
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002114 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002115 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002116 if (mSwitchingDialog != null) return false;
Yohei Yukawad2bc3092017-07-31 15:37:14 -07002117 if (mWindowManagerInternal.isKeyguardShowingAndNotOccluded()
2118 && mKeyguardManager != null && mKeyguardManager.isKeyguardSecure()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002119 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002120 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002121 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2122 // When physical keyboard is attached, we show the ime switcher (or notification if
2123 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2124 // exists in the IME switcher dialog. Might be OK to remove this condition once
2125 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2126 return true;
2127 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002128 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2129 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002130 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002131
2132 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2133 final int N = imis.size();
2134 if (N > 2) return true;
2135 if (N < 1) return false;
2136 int nonAuxCount = 0;
2137 int auxCount = 0;
2138 InputMethodSubtype nonAuxSubtype = null;
2139 InputMethodSubtype auxSubtype = null;
2140 for(int i = 0; i < N; ++i) {
2141 final InputMethodInfo imi = imis.get(i);
2142 final List<InputMethodSubtype> subtypes =
2143 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2144 final int subtypeCount = subtypes.size();
2145 if (subtypeCount == 0) {
2146 ++nonAuxCount;
2147 } else {
2148 for (int j = 0; j < subtypeCount; ++j) {
2149 final InputMethodSubtype subtype = subtypes.get(j);
2150 if (!subtype.isAuxiliary()) {
2151 ++nonAuxCount;
2152 nonAuxSubtype = subtype;
2153 } else {
2154 ++auxCount;
2155 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002156 }
2157 }
satok7cfc0ed2011-06-20 21:29:36 +09002158 }
2159 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002160 if (nonAuxCount > 1 || auxCount > 1) {
2161 return true;
2162 } else if (nonAuxCount == 1 && auxCount == 1) {
2163 if (nonAuxSubtype != null && auxSubtype != null
2164 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2165 || auxSubtype.overridesImplicitlyEnabledSubtype()
2166 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2167 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2168 return false;
2169 }
2170 return true;
2171 }
2172 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002173 }
2174
John Spurlocke0980502013-10-25 11:59:29 -04002175 private boolean isKeyguardLocked() {
2176 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2177 }
2178
Yohei Yukawad6475a62017-04-17 10:35:27 -07002179 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002180 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002181 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002182 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2183 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002184 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002185 return;
2186 }
2187
Yohei Yukawa69e68022017-02-13 12:04:50 -08002188 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002189 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002190 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002191 mImeWindowVis = vis;
2192 mBackDisposition = backDisposition;
2193 updateSystemUiLocked(token, vis, backDisposition);
2194 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002195
2196 final boolean dismissImeOnBackKeyPressed;
2197 switch (backDisposition) {
2198 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2199 dismissImeOnBackKeyPressed = true;
2200 break;
2201 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2202 dismissImeOnBackKeyPressed = false;
2203 break;
2204 default:
2205 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2206 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2207 break;
2208 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002209 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2210 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002211 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002212 }
2213
2214 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2215 synchronized (mMethodMap) {
2216 updateSystemUiLocked(token, vis, backDisposition);
2217 }
2218 }
2219
2220 // Caution! This method is called in this class. Handle multi-user carefully
2221 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2222 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002223 return;
2224 }
2225
2226 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2227 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002228 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002229 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002230 // apply policy for binder calls
2231 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2232 vis = 0;
satok06487a52010-10-29 11:37:18 +09002233 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002234 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2235 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2236 if (mStatusBar != null) {
2237 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2238 needsToShowImeSwitcher);
2239 }
2240 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2241 if (imi != null && needsToShowImeSwitcher) {
2242 // Used to load label
2243 final CharSequence title = mRes.getText(
2244 com.android.internal.R.string.select_input_method);
2245 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2246 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002247 mImeSwitcherNotification.setContentTitle(title)
2248 .setContentText(summary)
2249 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002250 try {
2251 if ((mNotificationManager != null)
2252 && !mIWindowManager.hasNavigationBar()) {
2253 if (DEBUG) {
2254 Slog.d(TAG, "--- show notification: label = " + summary);
2255 }
2256 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002257 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002258 mImeSwitcherNotification.build(), UserHandle.ALL);
2259 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002260 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002261 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002262 }
2263 } else {
2264 if (mNotificationShown && mNotificationManager != null) {
2265 if (DEBUG) {
2266 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002267 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002268 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002269 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002270 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002271 }
satok06487a52010-10-29 11:37:18 +09002272 }
2273 } finally {
2274 Binder.restoreCallingIdentity(ident);
2275 }
2276 }
2277
satoke7c6998e2011-06-03 17:57:59 +09002278 @Override
satokf9f01002011-05-19 21:31:50 +09002279 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002280 if (!calledFromValidUser()) {
2281 return;
2282 }
satokf9f01002011-05-19 21:31:50 +09002283 synchronized (mMethodMap) {
2284 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2285 for (int i = 0; i < spans.length; ++i) {
2286 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002287 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002288 mSecureSuggestionSpans.put(ss, currentImi);
2289 }
2290 }
2291 }
2292 }
2293
satoke7c6998e2011-06-03 17:57:59 +09002294 @Override
satokf9f01002011-05-19 21:31:50 +09002295 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002296 if (!calledFromValidUser()) {
2297 return false;
2298 }
satokf9f01002011-05-19 21:31:50 +09002299 synchronized (mMethodMap) {
2300 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2301 // TODO: Do not send the intent if the process of the targetImi is already dead.
2302 if (targetImi != null) {
2303 final String[] suggestions = span.getSuggestions();
2304 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002305 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002306 final Intent intent = new Intent();
2307 // Ensures that only a class in the original IME package will receive the
2308 // notification.
satok42c5a162011-05-26 16:46:14 +09002309 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002310 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2311 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2312 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2313 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002314 final long ident = Binder.clearCallingIdentity();
2315 try {
2316 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2317 } finally {
2318 Binder.restoreCallingIdentity(ident);
2319 }
satokf9f01002011-05-19 21:31:50 +09002320 return true;
2321 }
2322 }
2323 return false;
2324 }
2325
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002326 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002327 updateInputMethodsFromSettingsLocked(enabledMayChange);
2328 updateKeyboardFromSettingsLocked();
2329 }
2330
2331 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002332 if (enabledMayChange) {
2333 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2334 for (int i=0; i<enabled.size(); i++) {
2335 // We allow the user to select "disabled until used" apps, so if they
2336 // are enabling one of those here we now need to make it enabled.
2337 InputMethodInfo imm = enabled.get(i);
2338 try {
2339 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2340 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2341 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002342 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002343 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002344 if (DEBUG) {
2345 Slog.d(TAG, "Update state(" + imm.getId()
2346 + "): DISABLED_UNTIL_USED -> DEFAULT");
2347 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002348 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2349 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002350 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2351 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002352 }
2353 } catch (RemoteException e) {
2354 }
2355 }
2356 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002357 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2358 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2359 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2360 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002361 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002362 // There is no input method selected, try to choose new applicable input method.
2363 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002364 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002365 }
2366 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002368 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002370 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002371 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
satokf3db1af2010-11-23 13:34:33 +09002373 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002374 } else {
2375 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002376 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002378 // Here is not the perfect place to reset the switching controller. Ideally
2379 // mSwitchingController and mSettings should be able to share the same state.
2380 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2381 // the same enabled IMEs list.
2382 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002383
2384 }
2385
2386 public void updateKeyboardFromSettingsLocked() {
2387 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2388 if (mSwitchingDialog != null
2389 && mSwitchingDialogTitleView != null
2390 && mSwitchingDialog.isShowing()) {
2391 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2392 com.android.internal.R.id.hard_keyboard_switch);
2393 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002396
Yohei Yukawab097b822015-12-01 10:43:08 -08002397 private void notifyInputMethodSubtypeChanged(final int userId,
2398 @Nullable final InputMethodInfo inputMethodInfo,
2399 @Nullable final InputMethodSubtype subtype) {
2400 final InputManagerInternal inputManagerInternal =
2401 LocalServices.getService(InputManagerInternal.class);
2402 if (inputManagerInternal != null) {
2403 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2404 }
2405 }
2406
satokab751aa2010-09-14 19:17:36 +09002407 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 InputMethodInfo info = mMethodMap.get(id);
2409 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002410 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002412
satokd81e9502012-05-21 12:58:45 +09002413 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002415 final int subtypeCount = info.getSubtypeCount();
2416 if (subtypeCount <= 0) {
2417 return;
satokcd7cd292010-11-20 15:46:23 +09002418 }
satokd81e9502012-05-21 12:58:45 +09002419 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2420 final InputMethodSubtype newSubtype;
2421 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2422 newSubtype = info.getSubtypeAt(subtypeId);
2423 } else {
2424 // If subtype is null, try to find the most applicable one from
2425 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002427 }
2428 if (newSubtype == null || oldSubtype == null) {
2429 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2430 + ", new subtype = " + newSubtype);
2431 return;
2432 }
2433 if (newSubtype != oldSubtype) {
2434 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2435 if (mCurMethod != null) {
2436 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002437 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002438 mCurMethod.changeInputMethodSubtype(newSubtype);
2439 } catch (RemoteException e) {
2440 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002441 return;
satokab751aa2010-09-14 19:17:36 +09002442 }
2443 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002444 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 return;
2447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002448
satokd81e9502012-05-21 12:58:45 +09002449 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 final long ident = Binder.clearCallingIdentity();
2451 try {
satokab751aa2010-09-14 19:17:36 +09002452 // Set a subtype to this input method.
2453 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002454 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2455 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2456 // because mCurMethodId is stored as a history in
2457 // setSelectedInputMethodAndSubtypeLocked().
2458 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002460 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002462 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002464 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002466 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 } finally {
2468 Binder.restoreCallingIdentity(ident);
2469 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002470
2471 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2472 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002474
satok42c5a162011-05-26 16:46:14 +09002475 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002476 public boolean showSoftInput(IInputMethodClient client, int flags,
2477 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002478 if (!calledFromValidUser()) {
2479 return false;
2480 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002481 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 long ident = Binder.clearCallingIdentity();
2483 try {
2484 synchronized (mMethodMap) {
2485 if (mCurClient == null || client == null
2486 || mCurClient.client.asBinder() != client.asBinder()) {
2487 try {
2488 // We need to check if this is the current client with
2489 // focus in the window manager, to allow this call to
2490 // be made before input is started in it.
2491 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002492 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002493 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 }
2495 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002496 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 }
2498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002499
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002500 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002501 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 }
2503 } finally {
2504 Binder.restoreCallingIdentity(ident);
2505 }
2506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002507
The Android Open Source Project4df24232009-03-05 14:34:35 -08002508 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002510 if (mAccessibilityRequestingNoSoftKeyboard) {
2511 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 }
Anna Galusza9b278112016-01-04 11:37:37 -08002513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2515 mShowExplicitlyRequested = true;
2516 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002517 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2518 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002520
Dianne Hackborncc278702009-09-02 23:07:23 -07002521 if (!mSystemReady) {
2522 return false;
2523 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002524
The Android Open Source Project4df24232009-03-05 14:34:35 -08002525 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002527 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002528 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2529 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2530 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002532 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002533 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002534 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002535 mVisibleBound = true;
2536 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002537 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002539 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 // The client has asked to have the input method shown, but
2541 // we have been sitting here too long with a connection to the
2542 // service and no interface received, so let's disconnect/connect
2543 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002544 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002546 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002548 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002549 } else {
2550 if (DEBUG) {
2551 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2552 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002555
The Android Open Source Project4df24232009-03-05 14:34:35 -08002556 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002558
satok42c5a162011-05-26 16:46:14 +09002559 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002560 public boolean hideSoftInput(IInputMethodClient client, int flags,
2561 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002562 if (!calledFromValidUser()) {
2563 return false;
2564 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002565 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 long ident = Binder.clearCallingIdentity();
2567 try {
2568 synchronized (mMethodMap) {
2569 if (mCurClient == null || client == null
2570 || mCurClient.client.asBinder() != client.asBinder()) {
2571 try {
2572 // We need to check if this is the current client with
2573 // focus in the window manager, to allow this call to
2574 // be made before input is started in it.
2575 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002576 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2577 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002578 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 }
2580 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002581 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
2583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002584
Joe Onorato8a9b2202010-02-26 18:56:32 -08002585 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002586 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 }
2588 } finally {
2589 Binder.restoreCallingIdentity(ident);
2590 }
2591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002592
The Android Open Source Project4df24232009-03-05 14:34:35 -08002593 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2595 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002596 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 -08002597 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
2599 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002600 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 -08002601 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002603
2604 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2605 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2606 // to be updated with the new value sent from IME process. Even in such a transient state
2607 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2608 // application process as a valid request, and have even promised such a behavior with CTS
2609 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2610 // IMMS#InputShown indicates that the software keyboard is shown.
2611 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2612 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2613 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002614 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002615 if (shouldHideSoftInput) {
2616 // The IME will report its visible state again after the following message finally
2617 // delivered to the IME process as an IPC. Hence the inconsistency between
2618 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2619 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002620 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2621 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2622 res = true;
2623 } else {
2624 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002626 if (mHaveConnection && mVisibleBound) {
2627 mContext.unbindService(mVisibleConnection);
2628 mVisibleBound = false;
2629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 mInputShown = false;
2631 mShowRequested = false;
2632 mShowExplicitlyRequested = false;
2633 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002634 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002636
satok42c5a162011-05-26 16:46:14 +09002637 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002638 public InputBindResult startInputOrWindowGainedFocus(
2639 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2640 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002641 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002642 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002643 if (windowToken != null) {
2644 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002645 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002646 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002647 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2648 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002649 }
2650 }
2651
2652 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002653 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002654 IInputMethodClient client, IBinder windowToken, int controlFlags,
2655 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002656 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2657 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002658 // Needs to check the validity before clearing calling identity
2659 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002660 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 long ident = Binder.clearCallingIdentity();
2662 try {
2663 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002664 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2665 + InputMethodClient.getStartInputReason(startInputReason)
2666 + " client=" + client.asBinder()
2667 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002668 + " missingMethods="
2669 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002670 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002671 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002672 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002673 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002674
Dianne Hackborn7663d802012-02-24 13:08:49 -08002675 ClientState cs = mClients.get(client.asBinder());
2676 if (cs == null) {
2677 throw new IllegalArgumentException("unknown client "
2678 + client.asBinder());
2679 }
2680
2681 try {
2682 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2683 // Check with the window manager to make sure this client actually
2684 // has a window with focus. If not, reject. This is thread safe
2685 // because if the focus changes some time before or after, the
2686 // next client receiving focus that has any interest in input will
2687 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002688 if (DEBUG) {
2689 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2690 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2691 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002692 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002694 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002696
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002697 if (!calledFromValidUser) {
2698 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2699 Slog.w(TAG, "If you want to interect with IME, you need "
2700 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2701 hideCurrentInputLocked(0, null);
2702 return null;
2703 }
2704
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002705 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002706 if (DEBUG) {
2707 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2708 + " attribute=" + attribute + ", token = " + windowToken);
2709 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002710 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002711 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002712 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002713 }
2714 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002715 }
2716 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002717 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002718 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002719
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002720 // Should we auto-show the IME even if the caller has not
2721 // specified what should be done with it?
2722 // We only do this automatically if the window can resize
2723 // to accommodate the IME (so what the user sees will give
2724 // them good context without input information being obscured
2725 // by the IME) or if running on a large screen where there
2726 // is more room for the target window + IME.
2727 final boolean doAutoShow =
2728 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2729 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2730 || mRes.getConfiguration().isLayoutSizeAtLeast(
2731 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002732 final boolean isTextEditor =
2733 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2734
2735 // We want to start input before showing the IME, but after closing
2736 // it. We want to do this after closing it to help the IME disappear
2737 // more quickly (not get stuck behind it initializing itself for the
2738 // new focused input, even if its window wants to hide the IME).
2739 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2742 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002743 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2745 // There is no focus view, and this window will
2746 // be behind any soft input window, so hide the
2747 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002748 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002749 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002751 } else if (isTextEditor && doAutoShow && (softInputMode &
2752 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 // There is a focus view, and we are navigating forward
2754 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002755 // We only do this automatically if the window can resize
2756 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002757 // them good context without input information being obscured
2758 // by the IME) or if running on a large screen where there
2759 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002760 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002761 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002762 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002763 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002764 didStart = true;
2765 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002766 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 }
2768 break;
2769 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2770 // Do nothing.
2771 break;
2772 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2773 if ((softInputMode &
2774 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002775 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002776 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 }
2778 break;
2779 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002780 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002781 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 break;
2783 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2784 if ((softInputMode &
2785 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002786 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002787 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002788 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002789 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002790 didStart = true;
2791 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002792 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
2794 break;
2795 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002796 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002797 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002798 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002799 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002800 didStart = true;
2801 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002802 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 break;
2804 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002805
2806 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002807 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002808 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
2811 } finally {
2812 Binder.restoreCallingIdentity(ident);
2813 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002814
2815 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002817
Guliz Tuncay6908c152017-06-02 16:06:10 -07002818 private boolean canShowInputMethodPickerLocked(IInputMethodClient client) {
2819 final int uid = Binder.getCallingUid();
2820 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
2821 return true;
2822 } else if (mCurClient != null && client != null
2823 && mCurClient.client.asBinder() == client.asBinder()) {
2824 return true;
2825 } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid(
2826 mAppOpsManager,
2827 uid,
2828 mCurIntent.getComponent().getPackageName())) {
2829 return true;
2830 } else if (mContext.checkCallingPermission(
2831 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2832 == PackageManager.PERMISSION_GRANTED) {
2833 return true;
2834 }
2835
2836 return false;
2837 }
2838
satok42c5a162011-05-26 16:46:14 +09002839 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002840 public void showInputMethodPickerFromClient(
2841 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002842 if (!calledFromValidUser()) {
2843 return;
2844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 synchronized (mMethodMap) {
Guliz Tuncay6908c152017-06-02 16:06:10 -07002846 if(!canShowInputMethodPickerLocked(client)) {
satok47a44912010-10-06 16:03:58 +09002847 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002848 + Binder.getCallingUid() + ": " + client);
Guliz Tuncay6908c152017-06-02 16:06:10 -07002849 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 }
2851
satok440aab52010-11-25 09:43:11 +09002852 // Always call subtype picker, because subtype picker is a superset of input method
2853 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002854 mHandler.sendMessage(mCaller.obtainMessageI(
2855 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002856 }
2857 }
2858
satok42c5a162011-05-26 16:46:14 +09002859 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002861 if (!calledFromValidUser()) {
2862 return;
2863 }
satok28203512010-11-24 11:06:49 +09002864 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2865 }
2866
satok42c5a162011-05-26 16:46:14 +09002867 @Override
satok28203512010-11-24 11:06:49 +09002868 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002869 if (!calledFromValidUser()) {
2870 return;
2871 }
satok28203512010-11-24 11:06:49 +09002872 synchronized (mMethodMap) {
2873 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002874 setInputMethodWithSubtypeIdLocked(token, id,
2875 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2876 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002877 } else {
2878 setInputMethod(token, id);
2879 }
2880 }
satokab751aa2010-09-14 19:17:36 +09002881 }
2882
satok42c5a162011-05-26 16:46:14 +09002883 @Override
satokb416a712010-11-25 20:42:14 +09002884 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002885 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002886 if (!calledFromValidUser()) {
2887 return;
2888 }
satokb416a712010-11-25 20:42:14 +09002889 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002890 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2891 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002892 }
2893 }
2894
satok4fc87d62011-05-20 16:13:43 +09002895 @Override
satok735cf382010-11-11 20:40:09 +09002896 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002897 if (!calledFromValidUser()) {
2898 return false;
2899 }
satok735cf382010-11-11 20:40:09 +09002900 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002901 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002902 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002903 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002904 lastImi = mMethodMap.get(lastIme.first);
2905 } else {
2906 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002907 }
satok4fc87d62011-05-20 16:13:43 +09002908 String targetLastImiId = null;
2909 int subtypeId = NOT_A_SUBTYPE_ID;
2910 if (lastIme != null && lastImi != null) {
2911 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002912 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002913 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2914 : mCurrentSubtype.hashCode();
2915 // If the last IME is the same as the current IME and the last subtype is not
2916 // defined, there is no need to switch to the last IME.
2917 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2918 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002919 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002920 }
2921 }
2922
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002923 if (TextUtils.isEmpty(targetLastImiId)
2924 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002925 // This is a safety net. If the currentSubtype can't be added to the history
2926 // and the framework couldn't find the last ime, we will make the last ime be
2927 // the most applicable enabled keyboard subtype of the system imes.
2928 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2929 if (enabled != null) {
2930 final int N = enabled.size();
2931 final String locale = mCurrentSubtype == null
2932 ? mRes.getConfiguration().locale.toString()
2933 : mCurrentSubtype.getLocale();
2934 for (int i = 0; i < N; ++i) {
2935 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002936 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002937 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002938 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2939 InputMethodUtils.getSubtypes(imi),
2940 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002941 if (keyboardSubtype != null) {
2942 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002943 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002944 imi, keyboardSubtype.hashCode());
2945 if(keyboardSubtype.getLocale().equals(locale)) {
2946 break;
2947 }
2948 }
2949 }
2950 }
2951 }
2952 }
2953
2954 if (!TextUtils.isEmpty(targetLastImiId)) {
2955 if (DEBUG) {
2956 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2957 + ", from: " + mCurMethodId + ", " + subtypeId);
2958 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002959 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002960 return true;
2961 } else {
2962 return false;
2963 }
satok735cf382010-11-11 20:40:09 +09002964 }
2965 }
2966
satoke7c6998e2011-06-03 17:57:59 +09002967 @Override
satok688bd472012-02-09 20:09:17 +09002968 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002969 if (!calledFromValidUser()) {
2970 return false;
2971 }
satok688bd472012-02-09 20:09:17 +09002972 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002973 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002974 return false;
2975 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002976 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002977 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2978 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002979 if (nextSubtype == null) {
2980 return false;
2981 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002982 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2983 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002984 return true;
2985 }
2986 }
2987
2988 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002989 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2990 if (!calledFromValidUser()) {
2991 return false;
2992 }
2993 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002994 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002995 return false;
2996 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002997 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002998 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2999 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09003000 if (nextSubtype == null) {
3001 return false;
3002 }
3003 return true;
3004 }
3005 }
3006
3007 @Override
satok68f1b782011-04-11 14:26:04 +09003008 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003009 if (!calledFromValidUser()) {
3010 return null;
3011 }
satok68f1b782011-04-11 14:26:04 +09003012 synchronized (mMethodMap) {
3013 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
3014 // TODO: Handle the case of the last IME with no subtypes
3015 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
3016 || TextUtils.isEmpty(lastIme.second)) return null;
3017 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
3018 if (lastImi == null) return null;
3019 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003020 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003021 final int lastSubtypeId =
3022 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09003023 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
3024 return null;
3025 }
3026 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09003027 } catch (NumberFormatException e) {
3028 return null;
3029 }
3030 }
3031 }
3032
satoke7c6998e2011-06-03 17:57:59 +09003033 @Override
satokee5e77c2011-09-02 18:50:15 +09003034 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003035 if (!calledFromValidUser()) {
3036 return;
3037 }
satok91e88122011-07-18 11:11:42 +09003038 // By this IPC call, only a process which shares the same uid with the IME can add
3039 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08003040 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09003041 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08003042 if (!mSystemReady) {
3043 return;
3044 }
satok91e88122011-07-18 11:11:42 +09003045 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09003046 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003047 final String[] packageInfos;
3048 try {
3049 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3050 } catch (RemoteException e) {
3051 Slog.e(TAG, "Failed to get package infos");
3052 return;
3053 }
satok91e88122011-07-18 11:11:42 +09003054 if (packageInfos != null) {
3055 final int packageNum = packageInfos.length;
3056 for (int i = 0; i < packageNum; ++i) {
3057 if (packageInfos[i].equals(imi.getPackageName())) {
3058 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003059 final long ident = Binder.clearCallingIdentity();
3060 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003061 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003062 } finally {
3063 Binder.restoreCallingIdentity(ident);
3064 }
satokee5e77c2011-09-02 18:50:15 +09003065 return;
satok91e88122011-07-18 11:11:42 +09003066 }
3067 }
3068 }
satoke7c6998e2011-06-03 17:57:59 +09003069 }
satokee5e77c2011-09-02 18:50:15 +09003070 return;
satoke7c6998e2011-06-03 17:57:59 +09003071 }
3072
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003073 @Override
3074 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003075 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003076 }
3077
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003078 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003079 public void clearLastInputMethodWindowForTransition(IBinder token) {
3080 if (!calledFromValidUser()) {
3081 return;
3082 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003083 synchronized (mMethodMap) {
3084 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003085 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003086 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003087 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003088 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003089 }
3090
3091 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003092 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003093 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003094 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003095 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003096 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003097 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3098 if (DEBUG) {
3099 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3100 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3101 + " actual: " + sequenceNumber);
3102 }
3103 return;
3104 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003105 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3106 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003107 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003108 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003109 }
3110 }
3111
satok28203512010-11-24 11:06:49 +09003112 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003114 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3115 }
3116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003118 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3119 if (token == null) {
3120 if (mContext.checkCallingOrSelfPermission(
3121 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3122 != PackageManager.PERMISSION_GRANTED) {
3123 throw new SecurityException(
3124 "Using null token requires permission "
3125 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003127 } else if (mCurToken != token) {
3128 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3129 + " token: " + token);
3130 return;
3131 }
3132
3133 final long ident = Binder.clearCallingIdentity();
3134 try {
3135 setInputMethodLocked(id, subtypeId);
3136 } finally {
3137 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 }
3139 }
3140
satok42c5a162011-05-26 16:46:14 +09003141 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003143 if (!calledFromValidUser()) {
3144 return;
3145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003147 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 return;
3149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 long ident = Binder.clearCallingIdentity();
3151 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003152 hideCurrentInputLocked(flags, null);
3153 } finally {
3154 Binder.restoreCallingIdentity(ident);
3155 }
3156 }
3157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003158
satok42c5a162011-05-26 16:46:14 +09003159 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003160 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003161 if (!calledFromValidUser()) {
3162 return;
3163 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003164 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003165 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003166 return;
3167 }
3168 long ident = Binder.clearCallingIdentity();
3169 try {
3170 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 } finally {
3172 Binder.restoreCallingIdentity(ident);
3173 }
3174 }
3175 }
3176
3177 void setEnabledSessionInMainThread(SessionState session) {
3178 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003179 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003181 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003182 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 } catch (RemoteException e) {
3184 }
3185 }
3186 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003187 if (mEnabledSession != null && mEnabledSession.session != null) {
3188 try {
3189 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3190 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3191 } catch (RemoteException e) {
3192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
3194 }
3195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003196
Yohei Yukawa930328c2017-10-18 20:19:53 -07003197 @MainThread
satok42c5a162011-05-26 16:46:14 +09003198 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003200 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003202 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003203 final boolean showAuxSubtypes;
3204 switch (msg.arg1) {
3205 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3206 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3207 // implemented so that auxiliary subtypes will be excluded when the soft
3208 // keyboard is invisible.
3209 showAuxSubtypes = mInputShown;
3210 break;
3211 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3212 showAuxSubtypes = true;
3213 break;
3214 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3215 showAuxSubtypes = false;
3216 break;
3217 default:
3218 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3219 return false;
3220 }
3221 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003222 return true;
3223
satok47a44912010-10-06 16:03:58 +09003224 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003225 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003226 return true;
3227
3228 case MSG_SHOW_IM_CONFIG:
3229 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003230 return true;
3231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 case MSG_UNBIND_INPUT:
3235 try {
3236 ((IInputMethod)msg.obj).unbindInput();
3237 } catch (RemoteException e) {
3238 // There is nothing interesting about the method dying.
3239 }
3240 return true;
3241 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003242 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 try {
3244 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3245 } catch (RemoteException e) {
3246 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003247 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 return true;
3249 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003250 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003252 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003253 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003254 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 } catch (RemoteException e) {
3256 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003257 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 return true;
3259 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003260 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003262 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003263 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003264 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 } catch (RemoteException e) {
3266 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003267 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003269 case MSG_HIDE_CURRENT_INPUT_METHOD:
3270 synchronized (mMethodMap) {
3271 hideCurrentInputLocked(0, null);
3272 }
3273 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003275 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003277 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3279 } catch (RemoteException e) {
3280 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003281 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003283 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003284 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003285 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003286 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003288 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003290 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003291 // Dispose the channel if the input method is not local to this process
3292 // because the remote proxy will get its own copy when unparceled.
3293 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003294 channel.dispose();
3295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003297 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003301
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003302 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003303 final int missingMethods = msg.arg1;
3304 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003305 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003306 final IBinder startInputToken = (IBinder) args.arg1;
3307 final SessionState session = (SessionState) args.arg2;
3308 final IInputContext inputContext = (IInputContext) args.arg3;
3309 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003312 session.method.startInput(startInputToken, inputContext, missingMethods,
3313 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 } catch (RemoteException e) {
3315 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003316 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003321
Yohei Yukawa33e81792015-11-17 21:14:42 -08003322 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003324 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 } catch (RemoteException e) {
3326 // There is nothing interesting about the last client dying.
3327 }
3328 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003329 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003330 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003331 IInputMethodClient client = (IInputMethodClient)args.arg1;
3332 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003334 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003336 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003337 } finally {
3338 // Dispose the channel if the input method is not local to this process
3339 // because the remote proxy will get its own copy when unparceled.
3340 if (res.channel != null && Binder.isProxy(client)) {
3341 res.channel.dispose();
3342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003344 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003346 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003347 case MSG_SET_ACTIVE:
3348 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003349 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003350 } catch (RemoteException e) {
3351 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3352 + ((ClientState)msg.obj).pid + " uid "
3353 + ((ClientState)msg.obj).uid);
3354 }
3355 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003356 case MSG_SET_INTERACTIVE:
3357 handleSetInteractive(msg.arg1 != 0);
3358 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003359 case MSG_SWITCH_IME:
3360 handleSwitchInputMethod(msg.arg1 != 0);
3361 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003362 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3363 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003364 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003365 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003366 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003367 } catch (RemoteException e) {
3368 Slog.w(TAG, "Got RemoteException sending "
3369 + "setUserActionNotificationSequenceNumber("
3370 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003371 + clientState.pid + " uid "
3372 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003373 }
3374 return true;
3375 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003376 case MSG_REPORT_FULLSCREEN_MODE: {
3377 final boolean fullscreen = msg.arg1 != 0;
3378 final ClientState clientState = (ClientState)msg.obj;
3379 try {
3380 clientState.client.reportFullscreenMode(fullscreen);
3381 } catch (RemoteException e) {
3382 Slog.w(TAG, "Got RemoteException sending "
3383 + "reportFullscreen(" + fullscreen + ") notification to pid="
3384 + clientState.pid + " uid=" + clientState.uid);
3385 }
3386 return true;
3387 }
satok01038492012-04-09 21:08:27 +09003388
3389 // --------------------------------------------------------------
3390 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003391 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003392 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003393 case MSG_SYSTEM_UNLOCK_USER:
3394 final int userId = msg.arg1;
3395 onUnlockUser(userId);
3396 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 }
3398 return false;
3399 }
3400
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003401 private void handleSetInteractive(final boolean interactive) {
3402 synchronized (mMethodMap) {
3403 mIsInteractive = interactive;
3404 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3405
3406 // Inform the current client of the change in active status
3407 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003408 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3409 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3410 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003411 }
3412 }
3413 }
3414
Yohei Yukawaae61f712015-12-09 13:00:10 -08003415 private void handleSwitchInputMethod(final boolean forwardDirection) {
3416 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003417 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003418 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003419 if (nextSubtype == null) {
3420 return;
3421 }
3422 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003423 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3424 if (newInputMethodInfo == null) {
3425 return;
3426 }
3427 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3428 newInputMethodInfo, mCurrentSubtype);
3429 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003430 if (mSubtypeSwitchedByShortCutToast == null) {
3431 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3432 Toast.LENGTH_SHORT);
3433 } else {
3434 mSubtypeSwitchedByShortCutToast.setText(toastText);
3435 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003436 mSubtypeSwitchedByShortCutToast.show();
3437 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003438 }
3439 }
3440
satokdc9ddae2011-10-06 12:22:36 +09003441 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003442 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3443 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003444 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003445 if (DEBUG) {
3446 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3447 }
satok723a27e2010-11-11 14:58:11 +09003448 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003449 return true;
3450 }
3451
3452 return false;
3453 }
3454
Yohei Yukawa94e33302016-02-12 19:37:03 -08003455 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003456 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003457 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003458 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003459 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003460 if (!mSystemReady) {
3461 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3462 return;
3463 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003464 mMethodList.clear();
3465 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003466 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003467 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003468
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003469 // Use for queryIntentServicesAsUser
3470 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003472 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3473 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3474 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003475 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003477 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3478 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003479
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003480 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003481 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 for (int i = 0; i < services.size(); ++i) {
3483 ResolveInfo ri = services.get(i);
3484 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003485 final String imeId = InputMethodInfo.computeId(ri);
3486 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3487 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 + ": it does not require the permission "
3489 + android.Manifest.permission.BIND_INPUT_METHOD);
3490 continue;
3491 }
3492
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003493 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003495 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 try {
satoke7c6998e2011-06-03 17:57:59 +09003497 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003498 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003499 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003500 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501
3502 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003503 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003505 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003506 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 }
3508 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003509
Yohei Yukawac4e44912017-02-09 19:30:22 -08003510 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3511 // negatives when the package state remains to be the same but only the component state is
3512 // changed.
3513 {
3514 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3515 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3516 // conservative, but it seems we cannot use it for now (Issue 35176630).
3517 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3518 new Intent(InputMethod.SERVICE_INTERFACE),
3519 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3520 final int N = allInputMethodServices.size();
3521 for (int i = 0; i < N; ++i) {
3522 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003523 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3524 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003525 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003526 }
3527 }
3528
Yohei Yukawa859df052016-02-17 07:56:46 -08003529 // TODO: The following code should find better place to live.
3530 if (!resetDefaultEnabledIme) {
3531 boolean enabledImeFound = false;
3532 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3533 final int N = enabledImes.size();
3534 for (int i = 0; i < N; ++i) {
3535 final InputMethodInfo imi = enabledImes.get(i);
3536 if (mMethodList.contains(imi)) {
3537 enabledImeFound = true;
3538 break;
3539 }
3540 }
3541 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003542 if (DEBUG) {
3543 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3544 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003545 resetDefaultEnabledIme = true;
3546 resetSelectedInputMethodAndSubtypeLocked("");
3547 }
3548 }
3549
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003550 if (resetDefaultEnabledIme) {
3551 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003552 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003553 final int N = defaultEnabledIme.size();
3554 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003555 final InputMethodInfo imi = defaultEnabledIme.get(i);
3556 if (DEBUG) {
3557 Slog.d(TAG, "--- enable ime = " + imi);
3558 }
3559 setInputMethodEnabledLocked(imi.getId(), true);
3560 }
3561 }
3562
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003563 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003564 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003565 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003566 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3567 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003568 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003569 }
3570 } else {
3571 // Double check that the default IME is certainly enabled.
3572 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003573 }
3574 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003575 // Here is not the perfect place to reset the switching controller. Ideally
3576 // mSwitchingController and mSettings should be able to share the same state.
3577 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3578 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003579 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003583
satok217f5482010-12-15 05:19:19 +09003584 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003585 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003586 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003587 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3588 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003589 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003590 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003591 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003592 final int userId;
3593 synchronized (mMethodMap) {
3594 userId = mSettings.getCurrentUserId();
3595 }
3596 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003597 }
3598
3599 private void showConfigureInputMethods() {
3600 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3601 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3602 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3603 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003604 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003605 }
3606
satok2c93efc2012-04-02 19:33:47 +09003607 private boolean isScreenLocked() {
3608 return mKeyguardManager != null
3609 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3610 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003611
Seigo Nonaka14e13912015-05-06 21:04:13 -07003612 private void showInputMethodMenu(boolean showAuxSubtypes) {
3613 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614
satok2c93efc2012-04-02 19:33:47 +09003615 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003616
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003617 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003618 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003619 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003620
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003621 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003622 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003623 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3624 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003625 if (immis == null || immis.size() == 0) {
3626 return;
3627 }
3628
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003629 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630
satok688bd472012-02-09 20:09:17 +09003631 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003632 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003633 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003634
satokc3690562012-01-10 20:14:43 +09003635 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003636 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003637 if (currentSubtype != null) {
3638 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003639 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3640 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003641 }
3642 }
3643
Ken Wakasa761eb372011-03-04 19:06:18 +09003644 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003645 mIms = new InputMethodInfo[N];
3646 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003647 int checkedItem = 0;
3648 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003649 final ImeSubtypeListItem item = imList.get(i);
3650 mIms[i] = item.mImi;
3651 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003652 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003653 int subtypeId = mSubtypeIds[i];
3654 if ((subtypeId == NOT_A_SUBTYPE_ID)
3655 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3656 || (subtypeId == lastInputMethodSubtypeId)) {
3657 checkedItem = i;
3658 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003661
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003662 final Context settingsContext = new ContextThemeWrapper(
3663 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003664 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3665
3666 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003667 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3668 @Override
3669 public void onCancel(DialogInterface dialog) {
3670 hideInputMethodMenu();
3671 }
3672 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003673
3674 final Context dialogContext = mDialogBuilder.getContext();
3675 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3676 com.android.internal.R.styleable.DialogPreference,
3677 com.android.internal.R.attr.alertDialogStyle, 0);
3678 final Drawable dialogIcon = a.getDrawable(
3679 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3680 a.recycle();
3681
3682 mDialogBuilder.setIcon(dialogIcon);
3683
Yohei Yukawad34e1482016-02-11 08:03:52 -08003684 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003685 final View tv = inflater.inflate(
3686 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3687 mDialogBuilder.setCustomTitle(tv);
3688
3689 // Setup layout for a toggle switch of the hardware keyboard
3690 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003691 mSwitchingDialogTitleView
3692 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003693 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003694 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003695 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003696 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003697 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003698 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3699 @Override
3700 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003701 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003702 // Ensure that the input method dialog is dismissed when changing
3703 // the hardware keyboard state.
3704 hideInputMethodMenu();
3705 }
3706 });
3707
Alan Viverette505e3ab2014-11-24 15:22:11 -08003708 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003709 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3710 final OnClickListener choiceListener = new OnClickListener() {
3711 @Override
3712 public void onClick(final DialogInterface dialog, final int which) {
3713 synchronized (mMethodMap) {
3714 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3715 || mSubtypeIds.length <= which) {
3716 return;
satok01038492012-04-09 21:08:27 +09003717 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003718 final InputMethodInfo im = mIms[which];
3719 int subtypeId = mSubtypeIds[which];
3720 adapter.mCheckedItem = which;
3721 adapter.notifyDataSetChanged();
3722 hideInputMethodMenu();
3723 if (im != null) {
3724 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3725 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003726 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003727 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003729 }
3730 }
3731 };
3732 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003735 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003736 final Window w = mSwitchingDialog.getWindow();
3737 final WindowManager.LayoutParams attrs = w.getAttributes();
3738 w.setType(TYPE_INPUT_METHOD_DIALOG);
3739 // Use an alternate token for the dialog for that window manager can group the token
3740 // with other IME windows based on type vs. grouping based on whichever token happens
3741 // to get selected by the system later on.
3742 attrs.token = mSwitchingDialogToken;
3743 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3744 attrs.setTitle("Select input method");
3745 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003746 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 mSwitchingDialog.show();
3748 }
3749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003750
Ken Wakasa05dbb652011-08-22 15:22:43 +09003751 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3752 private final LayoutInflater mInflater;
3753 private final int mTextViewResourceId;
3754 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003755 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003756 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3757 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3758 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003759
Ken Wakasa05dbb652011-08-22 15:22:43 +09003760 mTextViewResourceId = textViewResourceId;
3761 mItemsList = itemsList;
3762 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003763 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003764 }
3765
3766 @Override
3767 public View getView(int position, View convertView, ViewGroup parent) {
3768 final View view = convertView != null ? convertView
3769 : mInflater.inflate(mTextViewResourceId, null);
3770 if (position < 0 || position >= mItemsList.size()) return view;
3771 final ImeSubtypeListItem item = mItemsList.get(position);
3772 final CharSequence imeName = item.mImeName;
3773 final CharSequence subtypeName = item.mSubtypeName;
3774 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3775 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3776 if (TextUtils.isEmpty(subtypeName)) {
3777 firstTextView.setText(imeName);
3778 secondTextView.setVisibility(View.GONE);
3779 } else {
3780 firstTextView.setText(subtypeName);
3781 secondTextView.setText(imeName);
3782 secondTextView.setVisibility(View.VISIBLE);
3783 }
3784 final RadioButton radioButton =
3785 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3786 radioButton.setChecked(position == mCheckedItem);
3787 return view;
3788 }
3789 }
3790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003792 synchronized (mMethodMap) {
3793 hideInputMethodMenuLocked();
3794 }
3795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003796
The Android Open Source Project10592532009-03-18 17:39:46 -07003797 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003798 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799
The Android Open Source Project10592532009-03-18 17:39:46 -07003800 if (mSwitchingDialog != null) {
3801 mSwitchingDialog.dismiss();
3802 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003804
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003805 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003806 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003807 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003811
satok42c5a162011-05-26 16:46:14 +09003812 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003814 // TODO: Make this work even for non-current users?
3815 if (!calledFromValidUser()) {
3816 return false;
3817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 synchronized (mMethodMap) {
3819 if (mContext.checkCallingOrSelfPermission(
3820 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3821 != PackageManager.PERMISSION_GRANTED) {
3822 throw new SecurityException(
3823 "Requires permission "
3824 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3825 }
Anna Galusza9b278112016-01-04 11:37:37 -08003826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 long ident = Binder.clearCallingIdentity();
3828 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003829 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 } finally {
3831 Binder.restoreCallingIdentity(ident);
3832 }
3833 }
3834 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003835
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003836 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3837 // Make sure this is a valid input method.
3838 InputMethodInfo imm = mMethodMap.get(id);
3839 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003840 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003841 }
3842
satokd87c2592010-09-29 11:52:06 +09003843 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3844 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003845
satokd87c2592010-09-29 11:52:06 +09003846 if (enabled) {
3847 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3848 if (pair.first.equals(id)) {
3849 // We are enabling this input method, but it is already enabled.
3850 // Nothing to do. The previous state was enabled.
3851 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003852 }
3853 }
satokd87c2592010-09-29 11:52:06 +09003854 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3855 // Previous state was disabled.
3856 return false;
3857 } else {
3858 StringBuilder builder = new StringBuilder();
3859 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3860 builder, enabledInputMethodsList, id)) {
3861 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003862 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003863 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3864 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3865 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003866 }
3867 // Previous state was enabled.
3868 return true;
3869 } else {
3870 // We are disabling the input method but it is already disabled.
3871 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003872 return false;
3873 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003874 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003875 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003876
satok723a27e2010-11-11 14:58:11 +09003877 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3878 boolean setSubtypeOnly) {
3879 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003880 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003881
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003882 mCurUserActionNotificationSequenceNumber =
3883 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3884 if (DEBUG) {
3885 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3886 + mCurUserActionNotificationSequenceNumber);
3887 }
3888
3889 if (mCurClient != null && mCurClient.client != null) {
3890 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3891 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003892 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003893 }
3894
satok723a27e2010-11-11 14:58:11 +09003895 // Set Subtype here
3896 if (imi == null || subtypeId < 0) {
3897 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003898 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003899 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003900 if (subtypeId < imi.getSubtypeCount()) {
3901 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3902 mSettings.putSelectedSubtype(subtype.hashCode());
3903 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003904 } else {
3905 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003906 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003907 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003908 }
satokab751aa2010-09-14 19:17:36 +09003909 }
satok723a27e2010-11-11 14:58:11 +09003910
Yohei Yukawa68645a62016-02-17 07:54:20 -08003911 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003912 // Set InputMethod here
3913 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3914 }
3915 }
3916
3917 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3918 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3919 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3920 // newDefaultIme is empty when there is no candidate for the selected IME.
3921 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3922 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3923 if (subtypeHashCode != null) {
3924 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003925 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003926 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003927 } catch (NumberFormatException e) {
3928 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3929 }
3930 }
3931 }
3932 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003933 }
3934
satok4e4569d2010-11-19 18:45:53 +09003935 // If there are no selected shortcuts, tries finding the most applicable ones.
3936 private Pair<InputMethodInfo, InputMethodSubtype>
3937 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3938 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3939 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003940 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003941 boolean foundInSystemIME = false;
3942
3943 // Search applicable subtype for each InputMethodInfo
3944 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003945 final String imiId = imi.getId();
3946 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3947 continue;
3948 }
satokcd7cd292010-11-20 15:46:23 +09003949 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003950 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003951 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003952 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003953 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003954 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003955 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003956 }
satokdf31ae62011-01-15 06:19:44 +09003957 // 2. Search by the system locale from enabledSubtypes.
3958 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003959 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003960 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003961 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003962 }
satoka86f5e42011-09-02 17:12:42 +09003963 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003964 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003965 final ArrayList<InputMethodSubtype> subtypesForSearch =
3966 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003967 ? InputMethodUtils.getSubtypes(imi)
3968 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003969 // 4. Search by the current subtype's locale from all subtypes.
3970 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003971 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003972 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003973 }
3974 // 5. Search by the system locale from all subtypes.
3975 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003976 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003977 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003978 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003979 }
satokcd7cd292010-11-20 15:46:23 +09003980 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003981 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003982 // The current input method is the most applicable IME.
3983 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003984 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003985 break;
satok7599a7f2010-12-22 13:45:23 +09003986 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003987 // The system input method is 2nd applicable IME.
3988 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003989 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003990 if ((imi.getServiceInfo().applicationInfo.flags
3991 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3992 foundInSystemIME = true;
3993 }
satok4e4569d2010-11-19 18:45:53 +09003994 }
3995 }
3996 }
3997 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003998 if (mostApplicableIMI != null) {
3999 Slog.w(TAG, "Most applicable shortcut input method was:"
4000 + mostApplicableIMI.getId());
4001 if (mostApplicableSubtype != null) {
4002 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
4003 + "," + mostApplicableSubtype.getMode() + ","
4004 + mostApplicableSubtype.getLocale());
4005 }
4006 }
satok4e4569d2010-11-19 18:45:53 +09004007 }
satokcd7cd292010-11-20 15:46:23 +09004008 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004009 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09004010 } else {
4011 return null;
4012 }
4013 }
4014
satokab751aa2010-09-14 19:17:36 +09004015 /**
4016 * @return Return the current subtype of this input method.
4017 */
satok42c5a162011-05-26 16:46:14 +09004018 @Override
satokab751aa2010-09-14 19:17:36 +09004019 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004020 // TODO: Make this work even for non-current users?
4021 if (!calledFromValidUser()) {
4022 return null;
4023 }
4024 synchronized (mMethodMap) {
4025 return getCurrentInputMethodSubtypeLocked();
4026 }
4027 }
4028
4029 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09004030 if (mCurMethodId == null) {
4031 return null;
4032 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004033 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004034 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
4035 if (imi == null || imi.getSubtypeCount() == 0) {
4036 return null;
satok4e4569d2010-11-19 18:45:53 +09004037 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004038 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004039 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
4040 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004041 if (subtypeId == NOT_A_SUBTYPE_ID) {
4042 // If there are no selected subtypes, the framework will try to find
4043 // the most applicable subtype from explicitly or implicitly enabled
4044 // subtypes.
4045 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004046 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004047 // If there is only one explicitly or implicitly enabled subtype,
4048 // just returns it.
4049 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4050 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4051 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004052 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004053 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004054 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004055 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004056 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004057 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4058 true);
satok4e4569d2010-11-19 18:45:53 +09004059 }
satok3ef8b292010-11-23 06:06:29 +09004060 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004061 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004062 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004063 }
4064 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004065 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004066 }
4067
satok4e4569d2010-11-19 18:45:53 +09004068 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004069 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004070 @Override
satok4e4569d2010-11-19 18:45:53 +09004071 public List getShortcutInputMethodsAndSubtypes() {
4072 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004073 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004074 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004075 // If there are no selected shortcut subtypes, the framework will try to find
4076 // the most applicable subtype from all subtypes whose mode is
4077 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004078 Pair<InputMethodInfo, InputMethodSubtype> info =
4079 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004080 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004081 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004082 ret.add(info.first);
4083 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004084 }
satok3da92232011-01-11 22:46:30 +09004085 return ret;
satokf3db1af2010-11-23 13:34:33 +09004086 }
satokf3db1af2010-11-23 13:34:33 +09004087 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4088 ret.add(imi);
4089 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4090 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004091 }
4092 }
satokf3db1af2010-11-23 13:34:33 +09004093 return ret;
satok4e4569d2010-11-19 18:45:53 +09004094 }
4095 }
4096
satok42c5a162011-05-26 16:46:14 +09004097 @Override
satokb66d2872010-11-10 01:04:04 +09004098 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004099 // TODO: Make this work even for non-current users?
4100 if (!calledFromValidUser()) {
4101 return false;
4102 }
satokb66d2872010-11-10 01:04:04 +09004103 synchronized (mMethodMap) {
4104 if (subtype != null && mCurMethodId != null) {
4105 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004106 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004107 if (subtypeId != NOT_A_SUBTYPE_ID) {
4108 setInputMethodLocked(mCurMethodId, subtypeId);
4109 return true;
4110 }
4111 }
4112 return false;
4113 }
4114 }
4115
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004116 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004117 private static class InputMethodFileManager {
4118 private static final String SYSTEM_PATH = "system";
4119 private static final String INPUT_METHOD_PATH = "inputmethod";
4120 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4121 private static final String NODE_SUBTYPES = "subtypes";
4122 private static final String NODE_SUBTYPE = "subtype";
4123 private static final String NODE_IMI = "imi";
4124 private static final String ATTR_ID = "id";
4125 private static final String ATTR_LABEL = "label";
4126 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004127 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004128 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004129 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004130 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4131 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4132 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004133 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004134 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4135 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004136 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004137 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004138 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004139 if (methodMap == null) {
4140 throw new NullPointerException("methodMap is null");
4141 }
4142 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004143 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004144 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4145 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004146 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004147 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004148 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4149 }
4150 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4151 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4152 if (!subtypeFile.exists()) {
4153 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004154 writeAdditionalInputMethodSubtypes(
4155 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004156 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004157 readAdditionalInputMethodSubtypes(
4158 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004159 }
4160 }
4161
4162 private void deleteAllInputMethodSubtypes(String imiId) {
4163 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004164 mAdditionalSubtypesMap.remove(imiId);
4165 writeAdditionalInputMethodSubtypes(
4166 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004167 }
4168 }
4169
4170 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004171 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004172 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004173 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004174 final int N = additionalSubtypes.length;
4175 for (int i = 0; i < N; ++i) {
4176 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004177 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004178 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004179 } else {
4180 Slog.w(TAG, "Duplicated subtype definition found: "
4181 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004182 }
4183 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004184 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4185 writeAdditionalInputMethodSubtypes(
4186 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004187 }
4188 }
4189
4190 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4191 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004192 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004193 }
4194 }
4195
4196 private static void writeAdditionalInputMethodSubtypes(
4197 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4198 HashMap<String, InputMethodInfo> methodMap) {
4199 // Safety net for the case that this function is called before methodMap is set.
4200 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4201 FileOutputStream fos = null;
4202 try {
4203 fos = subtypesFile.startWrite();
4204 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004205 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004206 out.startDocument(null, true);
4207 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4208 out.startTag(null, NODE_SUBTYPES);
4209 for (String imiId : allSubtypes.keySet()) {
4210 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4211 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4212 continue;
4213 }
4214 out.startTag(null, NODE_IMI);
4215 out.attribute(null, ATTR_ID, imiId);
4216 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4217 final int N = subtypesList.size();
4218 for (int i = 0; i < N; ++i) {
4219 final InputMethodSubtype subtype = subtypesList.get(i);
4220 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004221 if (subtype.hasSubtypeId()) {
4222 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4223 String.valueOf(subtype.getSubtypeId()));
4224 }
satoke7c6998e2011-06-03 17:57:59 +09004225 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4226 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4227 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004228 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4229 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004230 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4231 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4232 out.attribute(null, ATTR_IS_AUXILIARY,
4233 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004234 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4235 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004236 out.endTag(null, NODE_SUBTYPE);
4237 }
4238 out.endTag(null, NODE_IMI);
4239 }
4240 out.endTag(null, NODE_SUBTYPES);
4241 out.endDocument();
4242 subtypesFile.finishWrite(fos);
4243 } catch (java.io.IOException e) {
4244 Slog.w(TAG, "Error writing subtypes", e);
4245 if (fos != null) {
4246 subtypesFile.failWrite(fos);
4247 }
4248 }
4249 }
4250
4251 private static void readAdditionalInputMethodSubtypes(
4252 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4253 if (allSubtypes == null || subtypesFile == null) return;
4254 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004255 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004256 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004257 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004258 int type = parser.getEventType();
4259 // Skip parsing until START_TAG
4260 while ((type = parser.next()) != XmlPullParser.START_TAG
4261 && type != XmlPullParser.END_DOCUMENT) {}
4262 String firstNodeName = parser.getName();
4263 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4264 throw new XmlPullParserException("Xml doesn't start with subtypes");
4265 }
4266 final int depth =parser.getDepth();
4267 String currentImiId = null;
4268 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4269 while (((type = parser.next()) != XmlPullParser.END_TAG
4270 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4271 if (type != XmlPullParser.START_TAG)
4272 continue;
4273 final String nodeName = parser.getName();
4274 if (NODE_IMI.equals(nodeName)) {
4275 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4276 if (TextUtils.isEmpty(currentImiId)) {
4277 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4278 continue;
4279 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004280 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004281 allSubtypes.put(currentImiId, tempSubtypesArray);
4282 } else if (NODE_SUBTYPE.equals(nodeName)) {
4283 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4284 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4285 continue;
4286 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004287 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004288 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004289 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004290 parser.getAttributeValue(null, ATTR_LABEL));
4291 final String imeSubtypeLocale =
4292 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004293 final String languageTag =
4294 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004295 final String imeSubtypeMode =
4296 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4297 final String imeSubtypeExtraValue =
4298 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004299 final boolean isAuxiliary = "1".equals(String.valueOf(
4300 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004301 final boolean isAsciiCapable = "1".equals(String.valueOf(
4302 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004303 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004304 .setSubtypeNameResId(label)
4305 .setSubtypeIconResId(icon)
4306 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004307 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004308 .setSubtypeMode(imeSubtypeMode)
4309 .setSubtypeExtraValue(imeSubtypeExtraValue)
4310 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004311 .setIsAsciiCapable(isAsciiCapable);
4312 final String subtypeIdString =
4313 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4314 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004315 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004316 }
4317 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004318 }
4319 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004320 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4321 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004322 return;
satoke7c6998e2011-06-03 17:57:59 +09004323 }
4324 }
4325 }
4326
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004327 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4328 @NonNull
4329 private final Handler mHandler;
4330
4331 LocalServiceImpl(@NonNull final Handler handler) {
4332 mHandler = handler;
4333 }
4334
4335 @Override
4336 public void setInteractive(boolean interactive) {
4337 // Do everything in handler so as not to block the caller.
4338 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4339 interactive ? 1 : 0, 0));
4340 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004341
4342 @Override
4343 public void switchInputMethod(boolean forwardDirection) {
4344 // Do everything in handler so as not to block the caller.
4345 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4346 forwardDirection ? 1 : 0, 0));
4347 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004348
4349 @Override
4350 public void hideCurrentInputMethod() {
4351 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4352 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4353 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004354 }
4355
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004356 private static String imeWindowStatusToString(final int imeWindowVis) {
4357 final StringBuilder sb = new StringBuilder();
4358 boolean first = true;
4359 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4360 sb.append("Active");
4361 first = false;
4362 }
4363 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4364 if (!first) {
4365 sb.append("|");
4366 }
4367 sb.append("Visible");
4368 }
4369 return sb.toString();
4370 }
4371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004373 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4374 @Nullable Uri contentUri, @Nullable String packageName) {
4375 if (!calledFromValidUser()) {
4376 return null;
4377 }
4378
4379 if (token == null) {
4380 throw new NullPointerException("token");
4381 }
4382 if (packageName == null) {
4383 throw new NullPointerException("packageName");
4384 }
4385 if (contentUri == null) {
4386 throw new NullPointerException("contentUri");
4387 }
4388 final String contentUriScheme = contentUri.getScheme();
4389 if (!"content".equals(contentUriScheme)) {
4390 throw new InvalidParameterException("contentUri must have content scheme");
4391 }
4392
4393 synchronized (mMethodMap) {
4394 final int uid = Binder.getCallingUid();
4395 if (mCurMethodId == null) {
4396 return null;
4397 }
4398 if (mCurToken != token) {
4399 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4400 + " token=" + token);
4401 return null;
4402 }
4403 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4404 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4405 // nature of our system. Let's compare it with our internal record.
4406 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4407 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4408 + mCurAttribute.packageName + " packageName=" + packageName);
4409 return null;
4410 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004411 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004412 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004413 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004414 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004415 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4416 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4417 imeUserId);
4418 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4419 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4420 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4421 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4422 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4423 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4424 // actually allowed to "uid", which is guaranteed to be the IME's one.
4425 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4426 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004427 }
4428 }
4429
4430 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004431 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4432 if (!calledFromValidUser()) {
4433 return;
4434 }
4435 synchronized (mMethodMap) {
4436 if (!calledWithValidToken(token)) {
4437 return;
4438 }
4439 if (mCurClient != null && mCurClient.client != null) {
4440 mInFullscreenMode = fullscreen;
4441 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4442 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4443 }
4444 }
4445 }
4446
4447 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004449 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450
4451 IInputMethod method;
4452 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004453 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 synchronized (mMethodMap) {
4458 p.println("Current Input Method Manager state:");
4459 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004460 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 for (int i=0; i<N; i++) {
4462 InputMethodInfo info = mMethodList.get(i);
4463 p.println(" InputMethod #" + i + ":");
4464 info.dump(p, " ");
4465 }
4466 p.println(" Clients:");
4467 for (ClientState ci : mClients.values()) {
4468 p.println(" Client " + ci + ":");
4469 p.println(" client=" + ci.client);
4470 p.println(" inputContext=" + ci.inputContext);
4471 p.println(" sessionRequested=" + ci.sessionRequested);
4472 p.println(" curSession=" + ci.curSession);
4473 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004474 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004476 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004477 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4478 + " softInputMode=" +
4479 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4480 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004481 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4483 + " mBoundToMethod=" + mBoundToMethod);
4484 p.println(" mCurToken=" + mCurToken);
4485 p.println(" mCurIntent=" + mCurIntent);
4486 method = mCurMethod;
4487 p.println(" mCurMethod=" + mCurMethod);
4488 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004489 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 p.println(" mShowRequested=" + mShowRequested
4491 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4492 + " mShowForced=" + mShowForced
4493 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004494 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004495 p.println(" mCurUserActionNotificationSequenceNumber="
4496 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004497 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004498 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004499 p.println(" mSwitchingController:");
4500 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004501 p.println(" mSettings:");
4502 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004503
4504 p.println(" mStartInputHistory:");
4505 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004507
Jeff Brownb88102f2010-09-08 11:49:43 -07004508 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 pw.flush();
4511 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004512 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4513 } catch (IOException | RemoteException e) {
4514 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004516 } else {
4517 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004519
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004520 if (focusedWindowClient != null && client != focusedWindowClient) {
4521 p.println(" ");
4522 p.println("Warning: Current input method client doesn't match the last focused. "
4523 + "window.");
4524 p.println("Dumping input method client in the last focused window just in case.");
4525 p.println(" ");
4526 pw.flush();
4527 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004528 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4529 } catch (IOException | RemoteException e) {
4530 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004531 }
4532 }
4533
Jeff Brownb88102f2010-09-08 11:49:43 -07004534 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 pw.flush();
4537 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004538 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4539 } catch (IOException | RemoteException e) {
4540 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004542 } else {
4543 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 }
4545 }
4546}