blob: a8e2f32272406b3e98003ffbe666ba4a3b4df1af [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;
satok2c93efc2012-04-02 19:33:47 +09002091 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002092 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002093 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002094 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2095 // When physical keyboard is attached, we show the ime switcher (or notification if
2096 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2097 // exists in the IME switcher dialog. Might be OK to remove this condition once
2098 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2099 return true;
2100 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002101 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2102 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002103 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002104
2105 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2106 final int N = imis.size();
2107 if (N > 2) return true;
2108 if (N < 1) return false;
2109 int nonAuxCount = 0;
2110 int auxCount = 0;
2111 InputMethodSubtype nonAuxSubtype = null;
2112 InputMethodSubtype auxSubtype = null;
2113 for(int i = 0; i < N; ++i) {
2114 final InputMethodInfo imi = imis.get(i);
2115 final List<InputMethodSubtype> subtypes =
2116 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2117 final int subtypeCount = subtypes.size();
2118 if (subtypeCount == 0) {
2119 ++nonAuxCount;
2120 } else {
2121 for (int j = 0; j < subtypeCount; ++j) {
2122 final InputMethodSubtype subtype = subtypes.get(j);
2123 if (!subtype.isAuxiliary()) {
2124 ++nonAuxCount;
2125 nonAuxSubtype = subtype;
2126 } else {
2127 ++auxCount;
2128 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002129 }
2130 }
satok7cfc0ed2011-06-20 21:29:36 +09002131 }
2132 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002133 if (nonAuxCount > 1 || auxCount > 1) {
2134 return true;
2135 } else if (nonAuxCount == 1 && auxCount == 1) {
2136 if (nonAuxSubtype != null && auxSubtype != null
2137 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2138 || auxSubtype.overridesImplicitlyEnabledSubtype()
2139 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2140 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2141 return false;
2142 }
2143 return true;
2144 }
2145 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002146 }
2147
John Spurlocke0980502013-10-25 11:59:29 -04002148 private boolean isKeyguardLocked() {
2149 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2150 }
2151
Yohei Yukawad6475a62017-04-17 10:35:27 -07002152 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002153 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002154 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002155 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2156 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002157 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002158 return;
2159 }
2160
Yohei Yukawa69e68022017-02-13 12:04:50 -08002161 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002162 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002163 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002164 mImeWindowVis = vis;
2165 mBackDisposition = backDisposition;
2166 updateSystemUiLocked(token, vis, backDisposition);
2167 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002168
2169 final boolean dismissImeOnBackKeyPressed;
2170 switch (backDisposition) {
2171 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2172 dismissImeOnBackKeyPressed = true;
2173 break;
2174 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2175 dismissImeOnBackKeyPressed = false;
2176 break;
2177 default:
2178 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2179 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2180 break;
2181 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002182 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2183 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002184 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002185 }
2186
2187 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2188 synchronized (mMethodMap) {
2189 updateSystemUiLocked(token, vis, backDisposition);
2190 }
2191 }
2192
2193 // Caution! This method is called in this class. Handle multi-user carefully
2194 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2195 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002196 return;
2197 }
2198
2199 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2200 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002201 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002202 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002203 // apply policy for binder calls
2204 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2205 vis = 0;
satok06487a52010-10-29 11:37:18 +09002206 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002207 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2208 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2209 if (mStatusBar != null) {
2210 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2211 needsToShowImeSwitcher);
2212 }
2213 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2214 if (imi != null && needsToShowImeSwitcher) {
2215 // Used to load label
2216 final CharSequence title = mRes.getText(
2217 com.android.internal.R.string.select_input_method);
2218 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2219 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002220 mImeSwitcherNotification.setContentTitle(title)
2221 .setContentText(summary)
2222 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002223 try {
2224 if ((mNotificationManager != null)
2225 && !mIWindowManager.hasNavigationBar()) {
2226 if (DEBUG) {
2227 Slog.d(TAG, "--- show notification: label = " + summary);
2228 }
2229 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002230 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002231 mImeSwitcherNotification.build(), UserHandle.ALL);
2232 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002233 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002234 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002235 }
2236 } else {
2237 if (mNotificationShown && mNotificationManager != null) {
2238 if (DEBUG) {
2239 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002240 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002241 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002242 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002243 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002244 }
satok06487a52010-10-29 11:37:18 +09002245 }
2246 } finally {
2247 Binder.restoreCallingIdentity(ident);
2248 }
2249 }
2250
satoke7c6998e2011-06-03 17:57:59 +09002251 @Override
satokf9f01002011-05-19 21:31:50 +09002252 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002253 if (!calledFromValidUser()) {
2254 return;
2255 }
satokf9f01002011-05-19 21:31:50 +09002256 synchronized (mMethodMap) {
2257 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2258 for (int i = 0; i < spans.length; ++i) {
2259 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002260 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002261 mSecureSuggestionSpans.put(ss, currentImi);
2262 }
2263 }
2264 }
2265 }
2266
satoke7c6998e2011-06-03 17:57:59 +09002267 @Override
satokf9f01002011-05-19 21:31:50 +09002268 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002269 if (!calledFromValidUser()) {
2270 return false;
2271 }
satokf9f01002011-05-19 21:31:50 +09002272 synchronized (mMethodMap) {
2273 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2274 // TODO: Do not send the intent if the process of the targetImi is already dead.
2275 if (targetImi != null) {
2276 final String[] suggestions = span.getSuggestions();
2277 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002278 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002279 final Intent intent = new Intent();
2280 // Ensures that only a class in the original IME package will receive the
2281 // notification.
satok42c5a162011-05-26 16:46:14 +09002282 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002283 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2284 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2285 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2286 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002287 final long ident = Binder.clearCallingIdentity();
2288 try {
2289 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2290 } finally {
2291 Binder.restoreCallingIdentity(ident);
2292 }
satokf9f01002011-05-19 21:31:50 +09002293 return true;
2294 }
2295 }
2296 return false;
2297 }
2298
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002299 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002300 updateInputMethodsFromSettingsLocked(enabledMayChange);
2301 updateKeyboardFromSettingsLocked();
2302 }
2303
2304 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002305 if (enabledMayChange) {
2306 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2307 for (int i=0; i<enabled.size(); i++) {
2308 // We allow the user to select "disabled until used" apps, so if they
2309 // are enabling one of those here we now need to make it enabled.
2310 InputMethodInfo imm = enabled.get(i);
2311 try {
2312 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2313 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2314 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002315 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002316 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002317 if (DEBUG) {
2318 Slog.d(TAG, "Update state(" + imm.getId()
2319 + "): DISABLED_UNTIL_USED -> DEFAULT");
2320 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002321 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2322 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002323 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2324 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002325 }
2326 } catch (RemoteException e) {
2327 }
2328 }
2329 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002330 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2331 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2332 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2333 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002334 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002335 // There is no input method selected, try to choose new applicable input method.
2336 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002337 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002338 }
2339 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002341 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002343 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002344 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 }
satokf3db1af2010-11-23 13:34:33 +09002346 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002347 } else {
2348 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002349 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002351 // Here is not the perfect place to reset the switching controller. Ideally
2352 // mSwitchingController and mSettings should be able to share the same state.
2353 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2354 // the same enabled IMEs list.
2355 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002356
2357 }
2358
2359 public void updateKeyboardFromSettingsLocked() {
2360 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2361 if (mSwitchingDialog != null
2362 && mSwitchingDialogTitleView != null
2363 && mSwitchingDialog.isShowing()) {
2364 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2365 com.android.internal.R.id.hard_keyboard_switch);
2366 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002369
Yohei Yukawab097b822015-12-01 10:43:08 -08002370 private void notifyInputMethodSubtypeChanged(final int userId,
2371 @Nullable final InputMethodInfo inputMethodInfo,
2372 @Nullable final InputMethodSubtype subtype) {
2373 final InputManagerInternal inputManagerInternal =
2374 LocalServices.getService(InputManagerInternal.class);
2375 if (inputManagerInternal != null) {
2376 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2377 }
2378 }
2379
satokab751aa2010-09-14 19:17:36 +09002380 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 InputMethodInfo info = mMethodMap.get(id);
2382 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002383 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002385
satokd81e9502012-05-21 12:58:45 +09002386 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002388 final int subtypeCount = info.getSubtypeCount();
2389 if (subtypeCount <= 0) {
2390 return;
satokcd7cd292010-11-20 15:46:23 +09002391 }
satokd81e9502012-05-21 12:58:45 +09002392 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2393 final InputMethodSubtype newSubtype;
2394 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2395 newSubtype = info.getSubtypeAt(subtypeId);
2396 } else {
2397 // If subtype is null, try to find the most applicable one from
2398 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002399 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002400 }
2401 if (newSubtype == null || oldSubtype == null) {
2402 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2403 + ", new subtype = " + newSubtype);
2404 return;
2405 }
2406 if (newSubtype != oldSubtype) {
2407 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2408 if (mCurMethod != null) {
2409 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002410 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002411 mCurMethod.changeInputMethodSubtype(newSubtype);
2412 } catch (RemoteException e) {
2413 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002414 return;
satokab751aa2010-09-14 19:17:36 +09002415 }
2416 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002417 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 return;
2420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002421
satokd81e9502012-05-21 12:58:45 +09002422 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 final long ident = Binder.clearCallingIdentity();
2424 try {
satokab751aa2010-09-14 19:17:36 +09002425 // Set a subtype to this input method.
2426 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002427 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2428 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2429 // because mCurMethodId is stored as a history in
2430 // setSelectedInputMethodAndSubtypeLocked().
2431 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002433 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002435 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002437 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002439 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 } finally {
2441 Binder.restoreCallingIdentity(ident);
2442 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002443
2444 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2445 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002447
satok42c5a162011-05-26 16:46:14 +09002448 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002449 public boolean showSoftInput(IInputMethodClient client, int flags,
2450 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002451 if (!calledFromValidUser()) {
2452 return false;
2453 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002454 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 long ident = Binder.clearCallingIdentity();
2456 try {
2457 synchronized (mMethodMap) {
2458 if (mCurClient == null || client == null
2459 || mCurClient.client.asBinder() != client.asBinder()) {
2460 try {
2461 // We need to check if this is the current client with
2462 // focus in the window manager, to allow this call to
2463 // be made before input is started in it.
2464 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002465 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002466 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
2468 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002469 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 }
2471 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002472
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002473 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002474 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 }
2476 } finally {
2477 Binder.restoreCallingIdentity(ident);
2478 }
2479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002480
The Android Open Source Project4df24232009-03-05 14:34:35 -08002481 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002483 if (mAccessibilityRequestingNoSoftKeyboard) {
2484 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 }
Anna Galusza9b278112016-01-04 11:37:37 -08002486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2488 mShowExplicitlyRequested = true;
2489 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002490 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2491 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002493
Dianne Hackborncc278702009-09-02 23:07:23 -07002494 if (!mSystemReady) {
2495 return false;
2496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002497
The Android Open Source Project4df24232009-03-05 14:34:35 -08002498 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002500 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002501 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2502 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2503 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002505 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002506 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002507 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002508 mVisibleBound = true;
2509 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002510 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002512 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 // The client has asked to have the input method shown, but
2514 // we have been sitting here too long with a connection to the
2515 // service and no interface received, so let's disconnect/connect
2516 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002517 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002519 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002521 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002522 } else {
2523 if (DEBUG) {
2524 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2525 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002528
The Android Open Source Project4df24232009-03-05 14:34:35 -08002529 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002531
satok42c5a162011-05-26 16:46:14 +09002532 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002533 public boolean hideSoftInput(IInputMethodClient client, int flags,
2534 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002535 if (!calledFromValidUser()) {
2536 return false;
2537 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002538 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 long ident = Binder.clearCallingIdentity();
2540 try {
2541 synchronized (mMethodMap) {
2542 if (mCurClient == null || client == null
2543 || mCurClient.client.asBinder() != client.asBinder()) {
2544 try {
2545 // We need to check if this is the current client with
2546 // focus in the window manager, to allow this call to
2547 // be made before input is started in it.
2548 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002549 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2550 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002551 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 }
2553 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002554 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 }
2556 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002557
Joe Onorato8a9b2202010-02-26 18:56:32 -08002558 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002559 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 }
2561 } finally {
2562 Binder.restoreCallingIdentity(ident);
2563 }
2564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002565
The Android Open Source Project4df24232009-03-05 14:34:35 -08002566 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2568 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002569 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 -08002570 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 }
2572 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002573 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 -08002574 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002576
2577 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2578 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2579 // to be updated with the new value sent from IME process. Even in such a transient state
2580 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2581 // application process as a valid request, and have even promised such a behavior with CTS
2582 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2583 // IMMS#InputShown indicates that the software keyboard is shown.
2584 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2585 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2586 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002587 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002588 if (shouldHideSoftInput) {
2589 // The IME will report its visible state again after the following message finally
2590 // delivered to the IME process as an IPC. Hence the inconsistency between
2591 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2592 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002593 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2594 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2595 res = true;
2596 } else {
2597 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002599 if (mHaveConnection && mVisibleBound) {
2600 mContext.unbindService(mVisibleConnection);
2601 mVisibleBound = false;
2602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 mInputShown = false;
2604 mShowRequested = false;
2605 mShowExplicitlyRequested = false;
2606 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002607 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002609
satok42c5a162011-05-26 16:46:14 +09002610 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002611 public InputBindResult startInputOrWindowGainedFocus(
2612 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2613 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002614 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002615 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002616 if (windowToken != null) {
2617 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002618 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002619 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002620 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2621 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002622 }
2623 }
2624
2625 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002626 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002627 IInputMethodClient client, IBinder windowToken, int controlFlags,
2628 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002629 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2630 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002631 // Needs to check the validity before clearing calling identity
2632 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002633 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 long ident = Binder.clearCallingIdentity();
2635 try {
2636 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002637 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2638 + InputMethodClient.getStartInputReason(startInputReason)
2639 + " client=" + client.asBinder()
2640 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002641 + " missingMethods="
2642 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002643 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002644 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002645 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002646 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002647
Dianne Hackborn7663d802012-02-24 13:08:49 -08002648 ClientState cs = mClients.get(client.asBinder());
2649 if (cs == null) {
2650 throw new IllegalArgumentException("unknown client "
2651 + client.asBinder());
2652 }
2653
2654 try {
2655 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2656 // Check with the window manager to make sure this client actually
2657 // has a window with focus. If not, reject. This is thread safe
2658 // because if the focus changes some time before or after, the
2659 // next client receiving focus that has any interest in input will
2660 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002661 if (DEBUG) {
2662 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2663 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2664 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002665 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002667 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002669
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002670 if (!calledFromValidUser) {
2671 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2672 Slog.w(TAG, "If you want to interect with IME, you need "
2673 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2674 hideCurrentInputLocked(0, null);
2675 return null;
2676 }
2677
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002678 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002679 if (DEBUG) {
2680 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2681 + " attribute=" + attribute + ", token = " + windowToken);
2682 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002683 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002684 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002685 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002686 }
2687 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002688 }
2689 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002690 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002691 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002692
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002693 // Should we auto-show the IME even if the caller has not
2694 // specified what should be done with it?
2695 // We only do this automatically if the window can resize
2696 // to accommodate the IME (so what the user sees will give
2697 // them good context without input information being obscured
2698 // by the IME) or if running on a large screen where there
2699 // is more room for the target window + IME.
2700 final boolean doAutoShow =
2701 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2702 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2703 || mRes.getConfiguration().isLayoutSizeAtLeast(
2704 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002705 final boolean isTextEditor =
2706 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2707
2708 // We want to start input before showing the IME, but after closing
2709 // it. We want to do this after closing it to help the IME disappear
2710 // more quickly (not get stuck behind it initializing itself for the
2711 // new focused input, even if its window wants to hide the IME).
2712 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2715 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002716 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2718 // There is no focus view, and this window will
2719 // be behind any soft input window, so hide the
2720 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002721 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002722 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002724 } else if (isTextEditor && doAutoShow && (softInputMode &
2725 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 // There is a focus view, and we are navigating forward
2727 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002728 // We only do this automatically if the window can resize
2729 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002730 // them good context without input information being obscured
2731 // by the IME) or if running on a large screen where there
2732 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002733 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002734 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002735 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002736 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002737 didStart = true;
2738 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002739 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
2741 break;
2742 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2743 // Do nothing.
2744 break;
2745 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2746 if ((softInputMode &
2747 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002748 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002749 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 }
2751 break;
2752 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002753 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002754 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 break;
2756 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2757 if ((softInputMode &
2758 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002759 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002760 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002761 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002762 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002763 didStart = true;
2764 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002765 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 }
2767 break;
2768 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002769 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002770 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002771 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002772 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002773 didStart = true;
2774 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002775 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 break;
2777 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002778
2779 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002780 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002781 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 }
2784 } finally {
2785 Binder.restoreCallingIdentity(ident);
2786 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002787
2788 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002790
Guliz Tuncay6908c152017-06-02 16:06:10 -07002791 private boolean canShowInputMethodPickerLocked(IInputMethodClient client) {
2792 final int uid = Binder.getCallingUid();
2793 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
2794 return true;
2795 } else if (mCurClient != null && client != null
2796 && mCurClient.client.asBinder() == client.asBinder()) {
2797 return true;
2798 } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid(
2799 mAppOpsManager,
2800 uid,
2801 mCurIntent.getComponent().getPackageName())) {
2802 return true;
2803 } else if (mContext.checkCallingPermission(
2804 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2805 == PackageManager.PERMISSION_GRANTED) {
2806 return true;
2807 }
2808
2809 return false;
2810 }
2811
satok42c5a162011-05-26 16:46:14 +09002812 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002813 public void showInputMethodPickerFromClient(
2814 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002815 if (!calledFromValidUser()) {
2816 return;
2817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 synchronized (mMethodMap) {
Guliz Tuncay6908c152017-06-02 16:06:10 -07002819 if(!canShowInputMethodPickerLocked(client)) {
satok47a44912010-10-06 16:03:58 +09002820 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002821 + Binder.getCallingUid() + ": " + client);
Guliz Tuncay6908c152017-06-02 16:06:10 -07002822 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
2824
satok440aab52010-11-25 09:43:11 +09002825 // Always call subtype picker, because subtype picker is a superset of input method
2826 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002827 mHandler.sendMessage(mCaller.obtainMessageI(
2828 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002829 }
2830 }
2831
satok42c5a162011-05-26 16:46:14 +09002832 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002834 if (!calledFromValidUser()) {
2835 return;
2836 }
satok28203512010-11-24 11:06:49 +09002837 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2838 }
2839
satok42c5a162011-05-26 16:46:14 +09002840 @Override
satok28203512010-11-24 11:06:49 +09002841 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002842 if (!calledFromValidUser()) {
2843 return;
2844 }
satok28203512010-11-24 11:06:49 +09002845 synchronized (mMethodMap) {
2846 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002847 setInputMethodWithSubtypeIdLocked(token, id,
2848 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2849 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002850 } else {
2851 setInputMethod(token, id);
2852 }
2853 }
satokab751aa2010-09-14 19:17:36 +09002854 }
2855
satok42c5a162011-05-26 16:46:14 +09002856 @Override
satokb416a712010-11-25 20:42:14 +09002857 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002858 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002859 if (!calledFromValidUser()) {
2860 return;
2861 }
satokb416a712010-11-25 20:42:14 +09002862 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002863 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2864 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002865 }
2866 }
2867
satok4fc87d62011-05-20 16:13:43 +09002868 @Override
satok735cf382010-11-11 20:40:09 +09002869 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002870 if (!calledFromValidUser()) {
2871 return false;
2872 }
satok735cf382010-11-11 20:40:09 +09002873 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002874 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002875 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002876 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002877 lastImi = mMethodMap.get(lastIme.first);
2878 } else {
2879 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002880 }
satok4fc87d62011-05-20 16:13:43 +09002881 String targetLastImiId = null;
2882 int subtypeId = NOT_A_SUBTYPE_ID;
2883 if (lastIme != null && lastImi != null) {
2884 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002885 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002886 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2887 : mCurrentSubtype.hashCode();
2888 // If the last IME is the same as the current IME and the last subtype is not
2889 // defined, there is no need to switch to the last IME.
2890 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2891 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002892 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002893 }
2894 }
2895
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002896 if (TextUtils.isEmpty(targetLastImiId)
2897 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002898 // This is a safety net. If the currentSubtype can't be added to the history
2899 // and the framework couldn't find the last ime, we will make the last ime be
2900 // the most applicable enabled keyboard subtype of the system imes.
2901 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2902 if (enabled != null) {
2903 final int N = enabled.size();
2904 final String locale = mCurrentSubtype == null
2905 ? mRes.getConfiguration().locale.toString()
2906 : mCurrentSubtype.getLocale();
2907 for (int i = 0; i < N; ++i) {
2908 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002909 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002910 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002911 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2912 InputMethodUtils.getSubtypes(imi),
2913 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002914 if (keyboardSubtype != null) {
2915 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002916 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002917 imi, keyboardSubtype.hashCode());
2918 if(keyboardSubtype.getLocale().equals(locale)) {
2919 break;
2920 }
2921 }
2922 }
2923 }
2924 }
2925 }
2926
2927 if (!TextUtils.isEmpty(targetLastImiId)) {
2928 if (DEBUG) {
2929 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2930 + ", from: " + mCurMethodId + ", " + subtypeId);
2931 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002932 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002933 return true;
2934 } else {
2935 return false;
2936 }
satok735cf382010-11-11 20:40:09 +09002937 }
2938 }
2939
satoke7c6998e2011-06-03 17:57:59 +09002940 @Override
satok688bd472012-02-09 20:09:17 +09002941 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002942 if (!calledFromValidUser()) {
2943 return false;
2944 }
satok688bd472012-02-09 20:09:17 +09002945 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002946 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002947 return false;
2948 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002949 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002950 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2951 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002952 if (nextSubtype == null) {
2953 return false;
2954 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002955 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2956 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002957 return true;
2958 }
2959 }
2960
2961 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002962 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2963 if (!calledFromValidUser()) {
2964 return false;
2965 }
2966 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002967 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002968 return false;
2969 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002970 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002971 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2972 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002973 if (nextSubtype == null) {
2974 return false;
2975 }
2976 return true;
2977 }
2978 }
2979
2980 @Override
satok68f1b782011-04-11 14:26:04 +09002981 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002982 if (!calledFromValidUser()) {
2983 return null;
2984 }
satok68f1b782011-04-11 14:26:04 +09002985 synchronized (mMethodMap) {
2986 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2987 // TODO: Handle the case of the last IME with no subtypes
2988 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2989 || TextUtils.isEmpty(lastIme.second)) return null;
2990 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2991 if (lastImi == null) return null;
2992 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002993 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002994 final int lastSubtypeId =
2995 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002996 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2997 return null;
2998 }
2999 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09003000 } catch (NumberFormatException e) {
3001 return null;
3002 }
3003 }
3004 }
3005
satoke7c6998e2011-06-03 17:57:59 +09003006 @Override
satokee5e77c2011-09-02 18:50:15 +09003007 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003008 if (!calledFromValidUser()) {
3009 return;
3010 }
satok91e88122011-07-18 11:11:42 +09003011 // By this IPC call, only a process which shares the same uid with the IME can add
3012 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08003013 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09003014 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08003015 if (!mSystemReady) {
3016 return;
3017 }
satok91e88122011-07-18 11:11:42 +09003018 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09003019 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 final String[] packageInfos;
3021 try {
3022 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3023 } catch (RemoteException e) {
3024 Slog.e(TAG, "Failed to get package infos");
3025 return;
3026 }
satok91e88122011-07-18 11:11:42 +09003027 if (packageInfos != null) {
3028 final int packageNum = packageInfos.length;
3029 for (int i = 0; i < packageNum; ++i) {
3030 if (packageInfos[i].equals(imi.getPackageName())) {
3031 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003032 final long ident = Binder.clearCallingIdentity();
3033 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003034 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003035 } finally {
3036 Binder.restoreCallingIdentity(ident);
3037 }
satokee5e77c2011-09-02 18:50:15 +09003038 return;
satok91e88122011-07-18 11:11:42 +09003039 }
3040 }
3041 }
satoke7c6998e2011-06-03 17:57:59 +09003042 }
satokee5e77c2011-09-02 18:50:15 +09003043 return;
satoke7c6998e2011-06-03 17:57:59 +09003044 }
3045
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003046 @Override
3047 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003048 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003049 }
3050
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003051 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003052 public void clearLastInputMethodWindowForTransition(IBinder token) {
3053 if (!calledFromValidUser()) {
3054 return;
3055 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003056 synchronized (mMethodMap) {
3057 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003058 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003059 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003060 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003061 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003062 }
3063
3064 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003065 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003066 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003067 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003068 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003069 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003070 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3071 if (DEBUG) {
3072 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3073 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3074 + " actual: " + sequenceNumber);
3075 }
3076 return;
3077 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003078 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3079 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003080 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003081 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003082 }
3083 }
3084
satok28203512010-11-24 11:06:49 +09003085 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003087 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3088 }
3089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003091 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3092 if (token == null) {
3093 if (mContext.checkCallingOrSelfPermission(
3094 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3095 != PackageManager.PERMISSION_GRANTED) {
3096 throw new SecurityException(
3097 "Using null token requires permission "
3098 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003100 } else if (mCurToken != token) {
3101 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3102 + " token: " + token);
3103 return;
3104 }
3105
3106 final long ident = Binder.clearCallingIdentity();
3107 try {
3108 setInputMethodLocked(id, subtypeId);
3109 } finally {
3110 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 }
3112 }
3113
satok42c5a162011-05-26 16:46:14 +09003114 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003116 if (!calledFromValidUser()) {
3117 return;
3118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003120 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 return;
3122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 long ident = Binder.clearCallingIdentity();
3124 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003125 hideCurrentInputLocked(flags, null);
3126 } finally {
3127 Binder.restoreCallingIdentity(ident);
3128 }
3129 }
3130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003131
satok42c5a162011-05-26 16:46:14 +09003132 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003133 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003134 if (!calledFromValidUser()) {
3135 return;
3136 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003137 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003138 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003139 return;
3140 }
3141 long ident = Binder.clearCallingIdentity();
3142 try {
3143 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 } finally {
3145 Binder.restoreCallingIdentity(ident);
3146 }
3147 }
3148 }
3149
3150 void setEnabledSessionInMainThread(SessionState session) {
3151 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003152 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003154 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003155 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 } catch (RemoteException e) {
3157 }
3158 }
3159 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003160 if (mEnabledSession != null && mEnabledSession.session != null) {
3161 try {
3162 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3163 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3164 } catch (RemoteException e) {
3165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 }
3167 }
3168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003169
satok42c5a162011-05-26 16:46:14 +09003170 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003172 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003174 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003175 final boolean showAuxSubtypes;
3176 switch (msg.arg1) {
3177 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3178 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3179 // implemented so that auxiliary subtypes will be excluded when the soft
3180 // keyboard is invisible.
3181 showAuxSubtypes = mInputShown;
3182 break;
3183 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3184 showAuxSubtypes = true;
3185 break;
3186 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3187 showAuxSubtypes = false;
3188 break;
3189 default:
3190 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3191 return false;
3192 }
3193 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003194 return true;
3195
satok47a44912010-10-06 16:03:58 +09003196 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003197 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003198 return true;
3199
3200 case MSG_SHOW_IM_CONFIG:
3201 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003202 return true;
3203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 case MSG_UNBIND_INPUT:
3207 try {
3208 ((IInputMethod)msg.obj).unbindInput();
3209 } catch (RemoteException e) {
3210 // There is nothing interesting about the method dying.
3211 }
3212 return true;
3213 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003214 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 try {
3216 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3217 } 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;
3221 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003222 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003224 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003225 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003226 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 } catch (RemoteException e) {
3228 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003229 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 return true;
3231 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003232 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003234 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003235 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003236 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 } catch (RemoteException e) {
3238 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003239 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003241 case MSG_HIDE_CURRENT_INPUT_METHOD:
3242 synchronized (mMethodMap) {
3243 hideCurrentInputLocked(0, null);
3244 }
3245 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003247 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003249 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3251 } catch (RemoteException e) {
3252 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003253 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003255 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003256 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003257 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003258 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003260 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003262 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003263 // Dispose the channel if the input method is not local to this process
3264 // because the remote proxy will get its own copy when unparceled.
3265 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003266 channel.dispose();
3267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003269 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003273
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003274 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003275 final int missingMethods = msg.arg1;
3276 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003277 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003278 final IBinder startInputToken = (IBinder) args.arg1;
3279 final SessionState session = (SessionState) args.arg2;
3280 final IInputContext inputContext = (IInputContext) args.arg3;
3281 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003284 session.method.startInput(startInputToken, inputContext, missingMethods,
3285 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 } catch (RemoteException e) {
3287 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003288 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003293
Yohei Yukawa33e81792015-11-17 21:14:42 -08003294 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003296 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 } catch (RemoteException e) {
3298 // There is nothing interesting about the last client dying.
3299 }
3300 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003301 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003302 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003303 IInputMethodClient client = (IInputMethodClient)args.arg1;
3304 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003306 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003308 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003309 } finally {
3310 // Dispose the channel if the input method is not local to this process
3311 // because the remote proxy will get its own copy when unparceled.
3312 if (res.channel != null && Binder.isProxy(client)) {
3313 res.channel.dispose();
3314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003316 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003318 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003319 case MSG_SET_ACTIVE:
3320 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003321 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003322 } catch (RemoteException e) {
3323 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3324 + ((ClientState)msg.obj).pid + " uid "
3325 + ((ClientState)msg.obj).uid);
3326 }
3327 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003328 case MSG_SET_INTERACTIVE:
3329 handleSetInteractive(msg.arg1 != 0);
3330 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003331 case MSG_SWITCH_IME:
3332 handleSwitchInputMethod(msg.arg1 != 0);
3333 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003334 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3335 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003336 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003337 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003338 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003339 } catch (RemoteException e) {
3340 Slog.w(TAG, "Got RemoteException sending "
3341 + "setUserActionNotificationSequenceNumber("
3342 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003343 + clientState.pid + " uid "
3344 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003345 }
3346 return true;
3347 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003348 case MSG_REPORT_FULLSCREEN_MODE: {
3349 final boolean fullscreen = msg.arg1 != 0;
3350 final ClientState clientState = (ClientState)msg.obj;
3351 try {
3352 clientState.client.reportFullscreenMode(fullscreen);
3353 } catch (RemoteException e) {
3354 Slog.w(TAG, "Got RemoteException sending "
3355 + "reportFullscreen(" + fullscreen + ") notification to pid="
3356 + clientState.pid + " uid=" + clientState.uid);
3357 }
3358 return true;
3359 }
satok01038492012-04-09 21:08:27 +09003360
3361 // --------------------------------------------------------------
3362 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003363 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003364 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003365 case MSG_SYSTEM_UNLOCK_USER:
3366 final int userId = msg.arg1;
3367 onUnlockUser(userId);
3368 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 }
3370 return false;
3371 }
3372
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003373 private void handleSetInteractive(final boolean interactive) {
3374 synchronized (mMethodMap) {
3375 mIsInteractive = interactive;
3376 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3377
3378 // Inform the current client of the change in active status
3379 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003380 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3381 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3382 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003383 }
3384 }
3385 }
3386
Yohei Yukawaae61f712015-12-09 13:00:10 -08003387 private void handleSwitchInputMethod(final boolean forwardDirection) {
3388 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003389 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003390 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003391 if (nextSubtype == null) {
3392 return;
3393 }
3394 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003395 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3396 if (newInputMethodInfo == null) {
3397 return;
3398 }
3399 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3400 newInputMethodInfo, mCurrentSubtype);
3401 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003402 if (mSubtypeSwitchedByShortCutToast == null) {
3403 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3404 Toast.LENGTH_SHORT);
3405 } else {
3406 mSubtypeSwitchedByShortCutToast.setText(toastText);
3407 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003408 mSubtypeSwitchedByShortCutToast.show();
3409 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003410 }
3411 }
3412
satokdc9ddae2011-10-06 12:22:36 +09003413 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003414 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3415 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003416 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003417 if (DEBUG) {
3418 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3419 }
satok723a27e2010-11-11 14:58:11 +09003420 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003421 return true;
3422 }
3423
3424 return false;
3425 }
3426
Yohei Yukawa94e33302016-02-12 19:37:03 -08003427 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003428 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003429 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003430 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003431 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003432 if (!mSystemReady) {
3433 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3434 return;
3435 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003436 mMethodList.clear();
3437 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003438 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003439 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003440
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003441 // Use for queryIntentServicesAsUser
3442 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003444 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3445 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3446 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003447 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003449 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3450 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003451
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003452 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003453 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 for (int i = 0; i < services.size(); ++i) {
3455 ResolveInfo ri = services.get(i);
3456 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003457 final String imeId = InputMethodInfo.computeId(ri);
3458 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3459 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 + ": it does not require the permission "
3461 + android.Manifest.permission.BIND_INPUT_METHOD);
3462 continue;
3463 }
3464
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003465 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003467 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 try {
satoke7c6998e2011-06-03 17:57:59 +09003469 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003470 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003471 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003472 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473
3474 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003475 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003477 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003478 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
3480 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003481
Yohei Yukawac4e44912017-02-09 19:30:22 -08003482 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3483 // negatives when the package state remains to be the same but only the component state is
3484 // changed.
3485 {
3486 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3487 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3488 // conservative, but it seems we cannot use it for now (Issue 35176630).
3489 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3490 new Intent(InputMethod.SERVICE_INTERFACE),
3491 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3492 final int N = allInputMethodServices.size();
3493 for (int i = 0; i < N; ++i) {
3494 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003495 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3496 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003497 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003498 }
3499 }
3500
Yohei Yukawa859df052016-02-17 07:56:46 -08003501 // TODO: The following code should find better place to live.
3502 if (!resetDefaultEnabledIme) {
3503 boolean enabledImeFound = false;
3504 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3505 final int N = enabledImes.size();
3506 for (int i = 0; i < N; ++i) {
3507 final InputMethodInfo imi = enabledImes.get(i);
3508 if (mMethodList.contains(imi)) {
3509 enabledImeFound = true;
3510 break;
3511 }
3512 }
3513 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003514 if (DEBUG) {
3515 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3516 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003517 resetDefaultEnabledIme = true;
3518 resetSelectedInputMethodAndSubtypeLocked("");
3519 }
3520 }
3521
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003522 if (resetDefaultEnabledIme) {
3523 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003524 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003525 final int N = defaultEnabledIme.size();
3526 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003527 final InputMethodInfo imi = defaultEnabledIme.get(i);
3528 if (DEBUG) {
3529 Slog.d(TAG, "--- enable ime = " + imi);
3530 }
3531 setInputMethodEnabledLocked(imi.getId(), true);
3532 }
3533 }
3534
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003535 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003536 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003537 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003538 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3539 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003540 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003541 }
3542 } else {
3543 // Double check that the default IME is certainly enabled.
3544 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003545 }
3546 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003547 // Here is not the perfect place to reset the switching controller. Ideally
3548 // mSwitchingController and mSettings should be able to share the same state.
3549 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3550 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003551 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003555
satok217f5482010-12-15 05:19:19 +09003556 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003557 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003558 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003559 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3560 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003561 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003562 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003563 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003564 final int userId;
3565 synchronized (mMethodMap) {
3566 userId = mSettings.getCurrentUserId();
3567 }
3568 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003569 }
3570
3571 private void showConfigureInputMethods() {
3572 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3573 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3574 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3575 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003576 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003577 }
3578
satok2c93efc2012-04-02 19:33:47 +09003579 private boolean isScreenLocked() {
3580 return mKeyguardManager != null
3581 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3582 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003583
Seigo Nonaka14e13912015-05-06 21:04:13 -07003584 private void showInputMethodMenu(boolean showAuxSubtypes) {
3585 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586
satok2c93efc2012-04-02 19:33:47 +09003587 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003588
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003589 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003590 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003591 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003592
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003593 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003594 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003595 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3596 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003597 if (immis == null || immis.size() == 0) {
3598 return;
3599 }
3600
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003601 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602
satok688bd472012-02-09 20:09:17 +09003603 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003604 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003605 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003606
satokc3690562012-01-10 20:14:43 +09003607 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003608 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003609 if (currentSubtype != null) {
3610 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003611 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3612 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003613 }
3614 }
3615
Ken Wakasa761eb372011-03-04 19:06:18 +09003616 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003617 mIms = new InputMethodInfo[N];
3618 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003619 int checkedItem = 0;
3620 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003621 final ImeSubtypeListItem item = imList.get(i);
3622 mIms[i] = item.mImi;
3623 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003624 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003625 int subtypeId = mSubtypeIds[i];
3626 if ((subtypeId == NOT_A_SUBTYPE_ID)
3627 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3628 || (subtypeId == lastInputMethodSubtypeId)) {
3629 checkedItem = i;
3630 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003633
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003634 final Context settingsContext = new ContextThemeWrapper(
3635 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003636 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3637
3638 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003639 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3640 @Override
3641 public void onCancel(DialogInterface dialog) {
3642 hideInputMethodMenu();
3643 }
3644 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003645
3646 final Context dialogContext = mDialogBuilder.getContext();
3647 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3648 com.android.internal.R.styleable.DialogPreference,
3649 com.android.internal.R.attr.alertDialogStyle, 0);
3650 final Drawable dialogIcon = a.getDrawable(
3651 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3652 a.recycle();
3653
3654 mDialogBuilder.setIcon(dialogIcon);
3655
Yohei Yukawad34e1482016-02-11 08:03:52 -08003656 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003657 final View tv = inflater.inflate(
3658 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3659 mDialogBuilder.setCustomTitle(tv);
3660
3661 // Setup layout for a toggle switch of the hardware keyboard
3662 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003663 mSwitchingDialogTitleView
3664 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003665 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003666 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003667 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003668 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003669 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003670 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3671 @Override
3672 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003673 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003674 // Ensure that the input method dialog is dismissed when changing
3675 // the hardware keyboard state.
3676 hideInputMethodMenu();
3677 }
3678 });
3679
Alan Viverette505e3ab2014-11-24 15:22:11 -08003680 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003681 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3682 final OnClickListener choiceListener = new OnClickListener() {
3683 @Override
3684 public void onClick(final DialogInterface dialog, final int which) {
3685 synchronized (mMethodMap) {
3686 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3687 || mSubtypeIds.length <= which) {
3688 return;
satok01038492012-04-09 21:08:27 +09003689 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003690 final InputMethodInfo im = mIms[which];
3691 int subtypeId = mSubtypeIds[which];
3692 adapter.mCheckedItem = which;
3693 adapter.notifyDataSetChanged();
3694 hideInputMethodMenu();
3695 if (im != null) {
3696 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3697 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003698 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003699 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003701 }
3702 }
3703 };
3704 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003707 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003708 final Window w = mSwitchingDialog.getWindow();
3709 final WindowManager.LayoutParams attrs = w.getAttributes();
3710 w.setType(TYPE_INPUT_METHOD_DIALOG);
3711 // Use an alternate token for the dialog for that window manager can group the token
3712 // with other IME windows based on type vs. grouping based on whichever token happens
3713 // to get selected by the system later on.
3714 attrs.token = mSwitchingDialogToken;
3715 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3716 attrs.setTitle("Select input method");
3717 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003718 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 mSwitchingDialog.show();
3720 }
3721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003722
Ken Wakasa05dbb652011-08-22 15:22:43 +09003723 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3724 private final LayoutInflater mInflater;
3725 private final int mTextViewResourceId;
3726 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003727 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003728 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3729 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3730 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003731
Ken Wakasa05dbb652011-08-22 15:22:43 +09003732 mTextViewResourceId = textViewResourceId;
3733 mItemsList = itemsList;
3734 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003735 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003736 }
3737
3738 @Override
3739 public View getView(int position, View convertView, ViewGroup parent) {
3740 final View view = convertView != null ? convertView
3741 : mInflater.inflate(mTextViewResourceId, null);
3742 if (position < 0 || position >= mItemsList.size()) return view;
3743 final ImeSubtypeListItem item = mItemsList.get(position);
3744 final CharSequence imeName = item.mImeName;
3745 final CharSequence subtypeName = item.mSubtypeName;
3746 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3747 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3748 if (TextUtils.isEmpty(subtypeName)) {
3749 firstTextView.setText(imeName);
3750 secondTextView.setVisibility(View.GONE);
3751 } else {
3752 firstTextView.setText(subtypeName);
3753 secondTextView.setText(imeName);
3754 secondTextView.setVisibility(View.VISIBLE);
3755 }
3756 final RadioButton radioButton =
3757 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3758 radioButton.setChecked(position == mCheckedItem);
3759 return view;
3760 }
3761 }
3762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003764 synchronized (mMethodMap) {
3765 hideInputMethodMenuLocked();
3766 }
3767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003768
The Android Open Source Project10592532009-03-18 17:39:46 -07003769 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003770 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771
The Android Open Source Project10592532009-03-18 17:39:46 -07003772 if (mSwitchingDialog != null) {
3773 mSwitchingDialog.dismiss();
3774 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003776
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003777 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003778 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003779 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003783
satok42c5a162011-05-26 16:46:14 +09003784 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003786 // TODO: Make this work even for non-current users?
3787 if (!calledFromValidUser()) {
3788 return false;
3789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 synchronized (mMethodMap) {
3791 if (mContext.checkCallingOrSelfPermission(
3792 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3793 != PackageManager.PERMISSION_GRANTED) {
3794 throw new SecurityException(
3795 "Requires permission "
3796 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3797 }
Anna Galusza9b278112016-01-04 11:37:37 -08003798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 long ident = Binder.clearCallingIdentity();
3800 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003801 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 } finally {
3803 Binder.restoreCallingIdentity(ident);
3804 }
3805 }
3806 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003807
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003808 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3809 // Make sure this is a valid input method.
3810 InputMethodInfo imm = mMethodMap.get(id);
3811 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003812 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003813 }
3814
satokd87c2592010-09-29 11:52:06 +09003815 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3816 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003817
satokd87c2592010-09-29 11:52:06 +09003818 if (enabled) {
3819 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3820 if (pair.first.equals(id)) {
3821 // We are enabling this input method, but it is already enabled.
3822 // Nothing to do. The previous state was enabled.
3823 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003824 }
3825 }
satokd87c2592010-09-29 11:52:06 +09003826 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3827 // Previous state was disabled.
3828 return false;
3829 } else {
3830 StringBuilder builder = new StringBuilder();
3831 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3832 builder, enabledInputMethodsList, id)) {
3833 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003834 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003835 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3836 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3837 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003838 }
3839 // Previous state was enabled.
3840 return true;
3841 } else {
3842 // We are disabling the input method but it is already disabled.
3843 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003844 return false;
3845 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003846 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003847 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003848
satok723a27e2010-11-11 14:58:11 +09003849 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3850 boolean setSubtypeOnly) {
3851 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003852 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003853
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003854 mCurUserActionNotificationSequenceNumber =
3855 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3856 if (DEBUG) {
3857 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3858 + mCurUserActionNotificationSequenceNumber);
3859 }
3860
3861 if (mCurClient != null && mCurClient.client != null) {
3862 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3863 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003864 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003865 }
3866
satok723a27e2010-11-11 14:58:11 +09003867 // Set Subtype here
3868 if (imi == null || subtypeId < 0) {
3869 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003870 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003871 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003872 if (subtypeId < imi.getSubtypeCount()) {
3873 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3874 mSettings.putSelectedSubtype(subtype.hashCode());
3875 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003876 } else {
3877 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003878 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003879 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003880 }
satokab751aa2010-09-14 19:17:36 +09003881 }
satok723a27e2010-11-11 14:58:11 +09003882
Yohei Yukawa68645a62016-02-17 07:54:20 -08003883 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003884 // Set InputMethod here
3885 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3886 }
3887 }
3888
3889 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3890 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3891 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3892 // newDefaultIme is empty when there is no candidate for the selected IME.
3893 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3894 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3895 if (subtypeHashCode != null) {
3896 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003897 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003898 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003899 } catch (NumberFormatException e) {
3900 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3901 }
3902 }
3903 }
3904 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003905 }
3906
satok4e4569d2010-11-19 18:45:53 +09003907 // If there are no selected shortcuts, tries finding the most applicable ones.
3908 private Pair<InputMethodInfo, InputMethodSubtype>
3909 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3910 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3911 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003912 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003913 boolean foundInSystemIME = false;
3914
3915 // Search applicable subtype for each InputMethodInfo
3916 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003917 final String imiId = imi.getId();
3918 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3919 continue;
3920 }
satokcd7cd292010-11-20 15:46:23 +09003921 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003922 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003923 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003924 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003925 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003926 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003927 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003928 }
satokdf31ae62011-01-15 06:19:44 +09003929 // 2. Search by the system locale from enabledSubtypes.
3930 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003931 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003932 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003933 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003934 }
satoka86f5e42011-09-02 17:12:42 +09003935 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003936 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003937 final ArrayList<InputMethodSubtype> subtypesForSearch =
3938 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003939 ? InputMethodUtils.getSubtypes(imi)
3940 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003941 // 4. Search by the current subtype's locale from all subtypes.
3942 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003943 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003944 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003945 }
3946 // 5. Search by the system locale from all subtypes.
3947 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003948 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003949 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003950 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003951 }
satokcd7cd292010-11-20 15:46:23 +09003952 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003953 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003954 // The current input method is the most applicable IME.
3955 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003956 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003957 break;
satok7599a7f2010-12-22 13:45:23 +09003958 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003959 // The system input method is 2nd applicable IME.
3960 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003961 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003962 if ((imi.getServiceInfo().applicationInfo.flags
3963 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3964 foundInSystemIME = true;
3965 }
satok4e4569d2010-11-19 18:45:53 +09003966 }
3967 }
3968 }
3969 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003970 if (mostApplicableIMI != null) {
3971 Slog.w(TAG, "Most applicable shortcut input method was:"
3972 + mostApplicableIMI.getId());
3973 if (mostApplicableSubtype != null) {
3974 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3975 + "," + mostApplicableSubtype.getMode() + ","
3976 + mostApplicableSubtype.getLocale());
3977 }
3978 }
satok4e4569d2010-11-19 18:45:53 +09003979 }
satokcd7cd292010-11-20 15:46:23 +09003980 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003981 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003982 } else {
3983 return null;
3984 }
3985 }
3986
satokab751aa2010-09-14 19:17:36 +09003987 /**
3988 * @return Return the current subtype of this input method.
3989 */
satok42c5a162011-05-26 16:46:14 +09003990 @Override
satokab751aa2010-09-14 19:17:36 +09003991 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003992 // TODO: Make this work even for non-current users?
3993 if (!calledFromValidUser()) {
3994 return null;
3995 }
3996 synchronized (mMethodMap) {
3997 return getCurrentInputMethodSubtypeLocked();
3998 }
3999 }
4000
4001 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09004002 if (mCurMethodId == null) {
4003 return null;
4004 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004005 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004006 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
4007 if (imi == null || imi.getSubtypeCount() == 0) {
4008 return null;
satok4e4569d2010-11-19 18:45:53 +09004009 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004010 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004011 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
4012 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004013 if (subtypeId == NOT_A_SUBTYPE_ID) {
4014 // If there are no selected subtypes, the framework will try to find
4015 // the most applicable subtype from explicitly or implicitly enabled
4016 // subtypes.
4017 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004018 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004019 // If there is only one explicitly or implicitly enabled subtype,
4020 // just returns it.
4021 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4022 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4023 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004024 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004025 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004026 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004027 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004028 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004029 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4030 true);
satok4e4569d2010-11-19 18:45:53 +09004031 }
satok3ef8b292010-11-23 06:06:29 +09004032 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004033 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004034 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004035 }
4036 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004037 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004038 }
4039
satok4e4569d2010-11-19 18:45:53 +09004040 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004041 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004042 @Override
satok4e4569d2010-11-19 18:45:53 +09004043 public List getShortcutInputMethodsAndSubtypes() {
4044 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004045 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004046 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004047 // If there are no selected shortcut subtypes, the framework will try to find
4048 // the most applicable subtype from all subtypes whose mode is
4049 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004050 Pair<InputMethodInfo, InputMethodSubtype> info =
4051 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004052 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004053 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004054 ret.add(info.first);
4055 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004056 }
satok3da92232011-01-11 22:46:30 +09004057 return ret;
satokf3db1af2010-11-23 13:34:33 +09004058 }
satokf3db1af2010-11-23 13:34:33 +09004059 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4060 ret.add(imi);
4061 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4062 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004063 }
4064 }
satokf3db1af2010-11-23 13:34:33 +09004065 return ret;
satok4e4569d2010-11-19 18:45:53 +09004066 }
4067 }
4068
satok42c5a162011-05-26 16:46:14 +09004069 @Override
satokb66d2872010-11-10 01:04:04 +09004070 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004071 // TODO: Make this work even for non-current users?
4072 if (!calledFromValidUser()) {
4073 return false;
4074 }
satokb66d2872010-11-10 01:04:04 +09004075 synchronized (mMethodMap) {
4076 if (subtype != null && mCurMethodId != null) {
4077 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004078 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004079 if (subtypeId != NOT_A_SUBTYPE_ID) {
4080 setInputMethodLocked(mCurMethodId, subtypeId);
4081 return true;
4082 }
4083 }
4084 return false;
4085 }
4086 }
4087
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004088 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004089 private static class InputMethodFileManager {
4090 private static final String SYSTEM_PATH = "system";
4091 private static final String INPUT_METHOD_PATH = "inputmethod";
4092 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4093 private static final String NODE_SUBTYPES = "subtypes";
4094 private static final String NODE_SUBTYPE = "subtype";
4095 private static final String NODE_IMI = "imi";
4096 private static final String ATTR_ID = "id";
4097 private static final String ATTR_LABEL = "label";
4098 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004099 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004100 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004101 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004102 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4103 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4104 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004105 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004106 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4107 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004108 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004109 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004110 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004111 if (methodMap == null) {
4112 throw new NullPointerException("methodMap is null");
4113 }
4114 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004115 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004116 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4117 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004118 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004119 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004120 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4121 }
4122 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4123 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4124 if (!subtypeFile.exists()) {
4125 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004126 writeAdditionalInputMethodSubtypes(
4127 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004128 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004129 readAdditionalInputMethodSubtypes(
4130 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004131 }
4132 }
4133
4134 private void deleteAllInputMethodSubtypes(String imiId) {
4135 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004136 mAdditionalSubtypesMap.remove(imiId);
4137 writeAdditionalInputMethodSubtypes(
4138 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004139 }
4140 }
4141
4142 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004143 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004144 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004145 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004146 final int N = additionalSubtypes.length;
4147 for (int i = 0; i < N; ++i) {
4148 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004149 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004150 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004151 } else {
4152 Slog.w(TAG, "Duplicated subtype definition found: "
4153 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004154 }
4155 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004156 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4157 writeAdditionalInputMethodSubtypes(
4158 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004159 }
4160 }
4161
4162 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4163 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004164 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004165 }
4166 }
4167
4168 private static void writeAdditionalInputMethodSubtypes(
4169 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4170 HashMap<String, InputMethodInfo> methodMap) {
4171 // Safety net for the case that this function is called before methodMap is set.
4172 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4173 FileOutputStream fos = null;
4174 try {
4175 fos = subtypesFile.startWrite();
4176 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004177 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004178 out.startDocument(null, true);
4179 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4180 out.startTag(null, NODE_SUBTYPES);
4181 for (String imiId : allSubtypes.keySet()) {
4182 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4183 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4184 continue;
4185 }
4186 out.startTag(null, NODE_IMI);
4187 out.attribute(null, ATTR_ID, imiId);
4188 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4189 final int N = subtypesList.size();
4190 for (int i = 0; i < N; ++i) {
4191 final InputMethodSubtype subtype = subtypesList.get(i);
4192 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004193 if (subtype.hasSubtypeId()) {
4194 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4195 String.valueOf(subtype.getSubtypeId()));
4196 }
satoke7c6998e2011-06-03 17:57:59 +09004197 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4198 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4199 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004200 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4201 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004202 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4203 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4204 out.attribute(null, ATTR_IS_AUXILIARY,
4205 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004206 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4207 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004208 out.endTag(null, NODE_SUBTYPE);
4209 }
4210 out.endTag(null, NODE_IMI);
4211 }
4212 out.endTag(null, NODE_SUBTYPES);
4213 out.endDocument();
4214 subtypesFile.finishWrite(fos);
4215 } catch (java.io.IOException e) {
4216 Slog.w(TAG, "Error writing subtypes", e);
4217 if (fos != null) {
4218 subtypesFile.failWrite(fos);
4219 }
4220 }
4221 }
4222
4223 private static void readAdditionalInputMethodSubtypes(
4224 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4225 if (allSubtypes == null || subtypesFile == null) return;
4226 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004227 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004228 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004229 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004230 int type = parser.getEventType();
4231 // Skip parsing until START_TAG
4232 while ((type = parser.next()) != XmlPullParser.START_TAG
4233 && type != XmlPullParser.END_DOCUMENT) {}
4234 String firstNodeName = parser.getName();
4235 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4236 throw new XmlPullParserException("Xml doesn't start with subtypes");
4237 }
4238 final int depth =parser.getDepth();
4239 String currentImiId = null;
4240 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4241 while (((type = parser.next()) != XmlPullParser.END_TAG
4242 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4243 if (type != XmlPullParser.START_TAG)
4244 continue;
4245 final String nodeName = parser.getName();
4246 if (NODE_IMI.equals(nodeName)) {
4247 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4248 if (TextUtils.isEmpty(currentImiId)) {
4249 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4250 continue;
4251 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004252 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004253 allSubtypes.put(currentImiId, tempSubtypesArray);
4254 } else if (NODE_SUBTYPE.equals(nodeName)) {
4255 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4256 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4257 continue;
4258 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004259 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004260 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004261 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004262 parser.getAttributeValue(null, ATTR_LABEL));
4263 final String imeSubtypeLocale =
4264 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004265 final String languageTag =
4266 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004267 final String imeSubtypeMode =
4268 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4269 final String imeSubtypeExtraValue =
4270 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004271 final boolean isAuxiliary = "1".equals(String.valueOf(
4272 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004273 final boolean isAsciiCapable = "1".equals(String.valueOf(
4274 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004275 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004276 .setSubtypeNameResId(label)
4277 .setSubtypeIconResId(icon)
4278 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004279 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004280 .setSubtypeMode(imeSubtypeMode)
4281 .setSubtypeExtraValue(imeSubtypeExtraValue)
4282 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004283 .setIsAsciiCapable(isAsciiCapable);
4284 final String subtypeIdString =
4285 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4286 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004287 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004288 }
4289 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004290 }
4291 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004292 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4293 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004294 return;
satoke7c6998e2011-06-03 17:57:59 +09004295 }
4296 }
4297 }
4298
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004299 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4300 @NonNull
4301 private final Handler mHandler;
4302
4303 LocalServiceImpl(@NonNull final Handler handler) {
4304 mHandler = handler;
4305 }
4306
4307 @Override
4308 public void setInteractive(boolean interactive) {
4309 // Do everything in handler so as not to block the caller.
4310 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4311 interactive ? 1 : 0, 0));
4312 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004313
4314 @Override
4315 public void switchInputMethod(boolean forwardDirection) {
4316 // Do everything in handler so as not to block the caller.
4317 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4318 forwardDirection ? 1 : 0, 0));
4319 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004320
4321 @Override
4322 public void hideCurrentInputMethod() {
4323 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4324 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4325 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004326 }
4327
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004328 private static String imeWindowStatusToString(final int imeWindowVis) {
4329 final StringBuilder sb = new StringBuilder();
4330 boolean first = true;
4331 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4332 sb.append("Active");
4333 first = false;
4334 }
4335 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4336 if (!first) {
4337 sb.append("|");
4338 }
4339 sb.append("Visible");
4340 }
4341 return sb.toString();
4342 }
4343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004344 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004345 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4346 @Nullable Uri contentUri, @Nullable String packageName) {
4347 if (!calledFromValidUser()) {
4348 return null;
4349 }
4350
4351 if (token == null) {
4352 throw new NullPointerException("token");
4353 }
4354 if (packageName == null) {
4355 throw new NullPointerException("packageName");
4356 }
4357 if (contentUri == null) {
4358 throw new NullPointerException("contentUri");
4359 }
4360 final String contentUriScheme = contentUri.getScheme();
4361 if (!"content".equals(contentUriScheme)) {
4362 throw new InvalidParameterException("contentUri must have content scheme");
4363 }
4364
4365 synchronized (mMethodMap) {
4366 final int uid = Binder.getCallingUid();
4367 if (mCurMethodId == null) {
4368 return null;
4369 }
4370 if (mCurToken != token) {
4371 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4372 + " token=" + token);
4373 return null;
4374 }
4375 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4376 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4377 // nature of our system. Let's compare it with our internal record.
4378 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4379 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4380 + mCurAttribute.packageName + " packageName=" + packageName);
4381 return null;
4382 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004383 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004384 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004385 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004386 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004387 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4388 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4389 imeUserId);
4390 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4391 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4392 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4393 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4394 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4395 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4396 // actually allowed to "uid", which is guaranteed to be the IME's one.
4397 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4398 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004399 }
4400 }
4401
4402 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004403 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4404 if (!calledFromValidUser()) {
4405 return;
4406 }
4407 synchronized (mMethodMap) {
4408 if (!calledWithValidToken(token)) {
4409 return;
4410 }
4411 if (mCurClient != null && mCurClient.client != null) {
4412 mInFullscreenMode = fullscreen;
4413 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4414 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4415 }
4416 }
4417 }
4418
4419 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004421 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422
4423 IInputMethod method;
4424 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004425 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 synchronized (mMethodMap) {
4430 p.println("Current Input Method Manager state:");
4431 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004432 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 for (int i=0; i<N; i++) {
4434 InputMethodInfo info = mMethodList.get(i);
4435 p.println(" InputMethod #" + i + ":");
4436 info.dump(p, " ");
4437 }
4438 p.println(" Clients:");
4439 for (ClientState ci : mClients.values()) {
4440 p.println(" Client " + ci + ":");
4441 p.println(" client=" + ci.client);
4442 p.println(" inputContext=" + ci.inputContext);
4443 p.println(" sessionRequested=" + ci.sessionRequested);
4444 p.println(" curSession=" + ci.curSession);
4445 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004446 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004448 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004449 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4450 + " softInputMode=" +
4451 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4452 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004453 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4455 + " mBoundToMethod=" + mBoundToMethod);
4456 p.println(" mCurToken=" + mCurToken);
4457 p.println(" mCurIntent=" + mCurIntent);
4458 method = mCurMethod;
4459 p.println(" mCurMethod=" + mCurMethod);
4460 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004461 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 p.println(" mShowRequested=" + mShowRequested
4463 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4464 + " mShowForced=" + mShowForced
4465 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004466 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004467 p.println(" mCurUserActionNotificationSequenceNumber="
4468 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004469 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004470 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004471 p.println(" mSwitchingController:");
4472 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004473 p.println(" mSettings:");
4474 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004475
4476 p.println(" mStartInputHistory:");
4477 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004479
Jeff Brownb88102f2010-09-08 11:49:43 -07004480 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 pw.flush();
4483 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004484 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4485 } catch (IOException | RemoteException e) {
4486 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004488 } else {
4489 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004491
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004492 if (focusedWindowClient != null && client != focusedWindowClient) {
4493 p.println(" ");
4494 p.println("Warning: Current input method client doesn't match the last focused. "
4495 + "window.");
4496 p.println("Dumping input method client in the last focused window just in case.");
4497 p.println(" ");
4498 pw.flush();
4499 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004500 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4501 } catch (IOException | RemoteException e) {
4502 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004503 }
4504 }
4505
Jeff Brownb88102f2010-09-08 11:49:43 -07004506 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508 pw.flush();
4509 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004510 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4511 } catch (IOException | RemoteException e) {
4512 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004514 } else {
4515 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 }
4517 }
4518}