blob: f007bcc829b12eec2d32f10407e0a5ac868cc526 [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;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700131import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900132import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800134import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700135import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import android.view.inputmethod.InputMethod;
137import android.view.inputmethod.InputMethodInfo;
138import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700139import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900140import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900141import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900142import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900143import android.widget.CompoundButton;
144import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900145import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900146import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900147import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700148import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
satoke7c6998e2011-06-03 17:57:59 +0900150import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900152import java.io.FileInputStream;
153import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154import java.io.IOException;
155import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700156import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100157import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700158import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800159import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900161import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800162import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163import java.util.HashMap;
164import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800165import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800166import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800167import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169/**
170 * This class provides a system service that manages input methods.
171 */
172public class InputMethodManagerService extends IInputMethodManager.Stub
173 implements ServiceConnection, Handler.Callback {
174 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700175 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700176 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700178 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
179 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
180 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 static final int MSG_UNBIND_INPUT = 1000;
183 static final int MSG_BIND_INPUT = 1010;
184 static final int MSG_SHOW_SOFT_INPUT = 1020;
185 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700186 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 static final int MSG_ATTACH_TOKEN = 1040;
188 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
Yohei Yukawa33e81792015-11-17 21:14:42 -0800192 static final int MSG_UNBIND_CLIENT = 3000;
193 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700194 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700195 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900196 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800197 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800198 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
satok01038492012-04-09 21:08:27 +0900200 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
201
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700202 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
203
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700204 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
satokf9f01002011-05-19 21:31:50 +0900206 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
207
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900208 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900209 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900210
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700211 /**
212 * Binding flags for establishing connection to the {@link InputMethodService}.
213 */
214 private static final int IME_CONNECTION_BIND_FLAGS =
215 Context.BIND_AUTO_CREATE
216 | Context.BIND_NOT_VISIBLE
217 | Context.BIND_NOT_FOREGROUND
Yohei Yukawaad78a612017-08-04 01:57:27 -0700218 | Context.BIND_IMPORTANT_BACKGROUND;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700219
220 /**
221 * Binding flags used only while the {@link InputMethodService} is showing window.
222 */
223 private static final int IME_VISIBLE_BIND_FLAGS =
224 Context.BIND_AUTO_CREATE
225 | Context.BIND_TREAT_LIKE_ACTIVITY
Yohei Yukawaad78a612017-08-04 01:57:27 -0700226 | Context.BIND_FOREGROUND_SERVICE
227 | Context.BIND_SHOWING_UI;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700228
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700229 @Retention(SOURCE)
230 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
231 private @interface HardKeyboardBehavior {
232 int WIRELESS_AFFORDANCE = 0;
233 int WIRED_AFFORDANCE = 1;
234 }
satok4e4569d2010-11-19 18:45:53 +0900235
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900236 /**
237 * A protected broadcast intent action for internal use for {@link PendingIntent} in
238 * the notification.
239 */
240 private static final String ACTION_SHOW_INPUT_METHOD_PICKER =
241 "com.android.server.InputMethodManagerService.SHOW_INPUT_METHOD_PICKER";
242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800244 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900246 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700249 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700251 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900252 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900253 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900254 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800255 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900257 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 // All known input methods. mMethodMap also serves as the global
260 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700261 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
262 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900263 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700264 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900265 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
Yohei Yukawae0733062017-02-09 22:49:35 -0800267 /**
268 * Tracks how many times {@link #mMethodMap} was updated.
269 */
270 @GuardedBy("mMethodMap")
271 private int mMethodMapUpdateCount = 0;
272
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700273 // Used to bring IME service up to visible adjustment while it is being shown.
274 final ServiceConnection mVisibleConnection = new ServiceConnection() {
275 @Override public void onServiceConnected(ComponentName name, IBinder service) {
276 }
277
278 @Override public void onServiceDisconnected(ComponentName name) {
279 }
280 };
281 boolean mVisibleBound = false;
282
satok7cfc0ed2011-06-20 21:29:36 +0900283 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700284 private NotificationManager mNotificationManager;
285 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700286 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400287 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700288 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900289 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900290 private boolean mNotificationShown;
291
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900292 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 final ClientState client;
294 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700295
296 IInputMethodSession session;
297 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 @Override
300 public String toString() {
301 return "SessionState{uid " + client.uid + " pid " + client.pid
302 + " method " + Integer.toHexString(
303 System.identityHashCode(method))
304 + " session " + Integer.toHexString(
305 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700306 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 + "}";
308 }
309
310 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700311 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 client = _client;
313 method = _method;
314 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700315 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
Jeff Brownc28867a2013-03-26 15:42:39 -0700319 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 final IInputMethodClient client;
321 final IInputContext inputContext;
322 final int uid;
323 final int pid;
324 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 boolean sessionRequested;
327 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 @Override
330 public String toString() {
331 return "ClientState{" + Integer.toHexString(
332 System.identityHashCode(this)) + " uid " + uid
333 + " pid " + pid + "}";
334 }
335
336 ClientState(IInputMethodClient _client, IInputContext _inputContext,
337 int _uid, int _pid) {
338 client = _client;
339 inputContext = _inputContext;
340 uid = _uid;
341 pid = _pid;
342 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
343 }
344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800345
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700346 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700349 * Set once the system is ready to run third party code.
350 */
351 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700353 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700354 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
355 * method. This is to be synchronized with the secure settings keyed with
356 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
357 *
358 * <p>This can be transiently {@code null} when the system is re-initializing input method
359 * settings, e.g., the system locale is just changed.</p>
360 *
361 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
362 * {@link InputMethodManagerService}.</p>
363 *
364 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700366 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
370 * The current binding sequence number, incremented every time there is
371 * a new bind performed.
372 */
373 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
376 * The client that is currently bound to an input method.
377 */
378 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800381 * The last window token that we confirmed to be focused. This is always updated upon reports
382 * from the input method client. If the window state is already changed before the report is
383 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700384 */
385 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700387 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800388 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
389 *
390 * @see #mCurFocusedWindow
391 */
392 int mCurFocusedWindowSoftInputMode;
393
394 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800395 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
396 */
397 ClientState mCurFocusedWindowClient;
398
399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * The input context last provided by the current client.
401 */
402 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700405 * The missing method flags for the input context last provided by the current client.
406 *
407 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
408 */
409 int mCurInputContextMissingMethods;
410
411 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 * The attributes last provided by the current client.
413 */
414 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700417 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700419 *
420 * <p>This can be {@code null} when no input method is connected.</p>
421 *
422 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700424 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 /**
satokab751aa2010-09-14 19:17:36 +0900428 * The current subtype of the current input method.
429 */
430 private InputMethodSubtype mCurrentSubtype;
431
satok4e4569d2010-11-19 18:45:53 +0900432 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900433 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700434 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900435
John Spurlocke0980502013-10-25 11:59:29 -0400436 // Was the keyguard locked when this client became current?
437 private boolean mCurClientInKeyguard;
438
satokab751aa2010-09-14 19:17:36 +0900439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 * Set to true if our ServiceConnection is currently actively bound to
441 * a service (whether or not we have gotten its IBinder back yet).
442 */
443 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
446 * Set if the client has asked for the input method to be shown.
447 */
448 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
451 * Set if we were explicitly told to show the input method.
452 */
453 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
456 * Set if we were forced to be shown.
457 */
458 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 /**
461 * Set if we last told the input method to show itself.
462 */
463 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800466 * {@code true} if the current input method is in fullscreen mode.
467 */
468 boolean mInFullscreenMode;
469
470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 * The Intent used to connect to the current input method.
472 */
473 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 /**
476 * The token we have made for the currently active input method, to
477 * identify it in the future.
478 */
479 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 /**
482 * If non-null, this is the input method service we are currently connected
483 * to.
484 */
485 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 /**
488 * Time that we last initiated a bind to the input method, to determine
489 * if we should try to disconnect and reconnect to it.
490 */
491 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 /**
494 * Have we called mCurMethod.bindInput()?
495 */
496 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 /**
499 * Currently enabled session. Only touched by service thread, not
500 * protected by a lock.
501 */
502 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700505 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700507 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800508
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900509 int mCurUserActionNotificationSequenceNumber = 0;
510
Joe Onorato857fd9b2011-01-27 15:08:35 -0800511 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900512
513 /**
514 * A set of status bits regarding the active IME.
515 *
516 * <p>This value is a combination of following two bits:</p>
517 * <dl>
518 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
519 * <dd>
520 * If this bit is ON, connected IME is ready to accept touch/key events.
521 * </dd>
522 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
523 * <dd>
524 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
525 * </dd>
526 * </dl>
527 * <em>Do not update this value outside of setImeWindowStatus.</em>
528 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800529 int mImeWindowVis;
530
Ken Wakasa05dbb652011-08-22 15:22:43 +0900531 private AlertDialog.Builder mDialogBuilder;
532 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700533 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900534 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700535 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900536 private InputMethodInfo[] mIms;
537 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800538 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700539 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800540 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900541 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
542 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500543 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700544 @HardKeyboardBehavior
545 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800546
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800547 /**
548 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
549 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
550 * will not affect those tasks that are already posted.
551 *
552 * <p>Posting {@link #MSG_START_INPUT} message basically means that
553 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
554 * back in the current IME process shortly, which will also affect what the current IME starts
555 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
556 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
557 * logical input session between the client application and the current IME.</p>
558 *
559 * <p>Be careful to not keep strong references to this object forever, which can prevent
560 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
561 * </p>
562 */
563 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800564 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
565
566 final int mSequenceNumber;
567 final long mTimestamp;
568 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800569 @NonNull
570 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800571 @NonNull
572 final String mImeId;
573 // @InputMethodClient.StartInputReason
574 final int mStartInputReason;
575 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800576 @Nullable
577 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800578 @NonNull
579 final EditorInfo mEditorInfo;
580 final int mTargetWindowSoftInputMode;
581 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800582
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800583 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
584 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
585 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
586 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
587 mSequenceNumber = sSequenceNumber.getAndIncrement();
588 mTimestamp = SystemClock.uptimeMillis();
589 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800590 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800591 mImeId = imeId;
592 mStartInputReason = startInputReason;
593 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800594 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800595 mEditorInfo = editorInfo;
596 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
597 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800598 }
599 }
600
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800601 @GuardedBy("mMethodMap")
602 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800603
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800604 /**
605 * A ring buffer to store the history of {@link StartInputInfo}.
606 */
607 private static final class StartInputHistory {
608 /**
609 * Entry size for non low-RAM devices.
610 *
611 * <p>TODO: Consider to follow what other system services have been doing to manage
612 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
613 */
614 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
615
616 /**
617 * Entry size for non low-RAM devices.
618 *
619 * <p>TODO: Consider to follow what other system services have been doing to manage
620 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
621 */
622 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
623
624 private static int getEntrySize() {
625 if (ActivityManager.isLowRamDeviceStatic()) {
626 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
627 } else {
628 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
629 }
630 }
631
632 /**
633 * Backing store for the ring bugger.
634 */
635 private final Entry[] mEntries = new Entry[getEntrySize()];
636
637 /**
638 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
639 * write.
640 */
641 private int mNextIndex = 0;
642
643 /**
644 * Recyclable entry to store the information in {@link StartInputInfo}.
645 */
646 private static final class Entry {
647 int mSequenceNumber;
648 long mTimestamp;
649 long mWallTime;
650 @NonNull
651 String mImeTokenString;
652 @NonNull
653 String mImeId;
654 /* @InputMethodClient.StartInputReason */
655 int mStartInputReason;
656 boolean mRestarting;
657 @NonNull
658 String mTargetWindowString;
659 @NonNull
660 EditorInfo mEditorInfo;
661 int mTargetWindowSoftInputMode;
662 int mClientBindSequenceNumber;
663
664 Entry(@NonNull StartInputInfo original) {
665 set(original);
666 }
667
668 void set(@NonNull StartInputInfo original) {
669 mSequenceNumber = original.mSequenceNumber;
670 mTimestamp = original.mTimestamp;
671 mWallTime = original.mWallTime;
672 // Intentionally convert to String so as not to keep a strong reference to a Binder
673 // object.
674 mImeTokenString = String.valueOf(original.mImeToken);
675 mImeId = original.mImeId;
676 mStartInputReason = original.mStartInputReason;
677 mRestarting = original.mRestarting;
678 // Intentionally convert to String so as not to keep a strong reference to a Binder
679 // object.
680 mTargetWindowString = String.valueOf(original.mTargetWindow);
681 mEditorInfo = original.mEditorInfo;
682 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
683 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
684 }
685 }
686
687 /**
688 * Add a new entry and discard the oldest entry as needed.
689 * @param info {@lin StartInputInfo} to be added.
690 */
691 void addEntry(@NonNull StartInputInfo info) {
692 final int index = mNextIndex;
693 if (mEntries[index] == null) {
694 mEntries[index] = new Entry(info);
695 } else {
696 mEntries[index].set(info);
697 }
698 mNextIndex = (mNextIndex + 1) % mEntries.length;
699 }
700
701 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
702 final SimpleDateFormat dataFormat =
703 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
704
705 for (int i = 0; i < mEntries.length; ++i) {
706 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
707 if (entry == null) {
708 continue;
709 }
710 pw.print(prefix);
711 pw.println("StartInput #" + entry.mSequenceNumber + ":");
712
713 pw.print(prefix);
714 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
715 + " (timestamp=" + entry.mTimestamp + ")"
716 + " reason="
717 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
718 + " restarting=" + entry.mRestarting);
719
720 pw.print(prefix);
721 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
722
723 pw.print(prefix);
724 pw.println(" targetWin=" + entry.mTargetWindowString
725 + " [" + entry.mEditorInfo.packageName + "]"
726 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
727
728 pw.print(prefix);
729 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
730 entry.mTargetWindowSoftInputMode));
731
732 pw.print(prefix);
733 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
734 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
735 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
736 + " fieldName=" + entry.mEditorInfo.fieldName
737 + " actionId=" + entry.mEditorInfo.actionId
738 + " actionLabel=" + entry.mEditorInfo.actionLabel);
739 }
740 }
741 }
742
743 @GuardedBy("mMethodMap")
744 @NonNull
745 private final StartInputHistory mStartInputHistory = new StartInputHistory();
746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700748 int mUserId;
749 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700750 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800751 String mLastEnabled = "";
752
Yohei Yukawa81482972015-06-04 00:58:59 -0700753 /**
754 * <em>This constructor must be called within the lock.</em>
755 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 SettingsObserver(Handler handler) {
757 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700758 }
759
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800760 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700761 if (mRegistered && mUserId == userId) {
762 return;
763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700765 if (mRegistered) {
766 mContext.getContentResolver().unregisterContentObserver(this);
767 mRegistered = false;
768 }
769 if (mUserId != userId) {
770 mLastEnabled = "";
771 mUserId = userId;
772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700774 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900775 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700776 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900777 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700778 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700779 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700780 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800781 resolver.registerContentObserver(Settings.Secure.getUriFor(
782 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700783 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800785
Michael Wright7b5a96b2014-08-09 19:28:42 -0700786 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800787 final Uri showImeUri = Settings.Secure.getUriFor(
788 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
789 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
790 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700792 if (showImeUri.equals(uri)) {
793 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800794 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
795 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
796 mContext.getContentResolver(),
797 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
798 0, mUserId) == 1;
799 if (mAccessibilityRequestingNoSoftKeyboard) {
800 final boolean showRequested = mShowRequested;
801 hideCurrentInputLocked(0, null);
802 mShowRequested = showRequested;
803 } else if (mShowRequested) {
804 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
805 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700806 } else {
807 boolean enabledChanged = false;
808 String newEnabled = mSettings.getEnabledInputMethodsStr();
809 if (!mLastEnabled.equals(newEnabled)) {
810 mLastEnabled = newEnabled;
811 enabledChanged = true;
812 }
813 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700817
818 @Override
819 public String toString() {
820 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
821 + " mLastEnabled=" + mLastEnabled + "}";
822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800824
Yohei Yukawa79247822017-01-23 15:26:15 -0800825 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900826 @Override
827 public void onReceive(Context context, Intent intent) {
828 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700829 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900830 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700831 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900832 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800833 } else if (Intent.ACTION_USER_ADDED.equals(action)
834 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100835 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800836 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700837 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
838 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
839 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
840 final String prevValue = intent.getStringExtra(
841 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
842 final String newValue = intent.getStringExtra(
843 Intent.EXTRA_SETTING_NEW_VALUE);
844 restoreEnabledInputMethods(mContext, prevValue, newValue);
845 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800846 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800847 onActionLocaleChanged();
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900848 } else if (ACTION_SHOW_INPUT_METHOD_PICKER.equals(action)) {
849 // ACTION_SHOW_INPUT_METHOD_PICKER action is a protected-broadcast and it is
850 // guaranteed to be send only from the system, so that there is no need for extra
851 // security check such as
852 // {@link #canShowInputMethodPickerLocked(IInputMethodClient)}.
853 mHandler.obtainMessage(
854 MSG_SHOW_IM_SUBTYPE_PICKER,
855 InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES,
856 0 /* arg2 */)
857 .sendToTarget();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900858 } else {
859 Slog.w(TAG, "Unexpected intent " + intent);
860 }
861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800864 /**
865 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
866 *
867 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
868 * the users. We should ignore this event if this is about any background user's locale.</p>
869 *
870 * <p>Caution: This method must not be called when system is not ready.</p>
871 */
872 void onActionLocaleChanged() {
873 synchronized (mMethodMap) {
874 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
875 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
876 return;
877 }
878 buildInputMethodListLocked(true);
879 // If the locale is changed, needs to reset the default ime
880 resetDefaultImeLocked(mContext);
881 updateFromSettingsLocked(true);
882 mLastSystemLocales = possibleNewLocale;
883 }
884 }
885
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700886 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
887 // does not attempt to validate on the fly with any installed device policy, so must only
888 // be run in the context of initial device setup.
889 //
890 // TODO: Move this method to InputMethodUtils with adding unit tests.
891 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
892 if (DEBUG_RESTORE) {
893 Slog.i(TAG, "Restoring enabled input methods:");
894 Slog.i(TAG, "prev=" + prevValue);
895 Slog.i(TAG, " new=" + newValue);
896 }
897 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900898 ArrayMap<String, ArraySet<String>> prevMap =
899 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
900 ArrayMap<String, ArraySet<String>> newMap =
901 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700902
903 // Merge the restored ime+subtype enabled states into the live state
904 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
905 final String imeId = entry.getKey();
906 ArraySet<String> prevSubtypes = prevMap.get(imeId);
907 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700908 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700909 prevMap.put(imeId, prevSubtypes);
910 }
911 prevSubtypes.addAll(entry.getValue());
912 }
913
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700914 final String mergedImesAndSubtypesString =
915 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700916 if (DEBUG_RESTORE) {
917 Slog.i(TAG, "Merged IME string:");
918 Slog.i(TAG, " " + mergedImesAndSubtypesString);
919 }
920 Settings.Secure.putString(context.getContentResolver(),
921 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
922 }
923
Yohei Yukawac4e44912017-02-09 19:30:22 -0800924 final class MyPackageMonitor extends PackageMonitor {
925 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800926 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800927 *
928 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
929 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
930 * dynamically unless the entire package is updated, which also always triggers package
931 * rescanning.</p>
932 */
933 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800934 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
935
936 /**
937 * Packages that are appeared, disappeared, or modified for whatever reason.
938 *
939 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
940 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
941 * duplicate elements for our use case.</p>
942 *
943 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
944 * which should be bound to {@link #getRegisteredHandler()}.</p>
945 */
946 private final ArrayList<String> mChangedPackages = new ArrayList<>();
947
948 /**
949 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
950 *
951 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
952 * which should be bound to {@link #getRegisteredHandler()}.</p>
953 */
954 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800955
956 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800957 void clearKnownImePackageNamesLocked() {
958 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800959 }
960
961 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800962 final void addKnownImePackageNameLocked(@NonNull String packageName) {
963 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800964 }
965
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800966 @GuardedBy("mMethodMap")
967 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 final int userId = getChangingUserId();
969 final boolean retval = userId == mSettings.getCurrentUserId();
970 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900971 if (!retval) {
972 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
973 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900974 }
975 return retval;
976 }
977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800979 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800981 if (!isChangingPackagesOfCurrentUserLocked()) {
982 return false;
983 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 final int N = mMethodList.size();
986 if (curInputMethodId != null) {
987 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800988 InputMethodInfo imi = mMethodList.get(i);
989 if (imi.getId().equals(curInputMethodId)) {
990 for (String pkg : packages) {
991 if (imi.getPackageName().equals(pkg)) {
992 if (!doit) {
993 return true;
994 }
satok723a27e2010-11-11 14:58:11 +0900995 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800996 chooseNewDefaultIMELocked();
997 return true;
998 }
999 }
1000 }
1001 }
1002 }
1003 }
1004 return false;
1005 }
1006
1007 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001008 public void onBeginPackageChanges() {
1009 clearPackageChangeState();
1010 }
1011
1012 @Override
1013 public void onPackageAppeared(String packageName, int reason) {
1014 if (!mImePackageAppeared) {
1015 final PackageManager pm = mContext.getPackageManager();
1016 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
1017 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
1018 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
1019 // No need to lock this because we access it only on getRegisteredHandler().
1020 if (!services.isEmpty()) {
1021 mImePackageAppeared = true;
1022 }
1023 }
1024 // No need to lock this because we access it only on getRegisteredHandler().
1025 mChangedPackages.add(packageName);
1026 }
1027
1028 @Override
1029 public void onPackageDisappeared(String packageName, int reason) {
1030 // No need to lock this because we access it only on getRegisteredHandler().
1031 mChangedPackages.add(packageName);
1032 }
1033
1034 @Override
1035 public void onPackageModified(String packageName) {
1036 // No need to lock this because we access it only on getRegisteredHandler().
1037 mChangedPackages.add(packageName);
1038 }
1039
1040 @Override
1041 public void onPackagesSuspended(String[] packages) {
1042 // No need to lock this because we access it only on getRegisteredHandler().
1043 for (String packageName : packages) {
1044 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001045 }
1046 }
1047
1048 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001049 public void onPackagesUnsuspended(String[] packages) {
1050 // No need to lock this because we access it only on getRegisteredHandler().
1051 for (String packageName : packages) {
1052 mChangedPackages.add(packageName);
1053 }
1054 }
1055
1056 @Override
1057 public void onFinishPackageChanges() {
1058 onFinishPackageChangesInternal();
1059 clearPackageChangeState();
1060 }
1061
1062 private void clearPackageChangeState() {
1063 // No need to lock them because we access these fields only on getRegisteredHandler().
1064 mChangedPackages.clear();
1065 mImePackageAppeared = false;
1066 }
1067
1068 private boolean shouldRebuildInputMethodListLocked() {
1069 // This method is guaranteed to be called only by getRegisteredHandler().
1070
1071 // If there is any new package that contains at least one IME, then rebuilt the list
1072 // of IMEs.
1073 if (mImePackageAppeared) {
1074 return true;
1075 }
1076
1077 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1078 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1079 // is an option, but it may still do some extra operations that we do not need here.
1080 final int N = mChangedPackages.size();
1081 for (int i = 0; i < N; ++i) {
1082 final String packageName = mChangedPackages.get(i);
1083 if (mKnownImePackageNames.contains(packageName)) {
1084 return true;
1085 }
1086 }
1087 return false;
1088 }
1089
1090 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001091 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001092 if (!isChangingPackagesOfCurrentUserLocked()) {
1093 return;
1094 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001095 if (!shouldRebuildInputMethodListLocked()) {
1096 return;
1097 }
1098
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001099 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001100 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001101 final int N = mMethodList.size();
1102 if (curInputMethodId != null) {
1103 for (int i=0; i<N; i++) {
1104 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001105 final String imiId = imi.getId();
1106 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001107 curIm = imi;
1108 }
satoke7c6998e2011-06-03 17:57:59 +09001109
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001110 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001111 if (isPackageModified(imi.getPackageName())) {
1112 mFileManager.deleteAllInputMethodSubtypes(imiId);
1113 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001114 if (change == PACKAGE_TEMPORARY_CHANGE
1115 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001116 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001117 + imi.getComponent());
1118 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 }
1121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001122
Yohei Yukawa94e33302016-02-12 19:37:03 -08001123 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001126
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001127 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001128 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001129 if (change == PACKAGE_TEMPORARY_CHANGE
1130 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001131 ServiceInfo si = null;
1132 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001133 si = mIPackageManager.getServiceInfo(
1134 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1135 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001136 }
1137 if (si == null) {
1138 // Uh oh, current input method is no longer around!
1139 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001140 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001141 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001142 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001143 changed = true;
1144 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001145 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001146 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001147 }
1148 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001149 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001150 }
satokab751aa2010-09-14 19:17:36 +09001151
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001152 if (curIm == null) {
1153 // We currently don't have a default input method... is
1154 // one now available?
1155 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001156 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1157 // Even if the current input method is still available, mCurrentSubtype could
1158 // be obsolete when the package is modified in practice.
1159 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001162 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001163 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 }
1166 }
1167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
Jeff Brownc28867a2013-03-26 15:42:39 -07001169 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001170 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001171 private final IInputMethod mMethod;
1172 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001173
Jeff Brownc28867a2013-03-26 15:42:39 -07001174 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1175 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001176 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001177 mMethod = method;
1178 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180
satoke7c6998e2011-06-03 17:57:59 +09001181 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001182 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001183 long ident = Binder.clearCallingIdentity();
1184 try {
1185 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1186 } finally {
1187 Binder.restoreCallingIdentity(ident);
1188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
satok01038492012-04-09 21:08:27 +09001192 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001193 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001194 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001195 public void onHardKeyboardStatusChange(boolean available) {
1196 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1197 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001198 }
1199
Michael Wright7b5a96b2014-08-09 19:28:42 -07001200 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001201 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001202 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001203 }
1204 synchronized(mMethodMap) {
1205 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1206 && mSwitchingDialog.isShowing()) {
1207 mSwitchingDialogTitleView.findViewById(
1208 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1209 available ? View.VISIBLE : View.GONE);
1210 }
1211 }
1212 }
1213 }
1214
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001215 public static final class Lifecycle extends SystemService {
1216 private InputMethodManagerService mService;
1217
1218 public Lifecycle(Context context) {
1219 super(context);
1220 mService = new InputMethodManagerService(context);
1221 }
1222
1223 @Override
1224 public void onStart() {
1225 LocalServices.addService(InputMethodManagerInternal.class,
1226 new LocalServiceImpl(mService.mHandler));
1227 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1228 }
1229
1230 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001231 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001232 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001233 // TODO: Dispatch this to a worker thread as needed.
1234 mService.onSwitchUser(userHandle);
1235 }
1236
1237 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001238 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001239 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001240 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001241 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1242 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1243 .getService(Context.STATUS_BAR_SERVICE);
1244 mService.systemRunning(statusBarService);
1245 }
1246 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001247
1248 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001249 public void onUnlockUser(final @UserIdInt int userHandle) {
1250 // Called on ActivityManager thread.
1251 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001252 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001253 }
1254 }
1255
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001256 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001257 synchronized(mMethodMap) {
1258 final int currentUserId = mSettings.getCurrentUserId();
1259 if (DEBUG) {
1260 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1261 }
1262 if (userId != currentUserId) {
1263 return;
1264 }
1265 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001266 if (mSystemReady) {
1267 // We need to rebuild IMEs.
1268 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1269 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1270 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001271 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001272 }
1273
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001274 void onSwitchUser(@UserIdInt int userId) {
1275 synchronized (mMethodMap) {
1276 switchUserLocked(userId);
1277 }
1278 }
1279
Seigo Nonaka7309b122015-08-17 18:34:13 -07001280 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001281 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001283 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001285 // Note: SettingsObserver doesn't register observers in its constructor.
1286 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 mIWindowManager = IWindowManager.Stub.asInterface(
1288 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001289 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001290 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001291 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 public void executeMessage(Message msg) {
1293 handleMessage(msg);
1294 }
Mita Yuned218c72012-12-06 17:18:25 -08001295 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001296 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001297 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001298 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001299 mHasFeature = context.getPackageManager().hasSystemFeature(
1300 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001301 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001302 mHardKeyboardBehavior = mContext.getResources().getInteger(
1303 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001304
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001305 Bundle extras = new Bundle();
1306 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001307 @ColorInt final int accentColor = mContext.getColor(
1308 com.android.internal.R.color.system_notification_accent_color);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001309 mImeSwitcherNotification =
1310 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1311 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1312 .setWhen(0)
1313 .setOngoing(true)
1314 .addExtras(extras)
1315 .setCategory(Notification.CATEGORY_SYSTEM)
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001316 .setColor(accentColor);
Daniel Sandler590d5152012-06-14 16:10:13 -04001317
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001318 Intent intent = new Intent(ACTION_SHOW_INPUT_METHOD_PICKER)
1319 .setPackage(mContext.getPackageName());
satok683e2382011-07-12 08:28:52 +09001320 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001321
1322 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001323
satok7cfc0ed2011-06-20 21:29:36 +09001324 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001325 int userId = 0;
1326 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001327 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001328 } catch (RemoteException e) {
1329 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1330 }
satok913a8922010-08-26 21:53:41 +09001331
satokd87c2592010-09-29 11:52:06 +09001332 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001333 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001334 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001335
Kenny Guy2a764942014-04-02 13:29:20 +01001336 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001337 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001338 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1339 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001340 }
1341
satok5b927c432012-05-01 20:09:34 +09001342 private void resetDefaultImeLocked(Context context) {
1343 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001344 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001345 return;
1346 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001347 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001348 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001349 if (suitableImes.isEmpty()) {
1350 Slog.i(TAG, "No default found");
1351 return;
satok5b927c432012-05-01 20:09:34 +09001352 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001353 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001354 if (DEBUG) {
1355 Slog.i(TAG, "Default found, using " + defIm.getId());
1356 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001357 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001358 }
1359
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001360 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001361 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1362 + " currentUserId=" + mSettings.getCurrentUserId());
1363
Yohei Yukawa81482972015-06-04 00:58:59 -07001364 // ContentObserver should be registered again when the user is changed
1365 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001366
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001367 // If the system is not ready or the device is not yed unlocked by the user, then we use
1368 // copy-on-write settings.
1369 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001370 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001371 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001372 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001373 // InputMethodFileManager should be reset when the user is changed
1374 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001375 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001376
1377 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1378 + " defaultImiId=" + defaultImiId);
1379
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001380 // For secondary users, the list of enabled IMEs may not have been updated since the
1381 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1382 // not be empty even if the IME has been uninstalled by the primary user.
1383 // Even in such cases, IMMS works fine because it will find the most applicable
1384 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001385 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001386 mLastSystemLocales = mRes.getConfiguration().getLocales();
1387
1388 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1389 if (mSystemReady) {
1390 hideCurrentInputLocked(0, null);
1391 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1392 buildInputMethodListLocked(initialUserSwitch);
1393 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1394 // This is the first time of the user switch and
1395 // set the current ime to the proper one.
1396 resetDefaultImeLocked(mContext);
1397 }
1398 updateFromSettingsLocked(true);
1399 try {
1400 startInputInnerLocked();
1401 } catch (RuntimeException e) {
1402 Slog.w(TAG, "Unexpected exception", e);
1403 }
1404 }
1405
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001406 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001407 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1408 mSettings.getEnabledInputMethodListLocked(), newUserId,
1409 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001410 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001411
1412 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1413 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001414 }
1415
Kenny Guy2a764942014-04-02 13:29:20 +01001416 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001417 mSettings.setCurrentProfileIds(
1418 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001419 }
1420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 @Override
1422 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1423 throws RemoteException {
1424 try {
1425 return super.onTransact(code, data, reply, flags);
1426 } catch (RuntimeException e) {
1427 // The input method manager only throws security exceptions, so let's
1428 // log all others.
1429 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001430 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432 throw e;
1433 }
1434 }
1435
Svetoslav Ganova0027152013-06-25 14:59:53 -07001436 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001437 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001438 if (DEBUG) {
1439 Slog.d(TAG, "--- systemReady");
1440 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001441 if (!mSystemReady) {
1442 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001443 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001444 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001445 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001446 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001447 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1448 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001449 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001450 if (mStatusBar != null) {
1451 mStatusBar.setIconVisibility(mSlotIme, false);
1452 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001453 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001454 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1455 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001456 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001457 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001458 mHardKeyboardListener);
1459 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001460
1461 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1462 mSettingsObserver.registerContentObserverLocked(currentUserId);
1463
1464 final IntentFilter broadcastFilter = new IntentFilter();
1465 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1466 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1467 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1468 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1469 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001470 broadcastFilter.addAction(ACTION_SHOW_INPUT_METHOD_PICKER);
Yohei Yukawa79247822017-01-23 15:26:15 -08001471 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1472
Yohei Yukawa1f9a3cb2017-10-26 15:00:59 -07001473 final String defaultImiId = mSettings.getSelectedInputMethod();
1474 final boolean imeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
1475 buildInputMethodListLocked(!imeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa79247822017-01-23 15:26:15 -08001476 resetDefaultImeLocked(mContext);
1477 updateFromSettingsLocked(true);
1478 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1479 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1480 mContext.getBasePackageName());
1481
Dianne Hackborncc278702009-09-02 23:07:23 -07001482 try {
1483 startInputInnerLocked();
1484 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001485 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001486 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001487 }
1488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001491 // ---------------------------------------------------------------------------------------
1492 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1493 // 1) it comes from the system process
1494 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1495 private boolean calledFromValidUser() {
1496 final int uid = Binder.getCallingUid();
1497 final int userId = UserHandle.getUserId(uid);
1498 if (DEBUG) {
1499 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1500 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1501 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001502 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1503 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001504 }
Kenny Guy2a764942014-04-02 13:29:20 +01001505 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001506 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001507 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001508
1509 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1510 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1511 // must not manage background users' states in any functions.
1512 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1513 // by a token.
1514 if (mContext.checkCallingOrSelfPermission(
1515 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1516 == PackageManager.PERMISSION_GRANTED) {
1517 if (DEBUG) {
1518 Slog.d(TAG, "--- Access granted because the calling process has "
1519 + "the INTERACT_ACROSS_USERS_FULL permission");
1520 }
1521 return true;
1522 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001523 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001524 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1525 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001526 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001527 }
1528
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001529
1530 /**
1531 * Returns true iff the caller is identified to be the current input method with the token.
1532 * @param token The window token given to the input method when it was started.
1533 * @return true if and only if non-null valid token is specified.
1534 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001535 private boolean calledWithValidToken(@Nullable IBinder token) {
1536 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1537 if (DEBUG) {
1538 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1539 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1540 }
1541 return false;
1542 }
1543 if (token == null || token != mCurToken) {
1544 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1545 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1546 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001547 return false;
1548 }
1549 return true;
1550 }
1551
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001552 private boolean bindCurrentInputMethodService(
1553 Intent service, ServiceConnection conn, int flags) {
1554 if (service == null || conn == null) {
1555 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1556 return false;
1557 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001558 return mContext.bindServiceAsUser(service, conn, flags,
1559 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001560 }
1561
satoke7c6998e2011-06-03 17:57:59 +09001562 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001564 // TODO: Make this work even for non-current users?
1565 if (!calledFromValidUser()) {
1566 return Collections.emptyList();
1567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001569 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571 }
1572
satoke7c6998e2011-06-03 17:57:59 +09001573 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001575 // TODO: Make this work even for non-current users?
1576 if (!calledFromValidUser()) {
1577 return Collections.emptyList();
1578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001580 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582 }
1583
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001584 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001585 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001586 * @return enabled subtypes of the specified imi
1587 */
satoke7c6998e2011-06-03 17:57:59 +09001588 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001589 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001590 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001591 // TODO: Make this work even for non-current users?
1592 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001593 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001594 }
satok67ddf9c2010-11-17 09:45:54 +09001595 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001596 final InputMethodInfo imi;
1597 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001598 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001599 } else {
1600 imi = mMethodMap.get(imiId);
1601 }
1602 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001603 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001604 }
1605 return mSettings.getEnabledInputMethodSubtypeListLocked(
1606 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001607 }
1608 }
1609
satoke7c6998e2011-06-03 17:57:59 +09001610 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 public void addClient(IInputMethodClient client,
1612 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001613 if (!calledFromValidUser()) {
1614 return;
1615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 synchronized (mMethodMap) {
1617 mClients.put(client.asBinder(), new ClientState(client,
1618 inputContext, uid, pid));
1619 }
1620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001621
satoke7c6998e2011-06-03 17:57:59 +09001622 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001624 if (!calledFromValidUser()) {
1625 return;
1626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001628 ClientState cs = mClients.remove(client.asBinder());
1629 if (cs != null) {
1630 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001631 if (mCurClient == cs) {
1632 mCurClient = null;
1633 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001634 if (mCurFocusedWindowClient == cs) {
1635 mCurFocusedWindowClient = null;
1636 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
1639 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 void executeOrSendMessage(IInterface target, Message msg) {
1642 if (target.asBinder() instanceof Binder) {
1643 mCaller.sendMessage(msg);
1644 } else {
1645 handleMessage(msg);
1646 msg.recycle();
1647 }
1648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001649
Yohei Yukawa33e81792015-11-17 21:14:42 -08001650 void unbindCurrentClientLocked(
1651 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001653 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 + mCurClient.client.asBinder());
1655 if (mBoundToMethod) {
1656 mBoundToMethod = false;
1657 if (mCurMethod != null) {
1658 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1659 MSG_UNBIND_INPUT, mCurMethod));
1660 }
1661 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001662
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001663 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1664 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001665 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1666 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001669
The Android Open Source Project10592532009-03-18 17:39:46 -07001670 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
1672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 private int getImeShowFlags() {
1675 int flags = 0;
1676 if (mShowForced) {
1677 flags |= InputMethod.SHOW_FORCED
1678 | InputMethod.SHOW_EXPLICIT;
1679 } else if (mShowExplicitlyRequested) {
1680 flags |= InputMethod.SHOW_EXPLICIT;
1681 }
1682 return flags;
1683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 private int getAppShowFlags() {
1686 int flags = 0;
1687 if (mShowForced) {
1688 flags |= InputMethodManager.SHOW_FORCED;
1689 } else if (!mShowExplicitlyRequested) {
1690 flags |= InputMethodManager.SHOW_IMPLICIT;
1691 }
1692 return flags;
1693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001694
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001695 InputBindResult attachNewInputLocked(
1696 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 if (!mBoundToMethod) {
1698 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1699 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1700 mBoundToMethod = true;
1701 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001702
1703 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001704 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1705 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1706 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001707 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001708 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001711 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001712 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001713 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001715 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001716 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001718 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001719 (session.channel != null ? session.channel.dup() : null),
1720 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001722
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001723 InputBindResult startInputLocked(
1724 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001725 IInputMethodClient client, IInputContext inputContext,
1726 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001727 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 // If no method is currently selected, do nothing.
1729 if (mCurMethodId == null) {
1730 return mNoBinding;
1731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 ClientState cs = mClients.get(client.asBinder());
1734 if (cs == null) {
1735 throw new IllegalArgumentException("unknown client "
1736 + client.asBinder());
1737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001738
Yohei Yukawa74750f22016-03-22 12:54:22 -07001739 if (attribute == null) {
1740 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1741 + " uid=" + cs.uid + " pid=" + cs.pid);
1742 return null;
1743 }
1744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 try {
1746 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1747 // Check with the window manager to make sure this client actually
1748 // has a window with focus. If not, reject. This is thread safe
1749 // because if the focus changes some time before or after, the
1750 // next client receiving focus that has any interest in input will
1751 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001752 if (DEBUG) {
1753 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1754 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 return null;
1757 }
1758 } catch (RemoteException e) {
1759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001760
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001761 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001762 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001763 }
1764
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001765 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001766 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001767 @NonNull EditorInfo attribute, int controlFlags,
1768 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001769 // If no method is currently selected, do nothing.
1770 if (mCurMethodId == null) {
1771 return mNoBinding;
1772 }
1773
Yohei Yukawad57ba672015-06-08 16:39:46 -07001774 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1775 attribute.packageName)) {
1776 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1777 + " uid=" + cs.uid + " package=" + attribute.packageName);
1778 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001779 }
1780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001782 // Was the keyguard locked when switching over to the new client?
1783 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 // If the client is changing, we need to switch over to the new
1785 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001786 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001787 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001788 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789
1790 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001791 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001792 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001793 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 }
1795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 // Bump up the sequence for this client and attach it.
1798 mCurSeq++;
1799 if (mCurSeq <= 0) mCurSeq = 1;
1800 mCurClient = cs;
1801 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001802 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 // Check if the input method is changing.
1806 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1807 if (cs.curSession != null) {
1808 // Fast case: if we are already connected to the input method,
1809 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001810 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001811 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
1813 if (mHaveConnection) {
1814 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 // Return to client, and we will get back with it when
1816 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001817 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001818 return new InputBindResult(null, null, mCurId, mCurSeq,
1819 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 } else if (SystemClock.uptimeMillis()
1821 < (mLastBindTime+TIME_TO_RECONNECT)) {
1822 // In this case we have connected to the service, but
1823 // don't yet have its interface. If it hasn't been too
1824 // long since we did the connection, we'll return to
1825 // the client and wait to get the service interface so
1826 // we can report back. If it has been too long, we want
1827 // to fall through so we can try a disconnect/reconnect
1828 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001829 return new InputBindResult(null, null, mCurId, mCurSeq,
1830 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001832 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1833 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 }
1835 }
1836 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001837
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001838 return startInputInnerLocked();
1839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001840
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001841 InputBindResult startInputInnerLocked() {
1842 if (mCurMethodId == null) {
1843 return mNoBinding;
1844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001845
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001846 if (!mSystemReady) {
1847 // If the system is not yet ready, we shouldn't be running third
1848 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001849 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1850 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1854 if (info == null) {
1855 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001857
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001858 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1861 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001862 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1863 com.android.internal.R.string.input_method_binding_label);
1864 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1865 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001866 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 mLastBindTime = SystemClock.uptimeMillis();
1868 mHaveConnection = true;
1869 mCurId = info.getId();
1870 mCurToken = new Binder();
1871 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001872 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001873 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 } catch (RemoteException e) {
1875 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001876 return new InputBindResult(null, null, mCurId, mCurSeq,
1877 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 } else {
1879 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001880 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 + mCurIntent);
1882 }
1883 return null;
1884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001885
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001886 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001887 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001888 IInputMethodClient client, IInputContext inputContext,
1889 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001890 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001891 if (!calledFromValidUser()) {
1892 return null;
1893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001895 if (DEBUG) {
1896 Slog.v(TAG, "startInput: reason="
1897 + InputMethodClient.getStartInputReason(startInputReason)
1898 + " client = " + client.asBinder()
1899 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001900 + " missingMethods="
1901 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001902 + " attribute=" + attribute
1903 + " controlFlags=#" + Integer.toHexString(controlFlags));
1904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 final long ident = Binder.clearCallingIdentity();
1906 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001907 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1908 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 } finally {
1910 Binder.restoreCallingIdentity(ident);
1911 }
1912 }
1913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001914
satoke7c6998e2011-06-03 17:57:59 +09001915 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 public void finishInput(IInputMethodClient client) {
1917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001918
satoke7c6998e2011-06-03 17:57:59 +09001919 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 public void onServiceConnected(ComponentName name, IBinder service) {
1921 synchronized (mMethodMap) {
1922 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1923 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001924 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001925 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001926 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001927 return;
1928 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001929 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001930 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1931 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001933 clearClientSessionLocked(mCurClient);
1934 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
1936 }
1937 }
1938 }
1939
Jeff Brownc28867a2013-03-26 15:42:39 -07001940 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1941 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 synchronized (mMethodMap) {
1943 if (mCurMethod != null && method != null
1944 && mCurMethod.asBinder() == method.asBinder()) {
1945 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001946 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001948 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001949 InputBindResult res = attachNewInputLocked(
1950 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 if (res.method != null) {
1952 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001953 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001955 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
1957 }
1958 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001959
1960 // Session abandoned. Close its associated input channel.
1961 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001963
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001964 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001965 if (mVisibleBound) {
1966 mContext.unbindService(mVisibleConnection);
1967 mVisibleBound = false;
1968 }
1969
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001970 if (mHaveConnection) {
1971 mContext.unbindService(this);
1972 mHaveConnection = false;
1973 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001974
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001975 if (mCurToken != null) {
1976 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001977 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001978 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001979 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001980 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001981 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001982 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001983 } catch (RemoteException e) {
1984 }
1985 mCurToken = null;
1986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001987
The Android Open Source Project10592532009-03-18 17:39:46 -07001988 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001989 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001990 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001991
Yohei Yukawa33e81792015-11-17 21:14:42 -08001992 void resetCurrentMethodAndClient(
1993 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001994 mCurMethodId = null;
1995 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001996 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001997 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001998
1999 void requestClientSessionLocked(ClientState cs) {
2000 if (!cs.sessionRequested) {
2001 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
2002 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
2003 cs.sessionRequested = true;
2004 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
2005 MSG_CREATE_SESSION, mCurMethod, channels[1],
2006 new MethodCallback(this, mCurMethod, channels[0])));
2007 }
2008 }
2009
2010 void clearClientSessionLocked(ClientState cs) {
2011 finishSessionLocked(cs.curSession);
2012 cs.curSession = null;
2013 cs.sessionRequested = false;
2014 }
2015
2016 private void finishSessionLocked(SessionState sessionState) {
2017 if (sessionState != null) {
2018 if (sessionState.session != null) {
2019 try {
2020 sessionState.session.finishSession();
2021 } catch (RemoteException e) {
2022 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002023 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07002024 }
2025 sessionState.session = null;
2026 }
2027 if (sessionState.channel != null) {
2028 sessionState.channel.dispose();
2029 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002030 }
2031 }
2032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002033
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002034 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 if (mCurMethod != null) {
2036 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002037 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002039
Jeff Brownc28867a2013-03-26 15:42:39 -07002040 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002041 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 mCurMethod = null;
2043 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002044 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002045 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002046 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002047 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002049
satoke7c6998e2011-06-03 17:57:59 +09002050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002052 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2053 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2054 // which is irregular but can eventually happen for everyone just by continuing using the
2055 // device. Thus it is important to make sure that all the internal states are properly
2056 // refreshed when this method is called back. Running
2057 // adb install -r <APK that implements the current IME>
2058 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002060 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 + " mCurIntent=" + mCurIntent);
2062 if (mCurMethod != null && mCurIntent != null
2063 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002064 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 // We consider this to be a new bind attempt, since the system
2066 // should now try to restart the service for us.
2067 mLastBindTime = SystemClock.uptimeMillis();
2068 mShowRequested = mInputShown;
2069 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002070 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
2072 }
2073 }
2074
satokf9f01002011-05-19 21:31:50 +09002075 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002077 synchronized (mMethodMap) {
2078 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002079 return;
2080 }
2081 final long ident = Binder.clearCallingIdentity();
2082 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002084 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002085 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002086 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002089 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002090 CharSequence contentDescription = null;
2091 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002092 // Use PackageManager to load label
2093 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002094 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002095 mIPackageManager.getApplicationInfo(packageName, 0,
2096 mSettings.getCurrentUserId()));
2097 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002098 /* ignore */
2099 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002100 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002101 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002102 contentDescription != null
2103 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002104 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002107 } finally {
2108 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
2111 }
2112
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002113 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002114 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002115 if (mSwitchingDialog != null) return false;
Yohei Yukawad2bc3092017-07-31 15:37:14 -07002116 if (mWindowManagerInternal.isKeyguardShowingAndNotOccluded()
2117 && mKeyguardManager != null && mKeyguardManager.isKeyguardSecure()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002118 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002119 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002120 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2121 // When physical keyboard is attached, we show the ime switcher (or notification if
2122 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2123 // exists in the IME switcher dialog. Might be OK to remove this condition once
2124 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2125 return true;
2126 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002127 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2128 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002129 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002130
2131 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2132 final int N = imis.size();
2133 if (N > 2) return true;
2134 if (N < 1) return false;
2135 int nonAuxCount = 0;
2136 int auxCount = 0;
2137 InputMethodSubtype nonAuxSubtype = null;
2138 InputMethodSubtype auxSubtype = null;
2139 for(int i = 0; i < N; ++i) {
2140 final InputMethodInfo imi = imis.get(i);
2141 final List<InputMethodSubtype> subtypes =
2142 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2143 final int subtypeCount = subtypes.size();
2144 if (subtypeCount == 0) {
2145 ++nonAuxCount;
2146 } else {
2147 for (int j = 0; j < subtypeCount; ++j) {
2148 final InputMethodSubtype subtype = subtypes.get(j);
2149 if (!subtype.isAuxiliary()) {
2150 ++nonAuxCount;
2151 nonAuxSubtype = subtype;
2152 } else {
2153 ++auxCount;
2154 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002155 }
2156 }
satok7cfc0ed2011-06-20 21:29:36 +09002157 }
2158 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002159 if (nonAuxCount > 1 || auxCount > 1) {
2160 return true;
2161 } else if (nonAuxCount == 1 && auxCount == 1) {
2162 if (nonAuxSubtype != null && auxSubtype != null
2163 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2164 || auxSubtype.overridesImplicitlyEnabledSubtype()
2165 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2166 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2167 return false;
2168 }
2169 return true;
2170 }
2171 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002172 }
2173
John Spurlocke0980502013-10-25 11:59:29 -04002174 private boolean isKeyguardLocked() {
2175 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2176 }
2177
Yohei Yukawad6475a62017-04-17 10:35:27 -07002178 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002179 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002180 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002181 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2182 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002183 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002184 return;
2185 }
2186
Yohei Yukawa69e68022017-02-13 12:04:50 -08002187 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002188 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002189 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002190 mImeWindowVis = vis;
2191 mBackDisposition = backDisposition;
2192 updateSystemUiLocked(token, vis, backDisposition);
2193 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002194
2195 final boolean dismissImeOnBackKeyPressed;
2196 switch (backDisposition) {
2197 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2198 dismissImeOnBackKeyPressed = true;
2199 break;
2200 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2201 dismissImeOnBackKeyPressed = false;
2202 break;
2203 default:
2204 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2205 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2206 break;
2207 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002208 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2209 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002210 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002211 }
2212
2213 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2214 synchronized (mMethodMap) {
2215 updateSystemUiLocked(token, vis, backDisposition);
2216 }
2217 }
2218
2219 // Caution! This method is called in this class. Handle multi-user carefully
2220 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2221 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002222 return;
2223 }
2224
2225 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2226 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002227 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002228 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002229 // apply policy for binder calls
2230 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2231 vis = 0;
satok06487a52010-10-29 11:37:18 +09002232 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002233 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2234 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2235 if (mStatusBar != null) {
2236 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2237 needsToShowImeSwitcher);
2238 }
2239 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2240 if (imi != null && needsToShowImeSwitcher) {
2241 // Used to load label
2242 final CharSequence title = mRes.getText(
2243 com.android.internal.R.string.select_input_method);
2244 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2245 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002246 mImeSwitcherNotification.setContentTitle(title)
2247 .setContentText(summary)
2248 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002249 try {
2250 if ((mNotificationManager != null)
2251 && !mIWindowManager.hasNavigationBar()) {
2252 if (DEBUG) {
2253 Slog.d(TAG, "--- show notification: label = " + summary);
2254 }
2255 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002256 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002257 mImeSwitcherNotification.build(), UserHandle.ALL);
2258 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002259 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002260 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002261 }
2262 } else {
2263 if (mNotificationShown && mNotificationManager != null) {
2264 if (DEBUG) {
2265 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002266 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002267 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002268 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002269 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002270 }
satok06487a52010-10-29 11:37:18 +09002271 }
2272 } finally {
2273 Binder.restoreCallingIdentity(ident);
2274 }
2275 }
2276
satoke7c6998e2011-06-03 17:57:59 +09002277 @Override
satokf9f01002011-05-19 21:31:50 +09002278 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002279 if (!calledFromValidUser()) {
2280 return;
2281 }
satokf9f01002011-05-19 21:31:50 +09002282 synchronized (mMethodMap) {
2283 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2284 for (int i = 0; i < spans.length; ++i) {
2285 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002286 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002287 mSecureSuggestionSpans.put(ss, currentImi);
2288 }
2289 }
2290 }
2291 }
2292
satoke7c6998e2011-06-03 17:57:59 +09002293 @Override
satokf9f01002011-05-19 21:31:50 +09002294 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002295 if (!calledFromValidUser()) {
2296 return false;
2297 }
satokf9f01002011-05-19 21:31:50 +09002298 synchronized (mMethodMap) {
2299 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2300 // TODO: Do not send the intent if the process of the targetImi is already dead.
2301 if (targetImi != null) {
2302 final String[] suggestions = span.getSuggestions();
2303 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002304 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002305 final Intent intent = new Intent();
2306 // Ensures that only a class in the original IME package will receive the
2307 // notification.
satok42c5a162011-05-26 16:46:14 +09002308 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002309 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2310 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2311 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2312 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002313 final long ident = Binder.clearCallingIdentity();
2314 try {
2315 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2316 } finally {
2317 Binder.restoreCallingIdentity(ident);
2318 }
satokf9f01002011-05-19 21:31:50 +09002319 return true;
2320 }
2321 }
2322 return false;
2323 }
2324
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002325 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002326 updateInputMethodsFromSettingsLocked(enabledMayChange);
2327 updateKeyboardFromSettingsLocked();
2328 }
2329
2330 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002331 if (enabledMayChange) {
2332 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2333 for (int i=0; i<enabled.size(); i++) {
2334 // We allow the user to select "disabled until used" apps, so if they
2335 // are enabling one of those here we now need to make it enabled.
2336 InputMethodInfo imm = enabled.get(i);
2337 try {
2338 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2339 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2340 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002341 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002342 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002343 if (DEBUG) {
2344 Slog.d(TAG, "Update state(" + imm.getId()
2345 + "): DISABLED_UNTIL_USED -> DEFAULT");
2346 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002347 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2348 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002349 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2350 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002351 }
2352 } catch (RemoteException e) {
2353 }
2354 }
2355 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002356 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2357 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2358 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2359 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002360 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002361 // There is no input method selected, try to choose new applicable input method.
2362 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002363 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002364 }
2365 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002367 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002369 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002370 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
satokf3db1af2010-11-23 13:34:33 +09002372 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002373 } else {
2374 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002375 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002377 // Here is not the perfect place to reset the switching controller. Ideally
2378 // mSwitchingController and mSettings should be able to share the same state.
2379 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2380 // the same enabled IMEs list.
2381 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002382
2383 }
2384
2385 public void updateKeyboardFromSettingsLocked() {
2386 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2387 if (mSwitchingDialog != null
2388 && mSwitchingDialogTitleView != null
2389 && mSwitchingDialog.isShowing()) {
2390 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2391 com.android.internal.R.id.hard_keyboard_switch);
2392 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002395
Yohei Yukawab097b822015-12-01 10:43:08 -08002396 private void notifyInputMethodSubtypeChanged(final int userId,
2397 @Nullable final InputMethodInfo inputMethodInfo,
2398 @Nullable final InputMethodSubtype subtype) {
2399 final InputManagerInternal inputManagerInternal =
2400 LocalServices.getService(InputManagerInternal.class);
2401 if (inputManagerInternal != null) {
2402 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2403 }
2404 }
2405
satokab751aa2010-09-14 19:17:36 +09002406 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 InputMethodInfo info = mMethodMap.get(id);
2408 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002409 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002411
satokd81e9502012-05-21 12:58:45 +09002412 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002414 final int subtypeCount = info.getSubtypeCount();
2415 if (subtypeCount <= 0) {
2416 return;
satokcd7cd292010-11-20 15:46:23 +09002417 }
satokd81e9502012-05-21 12:58:45 +09002418 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2419 final InputMethodSubtype newSubtype;
2420 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2421 newSubtype = info.getSubtypeAt(subtypeId);
2422 } else {
2423 // If subtype is null, try to find the most applicable one from
2424 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002425 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002426 }
2427 if (newSubtype == null || oldSubtype == null) {
2428 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2429 + ", new subtype = " + newSubtype);
2430 return;
2431 }
2432 if (newSubtype != oldSubtype) {
2433 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2434 if (mCurMethod != null) {
2435 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002436 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002437 mCurMethod.changeInputMethodSubtype(newSubtype);
2438 } catch (RemoteException e) {
2439 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002440 return;
satokab751aa2010-09-14 19:17:36 +09002441 }
2442 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002443 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 return;
2446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002447
satokd81e9502012-05-21 12:58:45 +09002448 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 final long ident = Binder.clearCallingIdentity();
2450 try {
satokab751aa2010-09-14 19:17:36 +09002451 // Set a subtype to this input method.
2452 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002453 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2454 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2455 // because mCurMethodId is stored as a history in
2456 // setSelectedInputMethodAndSubtypeLocked().
2457 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002459 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002461 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002463 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002465 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 } finally {
2467 Binder.restoreCallingIdentity(ident);
2468 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002469
2470 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2471 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002473
satok42c5a162011-05-26 16:46:14 +09002474 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002475 public boolean showSoftInput(IInputMethodClient client, int flags,
2476 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002477 if (!calledFromValidUser()) {
2478 return false;
2479 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002480 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 long ident = Binder.clearCallingIdentity();
2482 try {
2483 synchronized (mMethodMap) {
2484 if (mCurClient == null || client == null
2485 || mCurClient.client.asBinder() != client.asBinder()) {
2486 try {
2487 // We need to check if this is the current client with
2488 // focus in the window manager, to allow this call to
2489 // be made before input is started in it.
2490 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002491 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002492 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
2494 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002495 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 }
2497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002498
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002499 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002500 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 }
2502 } finally {
2503 Binder.restoreCallingIdentity(ident);
2504 }
2505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002506
The Android Open Source Project4df24232009-03-05 14:34:35 -08002507 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002509 if (mAccessibilityRequestingNoSoftKeyboard) {
2510 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 }
Anna Galusza9b278112016-01-04 11:37:37 -08002512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2514 mShowExplicitlyRequested = true;
2515 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002516 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2517 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002519
Dianne Hackborncc278702009-09-02 23:07:23 -07002520 if (!mSystemReady) {
2521 return false;
2522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002523
The Android Open Source Project4df24232009-03-05 14:34:35 -08002524 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002526 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002527 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2528 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2529 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002531 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002532 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002533 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002534 mVisibleBound = true;
2535 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002536 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002538 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 // The client has asked to have the input method shown, but
2540 // we have been sitting here too long with a connection to the
2541 // service and no interface received, so let's disconnect/connect
2542 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002543 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002545 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002547 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002548 } else {
2549 if (DEBUG) {
2550 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2551 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002554
The Android Open Source Project4df24232009-03-05 14:34:35 -08002555 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002557
satok42c5a162011-05-26 16:46:14 +09002558 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002559 public boolean hideSoftInput(IInputMethodClient client, int flags,
2560 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002561 if (!calledFromValidUser()) {
2562 return false;
2563 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002564 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 long ident = Binder.clearCallingIdentity();
2566 try {
2567 synchronized (mMethodMap) {
2568 if (mCurClient == null || client == null
2569 || mCurClient.client.asBinder() != client.asBinder()) {
2570 try {
2571 // We need to check if this is the current client with
2572 // focus in the window manager, to allow this call to
2573 // be made before input is started in it.
2574 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002575 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2576 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002577 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002580 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
2582 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002583
Joe Onorato8a9b2202010-02-26 18:56:32 -08002584 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002585 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 }
2587 } finally {
2588 Binder.restoreCallingIdentity(ident);
2589 }
2590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002591
The Android Open Source Project4df24232009-03-05 14:34:35 -08002592 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2594 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002595 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 -08002596 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 }
2598 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002599 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 -08002600 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002602
2603 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2604 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2605 // to be updated with the new value sent from IME process. Even in such a transient state
2606 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2607 // application process as a valid request, and have even promised such a behavior with CTS
2608 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2609 // IMMS#InputShown indicates that the software keyboard is shown.
2610 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2611 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2612 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002613 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002614 if (shouldHideSoftInput) {
2615 // The IME will report its visible state again after the following message finally
2616 // delivered to the IME process as an IPC. Hence the inconsistency between
2617 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2618 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002619 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2620 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2621 res = true;
2622 } else {
2623 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002625 if (mHaveConnection && mVisibleBound) {
2626 mContext.unbindService(mVisibleConnection);
2627 mVisibleBound = false;
2628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 mInputShown = false;
2630 mShowRequested = false;
2631 mShowExplicitlyRequested = false;
2632 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002633 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002635
satok42c5a162011-05-26 16:46:14 +09002636 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002637 public InputBindResult startInputOrWindowGainedFocus(
2638 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2639 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002640 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002641 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002642 if (windowToken != null) {
2643 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002644 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002645 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002646 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2647 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002648 }
2649 }
2650
2651 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002652 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002653 IInputMethodClient client, IBinder windowToken, int controlFlags,
2654 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002655 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2656 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002657 // Needs to check the validity before clearing calling identity
2658 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002659 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 long ident = Binder.clearCallingIdentity();
2661 try {
2662 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002663 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2664 + InputMethodClient.getStartInputReason(startInputReason)
2665 + " client=" + client.asBinder()
2666 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002667 + " missingMethods="
2668 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002669 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002670 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002671 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002672 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002673
Dianne Hackborn7663d802012-02-24 13:08:49 -08002674 ClientState cs = mClients.get(client.asBinder());
2675 if (cs == null) {
2676 throw new IllegalArgumentException("unknown client "
2677 + client.asBinder());
2678 }
2679
2680 try {
2681 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2682 // Check with the window manager to make sure this client actually
2683 // has a window with focus. If not, reject. This is thread safe
2684 // because if the focus changes some time before or after, the
2685 // next client receiving focus that has any interest in input will
2686 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002687 if (DEBUG) {
2688 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2689 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2690 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002691 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002693 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002695
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002696 if (!calledFromValidUser) {
2697 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2698 Slog.w(TAG, "If you want to interect with IME, you need "
2699 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2700 hideCurrentInputLocked(0, null);
2701 return null;
2702 }
2703
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002704 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002705 if (DEBUG) {
2706 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2707 + " attribute=" + attribute + ", token = " + windowToken);
2708 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002709 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002710 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002711 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002712 }
2713 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002714 }
2715 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002716 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002717 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002718
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002719 // Should we auto-show the IME even if the caller has not
2720 // specified what should be done with it?
2721 // We only do this automatically if the window can resize
2722 // to accommodate the IME (so what the user sees will give
2723 // them good context without input information being obscured
2724 // by the IME) or if running on a large screen where there
2725 // is more room for the target window + IME.
2726 final boolean doAutoShow =
2727 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2728 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2729 || mRes.getConfiguration().isLayoutSizeAtLeast(
2730 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002731 final boolean isTextEditor =
2732 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2733
2734 // We want to start input before showing the IME, but after closing
2735 // it. We want to do this after closing it to help the IME disappear
2736 // more quickly (not get stuck behind it initializing itself for the
2737 // new focused input, even if its window wants to hide the IME).
2738 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2741 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002742 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2744 // There is no focus view, and this window will
2745 // be behind any soft input window, so hide the
2746 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002747 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002748 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002750 } else if (isTextEditor && doAutoShow && (softInputMode &
2751 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 // There is a focus view, and we are navigating forward
2753 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002754 // We only do this automatically if the window can resize
2755 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002756 // them good context without input information being obscured
2757 // by the IME) or if running on a large screen where there
2758 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002759 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002760 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002761 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002762 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002763 didStart = true;
2764 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002765 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 }
2767 break;
2768 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2769 // Do nothing.
2770 break;
2771 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2772 if ((softInputMode &
2773 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002774 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002775 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 }
2777 break;
2778 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002779 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002780 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 break;
2782 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2783 if ((softInputMode &
2784 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002785 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002786 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002787 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002788 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002789 didStart = true;
2790 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002791 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 }
2793 break;
2794 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002795 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002796 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002797 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002798 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002799 didStart = true;
2800 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002801 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 break;
2803 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002804
2805 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002806 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002807 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 }
2810 } finally {
2811 Binder.restoreCallingIdentity(ident);
2812 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002813
2814 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002816
Guliz Tuncay6908c152017-06-02 16:06:10 -07002817 private boolean canShowInputMethodPickerLocked(IInputMethodClient client) {
2818 final int uid = Binder.getCallingUid();
2819 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
2820 return true;
2821 } else if (mCurClient != null && client != null
2822 && mCurClient.client.asBinder() == client.asBinder()) {
2823 return true;
2824 } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid(
2825 mAppOpsManager,
2826 uid,
2827 mCurIntent.getComponent().getPackageName())) {
2828 return true;
2829 } else if (mContext.checkCallingPermission(
2830 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2831 == PackageManager.PERMISSION_GRANTED) {
2832 return true;
2833 }
2834
2835 return false;
2836 }
2837
satok42c5a162011-05-26 16:46:14 +09002838 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002839 public void showInputMethodPickerFromClient(
2840 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002841 if (!calledFromValidUser()) {
2842 return;
2843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 synchronized (mMethodMap) {
Guliz Tuncay6908c152017-06-02 16:06:10 -07002845 if(!canShowInputMethodPickerLocked(client)) {
satok47a44912010-10-06 16:03:58 +09002846 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002847 + Binder.getCallingUid() + ": " + client);
Guliz Tuncay6908c152017-06-02 16:06:10 -07002848 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 }
2850
satok440aab52010-11-25 09:43:11 +09002851 // Always call subtype picker, because subtype picker is a superset of input method
2852 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002853 mHandler.sendMessage(mCaller.obtainMessageI(
2854 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002855 }
2856 }
2857
satok42c5a162011-05-26 16:46:14 +09002858 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002860 if (!calledFromValidUser()) {
2861 return;
2862 }
satok28203512010-11-24 11:06:49 +09002863 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2864 }
2865
satok42c5a162011-05-26 16:46:14 +09002866 @Override
satok28203512010-11-24 11:06:49 +09002867 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002868 if (!calledFromValidUser()) {
2869 return;
2870 }
satok28203512010-11-24 11:06:49 +09002871 synchronized (mMethodMap) {
2872 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002873 setInputMethodWithSubtypeIdLocked(token, id,
2874 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2875 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002876 } else {
2877 setInputMethod(token, id);
2878 }
2879 }
satokab751aa2010-09-14 19:17:36 +09002880 }
2881
satok42c5a162011-05-26 16:46:14 +09002882 @Override
satokb416a712010-11-25 20:42:14 +09002883 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002884 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002885 if (!calledFromValidUser()) {
2886 return;
2887 }
satokb416a712010-11-25 20:42:14 +09002888 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002889 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2890 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002891 }
2892 }
2893
satok4fc87d62011-05-20 16:13:43 +09002894 @Override
satok735cf382010-11-11 20:40:09 +09002895 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002896 if (!calledFromValidUser()) {
2897 return false;
2898 }
satok735cf382010-11-11 20:40:09 +09002899 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002900 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002901 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002902 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002903 lastImi = mMethodMap.get(lastIme.first);
2904 } else {
2905 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002906 }
satok4fc87d62011-05-20 16:13:43 +09002907 String targetLastImiId = null;
2908 int subtypeId = NOT_A_SUBTYPE_ID;
2909 if (lastIme != null && lastImi != null) {
2910 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002911 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002912 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2913 : mCurrentSubtype.hashCode();
2914 // If the last IME is the same as the current IME and the last subtype is not
2915 // defined, there is no need to switch to the last IME.
2916 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2917 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002918 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002919 }
2920 }
2921
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002922 if (TextUtils.isEmpty(targetLastImiId)
2923 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002924 // This is a safety net. If the currentSubtype can't be added to the history
2925 // and the framework couldn't find the last ime, we will make the last ime be
2926 // the most applicable enabled keyboard subtype of the system imes.
2927 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2928 if (enabled != null) {
2929 final int N = enabled.size();
2930 final String locale = mCurrentSubtype == null
2931 ? mRes.getConfiguration().locale.toString()
2932 : mCurrentSubtype.getLocale();
2933 for (int i = 0; i < N; ++i) {
2934 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002935 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002936 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002937 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2938 InputMethodUtils.getSubtypes(imi),
2939 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002940 if (keyboardSubtype != null) {
2941 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002942 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002943 imi, keyboardSubtype.hashCode());
2944 if(keyboardSubtype.getLocale().equals(locale)) {
2945 break;
2946 }
2947 }
2948 }
2949 }
2950 }
2951 }
2952
2953 if (!TextUtils.isEmpty(targetLastImiId)) {
2954 if (DEBUG) {
2955 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2956 + ", from: " + mCurMethodId + ", " + subtypeId);
2957 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002958 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002959 return true;
2960 } else {
2961 return false;
2962 }
satok735cf382010-11-11 20:40:09 +09002963 }
2964 }
2965
satoke7c6998e2011-06-03 17:57:59 +09002966 @Override
satok688bd472012-02-09 20:09:17 +09002967 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002968 if (!calledFromValidUser()) {
2969 return false;
2970 }
satok688bd472012-02-09 20:09:17 +09002971 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002972 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002973 return false;
2974 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002975 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002976 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2977 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002978 if (nextSubtype == null) {
2979 return false;
2980 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002981 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2982 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002983 return true;
2984 }
2985 }
2986
2987 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002988 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2989 if (!calledFromValidUser()) {
2990 return false;
2991 }
2992 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002993 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002994 return false;
2995 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002996 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002997 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2998 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002999 if (nextSubtype == null) {
3000 return false;
3001 }
3002 return true;
3003 }
3004 }
3005
3006 @Override
satok68f1b782011-04-11 14:26:04 +09003007 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003008 if (!calledFromValidUser()) {
3009 return null;
3010 }
satok68f1b782011-04-11 14:26:04 +09003011 synchronized (mMethodMap) {
3012 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
3013 // TODO: Handle the case of the last IME with no subtypes
3014 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
3015 || TextUtils.isEmpty(lastIme.second)) return null;
3016 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
3017 if (lastImi == null) return null;
3018 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003019 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003020 final int lastSubtypeId =
3021 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09003022 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
3023 return null;
3024 }
3025 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09003026 } catch (NumberFormatException e) {
3027 return null;
3028 }
3029 }
3030 }
3031
satoke7c6998e2011-06-03 17:57:59 +09003032 @Override
satokee5e77c2011-09-02 18:50:15 +09003033 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003034 if (!calledFromValidUser()) {
3035 return;
3036 }
satok91e88122011-07-18 11:11:42 +09003037 // By this IPC call, only a process which shares the same uid with the IME can add
3038 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08003039 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09003040 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08003041 if (!mSystemReady) {
3042 return;
3043 }
satok91e88122011-07-18 11:11:42 +09003044 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09003045 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003046 final String[] packageInfos;
3047 try {
3048 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3049 } catch (RemoteException e) {
3050 Slog.e(TAG, "Failed to get package infos");
3051 return;
3052 }
satok91e88122011-07-18 11:11:42 +09003053 if (packageInfos != null) {
3054 final int packageNum = packageInfos.length;
3055 for (int i = 0; i < packageNum; ++i) {
3056 if (packageInfos[i].equals(imi.getPackageName())) {
3057 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003058 final long ident = Binder.clearCallingIdentity();
3059 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003060 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003061 } finally {
3062 Binder.restoreCallingIdentity(ident);
3063 }
satokee5e77c2011-09-02 18:50:15 +09003064 return;
satok91e88122011-07-18 11:11:42 +09003065 }
3066 }
3067 }
satoke7c6998e2011-06-03 17:57:59 +09003068 }
satokee5e77c2011-09-02 18:50:15 +09003069 return;
satoke7c6998e2011-06-03 17:57:59 +09003070 }
3071
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003072 @Override
3073 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003074 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003075 }
3076
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003077 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003078 public void clearLastInputMethodWindowForTransition(IBinder token) {
3079 if (!calledFromValidUser()) {
3080 return;
3081 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003082 synchronized (mMethodMap) {
3083 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003084 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003085 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003086 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003087 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003088 }
3089
3090 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003091 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003092 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003093 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003094 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003095 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003096 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3097 if (DEBUG) {
3098 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3099 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3100 + " actual: " + sequenceNumber);
3101 }
3102 return;
3103 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003104 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3105 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003106 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003107 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003108 }
3109 }
3110
satok28203512010-11-24 11:06:49 +09003111 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003113 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3114 }
3115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003117 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3118 if (token == null) {
3119 if (mContext.checkCallingOrSelfPermission(
3120 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3121 != PackageManager.PERMISSION_GRANTED) {
3122 throw new SecurityException(
3123 "Using null token requires permission "
3124 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003126 } else if (mCurToken != token) {
3127 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3128 + " token: " + token);
3129 return;
3130 }
3131
3132 final long ident = Binder.clearCallingIdentity();
3133 try {
3134 setInputMethodLocked(id, subtypeId);
3135 } finally {
3136 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 }
3138 }
3139
satok42c5a162011-05-26 16:46:14 +09003140 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003142 if (!calledFromValidUser()) {
3143 return;
3144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003146 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 return;
3148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 long ident = Binder.clearCallingIdentity();
3150 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003151 hideCurrentInputLocked(flags, null);
3152 } finally {
3153 Binder.restoreCallingIdentity(ident);
3154 }
3155 }
3156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003157
satok42c5a162011-05-26 16:46:14 +09003158 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003159 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003160 if (!calledFromValidUser()) {
3161 return;
3162 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003163 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003164 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003165 return;
3166 }
3167 long ident = Binder.clearCallingIdentity();
3168 try {
3169 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 } finally {
3171 Binder.restoreCallingIdentity(ident);
3172 }
3173 }
3174 }
3175
3176 void setEnabledSessionInMainThread(SessionState session) {
3177 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003178 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003180 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003181 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 } catch (RemoteException e) {
3183 }
3184 }
3185 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003186 if (mEnabledSession != null && mEnabledSession.session != null) {
3187 try {
3188 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3189 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3190 } catch (RemoteException e) {
3191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 }
3193 }
3194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003195
Yohei Yukawa930328c2017-10-18 20:19:53 -07003196 @MainThread
satok42c5a162011-05-26 16:46:14 +09003197 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003199 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003201 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003202 final boolean showAuxSubtypes;
3203 switch (msg.arg1) {
3204 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3205 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3206 // implemented so that auxiliary subtypes will be excluded when the soft
3207 // keyboard is invisible.
3208 showAuxSubtypes = mInputShown;
3209 break;
3210 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3211 showAuxSubtypes = true;
3212 break;
3213 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3214 showAuxSubtypes = false;
3215 break;
3216 default:
3217 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3218 return false;
3219 }
3220 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003221 return true;
3222
satok47a44912010-10-06 16:03:58 +09003223 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003224 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003225 return true;
3226
3227 case MSG_SHOW_IM_CONFIG:
3228 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003229 return true;
3230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 case MSG_UNBIND_INPUT:
3234 try {
3235 ((IInputMethod)msg.obj).unbindInput();
3236 } catch (RemoteException e) {
3237 // There is nothing interesting about the method dying.
3238 }
3239 return true;
3240 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003241 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 try {
3243 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3244 } catch (RemoteException e) {
3245 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003246 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 return true;
3248 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003249 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003251 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003252 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003253 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 } catch (RemoteException e) {
3255 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003256 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 return true;
3258 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003259 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003261 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003262 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003263 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 } catch (RemoteException e) {
3265 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003266 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003268 case MSG_HIDE_CURRENT_INPUT_METHOD:
3269 synchronized (mMethodMap) {
3270 hideCurrentInputLocked(0, null);
3271 }
3272 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003274 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003276 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3278 } catch (RemoteException e) {
3279 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003280 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003282 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003283 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003284 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003285 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003287 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003289 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003290 // Dispose the channel if the input method is not local to this process
3291 // because the remote proxy will get its own copy when unparceled.
3292 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003293 channel.dispose();
3294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003296 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003300
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003301 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003302 final int missingMethods = msg.arg1;
3303 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003304 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003305 final IBinder startInputToken = (IBinder) args.arg1;
3306 final SessionState session = (SessionState) args.arg2;
3307 final IInputContext inputContext = (IInputContext) args.arg3;
3308 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003311 session.method.startInput(startInputToken, inputContext, missingMethods,
3312 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 } catch (RemoteException e) {
3314 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003315 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003320
Yohei Yukawa33e81792015-11-17 21:14:42 -08003321 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003323 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 } catch (RemoteException e) {
3325 // There is nothing interesting about the last client dying.
3326 }
3327 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003328 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003329 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003330 IInputMethodClient client = (IInputMethodClient)args.arg1;
3331 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003333 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003335 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003336 } finally {
3337 // Dispose the channel if the input method is not local to this process
3338 // because the remote proxy will get its own copy when unparceled.
3339 if (res.channel != null && Binder.isProxy(client)) {
3340 res.channel.dispose();
3341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003343 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003345 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003346 case MSG_SET_ACTIVE:
3347 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003348 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003349 } catch (RemoteException e) {
3350 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3351 + ((ClientState)msg.obj).pid + " uid "
3352 + ((ClientState)msg.obj).uid);
3353 }
3354 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003355 case MSG_SET_INTERACTIVE:
3356 handleSetInteractive(msg.arg1 != 0);
3357 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003358 case MSG_SWITCH_IME:
3359 handleSwitchInputMethod(msg.arg1 != 0);
3360 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003361 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3362 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003363 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003364 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003365 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003366 } catch (RemoteException e) {
3367 Slog.w(TAG, "Got RemoteException sending "
3368 + "setUserActionNotificationSequenceNumber("
3369 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003370 + clientState.pid + " uid "
3371 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003372 }
3373 return true;
3374 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003375 case MSG_REPORT_FULLSCREEN_MODE: {
3376 final boolean fullscreen = msg.arg1 != 0;
3377 final ClientState clientState = (ClientState)msg.obj;
3378 try {
3379 clientState.client.reportFullscreenMode(fullscreen);
3380 } catch (RemoteException e) {
3381 Slog.w(TAG, "Got RemoteException sending "
3382 + "reportFullscreen(" + fullscreen + ") notification to pid="
3383 + clientState.pid + " uid=" + clientState.uid);
3384 }
3385 return true;
3386 }
satok01038492012-04-09 21:08:27 +09003387
3388 // --------------------------------------------------------------
3389 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003390 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003391 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003392 case MSG_SYSTEM_UNLOCK_USER:
3393 final int userId = msg.arg1;
3394 onUnlockUser(userId);
3395 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 }
3397 return false;
3398 }
3399
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003400 private void handleSetInteractive(final boolean interactive) {
3401 synchronized (mMethodMap) {
3402 mIsInteractive = interactive;
3403 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3404
3405 // Inform the current client of the change in active status
3406 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003407 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3408 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3409 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003410 }
3411 }
3412 }
3413
Yohei Yukawaae61f712015-12-09 13:00:10 -08003414 private void handleSwitchInputMethod(final boolean forwardDirection) {
3415 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003416 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003417 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003418 if (nextSubtype == null) {
3419 return;
3420 }
3421 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003422 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3423 if (newInputMethodInfo == null) {
3424 return;
3425 }
3426 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3427 newInputMethodInfo, mCurrentSubtype);
3428 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003429 if (mSubtypeSwitchedByShortCutToast == null) {
3430 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3431 Toast.LENGTH_SHORT);
3432 } else {
3433 mSubtypeSwitchedByShortCutToast.setText(toastText);
3434 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003435 mSubtypeSwitchedByShortCutToast.show();
3436 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003437 }
3438 }
3439
satokdc9ddae2011-10-06 12:22:36 +09003440 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003441 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3442 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003443 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003444 if (DEBUG) {
3445 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3446 }
satok723a27e2010-11-11 14:58:11 +09003447 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003448 return true;
3449 }
3450
3451 return false;
3452 }
3453
Yohei Yukawa94e33302016-02-12 19:37:03 -08003454 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003455 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003456 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003457 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003458 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003459 if (!mSystemReady) {
3460 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3461 return;
3462 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003463 mMethodList.clear();
3464 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003465 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003466 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003467
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003468 // Use for queryIntentServicesAsUser
3469 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003471 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3472 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3473 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003474 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003476 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3477 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003478
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003479 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003480 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 for (int i = 0; i < services.size(); ++i) {
3482 ResolveInfo ri = services.get(i);
3483 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003484 final String imeId = InputMethodInfo.computeId(ri);
3485 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3486 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 + ": it does not require the permission "
3488 + android.Manifest.permission.BIND_INPUT_METHOD);
3489 continue;
3490 }
3491
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003492 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003494 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 try {
satoke7c6998e2011-06-03 17:57:59 +09003496 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003497 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003498 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003499 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500
3501 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003502 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003504 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003505 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 }
3507 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003508
Yohei Yukawac4e44912017-02-09 19:30:22 -08003509 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3510 // negatives when the package state remains to be the same but only the component state is
3511 // changed.
3512 {
3513 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3514 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3515 // conservative, but it seems we cannot use it for now (Issue 35176630).
3516 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3517 new Intent(InputMethod.SERVICE_INTERFACE),
3518 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3519 final int N = allInputMethodServices.size();
3520 for (int i = 0; i < N; ++i) {
3521 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003522 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3523 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003524 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003525 }
3526 }
3527
Yohei Yukawa859df052016-02-17 07:56:46 -08003528 // TODO: The following code should find better place to live.
3529 if (!resetDefaultEnabledIme) {
3530 boolean enabledImeFound = false;
3531 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3532 final int N = enabledImes.size();
3533 for (int i = 0; i < N; ++i) {
3534 final InputMethodInfo imi = enabledImes.get(i);
3535 if (mMethodList.contains(imi)) {
3536 enabledImeFound = true;
3537 break;
3538 }
3539 }
3540 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003541 if (DEBUG) {
3542 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3543 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003544 resetDefaultEnabledIme = true;
3545 resetSelectedInputMethodAndSubtypeLocked("");
3546 }
3547 }
3548
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003549 if (resetDefaultEnabledIme) {
3550 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003551 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003552 final int N = defaultEnabledIme.size();
3553 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003554 final InputMethodInfo imi = defaultEnabledIme.get(i);
3555 if (DEBUG) {
3556 Slog.d(TAG, "--- enable ime = " + imi);
3557 }
3558 setInputMethodEnabledLocked(imi.getId(), true);
3559 }
3560 }
3561
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003562 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003563 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003564 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003565 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3566 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003567 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003568 }
3569 } else {
3570 // Double check that the default IME is certainly enabled.
3571 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003572 }
3573 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003574 // Here is not the perfect place to reset the switching controller. Ideally
3575 // mSwitchingController and mSettings should be able to share the same state.
3576 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3577 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003578 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003582
satok217f5482010-12-15 05:19:19 +09003583 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003584 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003585 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003586 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3587 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003588 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003589 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003590 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003591 final int userId;
3592 synchronized (mMethodMap) {
3593 userId = mSettings.getCurrentUserId();
3594 }
3595 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003596 }
3597
3598 private void showConfigureInputMethods() {
3599 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3600 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3601 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3602 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003603 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003604 }
3605
satok2c93efc2012-04-02 19:33:47 +09003606 private boolean isScreenLocked() {
3607 return mKeyguardManager != null
3608 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3609 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003610
Seigo Nonaka14e13912015-05-06 21:04:13 -07003611 private void showInputMethodMenu(boolean showAuxSubtypes) {
3612 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613
satok2c93efc2012-04-02 19:33:47 +09003614 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003615
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003616 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003617 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003618 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003619
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003620 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003621 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003622 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3623 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003624 if (immis == null || immis.size() == 0) {
3625 return;
3626 }
3627
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003628 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629
satok688bd472012-02-09 20:09:17 +09003630 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003631 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003632 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003633
satokc3690562012-01-10 20:14:43 +09003634 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003635 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003636 if (currentSubtype != null) {
3637 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003638 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3639 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003640 }
3641 }
3642
Ken Wakasa761eb372011-03-04 19:06:18 +09003643 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003644 mIms = new InputMethodInfo[N];
3645 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003646 int checkedItem = 0;
3647 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003648 final ImeSubtypeListItem item = imList.get(i);
3649 mIms[i] = item.mImi;
3650 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003651 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003652 int subtypeId = mSubtypeIds[i];
3653 if ((subtypeId == NOT_A_SUBTYPE_ID)
3654 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3655 || (subtypeId == lastInputMethodSubtypeId)) {
3656 checkedItem = i;
3657 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003660
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003661 final Context settingsContext = new ContextThemeWrapper(
3662 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003663 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3664
3665 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003666 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3667 @Override
3668 public void onCancel(DialogInterface dialog) {
3669 hideInputMethodMenu();
3670 }
3671 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003672
3673 final Context dialogContext = mDialogBuilder.getContext();
3674 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3675 com.android.internal.R.styleable.DialogPreference,
3676 com.android.internal.R.attr.alertDialogStyle, 0);
3677 final Drawable dialogIcon = a.getDrawable(
3678 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3679 a.recycle();
3680
3681 mDialogBuilder.setIcon(dialogIcon);
3682
Yohei Yukawad34e1482016-02-11 08:03:52 -08003683 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003684 final View tv = inflater.inflate(
3685 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3686 mDialogBuilder.setCustomTitle(tv);
3687
3688 // Setup layout for a toggle switch of the hardware keyboard
3689 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003690 mSwitchingDialogTitleView
3691 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003692 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003693 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003694 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003695 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003696 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003697 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3698 @Override
3699 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003700 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003701 // Ensure that the input method dialog is dismissed when changing
3702 // the hardware keyboard state.
3703 hideInputMethodMenu();
3704 }
3705 });
3706
Alan Viverette505e3ab2014-11-24 15:22:11 -08003707 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003708 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3709 final OnClickListener choiceListener = new OnClickListener() {
3710 @Override
3711 public void onClick(final DialogInterface dialog, final int which) {
3712 synchronized (mMethodMap) {
3713 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3714 || mSubtypeIds.length <= which) {
3715 return;
satok01038492012-04-09 21:08:27 +09003716 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003717 final InputMethodInfo im = mIms[which];
3718 int subtypeId = mSubtypeIds[which];
3719 adapter.mCheckedItem = which;
3720 adapter.notifyDataSetChanged();
3721 hideInputMethodMenu();
3722 if (im != null) {
3723 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3724 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003725 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003726 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003728 }
3729 }
3730 };
3731 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003734 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003735 final Window w = mSwitchingDialog.getWindow();
3736 final WindowManager.LayoutParams attrs = w.getAttributes();
3737 w.setType(TYPE_INPUT_METHOD_DIALOG);
3738 // Use an alternate token for the dialog for that window manager can group the token
3739 // with other IME windows based on type vs. grouping based on whichever token happens
3740 // to get selected by the system later on.
3741 attrs.token = mSwitchingDialogToken;
3742 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3743 attrs.setTitle("Select input method");
3744 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003745 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 mSwitchingDialog.show();
3747 }
3748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003749
Ken Wakasa05dbb652011-08-22 15:22:43 +09003750 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3751 private final LayoutInflater mInflater;
3752 private final int mTextViewResourceId;
3753 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003754 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003755 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3756 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3757 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003758
Ken Wakasa05dbb652011-08-22 15:22:43 +09003759 mTextViewResourceId = textViewResourceId;
3760 mItemsList = itemsList;
3761 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003762 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003763 }
3764
3765 @Override
3766 public View getView(int position, View convertView, ViewGroup parent) {
3767 final View view = convertView != null ? convertView
3768 : mInflater.inflate(mTextViewResourceId, null);
3769 if (position < 0 || position >= mItemsList.size()) return view;
3770 final ImeSubtypeListItem item = mItemsList.get(position);
3771 final CharSequence imeName = item.mImeName;
3772 final CharSequence subtypeName = item.mSubtypeName;
3773 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3774 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3775 if (TextUtils.isEmpty(subtypeName)) {
3776 firstTextView.setText(imeName);
3777 secondTextView.setVisibility(View.GONE);
3778 } else {
3779 firstTextView.setText(subtypeName);
3780 secondTextView.setText(imeName);
3781 secondTextView.setVisibility(View.VISIBLE);
3782 }
3783 final RadioButton radioButton =
3784 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3785 radioButton.setChecked(position == mCheckedItem);
3786 return view;
3787 }
3788 }
3789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003791 synchronized (mMethodMap) {
3792 hideInputMethodMenuLocked();
3793 }
3794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003795
The Android Open Source Project10592532009-03-18 17:39:46 -07003796 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003797 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798
The Android Open Source Project10592532009-03-18 17:39:46 -07003799 if (mSwitchingDialog != null) {
3800 mSwitchingDialog.dismiss();
3801 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003803
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003804 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003805 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003806 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003810
satok42c5a162011-05-26 16:46:14 +09003811 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003813 // TODO: Make this work even for non-current users?
3814 if (!calledFromValidUser()) {
3815 return false;
3816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 synchronized (mMethodMap) {
3818 if (mContext.checkCallingOrSelfPermission(
3819 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3820 != PackageManager.PERMISSION_GRANTED) {
3821 throw new SecurityException(
3822 "Requires permission "
3823 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3824 }
Anna Galusza9b278112016-01-04 11:37:37 -08003825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 long ident = Binder.clearCallingIdentity();
3827 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003828 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 } finally {
3830 Binder.restoreCallingIdentity(ident);
3831 }
3832 }
3833 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003834
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003835 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3836 // Make sure this is a valid input method.
3837 InputMethodInfo imm = mMethodMap.get(id);
3838 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003839 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003840 }
3841
satokd87c2592010-09-29 11:52:06 +09003842 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3843 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003844
satokd87c2592010-09-29 11:52:06 +09003845 if (enabled) {
3846 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3847 if (pair.first.equals(id)) {
3848 // We are enabling this input method, but it is already enabled.
3849 // Nothing to do. The previous state was enabled.
3850 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003851 }
3852 }
satokd87c2592010-09-29 11:52:06 +09003853 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3854 // Previous state was disabled.
3855 return false;
3856 } else {
3857 StringBuilder builder = new StringBuilder();
3858 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3859 builder, enabledInputMethodsList, id)) {
3860 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003861 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003862 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3863 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3864 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003865 }
3866 // Previous state was enabled.
3867 return true;
3868 } else {
3869 // We are disabling the input method but it is already disabled.
3870 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003871 return false;
3872 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003873 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003874 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003875
satok723a27e2010-11-11 14:58:11 +09003876 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3877 boolean setSubtypeOnly) {
3878 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003879 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003880
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003881 mCurUserActionNotificationSequenceNumber =
3882 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3883 if (DEBUG) {
3884 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3885 + mCurUserActionNotificationSequenceNumber);
3886 }
3887
3888 if (mCurClient != null && mCurClient.client != null) {
3889 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3890 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003891 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003892 }
3893
satok723a27e2010-11-11 14:58:11 +09003894 // Set Subtype here
3895 if (imi == null || subtypeId < 0) {
3896 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003897 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003898 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003899 if (subtypeId < imi.getSubtypeCount()) {
3900 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3901 mSettings.putSelectedSubtype(subtype.hashCode());
3902 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003903 } else {
3904 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003905 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003906 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003907 }
satokab751aa2010-09-14 19:17:36 +09003908 }
satok723a27e2010-11-11 14:58:11 +09003909
Yohei Yukawa68645a62016-02-17 07:54:20 -08003910 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003911 // Set InputMethod here
3912 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3913 }
3914 }
3915
3916 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3917 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3918 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3919 // newDefaultIme is empty when there is no candidate for the selected IME.
3920 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3921 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3922 if (subtypeHashCode != null) {
3923 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003924 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003925 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003926 } catch (NumberFormatException e) {
3927 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3928 }
3929 }
3930 }
3931 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003932 }
3933
satok4e4569d2010-11-19 18:45:53 +09003934 // If there are no selected shortcuts, tries finding the most applicable ones.
3935 private Pair<InputMethodInfo, InputMethodSubtype>
3936 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3937 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3938 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003939 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003940 boolean foundInSystemIME = false;
3941
3942 // Search applicable subtype for each InputMethodInfo
3943 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003944 final String imiId = imi.getId();
3945 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3946 continue;
3947 }
satokcd7cd292010-11-20 15:46:23 +09003948 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003949 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003950 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003951 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003952 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003953 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003954 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003955 }
satokdf31ae62011-01-15 06:19:44 +09003956 // 2. Search by the system locale from enabledSubtypes.
3957 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003958 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003959 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003960 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003961 }
satoka86f5e42011-09-02 17:12:42 +09003962 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003963 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003964 final ArrayList<InputMethodSubtype> subtypesForSearch =
3965 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003966 ? InputMethodUtils.getSubtypes(imi)
3967 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003968 // 4. Search by the current subtype's locale from all subtypes.
3969 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003970 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003971 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003972 }
3973 // 5. Search by the system locale from all subtypes.
3974 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003975 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003976 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003977 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003978 }
satokcd7cd292010-11-20 15:46:23 +09003979 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003980 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003981 // The current input method is the most applicable IME.
3982 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003983 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003984 break;
satok7599a7f2010-12-22 13:45:23 +09003985 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003986 // The system input method is 2nd applicable IME.
3987 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003988 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003989 if ((imi.getServiceInfo().applicationInfo.flags
3990 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3991 foundInSystemIME = true;
3992 }
satok4e4569d2010-11-19 18:45:53 +09003993 }
3994 }
3995 }
3996 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003997 if (mostApplicableIMI != null) {
3998 Slog.w(TAG, "Most applicable shortcut input method was:"
3999 + mostApplicableIMI.getId());
4000 if (mostApplicableSubtype != null) {
4001 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
4002 + "," + mostApplicableSubtype.getMode() + ","
4003 + mostApplicableSubtype.getLocale());
4004 }
4005 }
satok4e4569d2010-11-19 18:45:53 +09004006 }
satokcd7cd292010-11-20 15:46:23 +09004007 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004008 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09004009 } else {
4010 return null;
4011 }
4012 }
4013
satokab751aa2010-09-14 19:17:36 +09004014 /**
4015 * @return Return the current subtype of this input method.
4016 */
satok42c5a162011-05-26 16:46:14 +09004017 @Override
satokab751aa2010-09-14 19:17:36 +09004018 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004019 // TODO: Make this work even for non-current users?
4020 if (!calledFromValidUser()) {
4021 return null;
4022 }
4023 synchronized (mMethodMap) {
4024 return getCurrentInputMethodSubtypeLocked();
4025 }
4026 }
4027
4028 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09004029 if (mCurMethodId == null) {
4030 return null;
4031 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004032 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004033 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
4034 if (imi == null || imi.getSubtypeCount() == 0) {
4035 return null;
satok4e4569d2010-11-19 18:45:53 +09004036 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004037 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004038 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
4039 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004040 if (subtypeId == NOT_A_SUBTYPE_ID) {
4041 // If there are no selected subtypes, the framework will try to find
4042 // the most applicable subtype from explicitly or implicitly enabled
4043 // subtypes.
4044 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004045 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004046 // If there is only one explicitly or implicitly enabled subtype,
4047 // just returns it.
4048 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4049 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4050 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004051 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004052 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004053 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004054 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004055 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004056 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4057 true);
satok4e4569d2010-11-19 18:45:53 +09004058 }
satok3ef8b292010-11-23 06:06:29 +09004059 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004060 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004061 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004062 }
4063 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004064 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004065 }
4066
satok4e4569d2010-11-19 18:45:53 +09004067 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004068 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004069 @Override
satok4e4569d2010-11-19 18:45:53 +09004070 public List getShortcutInputMethodsAndSubtypes() {
4071 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004072 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004073 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004074 // If there are no selected shortcut subtypes, the framework will try to find
4075 // the most applicable subtype from all subtypes whose mode is
4076 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004077 Pair<InputMethodInfo, InputMethodSubtype> info =
4078 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004079 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004080 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004081 ret.add(info.first);
4082 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004083 }
satok3da92232011-01-11 22:46:30 +09004084 return ret;
satokf3db1af2010-11-23 13:34:33 +09004085 }
satokf3db1af2010-11-23 13:34:33 +09004086 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4087 ret.add(imi);
4088 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4089 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004090 }
4091 }
satokf3db1af2010-11-23 13:34:33 +09004092 return ret;
satok4e4569d2010-11-19 18:45:53 +09004093 }
4094 }
4095
satok42c5a162011-05-26 16:46:14 +09004096 @Override
satokb66d2872010-11-10 01:04:04 +09004097 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004098 // TODO: Make this work even for non-current users?
4099 if (!calledFromValidUser()) {
4100 return false;
4101 }
satokb66d2872010-11-10 01:04:04 +09004102 synchronized (mMethodMap) {
4103 if (subtype != null && mCurMethodId != null) {
4104 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004105 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004106 if (subtypeId != NOT_A_SUBTYPE_ID) {
4107 setInputMethodLocked(mCurMethodId, subtypeId);
4108 return true;
4109 }
4110 }
4111 return false;
4112 }
4113 }
4114
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004115 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004116 private static class InputMethodFileManager {
4117 private static final String SYSTEM_PATH = "system";
4118 private static final String INPUT_METHOD_PATH = "inputmethod";
4119 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4120 private static final String NODE_SUBTYPES = "subtypes";
4121 private static final String NODE_SUBTYPE = "subtype";
4122 private static final String NODE_IMI = "imi";
4123 private static final String ATTR_ID = "id";
4124 private static final String ATTR_LABEL = "label";
4125 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004126 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004127 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004128 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004129 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4130 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4131 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004132 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004133 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4134 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004135 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004136 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004137 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004138 if (methodMap == null) {
4139 throw new NullPointerException("methodMap is null");
4140 }
4141 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004142 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004143 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4144 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004145 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004146 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004147 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4148 }
4149 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4150 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4151 if (!subtypeFile.exists()) {
4152 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004153 writeAdditionalInputMethodSubtypes(
4154 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004155 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004156 readAdditionalInputMethodSubtypes(
4157 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004158 }
4159 }
4160
4161 private void deleteAllInputMethodSubtypes(String imiId) {
4162 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004163 mAdditionalSubtypesMap.remove(imiId);
4164 writeAdditionalInputMethodSubtypes(
4165 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004166 }
4167 }
4168
4169 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004170 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004171 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004172 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004173 final int N = additionalSubtypes.length;
4174 for (int i = 0; i < N; ++i) {
4175 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004176 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004177 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004178 } else {
4179 Slog.w(TAG, "Duplicated subtype definition found: "
4180 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004181 }
4182 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004183 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4184 writeAdditionalInputMethodSubtypes(
4185 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004186 }
4187 }
4188
4189 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4190 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004191 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004192 }
4193 }
4194
4195 private static void writeAdditionalInputMethodSubtypes(
4196 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4197 HashMap<String, InputMethodInfo> methodMap) {
4198 // Safety net for the case that this function is called before methodMap is set.
4199 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4200 FileOutputStream fos = null;
4201 try {
4202 fos = subtypesFile.startWrite();
4203 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004204 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004205 out.startDocument(null, true);
4206 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4207 out.startTag(null, NODE_SUBTYPES);
4208 for (String imiId : allSubtypes.keySet()) {
4209 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4210 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4211 continue;
4212 }
4213 out.startTag(null, NODE_IMI);
4214 out.attribute(null, ATTR_ID, imiId);
4215 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4216 final int N = subtypesList.size();
4217 for (int i = 0; i < N; ++i) {
4218 final InputMethodSubtype subtype = subtypesList.get(i);
4219 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004220 if (subtype.hasSubtypeId()) {
4221 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4222 String.valueOf(subtype.getSubtypeId()));
4223 }
satoke7c6998e2011-06-03 17:57:59 +09004224 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4225 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4226 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004227 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4228 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004229 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4230 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4231 out.attribute(null, ATTR_IS_AUXILIARY,
4232 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004233 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4234 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004235 out.endTag(null, NODE_SUBTYPE);
4236 }
4237 out.endTag(null, NODE_IMI);
4238 }
4239 out.endTag(null, NODE_SUBTYPES);
4240 out.endDocument();
4241 subtypesFile.finishWrite(fos);
4242 } catch (java.io.IOException e) {
4243 Slog.w(TAG, "Error writing subtypes", e);
4244 if (fos != null) {
4245 subtypesFile.failWrite(fos);
4246 }
4247 }
4248 }
4249
4250 private static void readAdditionalInputMethodSubtypes(
4251 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4252 if (allSubtypes == null || subtypesFile == null) return;
4253 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004254 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004255 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004256 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004257 int type = parser.getEventType();
4258 // Skip parsing until START_TAG
4259 while ((type = parser.next()) != XmlPullParser.START_TAG
4260 && type != XmlPullParser.END_DOCUMENT) {}
4261 String firstNodeName = parser.getName();
4262 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4263 throw new XmlPullParserException("Xml doesn't start with subtypes");
4264 }
4265 final int depth =parser.getDepth();
4266 String currentImiId = null;
4267 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4268 while (((type = parser.next()) != XmlPullParser.END_TAG
4269 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4270 if (type != XmlPullParser.START_TAG)
4271 continue;
4272 final String nodeName = parser.getName();
4273 if (NODE_IMI.equals(nodeName)) {
4274 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4275 if (TextUtils.isEmpty(currentImiId)) {
4276 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4277 continue;
4278 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004279 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004280 allSubtypes.put(currentImiId, tempSubtypesArray);
4281 } else if (NODE_SUBTYPE.equals(nodeName)) {
4282 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4283 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4284 continue;
4285 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004286 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004287 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004288 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004289 parser.getAttributeValue(null, ATTR_LABEL));
4290 final String imeSubtypeLocale =
4291 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004292 final String languageTag =
4293 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004294 final String imeSubtypeMode =
4295 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4296 final String imeSubtypeExtraValue =
4297 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004298 final boolean isAuxiliary = "1".equals(String.valueOf(
4299 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004300 final boolean isAsciiCapable = "1".equals(String.valueOf(
4301 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004302 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004303 .setSubtypeNameResId(label)
4304 .setSubtypeIconResId(icon)
4305 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004306 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004307 .setSubtypeMode(imeSubtypeMode)
4308 .setSubtypeExtraValue(imeSubtypeExtraValue)
4309 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004310 .setIsAsciiCapable(isAsciiCapable);
4311 final String subtypeIdString =
4312 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4313 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004314 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004315 }
4316 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004317 }
4318 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004319 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4320 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004321 return;
satoke7c6998e2011-06-03 17:57:59 +09004322 }
4323 }
4324 }
4325
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004326 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4327 @NonNull
4328 private final Handler mHandler;
4329
4330 LocalServiceImpl(@NonNull final Handler handler) {
4331 mHandler = handler;
4332 }
4333
4334 @Override
4335 public void setInteractive(boolean interactive) {
4336 // Do everything in handler so as not to block the caller.
4337 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4338 interactive ? 1 : 0, 0));
4339 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004340
4341 @Override
4342 public void switchInputMethod(boolean forwardDirection) {
4343 // Do everything in handler so as not to block the caller.
4344 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4345 forwardDirection ? 1 : 0, 0));
4346 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004347
4348 @Override
4349 public void hideCurrentInputMethod() {
4350 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4351 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4352 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004353 }
4354
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004355 private static String imeWindowStatusToString(final int imeWindowVis) {
4356 final StringBuilder sb = new StringBuilder();
4357 boolean first = true;
4358 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4359 sb.append("Active");
4360 first = false;
4361 }
4362 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4363 if (!first) {
4364 sb.append("|");
4365 }
4366 sb.append("Visible");
4367 }
4368 return sb.toString();
4369 }
4370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004372 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4373 @Nullable Uri contentUri, @Nullable String packageName) {
4374 if (!calledFromValidUser()) {
4375 return null;
4376 }
4377
4378 if (token == null) {
4379 throw new NullPointerException("token");
4380 }
4381 if (packageName == null) {
4382 throw new NullPointerException("packageName");
4383 }
4384 if (contentUri == null) {
4385 throw new NullPointerException("contentUri");
4386 }
4387 final String contentUriScheme = contentUri.getScheme();
4388 if (!"content".equals(contentUriScheme)) {
4389 throw new InvalidParameterException("contentUri must have content scheme");
4390 }
4391
4392 synchronized (mMethodMap) {
4393 final int uid = Binder.getCallingUid();
4394 if (mCurMethodId == null) {
4395 return null;
4396 }
4397 if (mCurToken != token) {
4398 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4399 + " token=" + token);
4400 return null;
4401 }
4402 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4403 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4404 // nature of our system. Let's compare it with our internal record.
4405 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4406 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4407 + mCurAttribute.packageName + " packageName=" + packageName);
4408 return null;
4409 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004410 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004411 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004412 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004413 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004414 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4415 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4416 imeUserId);
4417 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4418 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4419 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4420 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4421 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4422 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4423 // actually allowed to "uid", which is guaranteed to be the IME's one.
4424 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4425 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004426 }
4427 }
4428
4429 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004430 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4431 if (!calledFromValidUser()) {
4432 return;
4433 }
4434 synchronized (mMethodMap) {
4435 if (!calledWithValidToken(token)) {
4436 return;
4437 }
4438 if (mCurClient != null && mCurClient.client != null) {
4439 mInFullscreenMode = fullscreen;
4440 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4441 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4442 }
4443 }
4444 }
4445
4446 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004448 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449
4450 IInputMethod method;
4451 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004452 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 synchronized (mMethodMap) {
4457 p.println("Current Input Method Manager state:");
4458 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004459 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 for (int i=0; i<N; i++) {
4461 InputMethodInfo info = mMethodList.get(i);
4462 p.println(" InputMethod #" + i + ":");
4463 info.dump(p, " ");
4464 }
4465 p.println(" Clients:");
4466 for (ClientState ci : mClients.values()) {
4467 p.println(" Client " + ci + ":");
4468 p.println(" client=" + ci.client);
4469 p.println(" inputContext=" + ci.inputContext);
4470 p.println(" sessionRequested=" + ci.sessionRequested);
4471 p.println(" curSession=" + ci.curSession);
4472 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004473 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004475 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004476 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4477 + " softInputMode=" +
4478 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4479 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004480 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4482 + " mBoundToMethod=" + mBoundToMethod);
4483 p.println(" mCurToken=" + mCurToken);
4484 p.println(" mCurIntent=" + mCurIntent);
4485 method = mCurMethod;
4486 p.println(" mCurMethod=" + mCurMethod);
4487 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004488 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 p.println(" mShowRequested=" + mShowRequested
4490 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4491 + " mShowForced=" + mShowForced
4492 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004493 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004494 p.println(" mCurUserActionNotificationSequenceNumber="
4495 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004496 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004497 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004498 p.println(" mSwitchingController:");
4499 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004500 p.println(" mSettings:");
4501 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004502
4503 p.println(" mStartInputHistory:");
4504 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004506
Jeff Brownb88102f2010-09-08 11:49:43 -07004507 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 pw.flush();
4510 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004511 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4512 } catch (IOException | RemoteException e) {
4513 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004515 } else {
4516 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004518
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004519 if (focusedWindowClient != null && client != focusedWindowClient) {
4520 p.println(" ");
4521 p.println("Warning: Current input method client doesn't match the last focused. "
4522 + "window.");
4523 p.println("Dumping input method client in the last focused window just in case.");
4524 p.println(" ");
4525 pw.flush();
4526 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004527 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4528 } catch (IOException | RemoteException e) {
4529 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004530 }
4531 }
4532
Jeff Brownb88102f2010-09-08 11:49:43 -07004533 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 pw.flush();
4536 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004537 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4538 } catch (IOException | RemoteException e) {
4539 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004541 } else {
4542 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 }
4544 }
4545}