blob: 9da375700a62b024b27107761f83afdb0ed3b832 [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
1473 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1474 resetDefaultImeLocked(mContext);
1475 updateFromSettingsLocked(true);
1476 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1477 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1478 mContext.getBasePackageName());
1479
Dianne Hackborncc278702009-09-02 23:07:23 -07001480 try {
1481 startInputInnerLocked();
1482 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001484 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001485 }
1486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001488
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001489 // ---------------------------------------------------------------------------------------
1490 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1491 // 1) it comes from the system process
1492 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1493 private boolean calledFromValidUser() {
1494 final int uid = Binder.getCallingUid();
1495 final int userId = UserHandle.getUserId(uid);
1496 if (DEBUG) {
1497 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1498 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1499 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001500 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1501 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001502 }
Kenny Guy2a764942014-04-02 13:29:20 +01001503 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001504 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001506
1507 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1508 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1509 // must not manage background users' states in any functions.
1510 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1511 // by a token.
1512 if (mContext.checkCallingOrSelfPermission(
1513 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1514 == PackageManager.PERMISSION_GRANTED) {
1515 if (DEBUG) {
1516 Slog.d(TAG, "--- Access granted because the calling process has "
1517 + "the INTERACT_ACROSS_USERS_FULL permission");
1518 }
1519 return true;
1520 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001521 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001522 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1523 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001524 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001525 }
1526
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001527
1528 /**
1529 * Returns true iff the caller is identified to be the current input method with the token.
1530 * @param token The window token given to the input method when it was started.
1531 * @return true if and only if non-null valid token is specified.
1532 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001533 private boolean calledWithValidToken(@Nullable IBinder token) {
1534 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1535 if (DEBUG) {
1536 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1537 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1538 }
1539 return false;
1540 }
1541 if (token == null || token != mCurToken) {
1542 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1543 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1544 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001545 return false;
1546 }
1547 return true;
1548 }
1549
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001550 private boolean bindCurrentInputMethodService(
1551 Intent service, ServiceConnection conn, int flags) {
1552 if (service == null || conn == null) {
1553 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1554 return false;
1555 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001556 return mContext.bindServiceAsUser(service, conn, flags,
1557 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001558 }
1559
satoke7c6998e2011-06-03 17:57:59 +09001560 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001562 // TODO: Make this work even for non-current users?
1563 if (!calledFromValidUser()) {
1564 return Collections.emptyList();
1565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001567 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569 }
1570
satoke7c6998e2011-06-03 17:57:59 +09001571 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001573 // TODO: Make this work even for non-current users?
1574 if (!calledFromValidUser()) {
1575 return Collections.emptyList();
1576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001578 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
1580 }
1581
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001582 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001583 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001584 * @return enabled subtypes of the specified imi
1585 */
satoke7c6998e2011-06-03 17:57:59 +09001586 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001587 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001588 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001589 // TODO: Make this work even for non-current users?
1590 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001591 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001592 }
satok67ddf9c2010-11-17 09:45:54 +09001593 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001594 final InputMethodInfo imi;
1595 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001596 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001597 } else {
1598 imi = mMethodMap.get(imiId);
1599 }
1600 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001601 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001602 }
1603 return mSettings.getEnabledInputMethodSubtypeListLocked(
1604 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001605 }
1606 }
1607
satoke7c6998e2011-06-03 17:57:59 +09001608 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 public void addClient(IInputMethodClient client,
1610 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001611 if (!calledFromValidUser()) {
1612 return;
1613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 synchronized (mMethodMap) {
1615 mClients.put(client.asBinder(), new ClientState(client,
1616 inputContext, uid, pid));
1617 }
1618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001619
satoke7c6998e2011-06-03 17:57:59 +09001620 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001622 if (!calledFromValidUser()) {
1623 return;
1624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001626 ClientState cs = mClients.remove(client.asBinder());
1627 if (cs != null) {
1628 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001629 if (mCurClient == cs) {
1630 mCurClient = null;
1631 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001632 if (mCurFocusedWindowClient == cs) {
1633 mCurFocusedWindowClient = null;
1634 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 }
1637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 void executeOrSendMessage(IInterface target, Message msg) {
1640 if (target.asBinder() instanceof Binder) {
1641 mCaller.sendMessage(msg);
1642 } else {
1643 handleMessage(msg);
1644 msg.recycle();
1645 }
1646 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001647
Yohei Yukawa33e81792015-11-17 21:14:42 -08001648 void unbindCurrentClientLocked(
1649 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001651 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 + mCurClient.client.asBinder());
1653 if (mBoundToMethod) {
1654 mBoundToMethod = false;
1655 if (mCurMethod != null) {
1656 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1657 MSG_UNBIND_INPUT, mCurMethod));
1658 }
1659 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001660
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001661 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1662 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001663 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1664 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667
The Android Open Source Project10592532009-03-18 17:39:46 -07001668 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
1670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 private int getImeShowFlags() {
1673 int flags = 0;
1674 if (mShowForced) {
1675 flags |= InputMethod.SHOW_FORCED
1676 | InputMethod.SHOW_EXPLICIT;
1677 } else if (mShowExplicitlyRequested) {
1678 flags |= InputMethod.SHOW_EXPLICIT;
1679 }
1680 return flags;
1681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 private int getAppShowFlags() {
1684 int flags = 0;
1685 if (mShowForced) {
1686 flags |= InputMethodManager.SHOW_FORCED;
1687 } else if (!mShowExplicitlyRequested) {
1688 flags |= InputMethodManager.SHOW_IMPLICIT;
1689 }
1690 return flags;
1691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001692
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001693 InputBindResult attachNewInputLocked(
1694 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 if (!mBoundToMethod) {
1696 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1697 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1698 mBoundToMethod = true;
1699 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001700
1701 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001702 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1703 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1704 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001705 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001706 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001709 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001710 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001711 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001713 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001714 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001716 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001717 (session.channel != null ? session.channel.dup() : null),
1718 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001720
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001721 InputBindResult startInputLocked(
1722 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001723 IInputMethodClient client, IInputContext inputContext,
1724 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001725 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 // If no method is currently selected, do nothing.
1727 if (mCurMethodId == null) {
1728 return mNoBinding;
1729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 ClientState cs = mClients.get(client.asBinder());
1732 if (cs == null) {
1733 throw new IllegalArgumentException("unknown client "
1734 + client.asBinder());
1735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001736
Yohei Yukawa74750f22016-03-22 12:54:22 -07001737 if (attribute == null) {
1738 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1739 + " uid=" + cs.uid + " pid=" + cs.pid);
1740 return null;
1741 }
1742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 try {
1744 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1745 // Check with the window manager to make sure this client actually
1746 // has a window with focus. If not, reject. This is thread safe
1747 // because if the focus changes some time before or after, the
1748 // next client receiving focus that has any interest in input will
1749 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001750 if (DEBUG) {
1751 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1752 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 return null;
1755 }
1756 } catch (RemoteException e) {
1757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001758
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001759 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001760 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001761 }
1762
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001763 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001764 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001765 @NonNull EditorInfo attribute, int controlFlags,
1766 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001767 // If no method is currently selected, do nothing.
1768 if (mCurMethodId == null) {
1769 return mNoBinding;
1770 }
1771
Yohei Yukawad57ba672015-06-08 16:39:46 -07001772 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1773 attribute.packageName)) {
1774 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1775 + " uid=" + cs.uid + " package=" + attribute.packageName);
1776 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001777 }
1778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001780 // Was the keyguard locked when switching over to the new client?
1781 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 // If the client is changing, we need to switch over to the new
1783 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001784 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001785 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001786 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787
1788 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001789 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001790 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001791 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 }
1793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 // Bump up the sequence for this client and attach it.
1796 mCurSeq++;
1797 if (mCurSeq <= 0) mCurSeq = 1;
1798 mCurClient = cs;
1799 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001800 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 // Check if the input method is changing.
1804 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1805 if (cs.curSession != null) {
1806 // Fast case: if we are already connected to the input method,
1807 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001808 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001809 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 }
1811 if (mHaveConnection) {
1812 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 // Return to client, and we will get back with it when
1814 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001815 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001816 return new InputBindResult(null, null, mCurId, mCurSeq,
1817 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 } else if (SystemClock.uptimeMillis()
1819 < (mLastBindTime+TIME_TO_RECONNECT)) {
1820 // In this case we have connected to the service, but
1821 // don't yet have its interface. If it hasn't been too
1822 // long since we did the connection, we'll return to
1823 // the client and wait to get the service interface so
1824 // we can report back. If it has been too long, we want
1825 // to fall through so we can try a disconnect/reconnect
1826 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001827 return new InputBindResult(null, null, mCurId, mCurSeq,
1828 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001830 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1831 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833 }
1834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001836 return startInputInnerLocked();
1837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001839 InputBindResult startInputInnerLocked() {
1840 if (mCurMethodId == null) {
1841 return mNoBinding;
1842 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001843
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001844 if (!mSystemReady) {
1845 // If the system is not yet ready, we shouldn't be running third
1846 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001847 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1848 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001849 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1852 if (info == null) {
1853 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001856 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1859 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001860 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1861 com.android.internal.R.string.input_method_binding_label);
1862 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1863 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001864 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 mLastBindTime = SystemClock.uptimeMillis();
1866 mHaveConnection = true;
1867 mCurId = info.getId();
1868 mCurToken = new Binder();
1869 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001870 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001871 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 } catch (RemoteException e) {
1873 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001874 return new InputBindResult(null, null, mCurId, mCurSeq,
1875 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 } else {
1877 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001878 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 + mCurIntent);
1880 }
1881 return null;
1882 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001883
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001884 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001885 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001886 IInputMethodClient client, IInputContext inputContext,
1887 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001888 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001889 if (!calledFromValidUser()) {
1890 return null;
1891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001893 if (DEBUG) {
1894 Slog.v(TAG, "startInput: reason="
1895 + InputMethodClient.getStartInputReason(startInputReason)
1896 + " client = " + client.asBinder()
1897 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001898 + " missingMethods="
1899 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001900 + " attribute=" + attribute
1901 + " controlFlags=#" + Integer.toHexString(controlFlags));
1902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 final long ident = Binder.clearCallingIdentity();
1904 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001905 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1906 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 } finally {
1908 Binder.restoreCallingIdentity(ident);
1909 }
1910 }
1911 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001912
satoke7c6998e2011-06-03 17:57:59 +09001913 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 public void finishInput(IInputMethodClient client) {
1915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001916
satoke7c6998e2011-06-03 17:57:59 +09001917 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 public void onServiceConnected(ComponentName name, IBinder service) {
1919 synchronized (mMethodMap) {
1920 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1921 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001922 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001923 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001924 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001925 return;
1926 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001927 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001928 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1929 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001931 clearClientSessionLocked(mCurClient);
1932 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934 }
1935 }
1936 }
1937
Jeff Brownc28867a2013-03-26 15:42:39 -07001938 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1939 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 synchronized (mMethodMap) {
1941 if (mCurMethod != null && method != null
1942 && mCurMethod.asBinder() == method.asBinder()) {
1943 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001944 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001946 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001947 InputBindResult res = attachNewInputLocked(
1948 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 if (res.method != null) {
1950 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001951 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001953 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
1955 }
1956 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001957
1958 // Session abandoned. Close its associated input channel.
1959 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001961
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001962 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001963 if (mVisibleBound) {
1964 mContext.unbindService(mVisibleConnection);
1965 mVisibleBound = false;
1966 }
1967
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001968 if (mHaveConnection) {
1969 mContext.unbindService(this);
1970 mHaveConnection = false;
1971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001972
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001973 if (mCurToken != null) {
1974 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001975 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001976 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001977 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001978 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001979 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001980 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001981 } catch (RemoteException e) {
1982 }
1983 mCurToken = null;
1984 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001985
The Android Open Source Project10592532009-03-18 17:39:46 -07001986 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001987 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001989
Yohei Yukawa33e81792015-11-17 21:14:42 -08001990 void resetCurrentMethodAndClient(
1991 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001992 mCurMethodId = null;
1993 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001994 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001995 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001996
1997 void requestClientSessionLocked(ClientState cs) {
1998 if (!cs.sessionRequested) {
1999 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
2000 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
2001 cs.sessionRequested = true;
2002 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
2003 MSG_CREATE_SESSION, mCurMethod, channels[1],
2004 new MethodCallback(this, mCurMethod, channels[0])));
2005 }
2006 }
2007
2008 void clearClientSessionLocked(ClientState cs) {
2009 finishSessionLocked(cs.curSession);
2010 cs.curSession = null;
2011 cs.sessionRequested = false;
2012 }
2013
2014 private void finishSessionLocked(SessionState sessionState) {
2015 if (sessionState != null) {
2016 if (sessionState.session != null) {
2017 try {
2018 sessionState.session.finishSession();
2019 } catch (RemoteException e) {
2020 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002021 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07002022 }
2023 sessionState.session = null;
2024 }
2025 if (sessionState.channel != null) {
2026 sessionState.channel.dispose();
2027 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002028 }
2029 }
2030 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002031
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002032 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 if (mCurMethod != null) {
2034 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002035 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002037
Jeff Brownc28867a2013-03-26 15:42:39 -07002038 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002039 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 mCurMethod = null;
2041 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002042 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002043 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002044 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002045 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002047
satoke7c6998e2011-06-03 17:57:59 +09002048 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002050 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2051 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2052 // which is irregular but can eventually happen for everyone just by continuing using the
2053 // device. Thus it is important to make sure that all the internal states are properly
2054 // refreshed when this method is called back. Running
2055 // adb install -r <APK that implements the current IME>
2056 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002058 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 + " mCurIntent=" + mCurIntent);
2060 if (mCurMethod != null && mCurIntent != null
2061 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002062 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 // We consider this to be a new bind attempt, since the system
2064 // should now try to restart the service for us.
2065 mLastBindTime = SystemClock.uptimeMillis();
2066 mShowRequested = mInputShown;
2067 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002068 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070 }
2071 }
2072
satokf9f01002011-05-19 21:31:50 +09002073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002075 synchronized (mMethodMap) {
2076 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002077 return;
2078 }
2079 final long ident = Binder.clearCallingIdentity();
2080 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002082 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002083 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002084 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002087 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002088 CharSequence contentDescription = null;
2089 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002090 // Use PackageManager to load label
2091 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002092 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002093 mIPackageManager.getApplicationInfo(packageName, 0,
2094 mSettings.getCurrentUserId()));
2095 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002096 /* ignore */
2097 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002098 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002099 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002100 contentDescription != null
2101 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002102 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002105 } finally {
2106 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
2109 }
2110
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002111 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002112 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002113 if (mSwitchingDialog != null) return false;
Yohei Yukawad2bc3092017-07-31 15:37:14 -07002114 if (mWindowManagerInternal.isKeyguardShowingAndNotOccluded()
2115 && mKeyguardManager != null && mKeyguardManager.isKeyguardSecure()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002116 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002117 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002118 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2119 // When physical keyboard is attached, we show the ime switcher (or notification if
2120 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2121 // exists in the IME switcher dialog. Might be OK to remove this condition once
2122 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2123 return true;
2124 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002125 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2126 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002127 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002128
2129 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2130 final int N = imis.size();
2131 if (N > 2) return true;
2132 if (N < 1) return false;
2133 int nonAuxCount = 0;
2134 int auxCount = 0;
2135 InputMethodSubtype nonAuxSubtype = null;
2136 InputMethodSubtype auxSubtype = null;
2137 for(int i = 0; i < N; ++i) {
2138 final InputMethodInfo imi = imis.get(i);
2139 final List<InputMethodSubtype> subtypes =
2140 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2141 final int subtypeCount = subtypes.size();
2142 if (subtypeCount == 0) {
2143 ++nonAuxCount;
2144 } else {
2145 for (int j = 0; j < subtypeCount; ++j) {
2146 final InputMethodSubtype subtype = subtypes.get(j);
2147 if (!subtype.isAuxiliary()) {
2148 ++nonAuxCount;
2149 nonAuxSubtype = subtype;
2150 } else {
2151 ++auxCount;
2152 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002153 }
2154 }
satok7cfc0ed2011-06-20 21:29:36 +09002155 }
2156 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002157 if (nonAuxCount > 1 || auxCount > 1) {
2158 return true;
2159 } else if (nonAuxCount == 1 && auxCount == 1) {
2160 if (nonAuxSubtype != null && auxSubtype != null
2161 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2162 || auxSubtype.overridesImplicitlyEnabledSubtype()
2163 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2164 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2165 return false;
2166 }
2167 return true;
2168 }
2169 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002170 }
2171
John Spurlocke0980502013-10-25 11:59:29 -04002172 private boolean isKeyguardLocked() {
2173 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2174 }
2175
Yohei Yukawad6475a62017-04-17 10:35:27 -07002176 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002177 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002178 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002179 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2180 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002181 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002182 return;
2183 }
2184
Yohei Yukawa69e68022017-02-13 12:04:50 -08002185 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002186 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002187 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002188 mImeWindowVis = vis;
2189 mBackDisposition = backDisposition;
2190 updateSystemUiLocked(token, vis, backDisposition);
2191 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002192
2193 final boolean dismissImeOnBackKeyPressed;
2194 switch (backDisposition) {
2195 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2196 dismissImeOnBackKeyPressed = true;
2197 break;
2198 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2199 dismissImeOnBackKeyPressed = false;
2200 break;
2201 default:
2202 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2203 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2204 break;
2205 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002206 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2207 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002208 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002209 }
2210
2211 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2212 synchronized (mMethodMap) {
2213 updateSystemUiLocked(token, vis, backDisposition);
2214 }
2215 }
2216
2217 // Caution! This method is called in this class. Handle multi-user carefully
2218 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2219 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002220 return;
2221 }
2222
2223 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2224 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002225 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002226 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002227 // apply policy for binder calls
2228 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2229 vis = 0;
satok06487a52010-10-29 11:37:18 +09002230 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002231 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2232 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2233 if (mStatusBar != null) {
2234 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2235 needsToShowImeSwitcher);
2236 }
2237 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2238 if (imi != null && needsToShowImeSwitcher) {
2239 // Used to load label
2240 final CharSequence title = mRes.getText(
2241 com.android.internal.R.string.select_input_method);
2242 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2243 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002244 mImeSwitcherNotification.setContentTitle(title)
2245 .setContentText(summary)
2246 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002247 try {
2248 if ((mNotificationManager != null)
2249 && !mIWindowManager.hasNavigationBar()) {
2250 if (DEBUG) {
2251 Slog.d(TAG, "--- show notification: label = " + summary);
2252 }
2253 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002254 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002255 mImeSwitcherNotification.build(), UserHandle.ALL);
2256 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002257 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002258 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002259 }
2260 } else {
2261 if (mNotificationShown && mNotificationManager != null) {
2262 if (DEBUG) {
2263 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002264 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002265 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002266 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002267 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002268 }
satok06487a52010-10-29 11:37:18 +09002269 }
2270 } finally {
2271 Binder.restoreCallingIdentity(ident);
2272 }
2273 }
2274
satoke7c6998e2011-06-03 17:57:59 +09002275 @Override
satokf9f01002011-05-19 21:31:50 +09002276 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002277 if (!calledFromValidUser()) {
2278 return;
2279 }
satokf9f01002011-05-19 21:31:50 +09002280 synchronized (mMethodMap) {
2281 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2282 for (int i = 0; i < spans.length; ++i) {
2283 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002284 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002285 mSecureSuggestionSpans.put(ss, currentImi);
2286 }
2287 }
2288 }
2289 }
2290
satoke7c6998e2011-06-03 17:57:59 +09002291 @Override
satokf9f01002011-05-19 21:31:50 +09002292 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002293 if (!calledFromValidUser()) {
2294 return false;
2295 }
satokf9f01002011-05-19 21:31:50 +09002296 synchronized (mMethodMap) {
2297 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2298 // TODO: Do not send the intent if the process of the targetImi is already dead.
2299 if (targetImi != null) {
2300 final String[] suggestions = span.getSuggestions();
2301 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002302 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002303 final Intent intent = new Intent();
2304 // Ensures that only a class in the original IME package will receive the
2305 // notification.
satok42c5a162011-05-26 16:46:14 +09002306 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002307 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2308 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2309 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2310 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002311 final long ident = Binder.clearCallingIdentity();
2312 try {
2313 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2314 } finally {
2315 Binder.restoreCallingIdentity(ident);
2316 }
satokf9f01002011-05-19 21:31:50 +09002317 return true;
2318 }
2319 }
2320 return false;
2321 }
2322
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002323 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002324 updateInputMethodsFromSettingsLocked(enabledMayChange);
2325 updateKeyboardFromSettingsLocked();
2326 }
2327
2328 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002329 if (enabledMayChange) {
2330 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2331 for (int i=0; i<enabled.size(); i++) {
2332 // We allow the user to select "disabled until used" apps, so if they
2333 // are enabling one of those here we now need to make it enabled.
2334 InputMethodInfo imm = enabled.get(i);
2335 try {
2336 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2337 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2338 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002339 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002340 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002341 if (DEBUG) {
2342 Slog.d(TAG, "Update state(" + imm.getId()
2343 + "): DISABLED_UNTIL_USED -> DEFAULT");
2344 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002345 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2346 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002347 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2348 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002349 }
2350 } catch (RemoteException e) {
2351 }
2352 }
2353 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002354 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2355 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2356 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2357 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002358 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002359 // There is no input method selected, try to choose new applicable input method.
2360 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002361 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002362 }
2363 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002365 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002367 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002368 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
satokf3db1af2010-11-23 13:34:33 +09002370 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002371 } else {
2372 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002373 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002375 // Here is not the perfect place to reset the switching controller. Ideally
2376 // mSwitchingController and mSettings should be able to share the same state.
2377 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2378 // the same enabled IMEs list.
2379 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002380
2381 }
2382
2383 public void updateKeyboardFromSettingsLocked() {
2384 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2385 if (mSwitchingDialog != null
2386 && mSwitchingDialogTitleView != null
2387 && mSwitchingDialog.isShowing()) {
2388 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2389 com.android.internal.R.id.hard_keyboard_switch);
2390 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002393
Yohei Yukawab097b822015-12-01 10:43:08 -08002394 private void notifyInputMethodSubtypeChanged(final int userId,
2395 @Nullable final InputMethodInfo inputMethodInfo,
2396 @Nullable final InputMethodSubtype subtype) {
2397 final InputManagerInternal inputManagerInternal =
2398 LocalServices.getService(InputManagerInternal.class);
2399 if (inputManagerInternal != null) {
2400 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2401 }
2402 }
2403
satokab751aa2010-09-14 19:17:36 +09002404 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 InputMethodInfo info = mMethodMap.get(id);
2406 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002407 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002409
satokd81e9502012-05-21 12:58:45 +09002410 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002412 final int subtypeCount = info.getSubtypeCount();
2413 if (subtypeCount <= 0) {
2414 return;
satokcd7cd292010-11-20 15:46:23 +09002415 }
satokd81e9502012-05-21 12:58:45 +09002416 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2417 final InputMethodSubtype newSubtype;
2418 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2419 newSubtype = info.getSubtypeAt(subtypeId);
2420 } else {
2421 // If subtype is null, try to find the most applicable one from
2422 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002423 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002424 }
2425 if (newSubtype == null || oldSubtype == null) {
2426 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2427 + ", new subtype = " + newSubtype);
2428 return;
2429 }
2430 if (newSubtype != oldSubtype) {
2431 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2432 if (mCurMethod != null) {
2433 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002434 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002435 mCurMethod.changeInputMethodSubtype(newSubtype);
2436 } catch (RemoteException e) {
2437 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002438 return;
satokab751aa2010-09-14 19:17:36 +09002439 }
2440 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002441 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 return;
2444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002445
satokd81e9502012-05-21 12:58:45 +09002446 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 final long ident = Binder.clearCallingIdentity();
2448 try {
satokab751aa2010-09-14 19:17:36 +09002449 // Set a subtype to this input method.
2450 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002451 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2452 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2453 // because mCurMethodId is stored as a history in
2454 // setSelectedInputMethodAndSubtypeLocked().
2455 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002457 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002459 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002461 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002463 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 } finally {
2465 Binder.restoreCallingIdentity(ident);
2466 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002467
2468 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2469 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002471
satok42c5a162011-05-26 16:46:14 +09002472 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002473 public boolean showSoftInput(IInputMethodClient client, int flags,
2474 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002475 if (!calledFromValidUser()) {
2476 return false;
2477 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002478 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 long ident = Binder.clearCallingIdentity();
2480 try {
2481 synchronized (mMethodMap) {
2482 if (mCurClient == null || client == null
2483 || mCurClient.client.asBinder() != client.asBinder()) {
2484 try {
2485 // We need to check if this is the current client with
2486 // focus in the window manager, to allow this call to
2487 // be made before input is started in it.
2488 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002489 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002490 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 }
2492 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002493 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 }
2495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002496
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002497 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002498 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 }
2500 } finally {
2501 Binder.restoreCallingIdentity(ident);
2502 }
2503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002504
The Android Open Source Project4df24232009-03-05 14:34:35 -08002505 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002507 if (mAccessibilityRequestingNoSoftKeyboard) {
2508 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 }
Anna Galusza9b278112016-01-04 11:37:37 -08002510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2512 mShowExplicitlyRequested = true;
2513 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002514 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2515 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002517
Dianne Hackborncc278702009-09-02 23:07:23 -07002518 if (!mSystemReady) {
2519 return false;
2520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002521
The Android Open Source Project4df24232009-03-05 14:34:35 -08002522 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002524 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002525 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2526 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2527 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002529 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002530 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002531 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002532 mVisibleBound = true;
2533 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002534 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002536 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 // The client has asked to have the input method shown, but
2538 // we have been sitting here too long with a connection to the
2539 // service and no interface received, so let's disconnect/connect
2540 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002541 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002543 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002545 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002546 } else {
2547 if (DEBUG) {
2548 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2549 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002552
The Android Open Source Project4df24232009-03-05 14:34:35 -08002553 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002555
satok42c5a162011-05-26 16:46:14 +09002556 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002557 public boolean hideSoftInput(IInputMethodClient client, int flags,
2558 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002559 if (!calledFromValidUser()) {
2560 return false;
2561 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002562 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 long ident = Binder.clearCallingIdentity();
2564 try {
2565 synchronized (mMethodMap) {
2566 if (mCurClient == null || client == null
2567 || mCurClient.client.asBinder() != client.asBinder()) {
2568 try {
2569 // We need to check if this is the current client with
2570 // focus in the window manager, to allow this call to
2571 // be made before input is started in it.
2572 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002573 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2574 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002575 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
2577 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002578 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 }
2580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002581
Joe Onorato8a9b2202010-02-26 18:56:32 -08002582 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002583 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585 } finally {
2586 Binder.restoreCallingIdentity(ident);
2587 }
2588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002589
The Android Open Source Project4df24232009-03-05 14:34:35 -08002590 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2592 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002593 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 -08002594 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 }
2596 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002597 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 -08002598 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002600
2601 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2602 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2603 // to be updated with the new value sent from IME process. Even in such a transient state
2604 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2605 // application process as a valid request, and have even promised such a behavior with CTS
2606 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2607 // IMMS#InputShown indicates that the software keyboard is shown.
2608 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2609 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2610 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002611 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002612 if (shouldHideSoftInput) {
2613 // The IME will report its visible state again after the following message finally
2614 // delivered to the IME process as an IPC. Hence the inconsistency between
2615 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2616 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002617 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2618 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2619 res = true;
2620 } else {
2621 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002623 if (mHaveConnection && mVisibleBound) {
2624 mContext.unbindService(mVisibleConnection);
2625 mVisibleBound = false;
2626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 mInputShown = false;
2628 mShowRequested = false;
2629 mShowExplicitlyRequested = false;
2630 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002631 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002633
satok42c5a162011-05-26 16:46:14 +09002634 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002635 public InputBindResult startInputOrWindowGainedFocus(
2636 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2637 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002638 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002639 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002640 if (windowToken != null) {
2641 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002642 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002643 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002644 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2645 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002646 }
2647 }
2648
2649 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002650 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002651 IInputMethodClient client, IBinder windowToken, int controlFlags,
2652 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002653 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2654 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002655 // Needs to check the validity before clearing calling identity
2656 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002657 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 long ident = Binder.clearCallingIdentity();
2659 try {
2660 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002661 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2662 + InputMethodClient.getStartInputReason(startInputReason)
2663 + " client=" + client.asBinder()
2664 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002665 + " missingMethods="
2666 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002667 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002668 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002669 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002670 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002671
Dianne Hackborn7663d802012-02-24 13:08:49 -08002672 ClientState cs = mClients.get(client.asBinder());
2673 if (cs == null) {
2674 throw new IllegalArgumentException("unknown client "
2675 + client.asBinder());
2676 }
2677
2678 try {
2679 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2680 // Check with the window manager to make sure this client actually
2681 // has a window with focus. If not, reject. This is thread safe
2682 // because if the focus changes some time before or after, the
2683 // next client receiving focus that has any interest in input will
2684 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002685 if (DEBUG) {
2686 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2687 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2688 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002689 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002691 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002693
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002694 if (!calledFromValidUser) {
2695 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2696 Slog.w(TAG, "If you want to interect with IME, you need "
2697 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2698 hideCurrentInputLocked(0, null);
2699 return null;
2700 }
2701
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002702 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002703 if (DEBUG) {
2704 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2705 + " attribute=" + attribute + ", token = " + windowToken);
2706 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002707 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002708 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002709 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002710 }
2711 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002712 }
2713 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002714 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002715 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002716
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002717 // Should we auto-show the IME even if the caller has not
2718 // specified what should be done with it?
2719 // We only do this automatically if the window can resize
2720 // to accommodate the IME (so what the user sees will give
2721 // them good context without input information being obscured
2722 // by the IME) or if running on a large screen where there
2723 // is more room for the target window + IME.
2724 final boolean doAutoShow =
2725 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2726 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2727 || mRes.getConfiguration().isLayoutSizeAtLeast(
2728 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002729 final boolean isTextEditor =
2730 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2731
2732 // We want to start input before showing the IME, but after closing
2733 // it. We want to do this after closing it to help the IME disappear
2734 // more quickly (not get stuck behind it initializing itself for the
2735 // new focused input, even if its window wants to hide the IME).
2736 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2739 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002740 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2742 // There is no focus view, and this window will
2743 // be behind any soft input window, so hide the
2744 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002745 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002746 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002748 } else if (isTextEditor && doAutoShow && (softInputMode &
2749 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 // There is a focus view, and we are navigating forward
2751 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002752 // We only do this automatically if the window can resize
2753 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002754 // them good context without input information being obscured
2755 // by the IME) or if running on a large screen where there
2756 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002757 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002758 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002759 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002760 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002761 didStart = true;
2762 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002763 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 }
2765 break;
2766 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2767 // Do nothing.
2768 break;
2769 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2770 if ((softInputMode &
2771 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002772 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002773 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 }
2775 break;
2776 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002777 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002778 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 break;
2780 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2781 if ((softInputMode &
2782 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002783 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002784 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002785 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002786 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002787 didStart = true;
2788 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002789 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
2791 break;
2792 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002793 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002794 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002795 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002796 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002797 didStart = true;
2798 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002799 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 break;
2801 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002802
2803 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002804 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002805 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 }
2808 } finally {
2809 Binder.restoreCallingIdentity(ident);
2810 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002811
2812 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002814
Guliz Tuncay6908c152017-06-02 16:06:10 -07002815 private boolean canShowInputMethodPickerLocked(IInputMethodClient client) {
2816 final int uid = Binder.getCallingUid();
2817 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
2818 return true;
2819 } else if (mCurClient != null && client != null
2820 && mCurClient.client.asBinder() == client.asBinder()) {
2821 return true;
2822 } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid(
2823 mAppOpsManager,
2824 uid,
2825 mCurIntent.getComponent().getPackageName())) {
2826 return true;
2827 } else if (mContext.checkCallingPermission(
2828 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2829 == PackageManager.PERMISSION_GRANTED) {
2830 return true;
2831 }
2832
2833 return false;
2834 }
2835
satok42c5a162011-05-26 16:46:14 +09002836 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002837 public void showInputMethodPickerFromClient(
2838 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002839 if (!calledFromValidUser()) {
2840 return;
2841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 synchronized (mMethodMap) {
Guliz Tuncay6908c152017-06-02 16:06:10 -07002843 if(!canShowInputMethodPickerLocked(client)) {
satok47a44912010-10-06 16:03:58 +09002844 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002845 + Binder.getCallingUid() + ": " + client);
Guliz Tuncay6908c152017-06-02 16:06:10 -07002846 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 }
2848
satok440aab52010-11-25 09:43:11 +09002849 // Always call subtype picker, because subtype picker is a superset of input method
2850 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002851 mHandler.sendMessage(mCaller.obtainMessageI(
2852 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002853 }
2854 }
2855
satok42c5a162011-05-26 16:46:14 +09002856 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002858 if (!calledFromValidUser()) {
2859 return;
2860 }
satok28203512010-11-24 11:06:49 +09002861 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2862 }
2863
satok42c5a162011-05-26 16:46:14 +09002864 @Override
satok28203512010-11-24 11:06:49 +09002865 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002866 if (!calledFromValidUser()) {
2867 return;
2868 }
satok28203512010-11-24 11:06:49 +09002869 synchronized (mMethodMap) {
2870 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002871 setInputMethodWithSubtypeIdLocked(token, id,
2872 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2873 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002874 } else {
2875 setInputMethod(token, id);
2876 }
2877 }
satokab751aa2010-09-14 19:17:36 +09002878 }
2879
satok42c5a162011-05-26 16:46:14 +09002880 @Override
satokb416a712010-11-25 20:42:14 +09002881 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002882 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002883 if (!calledFromValidUser()) {
2884 return;
2885 }
satokb416a712010-11-25 20:42:14 +09002886 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002887 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2888 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002889 }
2890 }
2891
satok4fc87d62011-05-20 16:13:43 +09002892 @Override
satok735cf382010-11-11 20:40:09 +09002893 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002894 if (!calledFromValidUser()) {
2895 return false;
2896 }
satok735cf382010-11-11 20:40:09 +09002897 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002898 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002899 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002900 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002901 lastImi = mMethodMap.get(lastIme.first);
2902 } else {
2903 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002904 }
satok4fc87d62011-05-20 16:13:43 +09002905 String targetLastImiId = null;
2906 int subtypeId = NOT_A_SUBTYPE_ID;
2907 if (lastIme != null && lastImi != null) {
2908 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002909 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002910 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2911 : mCurrentSubtype.hashCode();
2912 // If the last IME is the same as the current IME and the last subtype is not
2913 // defined, there is no need to switch to the last IME.
2914 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2915 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002916 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002917 }
2918 }
2919
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002920 if (TextUtils.isEmpty(targetLastImiId)
2921 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002922 // This is a safety net. If the currentSubtype can't be added to the history
2923 // and the framework couldn't find the last ime, we will make the last ime be
2924 // the most applicable enabled keyboard subtype of the system imes.
2925 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2926 if (enabled != null) {
2927 final int N = enabled.size();
2928 final String locale = mCurrentSubtype == null
2929 ? mRes.getConfiguration().locale.toString()
2930 : mCurrentSubtype.getLocale();
2931 for (int i = 0; i < N; ++i) {
2932 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002933 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002934 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002935 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2936 InputMethodUtils.getSubtypes(imi),
2937 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002938 if (keyboardSubtype != null) {
2939 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002940 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002941 imi, keyboardSubtype.hashCode());
2942 if(keyboardSubtype.getLocale().equals(locale)) {
2943 break;
2944 }
2945 }
2946 }
2947 }
2948 }
2949 }
2950
2951 if (!TextUtils.isEmpty(targetLastImiId)) {
2952 if (DEBUG) {
2953 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2954 + ", from: " + mCurMethodId + ", " + subtypeId);
2955 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002956 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002957 return true;
2958 } else {
2959 return false;
2960 }
satok735cf382010-11-11 20:40:09 +09002961 }
2962 }
2963
satoke7c6998e2011-06-03 17:57:59 +09002964 @Override
satok688bd472012-02-09 20:09:17 +09002965 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002966 if (!calledFromValidUser()) {
2967 return false;
2968 }
satok688bd472012-02-09 20:09:17 +09002969 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002970 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002971 return false;
2972 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002973 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002974 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2975 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002976 if (nextSubtype == null) {
2977 return false;
2978 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002979 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2980 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002981 return true;
2982 }
2983 }
2984
2985 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002986 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2987 if (!calledFromValidUser()) {
2988 return false;
2989 }
2990 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002991 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002992 return false;
2993 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002994 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002995 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2996 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002997 if (nextSubtype == null) {
2998 return false;
2999 }
3000 return true;
3001 }
3002 }
3003
3004 @Override
satok68f1b782011-04-11 14:26:04 +09003005 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003006 if (!calledFromValidUser()) {
3007 return null;
3008 }
satok68f1b782011-04-11 14:26:04 +09003009 synchronized (mMethodMap) {
3010 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
3011 // TODO: Handle the case of the last IME with no subtypes
3012 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
3013 || TextUtils.isEmpty(lastIme.second)) return null;
3014 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
3015 if (lastImi == null) return null;
3016 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003017 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003018 final int lastSubtypeId =
3019 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09003020 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
3021 return null;
3022 }
3023 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09003024 } catch (NumberFormatException e) {
3025 return null;
3026 }
3027 }
3028 }
3029
satoke7c6998e2011-06-03 17:57:59 +09003030 @Override
satokee5e77c2011-09-02 18:50:15 +09003031 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003032 if (!calledFromValidUser()) {
3033 return;
3034 }
satok91e88122011-07-18 11:11:42 +09003035 // By this IPC call, only a process which shares the same uid with the IME can add
3036 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08003037 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09003038 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08003039 if (!mSystemReady) {
3040 return;
3041 }
satok91e88122011-07-18 11:11:42 +09003042 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09003043 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003044 final String[] packageInfos;
3045 try {
3046 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3047 } catch (RemoteException e) {
3048 Slog.e(TAG, "Failed to get package infos");
3049 return;
3050 }
satok91e88122011-07-18 11:11:42 +09003051 if (packageInfos != null) {
3052 final int packageNum = packageInfos.length;
3053 for (int i = 0; i < packageNum; ++i) {
3054 if (packageInfos[i].equals(imi.getPackageName())) {
3055 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003056 final long ident = Binder.clearCallingIdentity();
3057 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003058 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003059 } finally {
3060 Binder.restoreCallingIdentity(ident);
3061 }
satokee5e77c2011-09-02 18:50:15 +09003062 return;
satok91e88122011-07-18 11:11:42 +09003063 }
3064 }
3065 }
satoke7c6998e2011-06-03 17:57:59 +09003066 }
satokee5e77c2011-09-02 18:50:15 +09003067 return;
satoke7c6998e2011-06-03 17:57:59 +09003068 }
3069
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003070 @Override
3071 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003072 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003073 }
3074
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003075 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003076 public void clearLastInputMethodWindowForTransition(IBinder token) {
3077 if (!calledFromValidUser()) {
3078 return;
3079 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003080 synchronized (mMethodMap) {
3081 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003082 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003083 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003084 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003085 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003086 }
3087
3088 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003089 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003090 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003091 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003092 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003093 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003094 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3095 if (DEBUG) {
3096 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3097 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3098 + " actual: " + sequenceNumber);
3099 }
3100 return;
3101 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003102 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3103 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003104 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003105 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003106 }
3107 }
3108
satok28203512010-11-24 11:06:49 +09003109 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003111 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3112 }
3113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003115 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3116 if (token == null) {
3117 if (mContext.checkCallingOrSelfPermission(
3118 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3119 != PackageManager.PERMISSION_GRANTED) {
3120 throw new SecurityException(
3121 "Using null token requires permission "
3122 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003124 } else if (mCurToken != token) {
3125 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3126 + " token: " + token);
3127 return;
3128 }
3129
3130 final long ident = Binder.clearCallingIdentity();
3131 try {
3132 setInputMethodLocked(id, subtypeId);
3133 } finally {
3134 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 }
3136 }
3137
satok42c5a162011-05-26 16:46:14 +09003138 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003140 if (!calledFromValidUser()) {
3141 return;
3142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003144 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 return;
3146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 long ident = Binder.clearCallingIdentity();
3148 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003149 hideCurrentInputLocked(flags, null);
3150 } finally {
3151 Binder.restoreCallingIdentity(ident);
3152 }
3153 }
3154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003155
satok42c5a162011-05-26 16:46:14 +09003156 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003157 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003158 if (!calledFromValidUser()) {
3159 return;
3160 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003161 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003162 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003163 return;
3164 }
3165 long ident = Binder.clearCallingIdentity();
3166 try {
3167 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 } finally {
3169 Binder.restoreCallingIdentity(ident);
3170 }
3171 }
3172 }
3173
3174 void setEnabledSessionInMainThread(SessionState session) {
3175 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003176 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003178 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003179 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 } catch (RemoteException e) {
3181 }
3182 }
3183 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003184 if (mEnabledSession != null && mEnabledSession.session != null) {
3185 try {
3186 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3187 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3188 } catch (RemoteException e) {
3189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 }
3191 }
3192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003193
Yohei Yukawa930328c2017-10-18 20:19:53 -07003194 @MainThread
satok42c5a162011-05-26 16:46:14 +09003195 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003197 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003199 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003200 final boolean showAuxSubtypes;
3201 switch (msg.arg1) {
3202 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3203 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3204 // implemented so that auxiliary subtypes will be excluded when the soft
3205 // keyboard is invisible.
3206 showAuxSubtypes = mInputShown;
3207 break;
3208 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3209 showAuxSubtypes = true;
3210 break;
3211 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3212 showAuxSubtypes = false;
3213 break;
3214 default:
3215 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3216 return false;
3217 }
3218 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003219 return true;
3220
satok47a44912010-10-06 16:03:58 +09003221 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003222 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003223 return true;
3224
3225 case MSG_SHOW_IM_CONFIG:
3226 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003227 return true;
3228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 case MSG_UNBIND_INPUT:
3232 try {
3233 ((IInputMethod)msg.obj).unbindInput();
3234 } catch (RemoteException e) {
3235 // There is nothing interesting about the method dying.
3236 }
3237 return true;
3238 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003239 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 try {
3241 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3242 } catch (RemoteException e) {
3243 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003244 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 return true;
3246 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003247 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003249 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003250 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003251 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 } catch (RemoteException e) {
3253 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003254 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 return true;
3256 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003257 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003259 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003260 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003261 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 } catch (RemoteException e) {
3263 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003264 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003266 case MSG_HIDE_CURRENT_INPUT_METHOD:
3267 synchronized (mMethodMap) {
3268 hideCurrentInputLocked(0, null);
3269 }
3270 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003272 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003274 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3276 } catch (RemoteException e) {
3277 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003278 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003280 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003281 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003282 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003283 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003285 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003287 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003288 // Dispose the channel if the input method is not local to this process
3289 // because the remote proxy will get its own copy when unparceled.
3290 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003291 channel.dispose();
3292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003294 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003298
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003299 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003300 final int missingMethods = msg.arg1;
3301 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003302 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003303 final IBinder startInputToken = (IBinder) args.arg1;
3304 final SessionState session = (SessionState) args.arg2;
3305 final IInputContext inputContext = (IInputContext) args.arg3;
3306 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003309 session.method.startInput(startInputToken, inputContext, missingMethods,
3310 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 } catch (RemoteException e) {
3312 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003313 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003318
Yohei Yukawa33e81792015-11-17 21:14:42 -08003319 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003321 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 } catch (RemoteException e) {
3323 // There is nothing interesting about the last client dying.
3324 }
3325 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003326 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003327 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003328 IInputMethodClient client = (IInputMethodClient)args.arg1;
3329 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003331 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003333 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003334 } finally {
3335 // Dispose the channel if the input method is not local to this process
3336 // because the remote proxy will get its own copy when unparceled.
3337 if (res.channel != null && Binder.isProxy(client)) {
3338 res.channel.dispose();
3339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003341 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003343 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003344 case MSG_SET_ACTIVE:
3345 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003346 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003347 } catch (RemoteException e) {
3348 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3349 + ((ClientState)msg.obj).pid + " uid "
3350 + ((ClientState)msg.obj).uid);
3351 }
3352 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003353 case MSG_SET_INTERACTIVE:
3354 handleSetInteractive(msg.arg1 != 0);
3355 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003356 case MSG_SWITCH_IME:
3357 handleSwitchInputMethod(msg.arg1 != 0);
3358 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003359 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3360 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003361 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003362 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003363 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003364 } catch (RemoteException e) {
3365 Slog.w(TAG, "Got RemoteException sending "
3366 + "setUserActionNotificationSequenceNumber("
3367 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003368 + clientState.pid + " uid "
3369 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003370 }
3371 return true;
3372 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003373 case MSG_REPORT_FULLSCREEN_MODE: {
3374 final boolean fullscreen = msg.arg1 != 0;
3375 final ClientState clientState = (ClientState)msg.obj;
3376 try {
3377 clientState.client.reportFullscreenMode(fullscreen);
3378 } catch (RemoteException e) {
3379 Slog.w(TAG, "Got RemoteException sending "
3380 + "reportFullscreen(" + fullscreen + ") notification to pid="
3381 + clientState.pid + " uid=" + clientState.uid);
3382 }
3383 return true;
3384 }
satok01038492012-04-09 21:08:27 +09003385
3386 // --------------------------------------------------------------
3387 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003388 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003389 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003390 case MSG_SYSTEM_UNLOCK_USER:
3391 final int userId = msg.arg1;
3392 onUnlockUser(userId);
3393 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 }
3395 return false;
3396 }
3397
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003398 private void handleSetInteractive(final boolean interactive) {
3399 synchronized (mMethodMap) {
3400 mIsInteractive = interactive;
3401 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3402
3403 // Inform the current client of the change in active status
3404 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003405 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3406 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3407 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003408 }
3409 }
3410 }
3411
Yohei Yukawaae61f712015-12-09 13:00:10 -08003412 private void handleSwitchInputMethod(final boolean forwardDirection) {
3413 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003414 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003415 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003416 if (nextSubtype == null) {
3417 return;
3418 }
3419 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003420 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3421 if (newInputMethodInfo == null) {
3422 return;
3423 }
3424 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3425 newInputMethodInfo, mCurrentSubtype);
3426 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003427 if (mSubtypeSwitchedByShortCutToast == null) {
3428 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3429 Toast.LENGTH_SHORT);
3430 } else {
3431 mSubtypeSwitchedByShortCutToast.setText(toastText);
3432 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003433 mSubtypeSwitchedByShortCutToast.show();
3434 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003435 }
3436 }
3437
satokdc9ddae2011-10-06 12:22:36 +09003438 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003439 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3440 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003441 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003442 if (DEBUG) {
3443 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3444 }
satok723a27e2010-11-11 14:58:11 +09003445 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003446 return true;
3447 }
3448
3449 return false;
3450 }
3451
Yohei Yukawa94e33302016-02-12 19:37:03 -08003452 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003453 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003454 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003455 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003456 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003457 if (!mSystemReady) {
3458 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3459 return;
3460 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003461 mMethodList.clear();
3462 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003463 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003464 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003465
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003466 // Use for queryIntentServicesAsUser
3467 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003469 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3470 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3471 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003472 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003474 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3475 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003476
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003477 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003478 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 for (int i = 0; i < services.size(); ++i) {
3480 ResolveInfo ri = services.get(i);
3481 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003482 final String imeId = InputMethodInfo.computeId(ri);
3483 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3484 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 + ": it does not require the permission "
3486 + android.Manifest.permission.BIND_INPUT_METHOD);
3487 continue;
3488 }
3489
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003490 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003492 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 try {
satoke7c6998e2011-06-03 17:57:59 +09003494 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003495 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003496 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003497 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498
3499 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003500 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003502 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003503 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 }
3505 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003506
Yohei Yukawac4e44912017-02-09 19:30:22 -08003507 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3508 // negatives when the package state remains to be the same but only the component state is
3509 // changed.
3510 {
3511 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3512 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3513 // conservative, but it seems we cannot use it for now (Issue 35176630).
3514 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3515 new Intent(InputMethod.SERVICE_INTERFACE),
3516 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3517 final int N = allInputMethodServices.size();
3518 for (int i = 0; i < N; ++i) {
3519 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003520 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3521 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003522 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003523 }
3524 }
3525
Yohei Yukawa859df052016-02-17 07:56:46 -08003526 // TODO: The following code should find better place to live.
3527 if (!resetDefaultEnabledIme) {
3528 boolean enabledImeFound = false;
3529 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3530 final int N = enabledImes.size();
3531 for (int i = 0; i < N; ++i) {
3532 final InputMethodInfo imi = enabledImes.get(i);
3533 if (mMethodList.contains(imi)) {
3534 enabledImeFound = true;
3535 break;
3536 }
3537 }
3538 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003539 if (DEBUG) {
3540 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3541 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003542 resetDefaultEnabledIme = true;
3543 resetSelectedInputMethodAndSubtypeLocked("");
3544 }
3545 }
3546
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003547 if (resetDefaultEnabledIme) {
3548 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003549 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003550 final int N = defaultEnabledIme.size();
3551 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003552 final InputMethodInfo imi = defaultEnabledIme.get(i);
3553 if (DEBUG) {
3554 Slog.d(TAG, "--- enable ime = " + imi);
3555 }
3556 setInputMethodEnabledLocked(imi.getId(), true);
3557 }
3558 }
3559
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003560 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003561 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003562 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003563 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3564 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003565 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003566 }
3567 } else {
3568 // Double check that the default IME is certainly enabled.
3569 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003570 }
3571 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003572 // Here is not the perfect place to reset the switching controller. Ideally
3573 // mSwitchingController and mSettings should be able to share the same state.
3574 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3575 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003576 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003580
satok217f5482010-12-15 05:19:19 +09003581 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003582 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003583 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003584 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3585 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003586 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003587 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003588 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003589 final int userId;
3590 synchronized (mMethodMap) {
3591 userId = mSettings.getCurrentUserId();
3592 }
3593 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003594 }
3595
3596 private void showConfigureInputMethods() {
3597 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3598 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3599 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3600 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003601 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003602 }
3603
satok2c93efc2012-04-02 19:33:47 +09003604 private boolean isScreenLocked() {
3605 return mKeyguardManager != null
3606 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3607 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003608
Seigo Nonaka14e13912015-05-06 21:04:13 -07003609 private void showInputMethodMenu(boolean showAuxSubtypes) {
3610 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611
satok2c93efc2012-04-02 19:33:47 +09003612 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003613
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003614 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003615 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003616 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003617
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003618 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003619 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003620 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3621 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003622 if (immis == null || immis.size() == 0) {
3623 return;
3624 }
3625
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003626 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627
satok688bd472012-02-09 20:09:17 +09003628 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003629 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003630 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003631
satokc3690562012-01-10 20:14:43 +09003632 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003633 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003634 if (currentSubtype != null) {
3635 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003636 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3637 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003638 }
3639 }
3640
Ken Wakasa761eb372011-03-04 19:06:18 +09003641 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003642 mIms = new InputMethodInfo[N];
3643 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003644 int checkedItem = 0;
3645 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003646 final ImeSubtypeListItem item = imList.get(i);
3647 mIms[i] = item.mImi;
3648 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003649 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003650 int subtypeId = mSubtypeIds[i];
3651 if ((subtypeId == NOT_A_SUBTYPE_ID)
3652 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3653 || (subtypeId == lastInputMethodSubtypeId)) {
3654 checkedItem = i;
3655 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003658
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003659 final Context settingsContext = new ContextThemeWrapper(
3660 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003661 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3662
3663 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003664 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3665 @Override
3666 public void onCancel(DialogInterface dialog) {
3667 hideInputMethodMenu();
3668 }
3669 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003670
3671 final Context dialogContext = mDialogBuilder.getContext();
3672 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3673 com.android.internal.R.styleable.DialogPreference,
3674 com.android.internal.R.attr.alertDialogStyle, 0);
3675 final Drawable dialogIcon = a.getDrawable(
3676 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3677 a.recycle();
3678
3679 mDialogBuilder.setIcon(dialogIcon);
3680
Yohei Yukawad34e1482016-02-11 08:03:52 -08003681 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003682 final View tv = inflater.inflate(
3683 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3684 mDialogBuilder.setCustomTitle(tv);
3685
3686 // Setup layout for a toggle switch of the hardware keyboard
3687 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003688 mSwitchingDialogTitleView
3689 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003690 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003691 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003692 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003693 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003694 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003695 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3696 @Override
3697 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003698 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003699 // Ensure that the input method dialog is dismissed when changing
3700 // the hardware keyboard state.
3701 hideInputMethodMenu();
3702 }
3703 });
3704
Alan Viverette505e3ab2014-11-24 15:22:11 -08003705 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003706 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3707 final OnClickListener choiceListener = new OnClickListener() {
3708 @Override
3709 public void onClick(final DialogInterface dialog, final int which) {
3710 synchronized (mMethodMap) {
3711 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3712 || mSubtypeIds.length <= which) {
3713 return;
satok01038492012-04-09 21:08:27 +09003714 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003715 final InputMethodInfo im = mIms[which];
3716 int subtypeId = mSubtypeIds[which];
3717 adapter.mCheckedItem = which;
3718 adapter.notifyDataSetChanged();
3719 hideInputMethodMenu();
3720 if (im != null) {
3721 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3722 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003723 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003724 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003726 }
3727 }
3728 };
3729 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003732 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003733 final Window w = mSwitchingDialog.getWindow();
3734 final WindowManager.LayoutParams attrs = w.getAttributes();
3735 w.setType(TYPE_INPUT_METHOD_DIALOG);
3736 // Use an alternate token for the dialog for that window manager can group the token
3737 // with other IME windows based on type vs. grouping based on whichever token happens
3738 // to get selected by the system later on.
3739 attrs.token = mSwitchingDialogToken;
3740 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3741 attrs.setTitle("Select input method");
3742 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003743 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 mSwitchingDialog.show();
3745 }
3746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003747
Ken Wakasa05dbb652011-08-22 15:22:43 +09003748 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3749 private final LayoutInflater mInflater;
3750 private final int mTextViewResourceId;
3751 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003752 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003753 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3754 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3755 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003756
Ken Wakasa05dbb652011-08-22 15:22:43 +09003757 mTextViewResourceId = textViewResourceId;
3758 mItemsList = itemsList;
3759 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003760 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003761 }
3762
3763 @Override
3764 public View getView(int position, View convertView, ViewGroup parent) {
3765 final View view = convertView != null ? convertView
3766 : mInflater.inflate(mTextViewResourceId, null);
3767 if (position < 0 || position >= mItemsList.size()) return view;
3768 final ImeSubtypeListItem item = mItemsList.get(position);
3769 final CharSequence imeName = item.mImeName;
3770 final CharSequence subtypeName = item.mSubtypeName;
3771 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3772 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3773 if (TextUtils.isEmpty(subtypeName)) {
3774 firstTextView.setText(imeName);
3775 secondTextView.setVisibility(View.GONE);
3776 } else {
3777 firstTextView.setText(subtypeName);
3778 secondTextView.setText(imeName);
3779 secondTextView.setVisibility(View.VISIBLE);
3780 }
3781 final RadioButton radioButton =
3782 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3783 radioButton.setChecked(position == mCheckedItem);
3784 return view;
3785 }
3786 }
3787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003789 synchronized (mMethodMap) {
3790 hideInputMethodMenuLocked();
3791 }
3792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003793
The Android Open Source Project10592532009-03-18 17:39:46 -07003794 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003795 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796
The Android Open Source Project10592532009-03-18 17:39:46 -07003797 if (mSwitchingDialog != null) {
3798 mSwitchingDialog.dismiss();
3799 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003801
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003802 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003803 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003804 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003808
satok42c5a162011-05-26 16:46:14 +09003809 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003811 // TODO: Make this work even for non-current users?
3812 if (!calledFromValidUser()) {
3813 return false;
3814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 synchronized (mMethodMap) {
3816 if (mContext.checkCallingOrSelfPermission(
3817 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3818 != PackageManager.PERMISSION_GRANTED) {
3819 throw new SecurityException(
3820 "Requires permission "
3821 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3822 }
Anna Galusza9b278112016-01-04 11:37:37 -08003823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 long ident = Binder.clearCallingIdentity();
3825 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003826 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 } finally {
3828 Binder.restoreCallingIdentity(ident);
3829 }
3830 }
3831 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003832
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003833 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3834 // Make sure this is a valid input method.
3835 InputMethodInfo imm = mMethodMap.get(id);
3836 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003837 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003838 }
3839
satokd87c2592010-09-29 11:52:06 +09003840 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3841 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003842
satokd87c2592010-09-29 11:52:06 +09003843 if (enabled) {
3844 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3845 if (pair.first.equals(id)) {
3846 // We are enabling this input method, but it is already enabled.
3847 // Nothing to do. The previous state was enabled.
3848 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003849 }
3850 }
satokd87c2592010-09-29 11:52:06 +09003851 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3852 // Previous state was disabled.
3853 return false;
3854 } else {
3855 StringBuilder builder = new StringBuilder();
3856 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3857 builder, enabledInputMethodsList, id)) {
3858 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003859 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003860 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3861 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3862 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003863 }
3864 // Previous state was enabled.
3865 return true;
3866 } else {
3867 // We are disabling the input method but it is already disabled.
3868 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003869 return false;
3870 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003871 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003872 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003873
satok723a27e2010-11-11 14:58:11 +09003874 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3875 boolean setSubtypeOnly) {
3876 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003877 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003878
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003879 mCurUserActionNotificationSequenceNumber =
3880 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3881 if (DEBUG) {
3882 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3883 + mCurUserActionNotificationSequenceNumber);
3884 }
3885
3886 if (mCurClient != null && mCurClient.client != null) {
3887 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3888 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003889 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003890 }
3891
satok723a27e2010-11-11 14:58:11 +09003892 // Set Subtype here
3893 if (imi == null || subtypeId < 0) {
3894 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003895 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003896 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003897 if (subtypeId < imi.getSubtypeCount()) {
3898 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3899 mSettings.putSelectedSubtype(subtype.hashCode());
3900 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003901 } else {
3902 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003903 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003904 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003905 }
satokab751aa2010-09-14 19:17:36 +09003906 }
satok723a27e2010-11-11 14:58:11 +09003907
Yohei Yukawa68645a62016-02-17 07:54:20 -08003908 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003909 // Set InputMethod here
3910 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3911 }
3912 }
3913
3914 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3915 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3916 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3917 // newDefaultIme is empty when there is no candidate for the selected IME.
3918 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3919 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3920 if (subtypeHashCode != null) {
3921 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003922 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003923 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003924 } catch (NumberFormatException e) {
3925 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3926 }
3927 }
3928 }
3929 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003930 }
3931
satok4e4569d2010-11-19 18:45:53 +09003932 // If there are no selected shortcuts, tries finding the most applicable ones.
3933 private Pair<InputMethodInfo, InputMethodSubtype>
3934 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3935 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3936 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003937 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003938 boolean foundInSystemIME = false;
3939
3940 // Search applicable subtype for each InputMethodInfo
3941 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003942 final String imiId = imi.getId();
3943 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3944 continue;
3945 }
satokcd7cd292010-11-20 15:46:23 +09003946 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003947 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003948 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003949 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003950 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003951 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003952 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003953 }
satokdf31ae62011-01-15 06:19:44 +09003954 // 2. Search by the system locale from enabledSubtypes.
3955 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003956 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003957 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003958 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003959 }
satoka86f5e42011-09-02 17:12:42 +09003960 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003961 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003962 final ArrayList<InputMethodSubtype> subtypesForSearch =
3963 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003964 ? InputMethodUtils.getSubtypes(imi)
3965 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003966 // 4. Search by the current subtype's locale from all subtypes.
3967 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003968 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003969 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003970 }
3971 // 5. Search by the system locale from all subtypes.
3972 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003973 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003974 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003975 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003976 }
satokcd7cd292010-11-20 15:46:23 +09003977 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003978 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003979 // The current input method is the most applicable IME.
3980 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003981 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003982 break;
satok7599a7f2010-12-22 13:45:23 +09003983 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003984 // The system input method is 2nd applicable IME.
3985 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003986 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003987 if ((imi.getServiceInfo().applicationInfo.flags
3988 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3989 foundInSystemIME = true;
3990 }
satok4e4569d2010-11-19 18:45:53 +09003991 }
3992 }
3993 }
3994 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003995 if (mostApplicableIMI != null) {
3996 Slog.w(TAG, "Most applicable shortcut input method was:"
3997 + mostApplicableIMI.getId());
3998 if (mostApplicableSubtype != null) {
3999 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
4000 + "," + mostApplicableSubtype.getMode() + ","
4001 + mostApplicableSubtype.getLocale());
4002 }
4003 }
satok4e4569d2010-11-19 18:45:53 +09004004 }
satokcd7cd292010-11-20 15:46:23 +09004005 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004006 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09004007 } else {
4008 return null;
4009 }
4010 }
4011
satokab751aa2010-09-14 19:17:36 +09004012 /**
4013 * @return Return the current subtype of this input method.
4014 */
satok42c5a162011-05-26 16:46:14 +09004015 @Override
satokab751aa2010-09-14 19:17:36 +09004016 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004017 // TODO: Make this work even for non-current users?
4018 if (!calledFromValidUser()) {
4019 return null;
4020 }
4021 synchronized (mMethodMap) {
4022 return getCurrentInputMethodSubtypeLocked();
4023 }
4024 }
4025
4026 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09004027 if (mCurMethodId == null) {
4028 return null;
4029 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004030 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004031 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
4032 if (imi == null || imi.getSubtypeCount() == 0) {
4033 return null;
satok4e4569d2010-11-19 18:45:53 +09004034 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004035 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004036 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
4037 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004038 if (subtypeId == NOT_A_SUBTYPE_ID) {
4039 // If there are no selected subtypes, the framework will try to find
4040 // the most applicable subtype from explicitly or implicitly enabled
4041 // subtypes.
4042 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004043 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004044 // If there is only one explicitly or implicitly enabled subtype,
4045 // just returns it.
4046 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4047 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4048 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004049 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004050 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004051 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004052 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004053 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004054 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4055 true);
satok4e4569d2010-11-19 18:45:53 +09004056 }
satok3ef8b292010-11-23 06:06:29 +09004057 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004058 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004059 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004060 }
4061 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004062 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004063 }
4064
satok4e4569d2010-11-19 18:45:53 +09004065 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004066 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004067 @Override
satok4e4569d2010-11-19 18:45:53 +09004068 public List getShortcutInputMethodsAndSubtypes() {
4069 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004070 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004071 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004072 // If there are no selected shortcut subtypes, the framework will try to find
4073 // the most applicable subtype from all subtypes whose mode is
4074 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004075 Pair<InputMethodInfo, InputMethodSubtype> info =
4076 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004077 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004078 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004079 ret.add(info.first);
4080 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004081 }
satok3da92232011-01-11 22:46:30 +09004082 return ret;
satokf3db1af2010-11-23 13:34:33 +09004083 }
satokf3db1af2010-11-23 13:34:33 +09004084 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4085 ret.add(imi);
4086 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4087 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004088 }
4089 }
satokf3db1af2010-11-23 13:34:33 +09004090 return ret;
satok4e4569d2010-11-19 18:45:53 +09004091 }
4092 }
4093
satok42c5a162011-05-26 16:46:14 +09004094 @Override
satokb66d2872010-11-10 01:04:04 +09004095 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004096 // TODO: Make this work even for non-current users?
4097 if (!calledFromValidUser()) {
4098 return false;
4099 }
satokb66d2872010-11-10 01:04:04 +09004100 synchronized (mMethodMap) {
4101 if (subtype != null && mCurMethodId != null) {
4102 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004103 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004104 if (subtypeId != NOT_A_SUBTYPE_ID) {
4105 setInputMethodLocked(mCurMethodId, subtypeId);
4106 return true;
4107 }
4108 }
4109 return false;
4110 }
4111 }
4112
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004113 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004114 private static class InputMethodFileManager {
4115 private static final String SYSTEM_PATH = "system";
4116 private static final String INPUT_METHOD_PATH = "inputmethod";
4117 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4118 private static final String NODE_SUBTYPES = "subtypes";
4119 private static final String NODE_SUBTYPE = "subtype";
4120 private static final String NODE_IMI = "imi";
4121 private static final String ATTR_ID = "id";
4122 private static final String ATTR_LABEL = "label";
4123 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004124 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004125 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004126 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004127 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4128 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4129 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004130 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004131 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4132 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004133 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004134 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004135 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004136 if (methodMap == null) {
4137 throw new NullPointerException("methodMap is null");
4138 }
4139 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004140 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004141 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4142 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004143 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004144 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004145 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4146 }
4147 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4148 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4149 if (!subtypeFile.exists()) {
4150 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004151 writeAdditionalInputMethodSubtypes(
4152 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004153 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004154 readAdditionalInputMethodSubtypes(
4155 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004156 }
4157 }
4158
4159 private void deleteAllInputMethodSubtypes(String imiId) {
4160 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004161 mAdditionalSubtypesMap.remove(imiId);
4162 writeAdditionalInputMethodSubtypes(
4163 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004164 }
4165 }
4166
4167 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004168 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004169 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004170 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004171 final int N = additionalSubtypes.length;
4172 for (int i = 0; i < N; ++i) {
4173 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004174 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004175 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004176 } else {
4177 Slog.w(TAG, "Duplicated subtype definition found: "
4178 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004179 }
4180 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004181 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4182 writeAdditionalInputMethodSubtypes(
4183 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004184 }
4185 }
4186
4187 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4188 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004189 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004190 }
4191 }
4192
4193 private static void writeAdditionalInputMethodSubtypes(
4194 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4195 HashMap<String, InputMethodInfo> methodMap) {
4196 // Safety net for the case that this function is called before methodMap is set.
4197 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4198 FileOutputStream fos = null;
4199 try {
4200 fos = subtypesFile.startWrite();
4201 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004202 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004203 out.startDocument(null, true);
4204 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4205 out.startTag(null, NODE_SUBTYPES);
4206 for (String imiId : allSubtypes.keySet()) {
4207 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4208 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4209 continue;
4210 }
4211 out.startTag(null, NODE_IMI);
4212 out.attribute(null, ATTR_ID, imiId);
4213 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4214 final int N = subtypesList.size();
4215 for (int i = 0; i < N; ++i) {
4216 final InputMethodSubtype subtype = subtypesList.get(i);
4217 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004218 if (subtype.hasSubtypeId()) {
4219 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4220 String.valueOf(subtype.getSubtypeId()));
4221 }
satoke7c6998e2011-06-03 17:57:59 +09004222 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4223 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4224 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004225 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4226 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004227 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4228 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4229 out.attribute(null, ATTR_IS_AUXILIARY,
4230 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004231 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4232 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004233 out.endTag(null, NODE_SUBTYPE);
4234 }
4235 out.endTag(null, NODE_IMI);
4236 }
4237 out.endTag(null, NODE_SUBTYPES);
4238 out.endDocument();
4239 subtypesFile.finishWrite(fos);
4240 } catch (java.io.IOException e) {
4241 Slog.w(TAG, "Error writing subtypes", e);
4242 if (fos != null) {
4243 subtypesFile.failWrite(fos);
4244 }
4245 }
4246 }
4247
4248 private static void readAdditionalInputMethodSubtypes(
4249 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4250 if (allSubtypes == null || subtypesFile == null) return;
4251 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004252 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004253 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004254 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004255 int type = parser.getEventType();
4256 // Skip parsing until START_TAG
4257 while ((type = parser.next()) != XmlPullParser.START_TAG
4258 && type != XmlPullParser.END_DOCUMENT) {}
4259 String firstNodeName = parser.getName();
4260 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4261 throw new XmlPullParserException("Xml doesn't start with subtypes");
4262 }
4263 final int depth =parser.getDepth();
4264 String currentImiId = null;
4265 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4266 while (((type = parser.next()) != XmlPullParser.END_TAG
4267 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4268 if (type != XmlPullParser.START_TAG)
4269 continue;
4270 final String nodeName = parser.getName();
4271 if (NODE_IMI.equals(nodeName)) {
4272 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4273 if (TextUtils.isEmpty(currentImiId)) {
4274 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4275 continue;
4276 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004277 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004278 allSubtypes.put(currentImiId, tempSubtypesArray);
4279 } else if (NODE_SUBTYPE.equals(nodeName)) {
4280 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4281 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4282 continue;
4283 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004284 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004285 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004286 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004287 parser.getAttributeValue(null, ATTR_LABEL));
4288 final String imeSubtypeLocale =
4289 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004290 final String languageTag =
4291 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004292 final String imeSubtypeMode =
4293 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4294 final String imeSubtypeExtraValue =
4295 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004296 final boolean isAuxiliary = "1".equals(String.valueOf(
4297 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004298 final boolean isAsciiCapable = "1".equals(String.valueOf(
4299 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004300 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004301 .setSubtypeNameResId(label)
4302 .setSubtypeIconResId(icon)
4303 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004304 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004305 .setSubtypeMode(imeSubtypeMode)
4306 .setSubtypeExtraValue(imeSubtypeExtraValue)
4307 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004308 .setIsAsciiCapable(isAsciiCapable);
4309 final String subtypeIdString =
4310 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4311 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004312 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004313 }
4314 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004315 }
4316 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004317 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4318 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004319 return;
satoke7c6998e2011-06-03 17:57:59 +09004320 }
4321 }
4322 }
4323
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004324 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4325 @NonNull
4326 private final Handler mHandler;
4327
4328 LocalServiceImpl(@NonNull final Handler handler) {
4329 mHandler = handler;
4330 }
4331
4332 @Override
4333 public void setInteractive(boolean interactive) {
4334 // Do everything in handler so as not to block the caller.
4335 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4336 interactive ? 1 : 0, 0));
4337 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004338
4339 @Override
4340 public void switchInputMethod(boolean forwardDirection) {
4341 // Do everything in handler so as not to block the caller.
4342 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4343 forwardDirection ? 1 : 0, 0));
4344 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004345
4346 @Override
4347 public void hideCurrentInputMethod() {
4348 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4349 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4350 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004351 }
4352
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004353 private static String imeWindowStatusToString(final int imeWindowVis) {
4354 final StringBuilder sb = new StringBuilder();
4355 boolean first = true;
4356 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4357 sb.append("Active");
4358 first = false;
4359 }
4360 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4361 if (!first) {
4362 sb.append("|");
4363 }
4364 sb.append("Visible");
4365 }
4366 return sb.toString();
4367 }
4368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004370 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4371 @Nullable Uri contentUri, @Nullable String packageName) {
4372 if (!calledFromValidUser()) {
4373 return null;
4374 }
4375
4376 if (token == null) {
4377 throw new NullPointerException("token");
4378 }
4379 if (packageName == null) {
4380 throw new NullPointerException("packageName");
4381 }
4382 if (contentUri == null) {
4383 throw new NullPointerException("contentUri");
4384 }
4385 final String contentUriScheme = contentUri.getScheme();
4386 if (!"content".equals(contentUriScheme)) {
4387 throw new InvalidParameterException("contentUri must have content scheme");
4388 }
4389
4390 synchronized (mMethodMap) {
4391 final int uid = Binder.getCallingUid();
4392 if (mCurMethodId == null) {
4393 return null;
4394 }
4395 if (mCurToken != token) {
4396 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4397 + " token=" + token);
4398 return null;
4399 }
4400 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4401 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4402 // nature of our system. Let's compare it with our internal record.
4403 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4404 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4405 + mCurAttribute.packageName + " packageName=" + packageName);
4406 return null;
4407 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004408 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004409 final int imeUserId = UserHandle.getUserId(uid);
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 appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004412 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4413 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4414 imeUserId);
4415 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4416 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4417 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4418 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4419 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4420 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4421 // actually allowed to "uid", which is guaranteed to be the IME's one.
4422 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4423 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004424 }
4425 }
4426
4427 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004428 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4429 if (!calledFromValidUser()) {
4430 return;
4431 }
4432 synchronized (mMethodMap) {
4433 if (!calledWithValidToken(token)) {
4434 return;
4435 }
4436 if (mCurClient != null && mCurClient.client != null) {
4437 mInFullscreenMode = fullscreen;
4438 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4439 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4440 }
4441 }
4442 }
4443
4444 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004446 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447
4448 IInputMethod method;
4449 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004450 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 synchronized (mMethodMap) {
4455 p.println("Current Input Method Manager state:");
4456 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004457 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 for (int i=0; i<N; i++) {
4459 InputMethodInfo info = mMethodList.get(i);
4460 p.println(" InputMethod #" + i + ":");
4461 info.dump(p, " ");
4462 }
4463 p.println(" Clients:");
4464 for (ClientState ci : mClients.values()) {
4465 p.println(" Client " + ci + ":");
4466 p.println(" client=" + ci.client);
4467 p.println(" inputContext=" + ci.inputContext);
4468 p.println(" sessionRequested=" + ci.sessionRequested);
4469 p.println(" curSession=" + ci.curSession);
4470 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004471 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004473 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004474 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4475 + " softInputMode=" +
4476 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4477 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004478 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4480 + " mBoundToMethod=" + mBoundToMethod);
4481 p.println(" mCurToken=" + mCurToken);
4482 p.println(" mCurIntent=" + mCurIntent);
4483 method = mCurMethod;
4484 p.println(" mCurMethod=" + mCurMethod);
4485 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004486 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 p.println(" mShowRequested=" + mShowRequested
4488 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4489 + " mShowForced=" + mShowForced
4490 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004491 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004492 p.println(" mCurUserActionNotificationSequenceNumber="
4493 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004494 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004495 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004496 p.println(" mSwitchingController:");
4497 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004498 p.println(" mSettings:");
4499 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004500
4501 p.println(" mStartInputHistory:");
4502 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004504
Jeff Brownb88102f2010-09-08 11:49:43 -07004505 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 pw.flush();
4508 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004509 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4510 } catch (IOException | RemoteException e) {
4511 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004513 } else {
4514 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004516
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004517 if (focusedWindowClient != null && client != focusedWindowClient) {
4518 p.println(" ");
4519 p.println("Warning: Current input method client doesn't match the last focused. "
4520 + "window.");
4521 p.println("Dumping input method client in the last focused window just in case.");
4522 p.println(" ");
4523 pw.flush();
4524 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004525 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4526 } catch (IOException | RemoteException e) {
4527 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004528 }
4529 }
4530
Jeff Brownb88102f2010-09-08 11:49:43 -07004531 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 pw.flush();
4534 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004535 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4536 } catch (IOException | RemoteException e) {
4537 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004539 } else {
4540 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 }
4542 }
4543}