blob: 192fa3a2bb384142b3b497e90bdd8c533291bafd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
Chris Wren282cfef2017-03-27 15:01:44 -040031import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050032import com.android.internal.notification.SystemNotificationChannels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070034import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070035import com.android.internal.os.TransferPipe;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060036import com.android.internal.util.DumpUtils;
satoke7c6998e2011-06-03 17:57:59 +090037import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import com.android.internal.view.IInputContext;
39import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.IInputMethodClient;
41import com.android.internal.view.IInputMethodManager;
42import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070043import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080045import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080046import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
satoke7c6998e2011-06-03 17:57:59 +090048import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090050import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Yohei Yukawad6475a62017-04-17 10:35:27 -070052import android.annotation.BinderThread;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070053import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070054import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070055import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080056import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080057import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070058import android.app.ActivityManagerInternal;
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -070059import android.app.ActivityThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070061import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090062import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090063import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090064import android.app.Notification;
65import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070066import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090067import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080069import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.ContentResolver;
71import android.content.Context;
72import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090074import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090076import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070078import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090079import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.content.pm.PackageManager;
81import android.content.pm.ResolveInfo;
82import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070083import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.content.res.Resources;
85import android.content.res.TypedArray;
86import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080087import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080088import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080089import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070090import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040092import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070093import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090094import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.os.Handler;
96import android.os.IBinder;
97import android.os.IInterface;
98import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070099import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -0700101import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800103import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.os.ServiceManager;
105import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900106import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800107import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.provider.Settings;
109import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900110import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700111import android.util.ArrayMap;
112import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700113import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900115import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900116import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.util.PrintWriterPrinter;
118import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900119import android.util.Slog;
120import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900121import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700123import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.view.LayoutInflater;
125import android.view.View;
126import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700127import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700129import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900130import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800132import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700133import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import android.view.inputmethod.InputMethod;
135import android.view.inputmethod.InputMethodInfo;
136import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700137import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900138import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900139import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900140import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900141import android.widget.CompoundButton;
142import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900143import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900144import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900145import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700146import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
satoke7c6998e2011-06-03 17:57:59 +0900148import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900150import java.io.FileInputStream;
151import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.io.IOException;
153import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700154import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100155import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700156import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800157import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900159import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800160import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161import java.util.HashMap;
162import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800163import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800164import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800165import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
167/**
168 * This class provides a system service that manages input methods.
169 */
170public class InputMethodManagerService extends IInputMethodManager.Stub
171 implements ServiceConnection, Handler.Callback {
172 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700173 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700174 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700176 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
177 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
178 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 static final int MSG_UNBIND_INPUT = 1000;
181 static final int MSG_BIND_INPUT = 1010;
182 static final int MSG_SHOW_SOFT_INPUT = 1020;
183 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700184 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 static final int MSG_ATTACH_TOKEN = 1040;
186 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
Yohei Yukawa33e81792015-11-17 21:14:42 -0800190 static final int MSG_UNBIND_CLIENT = 3000;
191 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700192 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700193 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900194 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800195 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800196 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
satok01038492012-04-09 21:08:27 +0900198 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
199
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700200 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
201
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700202 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
satokf9f01002011-05-19 21:31:50 +0900204 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
205
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900206 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900207 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900208
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700209 /**
210 * Binding flags for establishing connection to the {@link InputMethodService}.
211 */
212 private static final int IME_CONNECTION_BIND_FLAGS =
213 Context.BIND_AUTO_CREATE
214 | Context.BIND_NOT_VISIBLE
215 | Context.BIND_NOT_FOREGROUND
Dianne Hackborn83b40f62017-04-26 13:59:47 -0700216 | Context.BIND_IMPORTANT_BACKGROUND
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700217 | Context.BIND_SHOWING_UI;
218
219 /**
220 * Binding flags used only while the {@link InputMethodService} is showing window.
221 */
222 private static final int IME_VISIBLE_BIND_FLAGS =
223 Context.BIND_AUTO_CREATE
224 | Context.BIND_TREAT_LIKE_ACTIVITY
225 | Context.BIND_FOREGROUND_SERVICE;
226
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700227 @Retention(SOURCE)
228 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
229 private @interface HardKeyboardBehavior {
230 int WIRELESS_AFFORDANCE = 0;
231 int WIRED_AFFORDANCE = 1;
232 }
satok4e4569d2010-11-19 18:45:53 +0900233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800235 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900237 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700240 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700242 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900243 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900244 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900245 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800246 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800247
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900248 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 // All known input methods. mMethodMap also serves as the global
251 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700252 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
253 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900254 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700255 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900256 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
Yohei Yukawae0733062017-02-09 22:49:35 -0800258 /**
259 * Tracks how many times {@link #mMethodMap} was updated.
260 */
261 @GuardedBy("mMethodMap")
262 private int mMethodMapUpdateCount = 0;
263
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700264 // Used to bring IME service up to visible adjustment while it is being shown.
265 final ServiceConnection mVisibleConnection = new ServiceConnection() {
266 @Override public void onServiceConnected(ComponentName name, IBinder service) {
267 }
268
269 @Override public void onServiceDisconnected(ComponentName name) {
270 }
271 };
272 boolean mVisibleBound = false;
273
satok7cfc0ed2011-06-20 21:29:36 +0900274 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700275 private NotificationManager mNotificationManager;
276 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700277 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400278 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700279 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900280 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900281 private boolean mNotificationShown;
282
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900283 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 final ClientState client;
285 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700286
287 IInputMethodSession session;
288 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 @Override
291 public String toString() {
292 return "SessionState{uid " + client.uid + " pid " + client.pid
293 + " method " + Integer.toHexString(
294 System.identityHashCode(method))
295 + " session " + Integer.toHexString(
296 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700297 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 + "}";
299 }
300
301 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700302 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 client = _client;
304 method = _method;
305 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700306 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
Jeff Brownc28867a2013-03-26 15:42:39 -0700310 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 final IInputMethodClient client;
312 final IInputContext inputContext;
313 final int uid;
314 final int pid;
315 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 boolean sessionRequested;
318 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 @Override
321 public String toString() {
322 return "ClientState{" + Integer.toHexString(
323 System.identityHashCode(this)) + " uid " + uid
324 + " pid " + pid + "}";
325 }
326
327 ClientState(IInputMethodClient _client, IInputContext _inputContext,
328 int _uid, int _pid) {
329 client = _client;
330 inputContext = _inputContext;
331 uid = _uid;
332 pid = _pid;
333 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
334 }
335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700337 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700340 * Set once the system is ready to run third party code.
341 */
342 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700344 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700345 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
346 * method. This is to be synchronized with the secure settings keyed with
347 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
348 *
349 * <p>This can be transiently {@code null} when the system is re-initializing input method
350 * settings, e.g., the system locale is just changed.</p>
351 *
352 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
353 * {@link InputMethodManagerService}.</p>
354 *
355 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700357 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * The current binding sequence number, incremented every time there is
362 * a new bind performed.
363 */
364 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * The client that is currently bound to an input method.
368 */
369 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800372 * The last window token that we confirmed to be focused. This is always updated upon reports
373 * from the input method client. If the window state is already changed before the report is
374 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700375 */
376 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700378 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800379 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
380 *
381 * @see #mCurFocusedWindow
382 */
383 int mCurFocusedWindowSoftInputMode;
384
385 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800386 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
387 */
388 ClientState mCurFocusedWindowClient;
389
390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * The input context last provided by the current client.
392 */
393 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700396 * The missing method flags for the input context last provided by the current client.
397 *
398 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
399 */
400 int mCurInputContextMissingMethods;
401
402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 * The attributes last provided by the current client.
404 */
405 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700408 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700410 *
411 * <p>This can be {@code null} when no input method is connected.</p>
412 *
413 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700415 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
satokab751aa2010-09-14 19:17:36 +0900419 * The current subtype of the current input method.
420 */
421 private InputMethodSubtype mCurrentSubtype;
422
satok4e4569d2010-11-19 18:45:53 +0900423 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900424 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700425 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900426
John Spurlocke0980502013-10-25 11:59:29 -0400427 // Was the keyguard locked when this client became current?
428 private boolean mCurClientInKeyguard;
429
satokab751aa2010-09-14 19:17:36 +0900430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * Set to true if our ServiceConnection is currently actively bound to
432 * a service (whether or not we have gotten its IBinder back yet).
433 */
434 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Set if the client has asked for the input method to be shown.
438 */
439 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
442 * Set if we were explicitly told to show the input method.
443 */
444 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 /**
447 * Set if we were forced to be shown.
448 */
449 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
452 * Set if we last told the input method to show itself.
453 */
454 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800457 * {@code true} if the current input method is in fullscreen mode.
458 */
459 boolean mInFullscreenMode;
460
461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 * The Intent used to connect to the current input method.
463 */
464 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
467 * The token we have made for the currently active input method, to
468 * identify it in the future.
469 */
470 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 /**
473 * If non-null, this is the input method service we are currently connected
474 * to.
475 */
476 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 /**
479 * Time that we last initiated a bind to the input method, to determine
480 * if we should try to disconnect and reconnect to it.
481 */
482 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 /**
485 * Have we called mCurMethod.bindInput()?
486 */
487 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 /**
490 * Currently enabled session. Only touched by service thread, not
491 * protected by a lock.
492 */
493 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700496 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700498 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900500 int mCurUserActionNotificationSequenceNumber = 0;
501
Joe Onorato857fd9b2011-01-27 15:08:35 -0800502 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900503
504 /**
505 * A set of status bits regarding the active IME.
506 *
507 * <p>This value is a combination of following two bits:</p>
508 * <dl>
509 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
510 * <dd>
511 * If this bit is ON, connected IME is ready to accept touch/key events.
512 * </dd>
513 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
514 * <dd>
515 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
516 * </dd>
517 * </dl>
518 * <em>Do not update this value outside of setImeWindowStatus.</em>
519 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800520 int mImeWindowVis;
521
Ken Wakasa05dbb652011-08-22 15:22:43 +0900522 private AlertDialog.Builder mDialogBuilder;
523 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700524 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900525 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700526 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900527 private InputMethodInfo[] mIms;
528 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800529 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700530 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800531 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900532 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
533 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500534 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700535 @HardKeyboardBehavior
536 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800537
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800538 /**
539 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
540 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
541 * will not affect those tasks that are already posted.
542 *
543 * <p>Posting {@link #MSG_START_INPUT} message basically means that
544 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
545 * back in the current IME process shortly, which will also affect what the current IME starts
546 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
547 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
548 * logical input session between the client application and the current IME.</p>
549 *
550 * <p>Be careful to not keep strong references to this object forever, which can prevent
551 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
552 * </p>
553 */
554 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800555 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
556
557 final int mSequenceNumber;
558 final long mTimestamp;
559 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800560 @NonNull
561 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800562 @NonNull
563 final String mImeId;
564 // @InputMethodClient.StartInputReason
565 final int mStartInputReason;
566 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800567 @Nullable
568 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800569 @NonNull
570 final EditorInfo mEditorInfo;
571 final int mTargetWindowSoftInputMode;
572 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800573
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800574 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
575 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
576 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
577 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
578 mSequenceNumber = sSequenceNumber.getAndIncrement();
579 mTimestamp = SystemClock.uptimeMillis();
580 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800581 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800582 mImeId = imeId;
583 mStartInputReason = startInputReason;
584 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800585 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800586 mEditorInfo = editorInfo;
587 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
588 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800589 }
590 }
591
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800592 @GuardedBy("mMethodMap")
593 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800594
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800595 /**
596 * A ring buffer to store the history of {@link StartInputInfo}.
597 */
598 private static final class StartInputHistory {
599 /**
600 * Entry size for non low-RAM devices.
601 *
602 * <p>TODO: Consider to follow what other system services have been doing to manage
603 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
604 */
605 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
606
607 /**
608 * Entry size for non low-RAM devices.
609 *
610 * <p>TODO: Consider to follow what other system services have been doing to manage
611 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
612 */
613 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
614
615 private static int getEntrySize() {
616 if (ActivityManager.isLowRamDeviceStatic()) {
617 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
618 } else {
619 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
620 }
621 }
622
623 /**
624 * Backing store for the ring bugger.
625 */
626 private final Entry[] mEntries = new Entry[getEntrySize()];
627
628 /**
629 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
630 * write.
631 */
632 private int mNextIndex = 0;
633
634 /**
635 * Recyclable entry to store the information in {@link StartInputInfo}.
636 */
637 private static final class Entry {
638 int mSequenceNumber;
639 long mTimestamp;
640 long mWallTime;
641 @NonNull
642 String mImeTokenString;
643 @NonNull
644 String mImeId;
645 /* @InputMethodClient.StartInputReason */
646 int mStartInputReason;
647 boolean mRestarting;
648 @NonNull
649 String mTargetWindowString;
650 @NonNull
651 EditorInfo mEditorInfo;
652 int mTargetWindowSoftInputMode;
653 int mClientBindSequenceNumber;
654
655 Entry(@NonNull StartInputInfo original) {
656 set(original);
657 }
658
659 void set(@NonNull StartInputInfo original) {
660 mSequenceNumber = original.mSequenceNumber;
661 mTimestamp = original.mTimestamp;
662 mWallTime = original.mWallTime;
663 // Intentionally convert to String so as not to keep a strong reference to a Binder
664 // object.
665 mImeTokenString = String.valueOf(original.mImeToken);
666 mImeId = original.mImeId;
667 mStartInputReason = original.mStartInputReason;
668 mRestarting = original.mRestarting;
669 // Intentionally convert to String so as not to keep a strong reference to a Binder
670 // object.
671 mTargetWindowString = String.valueOf(original.mTargetWindow);
672 mEditorInfo = original.mEditorInfo;
673 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
674 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
675 }
676 }
677
678 /**
679 * Add a new entry and discard the oldest entry as needed.
680 * @param info {@lin StartInputInfo} to be added.
681 */
682 void addEntry(@NonNull StartInputInfo info) {
683 final int index = mNextIndex;
684 if (mEntries[index] == null) {
685 mEntries[index] = new Entry(info);
686 } else {
687 mEntries[index].set(info);
688 }
689 mNextIndex = (mNextIndex + 1) % mEntries.length;
690 }
691
692 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
693 final SimpleDateFormat dataFormat =
694 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
695
696 for (int i = 0; i < mEntries.length; ++i) {
697 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
698 if (entry == null) {
699 continue;
700 }
701 pw.print(prefix);
702 pw.println("StartInput #" + entry.mSequenceNumber + ":");
703
704 pw.print(prefix);
705 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
706 + " (timestamp=" + entry.mTimestamp + ")"
707 + " reason="
708 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
709 + " restarting=" + entry.mRestarting);
710
711 pw.print(prefix);
712 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
713
714 pw.print(prefix);
715 pw.println(" targetWin=" + entry.mTargetWindowString
716 + " [" + entry.mEditorInfo.packageName + "]"
717 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
718
719 pw.print(prefix);
720 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
721 entry.mTargetWindowSoftInputMode));
722
723 pw.print(prefix);
724 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
725 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
726 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
727 + " fieldName=" + entry.mEditorInfo.fieldName
728 + " actionId=" + entry.mEditorInfo.actionId
729 + " actionLabel=" + entry.mEditorInfo.actionLabel);
730 }
731 }
732 }
733
734 @GuardedBy("mMethodMap")
735 @NonNull
736 private final StartInputHistory mStartInputHistory = new StartInputHistory();
737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700739 int mUserId;
740 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700741 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800742 String mLastEnabled = "";
743
Yohei Yukawa81482972015-06-04 00:58:59 -0700744 /**
745 * <em>This constructor must be called within the lock.</em>
746 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 SettingsObserver(Handler handler) {
748 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700749 }
750
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800751 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700752 if (mRegistered && mUserId == userId) {
753 return;
754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700756 if (mRegistered) {
757 mContext.getContentResolver().unregisterContentObserver(this);
758 mRegistered = false;
759 }
760 if (mUserId != userId) {
761 mLastEnabled = "";
762 mUserId = userId;
763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700765 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900766 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700767 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900768 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700769 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700770 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700771 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800772 resolver.registerContentObserver(Settings.Secure.getUriFor(
773 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700774 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
Michael Wright7b5a96b2014-08-09 19:28:42 -0700777 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800778 final Uri showImeUri = Settings.Secure.getUriFor(
779 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
780 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
781 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700783 if (showImeUri.equals(uri)) {
784 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800785 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
786 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
787 mContext.getContentResolver(),
788 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
789 0, mUserId) == 1;
790 if (mAccessibilityRequestingNoSoftKeyboard) {
791 final boolean showRequested = mShowRequested;
792 hideCurrentInputLocked(0, null);
793 mShowRequested = showRequested;
794 } else if (mShowRequested) {
795 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
796 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700797 } else {
798 boolean enabledChanged = false;
799 String newEnabled = mSettings.getEnabledInputMethodsStr();
800 if (!mLastEnabled.equals(newEnabled)) {
801 mLastEnabled = newEnabled;
802 enabledChanged = true;
803 }
804 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700808
809 @Override
810 public String toString() {
811 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
812 + " mLastEnabled=" + mLastEnabled + "}";
813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800815
Yohei Yukawa79247822017-01-23 15:26:15 -0800816 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900817 @Override
818 public void onReceive(Context context, Intent intent) {
819 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700820 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900821 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700822 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900823 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800824 } else if (Intent.ACTION_USER_ADDED.equals(action)
825 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100826 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800827 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700828 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
829 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
830 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
831 final String prevValue = intent.getStringExtra(
832 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
833 final String newValue = intent.getStringExtra(
834 Intent.EXTRA_SETTING_NEW_VALUE);
835 restoreEnabledInputMethods(mContext, prevValue, newValue);
836 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800837 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800838 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900839 } else {
840 Slog.w(TAG, "Unexpected intent " + intent);
841 }
842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800844
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800845 /**
846 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
847 *
848 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
849 * the users. We should ignore this event if this is about any background user's locale.</p>
850 *
851 * <p>Caution: This method must not be called when system is not ready.</p>
852 */
853 void onActionLocaleChanged() {
854 synchronized (mMethodMap) {
855 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
856 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
857 return;
858 }
859 buildInputMethodListLocked(true);
860 // If the locale is changed, needs to reset the default ime
861 resetDefaultImeLocked(mContext);
862 updateFromSettingsLocked(true);
863 mLastSystemLocales = possibleNewLocale;
864 }
865 }
866
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700867 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
868 // does not attempt to validate on the fly with any installed device policy, so must only
869 // be run in the context of initial device setup.
870 //
871 // TODO: Move this method to InputMethodUtils with adding unit tests.
872 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
873 if (DEBUG_RESTORE) {
874 Slog.i(TAG, "Restoring enabled input methods:");
875 Slog.i(TAG, "prev=" + prevValue);
876 Slog.i(TAG, " new=" + newValue);
877 }
878 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900879 ArrayMap<String, ArraySet<String>> prevMap =
880 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
881 ArrayMap<String, ArraySet<String>> newMap =
882 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700883
884 // Merge the restored ime+subtype enabled states into the live state
885 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
886 final String imeId = entry.getKey();
887 ArraySet<String> prevSubtypes = prevMap.get(imeId);
888 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700889 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700890 prevMap.put(imeId, prevSubtypes);
891 }
892 prevSubtypes.addAll(entry.getValue());
893 }
894
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700895 final String mergedImesAndSubtypesString =
896 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700897 if (DEBUG_RESTORE) {
898 Slog.i(TAG, "Merged IME string:");
899 Slog.i(TAG, " " + mergedImesAndSubtypesString);
900 }
901 Settings.Secure.putString(context.getContentResolver(),
902 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
903 }
904
Yohei Yukawac4e44912017-02-09 19:30:22 -0800905 final class MyPackageMonitor extends PackageMonitor {
906 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800907 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800908 *
909 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
910 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
911 * dynamically unless the entire package is updated, which also always triggers package
912 * rescanning.</p>
913 */
914 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800915 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
916
917 /**
918 * Packages that are appeared, disappeared, or modified for whatever reason.
919 *
920 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
921 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
922 * duplicate elements for our use case.</p>
923 *
924 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
925 * which should be bound to {@link #getRegisteredHandler()}.</p>
926 */
927 private final ArrayList<String> mChangedPackages = new ArrayList<>();
928
929 /**
930 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
931 *
932 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
933 * which should be bound to {@link #getRegisteredHandler()}.</p>
934 */
935 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800936
937 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800938 void clearKnownImePackageNamesLocked() {
939 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800940 }
941
942 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800943 final void addKnownImePackageNameLocked(@NonNull String packageName) {
944 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800945 }
946
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800947 @GuardedBy("mMethodMap")
948 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900949 final int userId = getChangingUserId();
950 final boolean retval = userId == mSettings.getCurrentUserId();
951 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900952 if (!retval) {
953 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
954 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 }
956 return retval;
957 }
958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800960 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800962 if (!isChangingPackagesOfCurrentUserLocked()) {
963 return false;
964 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 final int N = mMethodList.size();
967 if (curInputMethodId != null) {
968 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800969 InputMethodInfo imi = mMethodList.get(i);
970 if (imi.getId().equals(curInputMethodId)) {
971 for (String pkg : packages) {
972 if (imi.getPackageName().equals(pkg)) {
973 if (!doit) {
974 return true;
975 }
satok723a27e2010-11-11 14:58:11 +0900976 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800977 chooseNewDefaultIMELocked();
978 return true;
979 }
980 }
981 }
982 }
983 }
984 }
985 return false;
986 }
987
988 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800989 public void onBeginPackageChanges() {
990 clearPackageChangeState();
991 }
992
993 @Override
994 public void onPackageAppeared(String packageName, int reason) {
995 if (!mImePackageAppeared) {
996 final PackageManager pm = mContext.getPackageManager();
997 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
998 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
999 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
1000 // No need to lock this because we access it only on getRegisteredHandler().
1001 if (!services.isEmpty()) {
1002 mImePackageAppeared = true;
1003 }
1004 }
1005 // No need to lock this because we access it only on getRegisteredHandler().
1006 mChangedPackages.add(packageName);
1007 }
1008
1009 @Override
1010 public void onPackageDisappeared(String packageName, int reason) {
1011 // No need to lock this because we access it only on getRegisteredHandler().
1012 mChangedPackages.add(packageName);
1013 }
1014
1015 @Override
1016 public void onPackageModified(String packageName) {
1017 // No need to lock this because we access it only on getRegisteredHandler().
1018 mChangedPackages.add(packageName);
1019 }
1020
1021 @Override
1022 public void onPackagesSuspended(String[] packages) {
1023 // No need to lock this because we access it only on getRegisteredHandler().
1024 for (String packageName : packages) {
1025 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001026 }
1027 }
1028
1029 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001030 public void onPackagesUnsuspended(String[] packages) {
1031 // No need to lock this because we access it only on getRegisteredHandler().
1032 for (String packageName : packages) {
1033 mChangedPackages.add(packageName);
1034 }
1035 }
1036
1037 @Override
1038 public void onFinishPackageChanges() {
1039 onFinishPackageChangesInternal();
1040 clearPackageChangeState();
1041 }
1042
1043 private void clearPackageChangeState() {
1044 // No need to lock them because we access these fields only on getRegisteredHandler().
1045 mChangedPackages.clear();
1046 mImePackageAppeared = false;
1047 }
1048
1049 private boolean shouldRebuildInputMethodListLocked() {
1050 // This method is guaranteed to be called only by getRegisteredHandler().
1051
1052 // If there is any new package that contains at least one IME, then rebuilt the list
1053 // of IMEs.
1054 if (mImePackageAppeared) {
1055 return true;
1056 }
1057
1058 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1059 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1060 // is an option, but it may still do some extra operations that we do not need here.
1061 final int N = mChangedPackages.size();
1062 for (int i = 0; i < N; ++i) {
1063 final String packageName = mChangedPackages.get(i);
1064 if (mKnownImePackageNames.contains(packageName)) {
1065 return true;
1066 }
1067 }
1068 return false;
1069 }
1070
1071 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001072 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001073 if (!isChangingPackagesOfCurrentUserLocked()) {
1074 return;
1075 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001076 if (!shouldRebuildInputMethodListLocked()) {
1077 return;
1078 }
1079
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001080 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001081 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001082 final int N = mMethodList.size();
1083 if (curInputMethodId != null) {
1084 for (int i=0; i<N; i++) {
1085 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001086 final String imiId = imi.getId();
1087 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001088 curIm = imi;
1089 }
satoke7c6998e2011-06-03 17:57:59 +09001090
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001091 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001092 if (isPackageModified(imi.getPackageName())) {
1093 mFileManager.deleteAllInputMethodSubtypes(imiId);
1094 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001095 if (change == PACKAGE_TEMPORARY_CHANGE
1096 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001097 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001098 + imi.getComponent());
1099 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
1101 }
1102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001103
Yohei Yukawa94e33302016-02-12 19:37:03 -08001104 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001108 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001109 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001110 if (change == PACKAGE_TEMPORARY_CHANGE
1111 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001112 ServiceInfo si = null;
1113 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001114 si = mIPackageManager.getServiceInfo(
1115 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1116 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001117 }
1118 if (si == null) {
1119 // Uh oh, current input method is no longer around!
1120 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001121 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001122 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001123 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001124 changed = true;
1125 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001126 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001127 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001128 }
1129 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001130 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001131 }
satokab751aa2010-09-14 19:17:36 +09001132
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001133 if (curIm == null) {
1134 // We currently don't have a default input method... is
1135 // one now available?
1136 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001137 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1138 // Even if the current input method is still available, mCurrentSubtype could
1139 // be obsolete when the package is modified in practice.
1140 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001142
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001143 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001144 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 }
1147 }
1148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001149
Jeff Brownc28867a2013-03-26 15:42:39 -07001150 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001151 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001152 private final IInputMethod mMethod;
1153 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001154
Jeff Brownc28867a2013-03-26 15:42:39 -07001155 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1156 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001157 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001158 mMethod = method;
1159 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
satoke7c6998e2011-06-03 17:57:59 +09001162 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001163 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001164 long ident = Binder.clearCallingIdentity();
1165 try {
1166 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1167 } finally {
1168 Binder.restoreCallingIdentity(ident);
1169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 }
1171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
satok01038492012-04-09 21:08:27 +09001173 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001174 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001175 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001176 public void onHardKeyboardStatusChange(boolean available) {
1177 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1178 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001179 }
1180
Michael Wright7b5a96b2014-08-09 19:28:42 -07001181 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001182 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001183 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001184 }
1185 synchronized(mMethodMap) {
1186 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1187 && mSwitchingDialog.isShowing()) {
1188 mSwitchingDialogTitleView.findViewById(
1189 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1190 available ? View.VISIBLE : View.GONE);
1191 }
1192 }
1193 }
1194 }
1195
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001196 public static final class Lifecycle extends SystemService {
1197 private InputMethodManagerService mService;
1198
1199 public Lifecycle(Context context) {
1200 super(context);
1201 mService = new InputMethodManagerService(context);
1202 }
1203
1204 @Override
1205 public void onStart() {
1206 LocalServices.addService(InputMethodManagerInternal.class,
1207 new LocalServiceImpl(mService.mHandler));
1208 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1209 }
1210
1211 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001212 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001213 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001214 // TODO: Dispatch this to a worker thread as needed.
1215 mService.onSwitchUser(userHandle);
1216 }
1217
1218 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001219 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001220 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001221 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001222 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1223 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1224 .getService(Context.STATUS_BAR_SERVICE);
1225 mService.systemRunning(statusBarService);
1226 }
1227 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001228
1229 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001230 public void onUnlockUser(final @UserIdInt int userHandle) {
1231 // Called on ActivityManager thread.
1232 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001233 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001234 }
1235 }
1236
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001237 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001238 synchronized(mMethodMap) {
1239 final int currentUserId = mSettings.getCurrentUserId();
1240 if (DEBUG) {
1241 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1242 }
1243 if (userId != currentUserId) {
1244 return;
1245 }
1246 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001247 if (mSystemReady) {
1248 // We need to rebuild IMEs.
1249 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1250 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1251 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001252 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001253 }
1254
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001255 void onSwitchUser(@UserIdInt int userId) {
1256 synchronized (mMethodMap) {
1257 switchUserLocked(userId);
1258 }
1259 }
1260
Seigo Nonaka7309b122015-08-17 18:34:13 -07001261 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001262 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001264 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001266 // Note: SettingsObserver doesn't register observers in its constructor.
1267 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 mIWindowManager = IWindowManager.Stub.asInterface(
1269 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001270 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001271 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001272 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 public void executeMessage(Message msg) {
1274 handleMessage(msg);
1275 }
Mita Yuned218c72012-12-06 17:18:25 -08001276 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001277 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001278 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001279 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001280 mHasFeature = context.getPackageManager().hasSystemFeature(
1281 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001282 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001283 mHardKeyboardBehavior = mContext.getResources().getInteger(
1284 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001285
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001286 Bundle extras = new Bundle();
1287 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001288 mImeSwitcherNotification =
1289 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1290 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1291 .setWhen(0)
1292 .setOngoing(true)
1293 .addExtras(extras)
1294 .setCategory(Notification.CATEGORY_SYSTEM)
1295 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001296
satok7cfc0ed2011-06-20 21:29:36 +09001297 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001298 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001299
1300 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001301
satok7cfc0ed2011-06-20 21:29:36 +09001302 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001303 int userId = 0;
1304 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001305 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001306 } catch (RemoteException e) {
1307 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1308 }
satok913a8922010-08-26 21:53:41 +09001309
satokd87c2592010-09-29 11:52:06 +09001310 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001311 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001312 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001313
Kenny Guy2a764942014-04-02 13:29:20 +01001314 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001315 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001316 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1317 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001318 }
1319
satok5b927c432012-05-01 20:09:34 +09001320 private void resetDefaultImeLocked(Context context) {
1321 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001322 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001323 return;
1324 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001325 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001326 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001327 if (suitableImes.isEmpty()) {
1328 Slog.i(TAG, "No default found");
1329 return;
satok5b927c432012-05-01 20:09:34 +09001330 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001331 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001332 if (DEBUG) {
1333 Slog.i(TAG, "Default found, using " + defIm.getId());
1334 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001335 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001336 }
1337
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001338 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001339 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1340 + " currentUserId=" + mSettings.getCurrentUserId());
1341
Yohei Yukawa81482972015-06-04 00:58:59 -07001342 // ContentObserver should be registered again when the user is changed
1343 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001344
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001345 // If the system is not ready or the device is not yed unlocked by the user, then we use
1346 // copy-on-write settings.
1347 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001348 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001349 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001350 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001351 // InputMethodFileManager should be reset when the user is changed
1352 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001353 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001354
1355 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1356 + " defaultImiId=" + defaultImiId);
1357
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001358 // For secondary users, the list of enabled IMEs may not have been updated since the
1359 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1360 // not be empty even if the IME has been uninstalled by the primary user.
1361 // Even in such cases, IMMS works fine because it will find the most applicable
1362 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001363 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001364 mLastSystemLocales = mRes.getConfiguration().getLocales();
1365
1366 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1367 if (mSystemReady) {
1368 hideCurrentInputLocked(0, null);
1369 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1370 buildInputMethodListLocked(initialUserSwitch);
1371 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1372 // This is the first time of the user switch and
1373 // set the current ime to the proper one.
1374 resetDefaultImeLocked(mContext);
1375 }
1376 updateFromSettingsLocked(true);
1377 try {
1378 startInputInnerLocked();
1379 } catch (RuntimeException e) {
1380 Slog.w(TAG, "Unexpected exception", e);
1381 }
1382 }
1383
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001384 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001385 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1386 mSettings.getEnabledInputMethodListLocked(), newUserId,
1387 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001388 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001389
1390 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1391 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001392 }
1393
Kenny Guy2a764942014-04-02 13:29:20 +01001394 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001395 mSettings.setCurrentProfileIds(
1396 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001397 }
1398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 @Override
1400 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1401 throws RemoteException {
1402 try {
1403 return super.onTransact(code, data, reply, flags);
1404 } catch (RuntimeException e) {
1405 // The input method manager only throws security exceptions, so let's
1406 // log all others.
1407 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001408 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410 throw e;
1411 }
1412 }
1413
Svetoslav Ganova0027152013-06-25 14:59:53 -07001414 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001415 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001416 if (DEBUG) {
1417 Slog.d(TAG, "--- systemReady");
1418 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001419 if (!mSystemReady) {
1420 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001421 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001422 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001423 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001424 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001425 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1426 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001427 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001428 if (mStatusBar != null) {
1429 mStatusBar.setIconVisibility(mSlotIme, false);
1430 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001431 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001432 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1433 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001434 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001435 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001436 mHardKeyboardListener);
1437 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001438
1439 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1440 mSettingsObserver.registerContentObserverLocked(currentUserId);
1441
1442 final IntentFilter broadcastFilter = new IntentFilter();
1443 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1444 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1445 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1446 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1447 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1448 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1449
1450 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1451 resetDefaultImeLocked(mContext);
1452 updateFromSettingsLocked(true);
1453 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1454 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1455 mContext.getBasePackageName());
1456
Dianne Hackborncc278702009-09-02 23:07:23 -07001457 try {
1458 startInputInnerLocked();
1459 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001460 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001461 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001462 }
1463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001465
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001466 // ---------------------------------------------------------------------------------------
1467 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1468 // 1) it comes from the system process
1469 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1470 private boolean calledFromValidUser() {
1471 final int uid = Binder.getCallingUid();
1472 final int userId = UserHandle.getUserId(uid);
1473 if (DEBUG) {
1474 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1475 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1476 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001477 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1478 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001479 }
Kenny Guy2a764942014-04-02 13:29:20 +01001480 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001482 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001483
1484 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1485 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1486 // must not manage background users' states in any functions.
1487 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1488 // by a token.
1489 if (mContext.checkCallingOrSelfPermission(
1490 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1491 == PackageManager.PERMISSION_GRANTED) {
1492 if (DEBUG) {
1493 Slog.d(TAG, "--- Access granted because the calling process has "
1494 + "the INTERACT_ACROSS_USERS_FULL permission");
1495 }
1496 return true;
1497 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001498 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001499 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1500 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001501 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001502 }
1503
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001504
1505 /**
1506 * Returns true iff the caller is identified to be the current input method with the token.
1507 * @param token The window token given to the input method when it was started.
1508 * @return true if and only if non-null valid token is specified.
1509 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001510 private boolean calledWithValidToken(@Nullable IBinder token) {
1511 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1512 if (DEBUG) {
1513 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1514 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1515 }
1516 return false;
1517 }
1518 if (token == null || token != mCurToken) {
1519 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1520 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1521 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001522 return false;
1523 }
1524 return true;
1525 }
1526
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001527 private boolean bindCurrentInputMethodService(
1528 Intent service, ServiceConnection conn, int flags) {
1529 if (service == null || conn == null) {
1530 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1531 return false;
1532 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001533 return mContext.bindServiceAsUser(service, conn, flags,
1534 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001535 }
1536
satoke7c6998e2011-06-03 17:57:59 +09001537 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001539 // TODO: Make this work even for non-current users?
1540 if (!calledFromValidUser()) {
1541 return Collections.emptyList();
1542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001544 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 }
1546 }
1547
satoke7c6998e2011-06-03 17:57:59 +09001548 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001550 // TODO: Make this work even for non-current users?
1551 if (!calledFromValidUser()) {
1552 return Collections.emptyList();
1553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001555 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 }
1557 }
1558
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001559 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001560 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001561 * @return enabled subtypes of the specified imi
1562 */
satoke7c6998e2011-06-03 17:57:59 +09001563 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001564 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001565 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001566 // TODO: Make this work even for non-current users?
1567 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001568 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001569 }
satok67ddf9c2010-11-17 09:45:54 +09001570 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001571 final InputMethodInfo imi;
1572 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001573 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001574 } else {
1575 imi = mMethodMap.get(imiId);
1576 }
1577 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001578 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001579 }
1580 return mSettings.getEnabledInputMethodSubtypeListLocked(
1581 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001582 }
1583 }
1584
satoke7c6998e2011-06-03 17:57:59 +09001585 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 public void addClient(IInputMethodClient client,
1587 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001588 if (!calledFromValidUser()) {
1589 return;
1590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 synchronized (mMethodMap) {
1592 mClients.put(client.asBinder(), new ClientState(client,
1593 inputContext, uid, pid));
1594 }
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
satoke7c6998e2011-06-03 17:57:59 +09001597 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001599 if (!calledFromValidUser()) {
1600 return;
1601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001603 ClientState cs = mClients.remove(client.asBinder());
1604 if (cs != null) {
1605 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001606 if (mCurClient == cs) {
1607 mCurClient = null;
1608 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001609 if (mCurFocusedWindowClient == cs) {
1610 mCurFocusedWindowClient = null;
1611 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 }
1614 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 void executeOrSendMessage(IInterface target, Message msg) {
1617 if (target.asBinder() instanceof Binder) {
1618 mCaller.sendMessage(msg);
1619 } else {
1620 handleMessage(msg);
1621 msg.recycle();
1622 }
1623 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001624
Yohei Yukawa33e81792015-11-17 21:14:42 -08001625 void unbindCurrentClientLocked(
1626 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001628 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 + mCurClient.client.asBinder());
1630 if (mBoundToMethod) {
1631 mBoundToMethod = false;
1632 if (mCurMethod != null) {
1633 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1634 MSG_UNBIND_INPUT, mCurMethod));
1635 }
1636 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001637
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001638 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1639 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001640 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1641 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001644
The Android Open Source Project10592532009-03-18 17:39:46 -07001645 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 private int getImeShowFlags() {
1650 int flags = 0;
1651 if (mShowForced) {
1652 flags |= InputMethod.SHOW_FORCED
1653 | InputMethod.SHOW_EXPLICIT;
1654 } else if (mShowExplicitlyRequested) {
1655 flags |= InputMethod.SHOW_EXPLICIT;
1656 }
1657 return flags;
1658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 private int getAppShowFlags() {
1661 int flags = 0;
1662 if (mShowForced) {
1663 flags |= InputMethodManager.SHOW_FORCED;
1664 } else if (!mShowExplicitlyRequested) {
1665 flags |= InputMethodManager.SHOW_IMPLICIT;
1666 }
1667 return flags;
1668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001669
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001670 InputBindResult attachNewInputLocked(
1671 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (!mBoundToMethod) {
1673 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1674 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1675 mBoundToMethod = true;
1676 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001677
1678 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001679 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1680 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1681 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001682 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001683 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001686 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001687 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001688 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001691 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001693 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001694 (session.channel != null ? session.channel.dup() : null),
1695 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001697
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001698 InputBindResult startInputLocked(
1699 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001700 IInputMethodClient client, IInputContext inputContext,
1701 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001702 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 // If no method is currently selected, do nothing.
1704 if (mCurMethodId == null) {
1705 return mNoBinding;
1706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 ClientState cs = mClients.get(client.asBinder());
1709 if (cs == null) {
1710 throw new IllegalArgumentException("unknown client "
1711 + client.asBinder());
1712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001713
Yohei Yukawa74750f22016-03-22 12:54:22 -07001714 if (attribute == null) {
1715 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1716 + " uid=" + cs.uid + " pid=" + cs.pid);
1717 return null;
1718 }
1719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 try {
1721 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1722 // Check with the window manager to make sure this client actually
1723 // has a window with focus. If not, reject. This is thread safe
1724 // because if the focus changes some time before or after, the
1725 // next client receiving focus that has any interest in input will
1726 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001727 if (DEBUG) {
1728 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1729 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 return null;
1732 }
1733 } catch (RemoteException e) {
1734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001735
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001736 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001737 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001738 }
1739
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001740 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001741 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001742 @NonNull EditorInfo attribute, int controlFlags,
1743 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001744 // If no method is currently selected, do nothing.
1745 if (mCurMethodId == null) {
1746 return mNoBinding;
1747 }
1748
Yohei Yukawad57ba672015-06-08 16:39:46 -07001749 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1750 attribute.packageName)) {
1751 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1752 + " uid=" + cs.uid + " package=" + attribute.packageName);
1753 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001754 }
1755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001757 // Was the keyguard locked when switching over to the new client?
1758 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 // If the client is changing, we need to switch over to the new
1760 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001761 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001762 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001763 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764
1765 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001766 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001767 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001768 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 // Bump up the sequence for this client and attach it.
1773 mCurSeq++;
1774 if (mCurSeq <= 0) mCurSeq = 1;
1775 mCurClient = cs;
1776 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001777 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 // Check if the input method is changing.
1781 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1782 if (cs.curSession != null) {
1783 // Fast case: if we are already connected to the input method,
1784 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001785 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001786 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788 if (mHaveConnection) {
1789 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 // Return to client, and we will get back with it when
1791 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001792 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001793 return new InputBindResult(null, null, mCurId, mCurSeq,
1794 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 } else if (SystemClock.uptimeMillis()
1796 < (mLastBindTime+TIME_TO_RECONNECT)) {
1797 // In this case we have connected to the service, but
1798 // don't yet have its interface. If it hasn't been too
1799 // long since we did the connection, we'll return to
1800 // the client and wait to get the service interface so
1801 // we can report back. If it has been too long, we want
1802 // to fall through so we can try a disconnect/reconnect
1803 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001804 return new InputBindResult(null, null, mCurId, mCurSeq,
1805 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001807 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1808 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810 }
1811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001812
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001813 return startInputInnerLocked();
1814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001815
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001816 InputBindResult startInputInnerLocked() {
1817 if (mCurMethodId == null) {
1818 return mNoBinding;
1819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001820
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001821 if (!mSystemReady) {
1822 // If the system is not yet ready, we shouldn't be running third
1823 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001824 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1825 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1829 if (info == null) {
1830 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1831 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001832
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001833 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1836 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001837 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1838 com.android.internal.R.string.input_method_binding_label);
1839 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1840 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001841 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 mLastBindTime = SystemClock.uptimeMillis();
1843 mHaveConnection = true;
1844 mCurId = info.getId();
1845 mCurToken = new Binder();
1846 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001847 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001848 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 } catch (RemoteException e) {
1850 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001851 return new InputBindResult(null, null, mCurId, mCurSeq,
1852 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 } else {
1854 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001855 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 + mCurIntent);
1857 }
1858 return null;
1859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001860
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001861 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001862 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001863 IInputMethodClient client, IInputContext inputContext,
1864 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001865 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001866 if (!calledFromValidUser()) {
1867 return null;
1868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001870 if (DEBUG) {
1871 Slog.v(TAG, "startInput: reason="
1872 + InputMethodClient.getStartInputReason(startInputReason)
1873 + " client = " + client.asBinder()
1874 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001875 + " missingMethods="
1876 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001877 + " attribute=" + attribute
1878 + " controlFlags=#" + Integer.toHexString(controlFlags));
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 final long ident = Binder.clearCallingIdentity();
1881 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001882 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1883 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 } finally {
1885 Binder.restoreCallingIdentity(ident);
1886 }
1887 }
1888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001889
satoke7c6998e2011-06-03 17:57:59 +09001890 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 public void finishInput(IInputMethodClient client) {
1892 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001893
satoke7c6998e2011-06-03 17:57:59 +09001894 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 public void onServiceConnected(ComponentName name, IBinder service) {
1896 synchronized (mMethodMap) {
1897 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1898 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001899 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001900 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001901 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001902 return;
1903 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001904 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001905 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1906 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001908 clearClientSessionLocked(mCurClient);
1909 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
1911 }
1912 }
1913 }
1914
Jeff Brownc28867a2013-03-26 15:42:39 -07001915 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1916 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 synchronized (mMethodMap) {
1918 if (mCurMethod != null && method != null
1919 && mCurMethod.asBinder() == method.asBinder()) {
1920 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001921 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001923 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001924 InputBindResult res = attachNewInputLocked(
1925 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if (res.method != null) {
1927 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001928 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001930 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
1932 }
1933 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001934
1935 // Session abandoned. Close its associated input channel.
1936 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001938
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001939 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001940 if (mVisibleBound) {
1941 mContext.unbindService(mVisibleConnection);
1942 mVisibleBound = false;
1943 }
1944
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001945 if (mHaveConnection) {
1946 mContext.unbindService(this);
1947 mHaveConnection = false;
1948 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001949
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001950 if (mCurToken != null) {
1951 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001952 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001953 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001954 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001955 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001956 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001957 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001958 } catch (RemoteException e) {
1959 }
1960 mCurToken = null;
1961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001962
The Android Open Source Project10592532009-03-18 17:39:46 -07001963 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001964 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001965 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001966
Yohei Yukawa33e81792015-11-17 21:14:42 -08001967 void resetCurrentMethodAndClient(
1968 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001969 mCurMethodId = null;
1970 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001971 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001972 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001973
1974 void requestClientSessionLocked(ClientState cs) {
1975 if (!cs.sessionRequested) {
1976 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1977 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1978 cs.sessionRequested = true;
1979 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1980 MSG_CREATE_SESSION, mCurMethod, channels[1],
1981 new MethodCallback(this, mCurMethod, channels[0])));
1982 }
1983 }
1984
1985 void clearClientSessionLocked(ClientState cs) {
1986 finishSessionLocked(cs.curSession);
1987 cs.curSession = null;
1988 cs.sessionRequested = false;
1989 }
1990
1991 private void finishSessionLocked(SessionState sessionState) {
1992 if (sessionState != null) {
1993 if (sessionState.session != null) {
1994 try {
1995 sessionState.session.finishSession();
1996 } catch (RemoteException e) {
1997 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001998 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001999 }
2000 sessionState.session = null;
2001 }
2002 if (sessionState.channel != null) {
2003 sessionState.channel.dispose();
2004 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002005 }
2006 }
2007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002008
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002009 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 if (mCurMethod != null) {
2011 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002012 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002014
Jeff Brownc28867a2013-03-26 15:42:39 -07002015 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002016 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 mCurMethod = null;
2018 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002019 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002020 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002021 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002022 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002024
satoke7c6998e2011-06-03 17:57:59 +09002025 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002027 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2028 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2029 // which is irregular but can eventually happen for everyone just by continuing using the
2030 // device. Thus it is important to make sure that all the internal states are properly
2031 // refreshed when this method is called back. Running
2032 // adb install -r <APK that implements the current IME>
2033 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002035 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 + " mCurIntent=" + mCurIntent);
2037 if (mCurMethod != null && mCurIntent != null
2038 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002039 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 // We consider this to be a new bind attempt, since the system
2041 // should now try to restart the service for us.
2042 mLastBindTime = SystemClock.uptimeMillis();
2043 mShowRequested = mInputShown;
2044 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002045 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
2047 }
2048 }
2049
satokf9f01002011-05-19 21:31:50 +09002050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002052 synchronized (mMethodMap) {
2053 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002054 return;
2055 }
2056 final long ident = Binder.clearCallingIdentity();
2057 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002059 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002060 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002061 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002064 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002065 CharSequence contentDescription = null;
2066 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002067 // Use PackageManager to load label
2068 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002069 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002070 mIPackageManager.getApplicationInfo(packageName, 0,
2071 mSettings.getCurrentUserId()));
2072 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002073 /* ignore */
2074 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002075 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002076 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002077 contentDescription != null
2078 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002079 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002082 } finally {
2083 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 }
2086 }
2087
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002088 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002089 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002090 if (mSwitchingDialog != null) return false;
Yohei Yukawad2bc3092017-07-31 15:37:14 -07002091 if (mWindowManagerInternal.isKeyguardShowingAndNotOccluded()
2092 && mKeyguardManager != null && mKeyguardManager.isKeyguardSecure()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002093 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002094 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002095 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2096 // When physical keyboard is attached, we show the ime switcher (or notification if
2097 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2098 // exists in the IME switcher dialog. Might be OK to remove this condition once
2099 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2100 return true;
2101 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002102 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2103 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002104 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002105
2106 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2107 final int N = imis.size();
2108 if (N > 2) return true;
2109 if (N < 1) return false;
2110 int nonAuxCount = 0;
2111 int auxCount = 0;
2112 InputMethodSubtype nonAuxSubtype = null;
2113 InputMethodSubtype auxSubtype = null;
2114 for(int i = 0; i < N; ++i) {
2115 final InputMethodInfo imi = imis.get(i);
2116 final List<InputMethodSubtype> subtypes =
2117 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2118 final int subtypeCount = subtypes.size();
2119 if (subtypeCount == 0) {
2120 ++nonAuxCount;
2121 } else {
2122 for (int j = 0; j < subtypeCount; ++j) {
2123 final InputMethodSubtype subtype = subtypes.get(j);
2124 if (!subtype.isAuxiliary()) {
2125 ++nonAuxCount;
2126 nonAuxSubtype = subtype;
2127 } else {
2128 ++auxCount;
2129 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002130 }
2131 }
satok7cfc0ed2011-06-20 21:29:36 +09002132 }
2133 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002134 if (nonAuxCount > 1 || auxCount > 1) {
2135 return true;
2136 } else if (nonAuxCount == 1 && auxCount == 1) {
2137 if (nonAuxSubtype != null && auxSubtype != null
2138 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2139 || auxSubtype.overridesImplicitlyEnabledSubtype()
2140 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2141 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2142 return false;
2143 }
2144 return true;
2145 }
2146 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002147 }
2148
John Spurlocke0980502013-10-25 11:59:29 -04002149 private boolean isKeyguardLocked() {
2150 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2151 }
2152
Yohei Yukawad6475a62017-04-17 10:35:27 -07002153 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002154 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002155 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002156 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2157 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002158 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002159 return;
2160 }
2161
Yohei Yukawa69e68022017-02-13 12:04:50 -08002162 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002163 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002164 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002165 mImeWindowVis = vis;
2166 mBackDisposition = backDisposition;
2167 updateSystemUiLocked(token, vis, backDisposition);
2168 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002169
2170 final boolean dismissImeOnBackKeyPressed;
2171 switch (backDisposition) {
2172 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2173 dismissImeOnBackKeyPressed = true;
2174 break;
2175 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2176 dismissImeOnBackKeyPressed = false;
2177 break;
2178 default:
2179 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2180 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2181 break;
2182 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002183 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2184 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002185 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002186 }
2187
2188 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2189 synchronized (mMethodMap) {
2190 updateSystemUiLocked(token, vis, backDisposition);
2191 }
2192 }
2193
2194 // Caution! This method is called in this class. Handle multi-user carefully
2195 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2196 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002197 return;
2198 }
2199
2200 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2201 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002202 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002203 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002204 // apply policy for binder calls
2205 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2206 vis = 0;
satok06487a52010-10-29 11:37:18 +09002207 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002208 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2209 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2210 if (mStatusBar != null) {
2211 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2212 needsToShowImeSwitcher);
2213 }
2214 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2215 if (imi != null && needsToShowImeSwitcher) {
2216 // Used to load label
2217 final CharSequence title = mRes.getText(
2218 com.android.internal.R.string.select_input_method);
2219 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2220 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002221 mImeSwitcherNotification.setContentTitle(title)
2222 .setContentText(summary)
2223 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002224 try {
2225 if ((mNotificationManager != null)
2226 && !mIWindowManager.hasNavigationBar()) {
2227 if (DEBUG) {
2228 Slog.d(TAG, "--- show notification: label = " + summary);
2229 }
2230 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002231 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002232 mImeSwitcherNotification.build(), UserHandle.ALL);
2233 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002234 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002235 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002236 }
2237 } else {
2238 if (mNotificationShown && mNotificationManager != null) {
2239 if (DEBUG) {
2240 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002241 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002242 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002243 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002244 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002245 }
satok06487a52010-10-29 11:37:18 +09002246 }
2247 } finally {
2248 Binder.restoreCallingIdentity(ident);
2249 }
2250 }
2251
satoke7c6998e2011-06-03 17:57:59 +09002252 @Override
satokf9f01002011-05-19 21:31:50 +09002253 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002254 if (!calledFromValidUser()) {
2255 return;
2256 }
satokf9f01002011-05-19 21:31:50 +09002257 synchronized (mMethodMap) {
2258 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2259 for (int i = 0; i < spans.length; ++i) {
2260 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002261 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002262 mSecureSuggestionSpans.put(ss, currentImi);
2263 }
2264 }
2265 }
2266 }
2267
satoke7c6998e2011-06-03 17:57:59 +09002268 @Override
satokf9f01002011-05-19 21:31:50 +09002269 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002270 if (!calledFromValidUser()) {
2271 return false;
2272 }
satokf9f01002011-05-19 21:31:50 +09002273 synchronized (mMethodMap) {
2274 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2275 // TODO: Do not send the intent if the process of the targetImi is already dead.
2276 if (targetImi != null) {
2277 final String[] suggestions = span.getSuggestions();
2278 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002279 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002280 final Intent intent = new Intent();
2281 // Ensures that only a class in the original IME package will receive the
2282 // notification.
satok42c5a162011-05-26 16:46:14 +09002283 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002284 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2285 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2286 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2287 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002288 final long ident = Binder.clearCallingIdentity();
2289 try {
2290 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2291 } finally {
2292 Binder.restoreCallingIdentity(ident);
2293 }
satokf9f01002011-05-19 21:31:50 +09002294 return true;
2295 }
2296 }
2297 return false;
2298 }
2299
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002300 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002301 updateInputMethodsFromSettingsLocked(enabledMayChange);
2302 updateKeyboardFromSettingsLocked();
2303 }
2304
2305 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002306 if (enabledMayChange) {
2307 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2308 for (int i=0; i<enabled.size(); i++) {
2309 // We allow the user to select "disabled until used" apps, so if they
2310 // are enabling one of those here we now need to make it enabled.
2311 InputMethodInfo imm = enabled.get(i);
2312 try {
2313 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2314 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2315 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002316 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002317 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002318 if (DEBUG) {
2319 Slog.d(TAG, "Update state(" + imm.getId()
2320 + "): DISABLED_UNTIL_USED -> DEFAULT");
2321 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002322 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2323 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002324 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2325 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002326 }
2327 } catch (RemoteException e) {
2328 }
2329 }
2330 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002331 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2332 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2333 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2334 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002335 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002336 // There is no input method selected, try to choose new applicable input method.
2337 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002338 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002339 }
2340 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002342 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002344 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002345 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
satokf3db1af2010-11-23 13:34:33 +09002347 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002348 } else {
2349 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002350 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002352 // Here is not the perfect place to reset the switching controller. Ideally
2353 // mSwitchingController and mSettings should be able to share the same state.
2354 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2355 // the same enabled IMEs list.
2356 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002357
2358 }
2359
2360 public void updateKeyboardFromSettingsLocked() {
2361 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2362 if (mSwitchingDialog != null
2363 && mSwitchingDialogTitleView != null
2364 && mSwitchingDialog.isShowing()) {
2365 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2366 com.android.internal.R.id.hard_keyboard_switch);
2367 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002370
Yohei Yukawab097b822015-12-01 10:43:08 -08002371 private void notifyInputMethodSubtypeChanged(final int userId,
2372 @Nullable final InputMethodInfo inputMethodInfo,
2373 @Nullable final InputMethodSubtype subtype) {
2374 final InputManagerInternal inputManagerInternal =
2375 LocalServices.getService(InputManagerInternal.class);
2376 if (inputManagerInternal != null) {
2377 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2378 }
2379 }
2380
satokab751aa2010-09-14 19:17:36 +09002381 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 InputMethodInfo info = mMethodMap.get(id);
2383 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002384 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002386
satokd81e9502012-05-21 12:58:45 +09002387 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002389 final int subtypeCount = info.getSubtypeCount();
2390 if (subtypeCount <= 0) {
2391 return;
satokcd7cd292010-11-20 15:46:23 +09002392 }
satokd81e9502012-05-21 12:58:45 +09002393 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2394 final InputMethodSubtype newSubtype;
2395 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2396 newSubtype = info.getSubtypeAt(subtypeId);
2397 } else {
2398 // If subtype is null, try to find the most applicable one from
2399 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002400 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002401 }
2402 if (newSubtype == null || oldSubtype == null) {
2403 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2404 + ", new subtype = " + newSubtype);
2405 return;
2406 }
2407 if (newSubtype != oldSubtype) {
2408 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2409 if (mCurMethod != null) {
2410 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002411 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002412 mCurMethod.changeInputMethodSubtype(newSubtype);
2413 } catch (RemoteException e) {
2414 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002415 return;
satokab751aa2010-09-14 19:17:36 +09002416 }
2417 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002418 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 return;
2421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002422
satokd81e9502012-05-21 12:58:45 +09002423 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 final long ident = Binder.clearCallingIdentity();
2425 try {
satokab751aa2010-09-14 19:17:36 +09002426 // Set a subtype to this input method.
2427 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002428 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2429 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2430 // because mCurMethodId is stored as a history in
2431 // setSelectedInputMethodAndSubtypeLocked().
2432 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002434 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002436 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002438 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002440 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 } finally {
2442 Binder.restoreCallingIdentity(ident);
2443 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002444
2445 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2446 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002448
satok42c5a162011-05-26 16:46:14 +09002449 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002450 public boolean showSoftInput(IInputMethodClient client, int flags,
2451 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002452 if (!calledFromValidUser()) {
2453 return false;
2454 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002455 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 long ident = Binder.clearCallingIdentity();
2457 try {
2458 synchronized (mMethodMap) {
2459 if (mCurClient == null || client == null
2460 || mCurClient.client.asBinder() != client.asBinder()) {
2461 try {
2462 // We need to check if this is the current client with
2463 // focus in the window manager, to allow this call to
2464 // be made before input is started in it.
2465 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002466 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002467 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
2469 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002470 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 }
2472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002473
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002474 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002475 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477 } finally {
2478 Binder.restoreCallingIdentity(ident);
2479 }
2480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002481
The Android Open Source Project4df24232009-03-05 14:34:35 -08002482 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002484 if (mAccessibilityRequestingNoSoftKeyboard) {
2485 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
Anna Galusza9b278112016-01-04 11:37:37 -08002487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2489 mShowExplicitlyRequested = true;
2490 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002491 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2492 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002494
Dianne Hackborncc278702009-09-02 23:07:23 -07002495 if (!mSystemReady) {
2496 return false;
2497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002498
The Android Open Source Project4df24232009-03-05 14:34:35 -08002499 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002501 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002502 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2503 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2504 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002506 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002507 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002508 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002509 mVisibleBound = true;
2510 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002511 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002513 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 // The client has asked to have the input method shown, but
2515 // we have been sitting here too long with a connection to the
2516 // service and no interface received, so let's disconnect/connect
2517 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002518 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002520 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002522 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002523 } else {
2524 if (DEBUG) {
2525 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2526 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002529
The Android Open Source Project4df24232009-03-05 14:34:35 -08002530 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002532
satok42c5a162011-05-26 16:46:14 +09002533 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002534 public boolean hideSoftInput(IInputMethodClient client, int flags,
2535 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002536 if (!calledFromValidUser()) {
2537 return false;
2538 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002539 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 long ident = Binder.clearCallingIdentity();
2541 try {
2542 synchronized (mMethodMap) {
2543 if (mCurClient == null || client == null
2544 || mCurClient.client.asBinder() != client.asBinder()) {
2545 try {
2546 // We need to check if this is the current client with
2547 // focus in the window manager, to allow this call to
2548 // be made before input is started in it.
2549 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002550 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2551 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002552 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 }
2554 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002555 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
2557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002558
Joe Onorato8a9b2202010-02-26 18:56:32 -08002559 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002560 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 }
2562 } finally {
2563 Binder.restoreCallingIdentity(ident);
2564 }
2565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002566
The Android Open Source Project4df24232009-03-05 14:34:35 -08002567 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2569 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002570 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 -08002571 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 }
2573 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002574 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 -08002575 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002577
2578 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2579 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2580 // to be updated with the new value sent from IME process. Even in such a transient state
2581 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2582 // application process as a valid request, and have even promised such a behavior with CTS
2583 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2584 // IMMS#InputShown indicates that the software keyboard is shown.
2585 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2586 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2587 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002588 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002589 if (shouldHideSoftInput) {
2590 // The IME will report its visible state again after the following message finally
2591 // delivered to the IME process as an IPC. Hence the inconsistency between
2592 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2593 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002594 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2595 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2596 res = true;
2597 } else {
2598 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002600 if (mHaveConnection && mVisibleBound) {
2601 mContext.unbindService(mVisibleConnection);
2602 mVisibleBound = false;
2603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 mInputShown = false;
2605 mShowRequested = false;
2606 mShowExplicitlyRequested = false;
2607 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002608 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002610
satok42c5a162011-05-26 16:46:14 +09002611 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002612 public InputBindResult startInputOrWindowGainedFocus(
2613 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2614 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002615 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002616 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002617 if (windowToken != null) {
2618 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002619 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002620 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002621 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2622 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002623 }
2624 }
2625
2626 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002627 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002628 IInputMethodClient client, IBinder windowToken, int controlFlags,
2629 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002630 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2631 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002632 // Needs to check the validity before clearing calling identity
2633 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002634 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 long ident = Binder.clearCallingIdentity();
2636 try {
2637 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002638 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2639 + InputMethodClient.getStartInputReason(startInputReason)
2640 + " client=" + client.asBinder()
2641 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002642 + " missingMethods="
2643 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002644 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002645 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002646 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002647 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002648
Dianne Hackborn7663d802012-02-24 13:08:49 -08002649 ClientState cs = mClients.get(client.asBinder());
2650 if (cs == null) {
2651 throw new IllegalArgumentException("unknown client "
2652 + client.asBinder());
2653 }
2654
2655 try {
2656 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2657 // Check with the window manager to make sure this client actually
2658 // has a window with focus. If not, reject. This is thread safe
2659 // because if the focus changes some time before or after, the
2660 // next client receiving focus that has any interest in input will
2661 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002662 if (DEBUG) {
2663 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2664 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2665 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002666 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002668 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002670
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002671 if (!calledFromValidUser) {
2672 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2673 Slog.w(TAG, "If you want to interect with IME, you need "
2674 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2675 hideCurrentInputLocked(0, null);
2676 return null;
2677 }
2678
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002679 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002680 if (DEBUG) {
2681 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2682 + " attribute=" + attribute + ", token = " + windowToken);
2683 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002684 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002685 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002686 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002687 }
2688 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002689 }
2690 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002691 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002692 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002693
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002694 // Should we auto-show the IME even if the caller has not
2695 // specified what should be done with it?
2696 // We only do this automatically if the window can resize
2697 // to accommodate the IME (so what the user sees will give
2698 // them good context without input information being obscured
2699 // by the IME) or if running on a large screen where there
2700 // is more room for the target window + IME.
2701 final boolean doAutoShow =
2702 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2703 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2704 || mRes.getConfiguration().isLayoutSizeAtLeast(
2705 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002706 final boolean isTextEditor =
2707 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2708
2709 // We want to start input before showing the IME, but after closing
2710 // it. We want to do this after closing it to help the IME disappear
2711 // more quickly (not get stuck behind it initializing itself for the
2712 // new focused input, even if its window wants to hide the IME).
2713 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2716 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002717 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2719 // There is no focus view, and this window will
2720 // be behind any soft input window, so hide the
2721 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002722 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002723 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002725 } else if (isTextEditor && doAutoShow && (softInputMode &
2726 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 // There is a focus view, and we are navigating forward
2728 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002729 // We only do this automatically if the window can resize
2730 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002731 // them good context without input information being obscured
2732 // by the IME) or if running on a large screen where there
2733 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002734 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002735 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002736 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002737 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002738 didStart = true;
2739 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002740 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
2742 break;
2743 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2744 // Do nothing.
2745 break;
2746 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2747 if ((softInputMode &
2748 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002749 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002750 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 }
2752 break;
2753 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002754 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002755 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 break;
2757 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2758 if ((softInputMode &
2759 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002760 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002761 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002762 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002763 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002764 didStart = true;
2765 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002766 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 }
2768 break;
2769 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002770 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002771 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002772 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002773 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002774 didStart = true;
2775 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002776 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 break;
2778 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002779
2780 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002781 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002782 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
2785 } finally {
2786 Binder.restoreCallingIdentity(ident);
2787 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002788
2789 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002791
satok42c5a162011-05-26 16:46:14 +09002792 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002793 public void showInputMethodPickerFromClient(
2794 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002795 if (!calledFromValidUser()) {
2796 return;
2797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 synchronized (mMethodMap) {
2799 if (mCurClient == null || client == null
2800 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002801 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002802 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 }
2804
satok440aab52010-11-25 09:43:11 +09002805 // Always call subtype picker, because subtype picker is a superset of input method
2806 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002807 mHandler.sendMessage(mCaller.obtainMessageI(
2808 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002809 }
2810 }
2811
satok42c5a162011-05-26 16:46:14 +09002812 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002814 if (!calledFromValidUser()) {
2815 return;
2816 }
satok28203512010-11-24 11:06:49 +09002817 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2818 }
2819
satok42c5a162011-05-26 16:46:14 +09002820 @Override
satok28203512010-11-24 11:06:49 +09002821 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002822 if (!calledFromValidUser()) {
2823 return;
2824 }
satok28203512010-11-24 11:06:49 +09002825 synchronized (mMethodMap) {
2826 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002827 setInputMethodWithSubtypeIdLocked(token, id,
2828 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2829 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002830 } else {
2831 setInputMethod(token, id);
2832 }
2833 }
satokab751aa2010-09-14 19:17:36 +09002834 }
2835
satok42c5a162011-05-26 16:46:14 +09002836 @Override
satokb416a712010-11-25 20:42:14 +09002837 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002838 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002839 if (!calledFromValidUser()) {
2840 return;
2841 }
satokb416a712010-11-25 20:42:14 +09002842 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002843 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2844 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002845 }
2846 }
2847
satok4fc87d62011-05-20 16:13:43 +09002848 @Override
satok735cf382010-11-11 20:40:09 +09002849 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002850 if (!calledFromValidUser()) {
2851 return false;
2852 }
satok735cf382010-11-11 20:40:09 +09002853 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002854 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002855 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002856 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002857 lastImi = mMethodMap.get(lastIme.first);
2858 } else {
2859 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002860 }
satok4fc87d62011-05-20 16:13:43 +09002861 String targetLastImiId = null;
2862 int subtypeId = NOT_A_SUBTYPE_ID;
2863 if (lastIme != null && lastImi != null) {
2864 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002865 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002866 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2867 : mCurrentSubtype.hashCode();
2868 // If the last IME is the same as the current IME and the last subtype is not
2869 // defined, there is no need to switch to the last IME.
2870 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2871 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002872 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002873 }
2874 }
2875
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002876 if (TextUtils.isEmpty(targetLastImiId)
2877 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002878 // This is a safety net. If the currentSubtype can't be added to the history
2879 // and the framework couldn't find the last ime, we will make the last ime be
2880 // the most applicable enabled keyboard subtype of the system imes.
2881 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2882 if (enabled != null) {
2883 final int N = enabled.size();
2884 final String locale = mCurrentSubtype == null
2885 ? mRes.getConfiguration().locale.toString()
2886 : mCurrentSubtype.getLocale();
2887 for (int i = 0; i < N; ++i) {
2888 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002889 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002890 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002891 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2892 InputMethodUtils.getSubtypes(imi),
2893 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002894 if (keyboardSubtype != null) {
2895 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002896 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002897 imi, keyboardSubtype.hashCode());
2898 if(keyboardSubtype.getLocale().equals(locale)) {
2899 break;
2900 }
2901 }
2902 }
2903 }
2904 }
2905 }
2906
2907 if (!TextUtils.isEmpty(targetLastImiId)) {
2908 if (DEBUG) {
2909 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2910 + ", from: " + mCurMethodId + ", " + subtypeId);
2911 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002912 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002913 return true;
2914 } else {
2915 return false;
2916 }
satok735cf382010-11-11 20:40:09 +09002917 }
2918 }
2919
satoke7c6998e2011-06-03 17:57:59 +09002920 @Override
satok688bd472012-02-09 20:09:17 +09002921 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002922 if (!calledFromValidUser()) {
2923 return false;
2924 }
satok688bd472012-02-09 20:09:17 +09002925 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002926 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002927 return false;
2928 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002929 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002930 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2931 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002932 if (nextSubtype == null) {
2933 return false;
2934 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002935 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2936 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002937 return true;
2938 }
2939 }
2940
2941 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002942 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2943 if (!calledFromValidUser()) {
2944 return false;
2945 }
2946 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002947 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002948 return false;
2949 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002950 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002951 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2952 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002953 if (nextSubtype == null) {
2954 return false;
2955 }
2956 return true;
2957 }
2958 }
2959
2960 @Override
satok68f1b782011-04-11 14:26:04 +09002961 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002962 if (!calledFromValidUser()) {
2963 return null;
2964 }
satok68f1b782011-04-11 14:26:04 +09002965 synchronized (mMethodMap) {
2966 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2967 // TODO: Handle the case of the last IME with no subtypes
2968 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2969 || TextUtils.isEmpty(lastIme.second)) return null;
2970 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2971 if (lastImi == null) return null;
2972 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002973 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002974 final int lastSubtypeId =
2975 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002976 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2977 return null;
2978 }
2979 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002980 } catch (NumberFormatException e) {
2981 return null;
2982 }
2983 }
2984 }
2985
satoke7c6998e2011-06-03 17:57:59 +09002986 @Override
satokee5e77c2011-09-02 18:50:15 +09002987 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002988 if (!calledFromValidUser()) {
2989 return;
2990 }
satok91e88122011-07-18 11:11:42 +09002991 // By this IPC call, only a process which shares the same uid with the IME can add
2992 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002993 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002994 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002995 if (!mSystemReady) {
2996 return;
2997 }
satok91e88122011-07-18 11:11:42 +09002998 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002999 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003000 final String[] packageInfos;
3001 try {
3002 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3003 } catch (RemoteException e) {
3004 Slog.e(TAG, "Failed to get package infos");
3005 return;
3006 }
satok91e88122011-07-18 11:11:42 +09003007 if (packageInfos != null) {
3008 final int packageNum = packageInfos.length;
3009 for (int i = 0; i < packageNum; ++i) {
3010 if (packageInfos[i].equals(imi.getPackageName())) {
3011 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003012 final long ident = Binder.clearCallingIdentity();
3013 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003014 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003015 } finally {
3016 Binder.restoreCallingIdentity(ident);
3017 }
satokee5e77c2011-09-02 18:50:15 +09003018 return;
satok91e88122011-07-18 11:11:42 +09003019 }
3020 }
3021 }
satoke7c6998e2011-06-03 17:57:59 +09003022 }
satokee5e77c2011-09-02 18:50:15 +09003023 return;
satoke7c6998e2011-06-03 17:57:59 +09003024 }
3025
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003026 @Override
3027 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003028 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003029 }
3030
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003031 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003032 public void clearLastInputMethodWindowForTransition(IBinder token) {
3033 if (!calledFromValidUser()) {
3034 return;
3035 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003036 synchronized (mMethodMap) {
3037 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003038 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003039 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003040 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003041 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003042 }
3043
3044 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003045 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003046 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003047 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003048 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003049 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003050 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3051 if (DEBUG) {
3052 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3053 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3054 + " actual: " + sequenceNumber);
3055 }
3056 return;
3057 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003058 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3059 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003060 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003061 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003062 }
3063 }
3064
satok28203512010-11-24 11:06:49 +09003065 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003067 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3068 }
3069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003071 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3072 if (token == null) {
3073 if (mContext.checkCallingOrSelfPermission(
3074 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3075 != PackageManager.PERMISSION_GRANTED) {
3076 throw new SecurityException(
3077 "Using null token requires permission "
3078 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003080 } else if (mCurToken != token) {
3081 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3082 + " token: " + token);
3083 return;
3084 }
3085
3086 final long ident = Binder.clearCallingIdentity();
3087 try {
3088 setInputMethodLocked(id, subtypeId);
3089 } finally {
3090 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 }
3092 }
3093
satok42c5a162011-05-26 16:46:14 +09003094 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003096 if (!calledFromValidUser()) {
3097 return;
3098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003100 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 return;
3102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 long ident = Binder.clearCallingIdentity();
3104 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003105 hideCurrentInputLocked(flags, null);
3106 } finally {
3107 Binder.restoreCallingIdentity(ident);
3108 }
3109 }
3110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003111
satok42c5a162011-05-26 16:46:14 +09003112 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003113 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003114 if (!calledFromValidUser()) {
3115 return;
3116 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003117 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003118 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003119 return;
3120 }
3121 long ident = Binder.clearCallingIdentity();
3122 try {
3123 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 } finally {
3125 Binder.restoreCallingIdentity(ident);
3126 }
3127 }
3128 }
3129
3130 void setEnabledSessionInMainThread(SessionState session) {
3131 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003132 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003134 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003135 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 } catch (RemoteException e) {
3137 }
3138 }
3139 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003140 if (mEnabledSession != null && mEnabledSession.session != null) {
3141 try {
3142 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3143 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3144 } catch (RemoteException e) {
3145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 }
3147 }
3148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003149
satok42c5a162011-05-26 16:46:14 +09003150 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003152 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003154 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003155 final boolean showAuxSubtypes;
3156 switch (msg.arg1) {
3157 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3158 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3159 // implemented so that auxiliary subtypes will be excluded when the soft
3160 // keyboard is invisible.
3161 showAuxSubtypes = mInputShown;
3162 break;
3163 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3164 showAuxSubtypes = true;
3165 break;
3166 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3167 showAuxSubtypes = false;
3168 break;
3169 default:
3170 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3171 return false;
3172 }
3173 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003174 return true;
3175
satok47a44912010-10-06 16:03:58 +09003176 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003177 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003178 return true;
3179
3180 case MSG_SHOW_IM_CONFIG:
3181 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003182 return true;
3183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 case MSG_UNBIND_INPUT:
3187 try {
3188 ((IInputMethod)msg.obj).unbindInput();
3189 } catch (RemoteException e) {
3190 // There is nothing interesting about the method dying.
3191 }
3192 return true;
3193 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003194 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 try {
3196 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3197 } catch (RemoteException e) {
3198 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003199 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 return true;
3201 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003202 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003204 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003205 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003206 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 } catch (RemoteException e) {
3208 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003209 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 return true;
3211 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003212 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003214 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003215 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003216 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 } catch (RemoteException e) {
3218 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003219 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003221 case MSG_HIDE_CURRENT_INPUT_METHOD:
3222 synchronized (mMethodMap) {
3223 hideCurrentInputLocked(0, null);
3224 }
3225 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003227 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003229 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3231 } catch (RemoteException e) {
3232 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003233 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003235 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003236 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003237 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003238 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003240 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003242 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003243 // Dispose the channel if the input method is not local to this process
3244 // because the remote proxy will get its own copy when unparceled.
3245 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003246 channel.dispose();
3247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003249 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003253
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003254 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003255 final int missingMethods = msg.arg1;
3256 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003257 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003258 final IBinder startInputToken = (IBinder) args.arg1;
3259 final SessionState session = (SessionState) args.arg2;
3260 final IInputContext inputContext = (IInputContext) args.arg3;
3261 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003264 session.method.startInput(startInputToken, inputContext, missingMethods,
3265 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 } catch (RemoteException e) {
3267 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003268 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003273
Yohei Yukawa33e81792015-11-17 21:14:42 -08003274 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003276 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 } catch (RemoteException e) {
3278 // There is nothing interesting about the last client dying.
3279 }
3280 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003281 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003282 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003283 IInputMethodClient client = (IInputMethodClient)args.arg1;
3284 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003286 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003288 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003289 } finally {
3290 // Dispose the channel if the input method is not local to this process
3291 // because the remote proxy will get its own copy when unparceled.
3292 if (res.channel != null && Binder.isProxy(client)) {
3293 res.channel.dispose();
3294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003296 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003298 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003299 case MSG_SET_ACTIVE:
3300 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003301 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003302 } catch (RemoteException e) {
3303 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3304 + ((ClientState)msg.obj).pid + " uid "
3305 + ((ClientState)msg.obj).uid);
3306 }
3307 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003308 case MSG_SET_INTERACTIVE:
3309 handleSetInteractive(msg.arg1 != 0);
3310 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003311 case MSG_SWITCH_IME:
3312 handleSwitchInputMethod(msg.arg1 != 0);
3313 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003314 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3315 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003316 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003317 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003318 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003319 } catch (RemoteException e) {
3320 Slog.w(TAG, "Got RemoteException sending "
3321 + "setUserActionNotificationSequenceNumber("
3322 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003323 + clientState.pid + " uid "
3324 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003325 }
3326 return true;
3327 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003328 case MSG_REPORT_FULLSCREEN_MODE: {
3329 final boolean fullscreen = msg.arg1 != 0;
3330 final ClientState clientState = (ClientState)msg.obj;
3331 try {
3332 clientState.client.reportFullscreenMode(fullscreen);
3333 } catch (RemoteException e) {
3334 Slog.w(TAG, "Got RemoteException sending "
3335 + "reportFullscreen(" + fullscreen + ") notification to pid="
3336 + clientState.pid + " uid=" + clientState.uid);
3337 }
3338 return true;
3339 }
satok01038492012-04-09 21:08:27 +09003340
3341 // --------------------------------------------------------------
3342 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003343 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003344 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003345 case MSG_SYSTEM_UNLOCK_USER:
3346 final int userId = msg.arg1;
3347 onUnlockUser(userId);
3348 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 }
3350 return false;
3351 }
3352
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003353 private void handleSetInteractive(final boolean interactive) {
3354 synchronized (mMethodMap) {
3355 mIsInteractive = interactive;
3356 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3357
3358 // Inform the current client of the change in active status
3359 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003360 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3361 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3362 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003363 }
3364 }
3365 }
3366
Yohei Yukawaae61f712015-12-09 13:00:10 -08003367 private void handleSwitchInputMethod(final boolean forwardDirection) {
3368 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003369 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003370 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003371 if (nextSubtype == null) {
3372 return;
3373 }
3374 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003375 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3376 if (newInputMethodInfo == null) {
3377 return;
3378 }
3379 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3380 newInputMethodInfo, mCurrentSubtype);
3381 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003382 if (mSubtypeSwitchedByShortCutToast == null) {
3383 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3384 Toast.LENGTH_SHORT);
3385 } else {
3386 mSubtypeSwitchedByShortCutToast.setText(toastText);
3387 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003388 mSubtypeSwitchedByShortCutToast.show();
3389 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003390 }
3391 }
3392
satokdc9ddae2011-10-06 12:22:36 +09003393 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003394 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3395 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003396 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003397 if (DEBUG) {
3398 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3399 }
satok723a27e2010-11-11 14:58:11 +09003400 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003401 return true;
3402 }
3403
3404 return false;
3405 }
3406
Yohei Yukawa94e33302016-02-12 19:37:03 -08003407 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003408 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003409 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003410 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003411 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003412 if (!mSystemReady) {
3413 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3414 return;
3415 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003416 mMethodList.clear();
3417 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003418 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003419 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003420
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003421 // Use for queryIntentServicesAsUser
3422 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003424 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3425 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3426 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003427 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003429 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3430 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003431
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003432 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003433 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 for (int i = 0; i < services.size(); ++i) {
3435 ResolveInfo ri = services.get(i);
3436 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003437 final String imeId = InputMethodInfo.computeId(ri);
3438 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3439 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 + ": it does not require the permission "
3441 + android.Manifest.permission.BIND_INPUT_METHOD);
3442 continue;
3443 }
3444
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003445 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003447 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 try {
satoke7c6998e2011-06-03 17:57:59 +09003449 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003450 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003451 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003452 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453
3454 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003455 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003457 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003458 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 }
3460 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003461
Yohei Yukawac4e44912017-02-09 19:30:22 -08003462 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3463 // negatives when the package state remains to be the same but only the component state is
3464 // changed.
3465 {
3466 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3467 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3468 // conservative, but it seems we cannot use it for now (Issue 35176630).
3469 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3470 new Intent(InputMethod.SERVICE_INTERFACE),
3471 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3472 final int N = allInputMethodServices.size();
3473 for (int i = 0; i < N; ++i) {
3474 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003475 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3476 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003477 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003478 }
3479 }
3480
Yohei Yukawa859df052016-02-17 07:56:46 -08003481 // TODO: The following code should find better place to live.
3482 if (!resetDefaultEnabledIme) {
3483 boolean enabledImeFound = false;
3484 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3485 final int N = enabledImes.size();
3486 for (int i = 0; i < N; ++i) {
3487 final InputMethodInfo imi = enabledImes.get(i);
3488 if (mMethodList.contains(imi)) {
3489 enabledImeFound = true;
3490 break;
3491 }
3492 }
3493 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003494 if (DEBUG) {
3495 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3496 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003497 resetDefaultEnabledIme = true;
3498 resetSelectedInputMethodAndSubtypeLocked("");
3499 }
3500 }
3501
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003502 if (resetDefaultEnabledIme) {
3503 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003504 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003505 final int N = defaultEnabledIme.size();
3506 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003507 final InputMethodInfo imi = defaultEnabledIme.get(i);
3508 if (DEBUG) {
3509 Slog.d(TAG, "--- enable ime = " + imi);
3510 }
3511 setInputMethodEnabledLocked(imi.getId(), true);
3512 }
3513 }
3514
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003515 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003516 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003517 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003518 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3519 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003520 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003521 }
3522 } else {
3523 // Double check that the default IME is certainly enabled.
3524 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003525 }
3526 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003527 // Here is not the perfect place to reset the switching controller. Ideally
3528 // mSwitchingController and mSettings should be able to share the same state.
3529 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3530 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003531 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003535
satok217f5482010-12-15 05:19:19 +09003536 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003537 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003538 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003539 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3540 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003541 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003542 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003543 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003544 final int userId;
3545 synchronized (mMethodMap) {
3546 userId = mSettings.getCurrentUserId();
3547 }
3548 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003549 }
3550
3551 private void showConfigureInputMethods() {
3552 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3553 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3554 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3555 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003556 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003557 }
3558
satok2c93efc2012-04-02 19:33:47 +09003559 private boolean isScreenLocked() {
3560 return mKeyguardManager != null
3561 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3562 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003563
Seigo Nonaka14e13912015-05-06 21:04:13 -07003564 private void showInputMethodMenu(boolean showAuxSubtypes) {
3565 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566
satok2c93efc2012-04-02 19:33:47 +09003567 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003568
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003569 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003570 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003571 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003572
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003573 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003574 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003575 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3576 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003577 if (immis == null || immis.size() == 0) {
3578 return;
3579 }
3580
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003581 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582
satok688bd472012-02-09 20:09:17 +09003583 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003584 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003585 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003586
satokc3690562012-01-10 20:14:43 +09003587 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003588 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003589 if (currentSubtype != null) {
3590 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003591 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3592 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003593 }
3594 }
3595
Ken Wakasa761eb372011-03-04 19:06:18 +09003596 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003597 mIms = new InputMethodInfo[N];
3598 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003599 int checkedItem = 0;
3600 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003601 final ImeSubtypeListItem item = imList.get(i);
3602 mIms[i] = item.mImi;
3603 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003604 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003605 int subtypeId = mSubtypeIds[i];
3606 if ((subtypeId == NOT_A_SUBTYPE_ID)
3607 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3608 || (subtypeId == lastInputMethodSubtypeId)) {
3609 checkedItem = i;
3610 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003613
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003614 final Context settingsContext = new ContextThemeWrapper(
3615 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003616 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3617
3618 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003619 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3620 @Override
3621 public void onCancel(DialogInterface dialog) {
3622 hideInputMethodMenu();
3623 }
3624 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003625
3626 final Context dialogContext = mDialogBuilder.getContext();
3627 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3628 com.android.internal.R.styleable.DialogPreference,
3629 com.android.internal.R.attr.alertDialogStyle, 0);
3630 final Drawable dialogIcon = a.getDrawable(
3631 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3632 a.recycle();
3633
3634 mDialogBuilder.setIcon(dialogIcon);
3635
Yohei Yukawad34e1482016-02-11 08:03:52 -08003636 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003637 final View tv = inflater.inflate(
3638 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3639 mDialogBuilder.setCustomTitle(tv);
3640
3641 // Setup layout for a toggle switch of the hardware keyboard
3642 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003643 mSwitchingDialogTitleView
3644 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003645 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003646 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003647 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003648 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003649 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003650 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3651 @Override
3652 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003653 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003654 // Ensure that the input method dialog is dismissed when changing
3655 // the hardware keyboard state.
3656 hideInputMethodMenu();
3657 }
3658 });
3659
Alan Viverette505e3ab2014-11-24 15:22:11 -08003660 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003661 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3662 final OnClickListener choiceListener = new OnClickListener() {
3663 @Override
3664 public void onClick(final DialogInterface dialog, final int which) {
3665 synchronized (mMethodMap) {
3666 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3667 || mSubtypeIds.length <= which) {
3668 return;
satok01038492012-04-09 21:08:27 +09003669 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003670 final InputMethodInfo im = mIms[which];
3671 int subtypeId = mSubtypeIds[which];
3672 adapter.mCheckedItem = which;
3673 adapter.notifyDataSetChanged();
3674 hideInputMethodMenu();
3675 if (im != null) {
3676 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3677 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003678 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003679 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003681 }
3682 }
3683 };
3684 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003687 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003688 final Window w = mSwitchingDialog.getWindow();
3689 final WindowManager.LayoutParams attrs = w.getAttributes();
3690 w.setType(TYPE_INPUT_METHOD_DIALOG);
3691 // Use an alternate token for the dialog for that window manager can group the token
3692 // with other IME windows based on type vs. grouping based on whichever token happens
3693 // to get selected by the system later on.
3694 attrs.token = mSwitchingDialogToken;
3695 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3696 attrs.setTitle("Select input method");
3697 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003698 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 mSwitchingDialog.show();
3700 }
3701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003702
Ken Wakasa05dbb652011-08-22 15:22:43 +09003703 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3704 private final LayoutInflater mInflater;
3705 private final int mTextViewResourceId;
3706 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003707 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003708 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3709 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3710 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003711
Ken Wakasa05dbb652011-08-22 15:22:43 +09003712 mTextViewResourceId = textViewResourceId;
3713 mItemsList = itemsList;
3714 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003715 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003716 }
3717
3718 @Override
3719 public View getView(int position, View convertView, ViewGroup parent) {
3720 final View view = convertView != null ? convertView
3721 : mInflater.inflate(mTextViewResourceId, null);
3722 if (position < 0 || position >= mItemsList.size()) return view;
3723 final ImeSubtypeListItem item = mItemsList.get(position);
3724 final CharSequence imeName = item.mImeName;
3725 final CharSequence subtypeName = item.mSubtypeName;
3726 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3727 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3728 if (TextUtils.isEmpty(subtypeName)) {
3729 firstTextView.setText(imeName);
3730 secondTextView.setVisibility(View.GONE);
3731 } else {
3732 firstTextView.setText(subtypeName);
3733 secondTextView.setText(imeName);
3734 secondTextView.setVisibility(View.VISIBLE);
3735 }
3736 final RadioButton radioButton =
3737 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3738 radioButton.setChecked(position == mCheckedItem);
3739 return view;
3740 }
3741 }
3742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003744 synchronized (mMethodMap) {
3745 hideInputMethodMenuLocked();
3746 }
3747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003748
The Android Open Source Project10592532009-03-18 17:39:46 -07003749 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003750 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751
The Android Open Source Project10592532009-03-18 17:39:46 -07003752 if (mSwitchingDialog != null) {
3753 mSwitchingDialog.dismiss();
3754 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003756
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003757 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003758 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003759 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003763
satok42c5a162011-05-26 16:46:14 +09003764 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003766 // TODO: Make this work even for non-current users?
3767 if (!calledFromValidUser()) {
3768 return false;
3769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 synchronized (mMethodMap) {
3771 if (mContext.checkCallingOrSelfPermission(
3772 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3773 != PackageManager.PERMISSION_GRANTED) {
3774 throw new SecurityException(
3775 "Requires permission "
3776 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3777 }
Anna Galusza9b278112016-01-04 11:37:37 -08003778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 long ident = Binder.clearCallingIdentity();
3780 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003781 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 } finally {
3783 Binder.restoreCallingIdentity(ident);
3784 }
3785 }
3786 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003787
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003788 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3789 // Make sure this is a valid input method.
3790 InputMethodInfo imm = mMethodMap.get(id);
3791 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003792 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003793 }
3794
satokd87c2592010-09-29 11:52:06 +09003795 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3796 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003797
satokd87c2592010-09-29 11:52:06 +09003798 if (enabled) {
3799 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3800 if (pair.first.equals(id)) {
3801 // We are enabling this input method, but it is already enabled.
3802 // Nothing to do. The previous state was enabled.
3803 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003804 }
3805 }
satokd87c2592010-09-29 11:52:06 +09003806 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3807 // Previous state was disabled.
3808 return false;
3809 } else {
3810 StringBuilder builder = new StringBuilder();
3811 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3812 builder, enabledInputMethodsList, id)) {
3813 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003814 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003815 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3816 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3817 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003818 }
3819 // Previous state was enabled.
3820 return true;
3821 } else {
3822 // We are disabling the input method but it is already disabled.
3823 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003824 return false;
3825 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003826 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003827 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003828
satok723a27e2010-11-11 14:58:11 +09003829 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3830 boolean setSubtypeOnly) {
3831 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003832 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003833
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003834 mCurUserActionNotificationSequenceNumber =
3835 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3836 if (DEBUG) {
3837 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3838 + mCurUserActionNotificationSequenceNumber);
3839 }
3840
3841 if (mCurClient != null && mCurClient.client != null) {
3842 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3843 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003844 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003845 }
3846
satok723a27e2010-11-11 14:58:11 +09003847 // Set Subtype here
3848 if (imi == null || subtypeId < 0) {
3849 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003850 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003851 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003852 if (subtypeId < imi.getSubtypeCount()) {
3853 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3854 mSettings.putSelectedSubtype(subtype.hashCode());
3855 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003856 } else {
3857 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003858 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003859 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003860 }
satokab751aa2010-09-14 19:17:36 +09003861 }
satok723a27e2010-11-11 14:58:11 +09003862
Yohei Yukawa68645a62016-02-17 07:54:20 -08003863 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003864 // Set InputMethod here
3865 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3866 }
3867 }
3868
3869 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3870 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3871 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3872 // newDefaultIme is empty when there is no candidate for the selected IME.
3873 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3874 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3875 if (subtypeHashCode != null) {
3876 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003877 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003878 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003879 } catch (NumberFormatException e) {
3880 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3881 }
3882 }
3883 }
3884 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003885 }
3886
satok4e4569d2010-11-19 18:45:53 +09003887 // If there are no selected shortcuts, tries finding the most applicable ones.
3888 private Pair<InputMethodInfo, InputMethodSubtype>
3889 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3890 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3891 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003892 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003893 boolean foundInSystemIME = false;
3894
3895 // Search applicable subtype for each InputMethodInfo
3896 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003897 final String imiId = imi.getId();
3898 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3899 continue;
3900 }
satokcd7cd292010-11-20 15:46:23 +09003901 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003902 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003903 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003904 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003905 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003906 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003907 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003908 }
satokdf31ae62011-01-15 06:19:44 +09003909 // 2. Search by the system locale from enabledSubtypes.
3910 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003911 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003912 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003913 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003914 }
satoka86f5e42011-09-02 17:12:42 +09003915 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003916 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003917 final ArrayList<InputMethodSubtype> subtypesForSearch =
3918 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003919 ? InputMethodUtils.getSubtypes(imi)
3920 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003921 // 4. Search by the current subtype's locale from all subtypes.
3922 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003923 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003924 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003925 }
3926 // 5. Search by the system locale from all subtypes.
3927 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003928 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003929 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003930 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003931 }
satokcd7cd292010-11-20 15:46:23 +09003932 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003933 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003934 // The current input method is the most applicable IME.
3935 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003936 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003937 break;
satok7599a7f2010-12-22 13:45:23 +09003938 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003939 // The system input method is 2nd applicable IME.
3940 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003941 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003942 if ((imi.getServiceInfo().applicationInfo.flags
3943 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3944 foundInSystemIME = true;
3945 }
satok4e4569d2010-11-19 18:45:53 +09003946 }
3947 }
3948 }
3949 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003950 if (mostApplicableIMI != null) {
3951 Slog.w(TAG, "Most applicable shortcut input method was:"
3952 + mostApplicableIMI.getId());
3953 if (mostApplicableSubtype != null) {
3954 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3955 + "," + mostApplicableSubtype.getMode() + ","
3956 + mostApplicableSubtype.getLocale());
3957 }
3958 }
satok4e4569d2010-11-19 18:45:53 +09003959 }
satokcd7cd292010-11-20 15:46:23 +09003960 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003961 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003962 } else {
3963 return null;
3964 }
3965 }
3966
satokab751aa2010-09-14 19:17:36 +09003967 /**
3968 * @return Return the current subtype of this input method.
3969 */
satok42c5a162011-05-26 16:46:14 +09003970 @Override
satokab751aa2010-09-14 19:17:36 +09003971 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003972 // TODO: Make this work even for non-current users?
3973 if (!calledFromValidUser()) {
3974 return null;
3975 }
3976 synchronized (mMethodMap) {
3977 return getCurrentInputMethodSubtypeLocked();
3978 }
3979 }
3980
3981 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003982 if (mCurMethodId == null) {
3983 return null;
3984 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003985 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003986 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3987 if (imi == null || imi.getSubtypeCount() == 0) {
3988 return null;
satok4e4569d2010-11-19 18:45:53 +09003989 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003990 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003991 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3992 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003993 if (subtypeId == NOT_A_SUBTYPE_ID) {
3994 // If there are no selected subtypes, the framework will try to find
3995 // the most applicable subtype from explicitly or implicitly enabled
3996 // subtypes.
3997 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003998 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003999 // If there is only one explicitly or implicitly enabled subtype,
4000 // just returns it.
4001 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4002 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4003 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004004 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004005 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004006 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004007 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004008 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004009 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4010 true);
satok4e4569d2010-11-19 18:45:53 +09004011 }
satok3ef8b292010-11-23 06:06:29 +09004012 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004013 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004014 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004015 }
4016 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004017 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004018 }
4019
satok4e4569d2010-11-19 18:45:53 +09004020 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004021 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004022 @Override
satok4e4569d2010-11-19 18:45:53 +09004023 public List getShortcutInputMethodsAndSubtypes() {
4024 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004025 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004026 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004027 // If there are no selected shortcut subtypes, the framework will try to find
4028 // the most applicable subtype from all subtypes whose mode is
4029 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004030 Pair<InputMethodInfo, InputMethodSubtype> info =
4031 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004032 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004033 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004034 ret.add(info.first);
4035 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004036 }
satok3da92232011-01-11 22:46:30 +09004037 return ret;
satokf3db1af2010-11-23 13:34:33 +09004038 }
satokf3db1af2010-11-23 13:34:33 +09004039 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4040 ret.add(imi);
4041 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4042 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004043 }
4044 }
satokf3db1af2010-11-23 13:34:33 +09004045 return ret;
satok4e4569d2010-11-19 18:45:53 +09004046 }
4047 }
4048
satok42c5a162011-05-26 16:46:14 +09004049 @Override
satokb66d2872010-11-10 01:04:04 +09004050 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004051 // TODO: Make this work even for non-current users?
4052 if (!calledFromValidUser()) {
4053 return false;
4054 }
satokb66d2872010-11-10 01:04:04 +09004055 synchronized (mMethodMap) {
4056 if (subtype != null && mCurMethodId != null) {
4057 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004058 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004059 if (subtypeId != NOT_A_SUBTYPE_ID) {
4060 setInputMethodLocked(mCurMethodId, subtypeId);
4061 return true;
4062 }
4063 }
4064 return false;
4065 }
4066 }
4067
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004068 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004069 private static class InputMethodFileManager {
4070 private static final String SYSTEM_PATH = "system";
4071 private static final String INPUT_METHOD_PATH = "inputmethod";
4072 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4073 private static final String NODE_SUBTYPES = "subtypes";
4074 private static final String NODE_SUBTYPE = "subtype";
4075 private static final String NODE_IMI = "imi";
4076 private static final String ATTR_ID = "id";
4077 private static final String ATTR_LABEL = "label";
4078 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004079 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004080 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004081 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004082 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4083 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4084 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004085 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004086 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4087 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004088 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004089 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004090 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004091 if (methodMap == null) {
4092 throw new NullPointerException("methodMap is null");
4093 }
4094 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004095 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004096 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4097 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004098 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004099 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004100 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4101 }
4102 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4103 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4104 if (!subtypeFile.exists()) {
4105 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004106 writeAdditionalInputMethodSubtypes(
4107 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004108 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004109 readAdditionalInputMethodSubtypes(
4110 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004111 }
4112 }
4113
4114 private void deleteAllInputMethodSubtypes(String imiId) {
4115 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004116 mAdditionalSubtypesMap.remove(imiId);
4117 writeAdditionalInputMethodSubtypes(
4118 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004119 }
4120 }
4121
4122 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004123 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004124 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004125 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004126 final int N = additionalSubtypes.length;
4127 for (int i = 0; i < N; ++i) {
4128 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004129 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004130 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004131 } else {
4132 Slog.w(TAG, "Duplicated subtype definition found: "
4133 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004134 }
4135 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004136 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4137 writeAdditionalInputMethodSubtypes(
4138 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004139 }
4140 }
4141
4142 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4143 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004144 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004145 }
4146 }
4147
4148 private static void writeAdditionalInputMethodSubtypes(
4149 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4150 HashMap<String, InputMethodInfo> methodMap) {
4151 // Safety net for the case that this function is called before methodMap is set.
4152 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4153 FileOutputStream fos = null;
4154 try {
4155 fos = subtypesFile.startWrite();
4156 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004157 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004158 out.startDocument(null, true);
4159 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4160 out.startTag(null, NODE_SUBTYPES);
4161 for (String imiId : allSubtypes.keySet()) {
4162 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4163 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4164 continue;
4165 }
4166 out.startTag(null, NODE_IMI);
4167 out.attribute(null, ATTR_ID, imiId);
4168 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4169 final int N = subtypesList.size();
4170 for (int i = 0; i < N; ++i) {
4171 final InputMethodSubtype subtype = subtypesList.get(i);
4172 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004173 if (subtype.hasSubtypeId()) {
4174 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4175 String.valueOf(subtype.getSubtypeId()));
4176 }
satoke7c6998e2011-06-03 17:57:59 +09004177 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4178 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4179 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004180 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4181 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004182 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4183 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4184 out.attribute(null, ATTR_IS_AUXILIARY,
4185 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004186 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4187 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004188 out.endTag(null, NODE_SUBTYPE);
4189 }
4190 out.endTag(null, NODE_IMI);
4191 }
4192 out.endTag(null, NODE_SUBTYPES);
4193 out.endDocument();
4194 subtypesFile.finishWrite(fos);
4195 } catch (java.io.IOException e) {
4196 Slog.w(TAG, "Error writing subtypes", e);
4197 if (fos != null) {
4198 subtypesFile.failWrite(fos);
4199 }
4200 }
4201 }
4202
4203 private static void readAdditionalInputMethodSubtypes(
4204 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4205 if (allSubtypes == null || subtypesFile == null) return;
4206 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004207 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004208 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004209 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004210 int type = parser.getEventType();
4211 // Skip parsing until START_TAG
4212 while ((type = parser.next()) != XmlPullParser.START_TAG
4213 && type != XmlPullParser.END_DOCUMENT) {}
4214 String firstNodeName = parser.getName();
4215 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4216 throw new XmlPullParserException("Xml doesn't start with subtypes");
4217 }
4218 final int depth =parser.getDepth();
4219 String currentImiId = null;
4220 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4221 while (((type = parser.next()) != XmlPullParser.END_TAG
4222 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4223 if (type != XmlPullParser.START_TAG)
4224 continue;
4225 final String nodeName = parser.getName();
4226 if (NODE_IMI.equals(nodeName)) {
4227 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4228 if (TextUtils.isEmpty(currentImiId)) {
4229 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4230 continue;
4231 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004232 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004233 allSubtypes.put(currentImiId, tempSubtypesArray);
4234 } else if (NODE_SUBTYPE.equals(nodeName)) {
4235 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4236 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4237 continue;
4238 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004239 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004240 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004241 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004242 parser.getAttributeValue(null, ATTR_LABEL));
4243 final String imeSubtypeLocale =
4244 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004245 final String languageTag =
4246 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004247 final String imeSubtypeMode =
4248 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4249 final String imeSubtypeExtraValue =
4250 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004251 final boolean isAuxiliary = "1".equals(String.valueOf(
4252 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004253 final boolean isAsciiCapable = "1".equals(String.valueOf(
4254 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004255 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004256 .setSubtypeNameResId(label)
4257 .setSubtypeIconResId(icon)
4258 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004259 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004260 .setSubtypeMode(imeSubtypeMode)
4261 .setSubtypeExtraValue(imeSubtypeExtraValue)
4262 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004263 .setIsAsciiCapable(isAsciiCapable);
4264 final String subtypeIdString =
4265 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4266 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004267 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004268 }
4269 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004270 }
4271 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004272 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4273 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004274 return;
satoke7c6998e2011-06-03 17:57:59 +09004275 }
4276 }
4277 }
4278
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004279 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4280 @NonNull
4281 private final Handler mHandler;
4282
4283 LocalServiceImpl(@NonNull final Handler handler) {
4284 mHandler = handler;
4285 }
4286
4287 @Override
4288 public void setInteractive(boolean interactive) {
4289 // Do everything in handler so as not to block the caller.
4290 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4291 interactive ? 1 : 0, 0));
4292 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004293
4294 @Override
4295 public void switchInputMethod(boolean forwardDirection) {
4296 // Do everything in handler so as not to block the caller.
4297 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4298 forwardDirection ? 1 : 0, 0));
4299 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004300
4301 @Override
4302 public void hideCurrentInputMethod() {
4303 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4304 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4305 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004306 }
4307
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004308 private static String imeWindowStatusToString(final int imeWindowVis) {
4309 final StringBuilder sb = new StringBuilder();
4310 boolean first = true;
4311 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4312 sb.append("Active");
4313 first = false;
4314 }
4315 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4316 if (!first) {
4317 sb.append("|");
4318 }
4319 sb.append("Visible");
4320 }
4321 return sb.toString();
4322 }
4323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004325 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4326 @Nullable Uri contentUri, @Nullable String packageName) {
4327 if (!calledFromValidUser()) {
4328 return null;
4329 }
4330
4331 if (token == null) {
4332 throw new NullPointerException("token");
4333 }
4334 if (packageName == null) {
4335 throw new NullPointerException("packageName");
4336 }
4337 if (contentUri == null) {
4338 throw new NullPointerException("contentUri");
4339 }
4340 final String contentUriScheme = contentUri.getScheme();
4341 if (!"content".equals(contentUriScheme)) {
4342 throw new InvalidParameterException("contentUri must have content scheme");
4343 }
4344
4345 synchronized (mMethodMap) {
4346 final int uid = Binder.getCallingUid();
4347 if (mCurMethodId == null) {
4348 return null;
4349 }
4350 if (mCurToken != token) {
4351 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4352 + " token=" + token);
4353 return null;
4354 }
4355 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4356 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4357 // nature of our system. Let's compare it with our internal record.
4358 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4359 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4360 + mCurAttribute.packageName + " packageName=" + packageName);
4361 return null;
4362 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004363 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004364 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004365 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004366 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004367 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4368 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4369 imeUserId);
4370 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4371 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4372 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4373 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4374 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4375 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4376 // actually allowed to "uid", which is guaranteed to be the IME's one.
4377 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4378 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004379 }
4380 }
4381
4382 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004383 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4384 if (!calledFromValidUser()) {
4385 return;
4386 }
4387 synchronized (mMethodMap) {
4388 if (!calledWithValidToken(token)) {
4389 return;
4390 }
4391 if (mCurClient != null && mCurClient.client != null) {
4392 mInFullscreenMode = fullscreen;
4393 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4394 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4395 }
4396 }
4397 }
4398
4399 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004401 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402
4403 IInputMethod method;
4404 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004405 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 synchronized (mMethodMap) {
4410 p.println("Current Input Method Manager state:");
4411 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004412 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 for (int i=0; i<N; i++) {
4414 InputMethodInfo info = mMethodList.get(i);
4415 p.println(" InputMethod #" + i + ":");
4416 info.dump(p, " ");
4417 }
4418 p.println(" Clients:");
4419 for (ClientState ci : mClients.values()) {
4420 p.println(" Client " + ci + ":");
4421 p.println(" client=" + ci.client);
4422 p.println(" inputContext=" + ci.inputContext);
4423 p.println(" sessionRequested=" + ci.sessionRequested);
4424 p.println(" curSession=" + ci.curSession);
4425 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004426 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004428 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004429 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4430 + " softInputMode=" +
4431 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4432 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004433 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4435 + " mBoundToMethod=" + mBoundToMethod);
4436 p.println(" mCurToken=" + mCurToken);
4437 p.println(" mCurIntent=" + mCurIntent);
4438 method = mCurMethod;
4439 p.println(" mCurMethod=" + mCurMethod);
4440 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004441 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 p.println(" mShowRequested=" + mShowRequested
4443 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4444 + " mShowForced=" + mShowForced
4445 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004446 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004447 p.println(" mCurUserActionNotificationSequenceNumber="
4448 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004449 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004450 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004451 p.println(" mSwitchingController:");
4452 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004453 p.println(" mSettings:");
4454 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004455
4456 p.println(" mStartInputHistory:");
4457 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004459
Jeff Brownb88102f2010-09-08 11:49:43 -07004460 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 pw.flush();
4463 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004464 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4465 } catch (IOException | RemoteException e) {
4466 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004468 } else {
4469 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004471
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004472 if (focusedWindowClient != null && client != focusedWindowClient) {
4473 p.println(" ");
4474 p.println("Warning: Current input method client doesn't match the last focused. "
4475 + "window.");
4476 p.println("Dumping input method client in the last focused window just in case.");
4477 p.println(" ");
4478 pw.flush();
4479 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004480 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4481 } catch (IOException | RemoteException e) {
4482 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004483 }
4484 }
4485
Jeff Brownb88102f2010-09-08 11:49:43 -07004486 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 pw.flush();
4489 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004490 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4491 } catch (IOException | RemoteException e) {
4492 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004494 } else {
4495 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496 }
4497 }
4498}