blob: 1d5f75e5f1d2a5d1dccbdaeefe8e506594e93c08 [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
216 | Context.BIND_SHOWING_UI;
217
218 /**
219 * Binding flags used only while the {@link InputMethodService} is showing window.
220 */
221 private static final int IME_VISIBLE_BIND_FLAGS =
222 Context.BIND_AUTO_CREATE
223 | Context.BIND_TREAT_LIKE_ACTIVITY
224 | Context.BIND_FOREGROUND_SERVICE;
225
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700226 @Retention(SOURCE)
227 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
228 private @interface HardKeyboardBehavior {
229 int WIRELESS_AFFORDANCE = 0;
230 int WIRED_AFFORDANCE = 1;
231 }
satok4e4569d2010-11-19 18:45:53 +0900232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800234 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900236 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700239 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700241 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900242 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900243 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900244 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800245 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900247 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 // All known input methods. mMethodMap also serves as the global
250 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700251 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
252 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900253 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700254 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900255 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
Yohei Yukawae0733062017-02-09 22:49:35 -0800257 /**
258 * Tracks how many times {@link #mMethodMap} was updated.
259 */
260 @GuardedBy("mMethodMap")
261 private int mMethodMapUpdateCount = 0;
262
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700263 // Used to bring IME service up to visible adjustment while it is being shown.
264 final ServiceConnection mVisibleConnection = new ServiceConnection() {
265 @Override public void onServiceConnected(ComponentName name, IBinder service) {
266 }
267
268 @Override public void onServiceDisconnected(ComponentName name) {
269 }
270 };
271 boolean mVisibleBound = false;
272
satok7cfc0ed2011-06-20 21:29:36 +0900273 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700274 private NotificationManager mNotificationManager;
275 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700276 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400277 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700278 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900279 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900280 private boolean mNotificationShown;
281
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900282 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 final ClientState client;
284 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700285
286 IInputMethodSession session;
287 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 @Override
290 public String toString() {
291 return "SessionState{uid " + client.uid + " pid " + client.pid
292 + " method " + Integer.toHexString(
293 System.identityHashCode(method))
294 + " session " + Integer.toHexString(
295 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700296 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 + "}";
298 }
299
300 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700301 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 client = _client;
303 method = _method;
304 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700305 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
Jeff Brownc28867a2013-03-26 15:42:39 -0700309 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 final IInputMethodClient client;
311 final IInputContext inputContext;
312 final int uid;
313 final int pid;
314 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 boolean sessionRequested;
317 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 @Override
320 public String toString() {
321 return "ClientState{" + Integer.toHexString(
322 System.identityHashCode(this)) + " uid " + uid
323 + " pid " + pid + "}";
324 }
325
326 ClientState(IInputMethodClient _client, IInputContext _inputContext,
327 int _uid, int _pid) {
328 client = _client;
329 inputContext = _inputContext;
330 uid = _uid;
331 pid = _pid;
332 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
333 }
334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700336 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700339 * Set once the system is ready to run third party code.
340 */
341 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700343 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700344 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
345 * method. This is to be synchronized with the secure settings keyed with
346 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
347 *
348 * <p>This can be transiently {@code null} when the system is re-initializing input method
349 * settings, e.g., the system locale is just changed.</p>
350 *
351 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
352 * {@link InputMethodManagerService}.</p>
353 *
354 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700356 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * The current binding sequence number, incremented every time there is
361 * a new bind performed.
362 */
363 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
366 * The client that is currently bound to an input method.
367 */
368 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800371 * The last window token that we confirmed to be focused. This is always updated upon reports
372 * from the input method client. If the window state is already changed before the report is
373 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700374 */
375 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700377 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800378 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
379 *
380 * @see #mCurFocusedWindow
381 */
382 int mCurFocusedWindowSoftInputMode;
383
384 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800385 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
386 */
387 ClientState mCurFocusedWindowClient;
388
389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * The input context last provided by the current client.
391 */
392 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700395 * The missing method flags for the input context last provided by the current client.
396 *
397 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
398 */
399 int mCurInputContextMissingMethods;
400
401 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * The attributes last provided by the current client.
403 */
404 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700407 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700409 *
410 * <p>This can be {@code null} when no input method is connected.</p>
411 *
412 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700414 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
satokab751aa2010-09-14 19:17:36 +0900418 * The current subtype of the current input method.
419 */
420 private InputMethodSubtype mCurrentSubtype;
421
satok4e4569d2010-11-19 18:45:53 +0900422 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900423 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700424 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900425
John Spurlocke0980502013-10-25 11:59:29 -0400426 // Was the keyguard locked when this client became current?
427 private boolean mCurClientInKeyguard;
428
satokab751aa2010-09-14 19:17:36 +0900429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * Set to true if our ServiceConnection is currently actively bound to
431 * a service (whether or not we have gotten its IBinder back yet).
432 */
433 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
436 * Set if the client has asked for the input method to be shown.
437 */
438 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 /**
441 * Set if we were explicitly told to show the input method.
442 */
443 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
446 * Set if we were forced to be shown.
447 */
448 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
451 * Set if we last told the input method to show itself.
452 */
453 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800456 * {@code true} if the current input method is in fullscreen mode.
457 */
458 boolean mInFullscreenMode;
459
460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 * The Intent used to connect to the current input method.
462 */
463 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 /**
466 * The token we have made for the currently active input method, to
467 * identify it in the future.
468 */
469 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 /**
472 * If non-null, this is the input method service we are currently connected
473 * to.
474 */
475 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 /**
478 * Time that we last initiated a bind to the input method, to determine
479 * if we should try to disconnect and reconnect to it.
480 */
481 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 /**
484 * Have we called mCurMethod.bindInput()?
485 */
486 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 /**
489 * Currently enabled session. Only touched by service thread, not
490 * protected by a lock.
491 */
492 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700495 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700497 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900499 int mCurUserActionNotificationSequenceNumber = 0;
500
Joe Onorato857fd9b2011-01-27 15:08:35 -0800501 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900502
503 /**
504 * A set of status bits regarding the active IME.
505 *
506 * <p>This value is a combination of following two bits:</p>
507 * <dl>
508 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
509 * <dd>
510 * If this bit is ON, connected IME is ready to accept touch/key events.
511 * </dd>
512 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
513 * <dd>
514 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
515 * </dd>
516 * </dl>
517 * <em>Do not update this value outside of setImeWindowStatus.</em>
518 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800519 int mImeWindowVis;
520
Ken Wakasa05dbb652011-08-22 15:22:43 +0900521 private AlertDialog.Builder mDialogBuilder;
522 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700523 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900524 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700525 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900526 private InputMethodInfo[] mIms;
527 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800528 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700529 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800530 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900531 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
532 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500533 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700534 @HardKeyboardBehavior
535 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800536
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800537 /**
538 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
539 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
540 * will not affect those tasks that are already posted.
541 *
542 * <p>Posting {@link #MSG_START_INPUT} message basically means that
543 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
544 * back in the current IME process shortly, which will also affect what the current IME starts
545 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
546 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
547 * logical input session between the client application and the current IME.</p>
548 *
549 * <p>Be careful to not keep strong references to this object forever, which can prevent
550 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
551 * </p>
552 */
553 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800554 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
555
556 final int mSequenceNumber;
557 final long mTimestamp;
558 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800559 @NonNull
560 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800561 @NonNull
562 final String mImeId;
563 // @InputMethodClient.StartInputReason
564 final int mStartInputReason;
565 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800566 @Nullable
567 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800568 @NonNull
569 final EditorInfo mEditorInfo;
570 final int mTargetWindowSoftInputMode;
571 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800572
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800573 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
574 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
575 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
576 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
577 mSequenceNumber = sSequenceNumber.getAndIncrement();
578 mTimestamp = SystemClock.uptimeMillis();
579 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800580 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800581 mImeId = imeId;
582 mStartInputReason = startInputReason;
583 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800584 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800585 mEditorInfo = editorInfo;
586 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
587 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800588 }
589 }
590
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800591 @GuardedBy("mMethodMap")
592 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800593
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800594 /**
595 * A ring buffer to store the history of {@link StartInputInfo}.
596 */
597 private static final class StartInputHistory {
598 /**
599 * Entry size for non low-RAM devices.
600 *
601 * <p>TODO: Consider to follow what other system services have been doing to manage
602 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
603 */
604 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
605
606 /**
607 * Entry size for non low-RAM devices.
608 *
609 * <p>TODO: Consider to follow what other system services have been doing to manage
610 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
611 */
612 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
613
614 private static int getEntrySize() {
615 if (ActivityManager.isLowRamDeviceStatic()) {
616 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
617 } else {
618 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
619 }
620 }
621
622 /**
623 * Backing store for the ring bugger.
624 */
625 private final Entry[] mEntries = new Entry[getEntrySize()];
626
627 /**
628 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
629 * write.
630 */
631 private int mNextIndex = 0;
632
633 /**
634 * Recyclable entry to store the information in {@link StartInputInfo}.
635 */
636 private static final class Entry {
637 int mSequenceNumber;
638 long mTimestamp;
639 long mWallTime;
640 @NonNull
641 String mImeTokenString;
642 @NonNull
643 String mImeId;
644 /* @InputMethodClient.StartInputReason */
645 int mStartInputReason;
646 boolean mRestarting;
647 @NonNull
648 String mTargetWindowString;
649 @NonNull
650 EditorInfo mEditorInfo;
651 int mTargetWindowSoftInputMode;
652 int mClientBindSequenceNumber;
653
654 Entry(@NonNull StartInputInfo original) {
655 set(original);
656 }
657
658 void set(@NonNull StartInputInfo original) {
659 mSequenceNumber = original.mSequenceNumber;
660 mTimestamp = original.mTimestamp;
661 mWallTime = original.mWallTime;
662 // Intentionally convert to String so as not to keep a strong reference to a Binder
663 // object.
664 mImeTokenString = String.valueOf(original.mImeToken);
665 mImeId = original.mImeId;
666 mStartInputReason = original.mStartInputReason;
667 mRestarting = original.mRestarting;
668 // Intentionally convert to String so as not to keep a strong reference to a Binder
669 // object.
670 mTargetWindowString = String.valueOf(original.mTargetWindow);
671 mEditorInfo = original.mEditorInfo;
672 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
673 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
674 }
675 }
676
677 /**
678 * Add a new entry and discard the oldest entry as needed.
679 * @param info {@lin StartInputInfo} to be added.
680 */
681 void addEntry(@NonNull StartInputInfo info) {
682 final int index = mNextIndex;
683 if (mEntries[index] == null) {
684 mEntries[index] = new Entry(info);
685 } else {
686 mEntries[index].set(info);
687 }
688 mNextIndex = (mNextIndex + 1) % mEntries.length;
689 }
690
691 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
692 final SimpleDateFormat dataFormat =
693 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
694
695 for (int i = 0; i < mEntries.length; ++i) {
696 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
697 if (entry == null) {
698 continue;
699 }
700 pw.print(prefix);
701 pw.println("StartInput #" + entry.mSequenceNumber + ":");
702
703 pw.print(prefix);
704 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
705 + " (timestamp=" + entry.mTimestamp + ")"
706 + " reason="
707 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
708 + " restarting=" + entry.mRestarting);
709
710 pw.print(prefix);
711 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
712
713 pw.print(prefix);
714 pw.println(" targetWin=" + entry.mTargetWindowString
715 + " [" + entry.mEditorInfo.packageName + "]"
716 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
717
718 pw.print(prefix);
719 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
720 entry.mTargetWindowSoftInputMode));
721
722 pw.print(prefix);
723 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
724 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
725 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
726 + " fieldName=" + entry.mEditorInfo.fieldName
727 + " actionId=" + entry.mEditorInfo.actionId
728 + " actionLabel=" + entry.mEditorInfo.actionLabel);
729 }
730 }
731 }
732
733 @GuardedBy("mMethodMap")
734 @NonNull
735 private final StartInputHistory mStartInputHistory = new StartInputHistory();
736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700738 int mUserId;
739 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700740 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800741 String mLastEnabled = "";
742
Yohei Yukawa81482972015-06-04 00:58:59 -0700743 /**
744 * <em>This constructor must be called within the lock.</em>
745 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 SettingsObserver(Handler handler) {
747 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700748 }
749
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800750 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700751 if (mRegistered && mUserId == userId) {
752 return;
753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700755 if (mRegistered) {
756 mContext.getContentResolver().unregisterContentObserver(this);
757 mRegistered = false;
758 }
759 if (mUserId != userId) {
760 mLastEnabled = "";
761 mUserId = userId;
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700764 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900765 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700766 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900767 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700768 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700769 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700770 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800771 resolver.registerContentObserver(Settings.Secure.getUriFor(
772 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700773 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800775
Michael Wright7b5a96b2014-08-09 19:28:42 -0700776 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800777 final Uri showImeUri = Settings.Secure.getUriFor(
778 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
779 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
780 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700782 if (showImeUri.equals(uri)) {
783 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800784 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
785 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
786 mContext.getContentResolver(),
787 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
788 0, mUserId) == 1;
789 if (mAccessibilityRequestingNoSoftKeyboard) {
790 final boolean showRequested = mShowRequested;
791 hideCurrentInputLocked(0, null);
792 mShowRequested = showRequested;
793 } else if (mShowRequested) {
794 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
795 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700796 } else {
797 boolean enabledChanged = false;
798 String newEnabled = mSettings.getEnabledInputMethodsStr();
799 if (!mLastEnabled.equals(newEnabled)) {
800 mLastEnabled = newEnabled;
801 enabledChanged = true;
802 }
803 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700807
808 @Override
809 public String toString() {
810 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
811 + " mLastEnabled=" + mLastEnabled + "}";
812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
Yohei Yukawa79247822017-01-23 15:26:15 -0800815 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900816 @Override
817 public void onReceive(Context context, Intent intent) {
818 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700819 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900820 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700821 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900822 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800823 } else if (Intent.ACTION_USER_ADDED.equals(action)
824 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100825 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800826 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700827 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
828 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
829 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
830 final String prevValue = intent.getStringExtra(
831 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
832 final String newValue = intent.getStringExtra(
833 Intent.EXTRA_SETTING_NEW_VALUE);
834 restoreEnabledInputMethods(mContext, prevValue, newValue);
835 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800836 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800837 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900838 } else {
839 Slog.w(TAG, "Unexpected intent " + intent);
840 }
841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800843
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800844 /**
845 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
846 *
847 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
848 * the users. We should ignore this event if this is about any background user's locale.</p>
849 *
850 * <p>Caution: This method must not be called when system is not ready.</p>
851 */
852 void onActionLocaleChanged() {
853 synchronized (mMethodMap) {
854 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
855 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
856 return;
857 }
858 buildInputMethodListLocked(true);
859 // If the locale is changed, needs to reset the default ime
860 resetDefaultImeLocked(mContext);
861 updateFromSettingsLocked(true);
862 mLastSystemLocales = possibleNewLocale;
863 }
864 }
865
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700866 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
867 // does not attempt to validate on the fly with any installed device policy, so must only
868 // be run in the context of initial device setup.
869 //
870 // TODO: Move this method to InputMethodUtils with adding unit tests.
871 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
872 if (DEBUG_RESTORE) {
873 Slog.i(TAG, "Restoring enabled input methods:");
874 Slog.i(TAG, "prev=" + prevValue);
875 Slog.i(TAG, " new=" + newValue);
876 }
877 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900878 ArrayMap<String, ArraySet<String>> prevMap =
879 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
880 ArrayMap<String, ArraySet<String>> newMap =
881 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700882
883 // Merge the restored ime+subtype enabled states into the live state
884 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
885 final String imeId = entry.getKey();
886 ArraySet<String> prevSubtypes = prevMap.get(imeId);
887 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700888 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700889 prevMap.put(imeId, prevSubtypes);
890 }
891 prevSubtypes.addAll(entry.getValue());
892 }
893
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700894 final String mergedImesAndSubtypesString =
895 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700896 if (DEBUG_RESTORE) {
897 Slog.i(TAG, "Merged IME string:");
898 Slog.i(TAG, " " + mergedImesAndSubtypesString);
899 }
900 Settings.Secure.putString(context.getContentResolver(),
901 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
902 }
903
Yohei Yukawac4e44912017-02-09 19:30:22 -0800904 final class MyPackageMonitor extends PackageMonitor {
905 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800906 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800907 *
908 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
909 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
910 * dynamically unless the entire package is updated, which also always triggers package
911 * rescanning.</p>
912 */
913 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800914 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
915
916 /**
917 * Packages that are appeared, disappeared, or modified for whatever reason.
918 *
919 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
920 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
921 * duplicate elements for our use case.</p>
922 *
923 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
924 * which should be bound to {@link #getRegisteredHandler()}.</p>
925 */
926 private final ArrayList<String> mChangedPackages = new ArrayList<>();
927
928 /**
929 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
930 *
931 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
932 * which should be bound to {@link #getRegisteredHandler()}.</p>
933 */
934 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800935
936 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800937 void clearKnownImePackageNamesLocked() {
938 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800939 }
940
941 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800942 final void addKnownImePackageNameLocked(@NonNull String packageName) {
943 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800944 }
945
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800946 @GuardedBy("mMethodMap")
947 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 final int userId = getChangingUserId();
949 final boolean retval = userId == mSettings.getCurrentUserId();
950 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900951 if (!retval) {
952 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
953 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 }
955 return retval;
956 }
957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800959 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800961 if (!isChangingPackagesOfCurrentUserLocked()) {
962 return false;
963 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 final int N = mMethodList.size();
966 if (curInputMethodId != null) {
967 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800968 InputMethodInfo imi = mMethodList.get(i);
969 if (imi.getId().equals(curInputMethodId)) {
970 for (String pkg : packages) {
971 if (imi.getPackageName().equals(pkg)) {
972 if (!doit) {
973 return true;
974 }
satok723a27e2010-11-11 14:58:11 +0900975 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800976 chooseNewDefaultIMELocked();
977 return true;
978 }
979 }
980 }
981 }
982 }
983 }
984 return false;
985 }
986
987 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800988 public void onBeginPackageChanges() {
989 clearPackageChangeState();
990 }
991
992 @Override
993 public void onPackageAppeared(String packageName, int reason) {
994 if (!mImePackageAppeared) {
995 final PackageManager pm = mContext.getPackageManager();
996 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
997 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
998 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
999 // No need to lock this because we access it only on getRegisteredHandler().
1000 if (!services.isEmpty()) {
1001 mImePackageAppeared = true;
1002 }
1003 }
1004 // No need to lock this because we access it only on getRegisteredHandler().
1005 mChangedPackages.add(packageName);
1006 }
1007
1008 @Override
1009 public void onPackageDisappeared(String packageName, int reason) {
1010 // No need to lock this because we access it only on getRegisteredHandler().
1011 mChangedPackages.add(packageName);
1012 }
1013
1014 @Override
1015 public void onPackageModified(String packageName) {
1016 // No need to lock this because we access it only on getRegisteredHandler().
1017 mChangedPackages.add(packageName);
1018 }
1019
1020 @Override
1021 public void onPackagesSuspended(String[] packages) {
1022 // No need to lock this because we access it only on getRegisteredHandler().
1023 for (String packageName : packages) {
1024 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001025 }
1026 }
1027
1028 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001029 public void onPackagesUnsuspended(String[] packages) {
1030 // No need to lock this because we access it only on getRegisteredHandler().
1031 for (String packageName : packages) {
1032 mChangedPackages.add(packageName);
1033 }
1034 }
1035
1036 @Override
1037 public void onFinishPackageChanges() {
1038 onFinishPackageChangesInternal();
1039 clearPackageChangeState();
1040 }
1041
1042 private void clearPackageChangeState() {
1043 // No need to lock them because we access these fields only on getRegisteredHandler().
1044 mChangedPackages.clear();
1045 mImePackageAppeared = false;
1046 }
1047
1048 private boolean shouldRebuildInputMethodListLocked() {
1049 // This method is guaranteed to be called only by getRegisteredHandler().
1050
1051 // If there is any new package that contains at least one IME, then rebuilt the list
1052 // of IMEs.
1053 if (mImePackageAppeared) {
1054 return true;
1055 }
1056
1057 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1058 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1059 // is an option, but it may still do some extra operations that we do not need here.
1060 final int N = mChangedPackages.size();
1061 for (int i = 0; i < N; ++i) {
1062 final String packageName = mChangedPackages.get(i);
1063 if (mKnownImePackageNames.contains(packageName)) {
1064 return true;
1065 }
1066 }
1067 return false;
1068 }
1069
1070 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001071 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001072 if (!isChangingPackagesOfCurrentUserLocked()) {
1073 return;
1074 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001075 if (!shouldRebuildInputMethodListLocked()) {
1076 return;
1077 }
1078
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001079 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001080 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001081 final int N = mMethodList.size();
1082 if (curInputMethodId != null) {
1083 for (int i=0; i<N; i++) {
1084 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001085 final String imiId = imi.getId();
1086 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001087 curIm = imi;
1088 }
satoke7c6998e2011-06-03 17:57:59 +09001089
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001090 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001091 if (isPackageModified(imi.getPackageName())) {
1092 mFileManager.deleteAllInputMethodSubtypes(imiId);
1093 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001094 if (change == PACKAGE_TEMPORARY_CHANGE
1095 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001096 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001097 + imi.getComponent());
1098 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100 }
1101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
Yohei Yukawa94e33302016-02-12 19:37:03 -08001103 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001106
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001107 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001108 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001109 if (change == PACKAGE_TEMPORARY_CHANGE
1110 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001111 ServiceInfo si = null;
1112 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001113 si = mIPackageManager.getServiceInfo(
1114 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1115 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001116 }
1117 if (si == null) {
1118 // Uh oh, current input method is no longer around!
1119 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001120 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001121 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001122 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001123 changed = true;
1124 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001126 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001127 }
1128 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001129 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001130 }
satokab751aa2010-09-14 19:17:36 +09001131
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001132 if (curIm == null) {
1133 // We currently don't have a default input method... is
1134 // one now available?
1135 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001136 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1137 // Even if the current input method is still available, mCurrentSubtype could
1138 // be obsolete when the package is modified in practice.
1139 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001141
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001142 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001143 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145 }
1146 }
1147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
Jeff Brownc28867a2013-03-26 15:42:39 -07001149 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001150 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001151 private final IInputMethod mMethod;
1152 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
Jeff Brownc28867a2013-03-26 15:42:39 -07001154 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1155 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001156 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001157 mMethod = method;
1158 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
satoke7c6998e2011-06-03 17:57:59 +09001161 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001162 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001163 long ident = Binder.clearCallingIdentity();
1164 try {
1165 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1166 } finally {
1167 Binder.restoreCallingIdentity(ident);
1168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001171
satok01038492012-04-09 21:08:27 +09001172 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001173 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001174 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001175 public void onHardKeyboardStatusChange(boolean available) {
1176 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1177 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001178 }
1179
Michael Wright7b5a96b2014-08-09 19:28:42 -07001180 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001181 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001182 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001183 }
1184 synchronized(mMethodMap) {
1185 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1186 && mSwitchingDialog.isShowing()) {
1187 mSwitchingDialogTitleView.findViewById(
1188 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1189 available ? View.VISIBLE : View.GONE);
1190 }
1191 }
1192 }
1193 }
1194
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001195 public static final class Lifecycle extends SystemService {
1196 private InputMethodManagerService mService;
1197
1198 public Lifecycle(Context context) {
1199 super(context);
1200 mService = new InputMethodManagerService(context);
1201 }
1202
1203 @Override
1204 public void onStart() {
1205 LocalServices.addService(InputMethodManagerInternal.class,
1206 new LocalServiceImpl(mService.mHandler));
1207 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1208 }
1209
1210 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001211 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001212 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001213 // TODO: Dispatch this to a worker thread as needed.
1214 mService.onSwitchUser(userHandle);
1215 }
1216
1217 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001218 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001219 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001220 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001221 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1222 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1223 .getService(Context.STATUS_BAR_SERVICE);
1224 mService.systemRunning(statusBarService);
1225 }
1226 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001227
1228 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001229 public void onUnlockUser(final @UserIdInt int userHandle) {
1230 // Called on ActivityManager thread.
1231 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001232 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001233 }
1234 }
1235
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001236 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001237 synchronized(mMethodMap) {
1238 final int currentUserId = mSettings.getCurrentUserId();
1239 if (DEBUG) {
1240 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1241 }
1242 if (userId != currentUserId) {
1243 return;
1244 }
1245 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001246 if (mSystemReady) {
1247 // We need to rebuild IMEs.
1248 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1249 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1250 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001251 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001252 }
1253
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001254 void onSwitchUser(@UserIdInt int userId) {
1255 synchronized (mMethodMap) {
1256 switchUserLocked(userId);
1257 }
1258 }
1259
Seigo Nonaka7309b122015-08-17 18:34:13 -07001260 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001261 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001263 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001265 // Note: SettingsObserver doesn't register observers in its constructor.
1266 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 mIWindowManager = IWindowManager.Stub.asInterface(
1268 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001269 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001270 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001271 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 public void executeMessage(Message msg) {
1273 handleMessage(msg);
1274 }
Mita Yuned218c72012-12-06 17:18:25 -08001275 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001276 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001277 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001278 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001279 mHasFeature = context.getPackageManager().hasSystemFeature(
1280 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001281 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001282 mHardKeyboardBehavior = mContext.getResources().getInteger(
1283 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001284
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001285 Bundle extras = new Bundle();
1286 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001287 mImeSwitcherNotification =
1288 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1289 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1290 .setWhen(0)
1291 .setOngoing(true)
1292 .addExtras(extras)
1293 .setCategory(Notification.CATEGORY_SYSTEM)
1294 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001295
satok7cfc0ed2011-06-20 21:29:36 +09001296 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001297 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001298
1299 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001300
satok7cfc0ed2011-06-20 21:29:36 +09001301 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001302 int userId = 0;
1303 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001304 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001305 } catch (RemoteException e) {
1306 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1307 }
satok913a8922010-08-26 21:53:41 +09001308
satokd87c2592010-09-29 11:52:06 +09001309 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001310 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001311 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001312
Kenny Guy2a764942014-04-02 13:29:20 +01001313 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001314 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001315 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1316 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001317 }
1318
satok5b927c432012-05-01 20:09:34 +09001319 private void resetDefaultImeLocked(Context context) {
1320 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001321 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001322 return;
1323 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001324 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001325 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001326 if (suitableImes.isEmpty()) {
1327 Slog.i(TAG, "No default found");
1328 return;
satok5b927c432012-05-01 20:09:34 +09001329 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001330 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001331 if (DEBUG) {
1332 Slog.i(TAG, "Default found, using " + defIm.getId());
1333 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001334 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001335 }
1336
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001337 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001338 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1339 + " currentUserId=" + mSettings.getCurrentUserId());
1340
Yohei Yukawa81482972015-06-04 00:58:59 -07001341 // ContentObserver should be registered again when the user is changed
1342 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001343
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001344 // If the system is not ready or the device is not yed unlocked by the user, then we use
1345 // copy-on-write settings.
1346 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001347 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001348 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001349 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001350 // InputMethodFileManager should be reset when the user is changed
1351 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001352 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001353
1354 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1355 + " defaultImiId=" + defaultImiId);
1356
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001357 // For secondary users, the list of enabled IMEs may not have been updated since the
1358 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1359 // not be empty even if the IME has been uninstalled by the primary user.
1360 // Even in such cases, IMMS works fine because it will find the most applicable
1361 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001362 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001363 mLastSystemLocales = mRes.getConfiguration().getLocales();
1364
1365 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1366 if (mSystemReady) {
1367 hideCurrentInputLocked(0, null);
1368 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1369 buildInputMethodListLocked(initialUserSwitch);
1370 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1371 // This is the first time of the user switch and
1372 // set the current ime to the proper one.
1373 resetDefaultImeLocked(mContext);
1374 }
1375 updateFromSettingsLocked(true);
1376 try {
1377 startInputInnerLocked();
1378 } catch (RuntimeException e) {
1379 Slog.w(TAG, "Unexpected exception", e);
1380 }
1381 }
1382
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001383 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001384 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1385 mSettings.getEnabledInputMethodListLocked(), newUserId,
1386 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001387 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001388
1389 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1390 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001391 }
1392
Kenny Guy2a764942014-04-02 13:29:20 +01001393 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001394 mSettings.setCurrentProfileIds(
1395 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001396 }
1397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 @Override
1399 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1400 throws RemoteException {
1401 try {
1402 return super.onTransact(code, data, reply, flags);
1403 } catch (RuntimeException e) {
1404 // The input method manager only throws security exceptions, so let's
1405 // log all others.
1406 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001407 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409 throw e;
1410 }
1411 }
1412
Svetoslav Ganova0027152013-06-25 14:59:53 -07001413 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001414 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001415 if (DEBUG) {
1416 Slog.d(TAG, "--- systemReady");
1417 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001418 if (!mSystemReady) {
1419 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001420 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001421 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001422 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001423 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001424 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1425 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001426 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001427 if (mStatusBar != null) {
1428 mStatusBar.setIconVisibility(mSlotIme, false);
1429 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001430 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001431 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1432 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001433 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001434 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001435 mHardKeyboardListener);
1436 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001437
1438 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1439 mSettingsObserver.registerContentObserverLocked(currentUserId);
1440
1441 final IntentFilter broadcastFilter = new IntentFilter();
1442 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1443 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1444 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1445 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1446 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1447 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1448
1449 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1450 resetDefaultImeLocked(mContext);
1451 updateFromSettingsLocked(true);
1452 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1453 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1454 mContext.getBasePackageName());
1455
Dianne Hackborncc278702009-09-02 23:07:23 -07001456 try {
1457 startInputInnerLocked();
1458 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001459 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001460 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001461 }
1462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001464
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001465 // ---------------------------------------------------------------------------------------
1466 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1467 // 1) it comes from the system process
1468 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1469 private boolean calledFromValidUser() {
1470 final int uid = Binder.getCallingUid();
1471 final int userId = UserHandle.getUserId(uid);
1472 if (DEBUG) {
1473 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1474 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1475 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001476 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1477 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001478 }
Kenny Guy2a764942014-04-02 13:29:20 +01001479 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001480 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001482
1483 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1484 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1485 // must not manage background users' states in any functions.
1486 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1487 // by a token.
1488 if (mContext.checkCallingOrSelfPermission(
1489 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1490 == PackageManager.PERMISSION_GRANTED) {
1491 if (DEBUG) {
1492 Slog.d(TAG, "--- Access granted because the calling process has "
1493 + "the INTERACT_ACROSS_USERS_FULL permission");
1494 }
1495 return true;
1496 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001497 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001498 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1499 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001500 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001501 }
1502
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001503
1504 /**
1505 * Returns true iff the caller is identified to be the current input method with the token.
1506 * @param token The window token given to the input method when it was started.
1507 * @return true if and only if non-null valid token is specified.
1508 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001509 private boolean calledWithValidToken(@Nullable IBinder token) {
1510 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1511 if (DEBUG) {
1512 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1513 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1514 }
1515 return false;
1516 }
1517 if (token == null || token != mCurToken) {
1518 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1519 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1520 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001521 return false;
1522 }
1523 return true;
1524 }
1525
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001526 private boolean bindCurrentInputMethodService(
1527 Intent service, ServiceConnection conn, int flags) {
1528 if (service == null || conn == null) {
1529 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1530 return false;
1531 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001532 return mContext.bindServiceAsUser(service, conn, flags,
1533 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001534 }
1535
satoke7c6998e2011-06-03 17:57:59 +09001536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001538 // TODO: Make this work even for non-current users?
1539 if (!calledFromValidUser()) {
1540 return Collections.emptyList();
1541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001543 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545 }
1546
satoke7c6998e2011-06-03 17:57:59 +09001547 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001549 // TODO: Make this work even for non-current users?
1550 if (!calledFromValidUser()) {
1551 return Collections.emptyList();
1552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001554 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556 }
1557
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001558 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001559 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001560 * @return enabled subtypes of the specified imi
1561 */
satoke7c6998e2011-06-03 17:57:59 +09001562 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001563 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001564 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001565 // TODO: Make this work even for non-current users?
1566 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001567 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001568 }
satok67ddf9c2010-11-17 09:45:54 +09001569 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001570 final InputMethodInfo imi;
1571 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001572 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001573 } else {
1574 imi = mMethodMap.get(imiId);
1575 }
1576 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001577 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001578 }
1579 return mSettings.getEnabledInputMethodSubtypeListLocked(
1580 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001581 }
1582 }
1583
satoke7c6998e2011-06-03 17:57:59 +09001584 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 public void addClient(IInputMethodClient client,
1586 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001587 if (!calledFromValidUser()) {
1588 return;
1589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 synchronized (mMethodMap) {
1591 mClients.put(client.asBinder(), new ClientState(client,
1592 inputContext, uid, pid));
1593 }
1594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001595
satoke7c6998e2011-06-03 17:57:59 +09001596 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001598 if (!calledFromValidUser()) {
1599 return;
1600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001602 ClientState cs = mClients.remove(client.asBinder());
1603 if (cs != null) {
1604 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001605 if (mCurClient == cs) {
1606 mCurClient = null;
1607 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001608 if (mCurFocusedWindowClient == cs) {
1609 mCurFocusedWindowClient = null;
1610 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 }
1613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 void executeOrSendMessage(IInterface target, Message msg) {
1616 if (target.asBinder() instanceof Binder) {
1617 mCaller.sendMessage(msg);
1618 } else {
1619 handleMessage(msg);
1620 msg.recycle();
1621 }
1622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001623
Yohei Yukawa33e81792015-11-17 21:14:42 -08001624 void unbindCurrentClientLocked(
1625 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001627 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 + mCurClient.client.asBinder());
1629 if (mBoundToMethod) {
1630 mBoundToMethod = false;
1631 if (mCurMethod != null) {
1632 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1633 MSG_UNBIND_INPUT, mCurMethod));
1634 }
1635 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001636
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001637 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1638 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001639 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1640 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001643
The Android Open Source Project10592532009-03-18 17:39:46 -07001644 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 }
1646 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 private int getImeShowFlags() {
1649 int flags = 0;
1650 if (mShowForced) {
1651 flags |= InputMethod.SHOW_FORCED
1652 | InputMethod.SHOW_EXPLICIT;
1653 } else if (mShowExplicitlyRequested) {
1654 flags |= InputMethod.SHOW_EXPLICIT;
1655 }
1656 return flags;
1657 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 private int getAppShowFlags() {
1660 int flags = 0;
1661 if (mShowForced) {
1662 flags |= InputMethodManager.SHOW_FORCED;
1663 } else if (!mShowExplicitlyRequested) {
1664 flags |= InputMethodManager.SHOW_IMPLICIT;
1665 }
1666 return flags;
1667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001668
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001669 InputBindResult attachNewInputLocked(
1670 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 if (!mBoundToMethod) {
1672 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1673 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1674 mBoundToMethod = true;
1675 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001676
1677 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001678 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1679 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1680 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001681 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001682 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001685 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001686 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001687 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001689 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001690 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001692 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001693 (session.channel != null ? session.channel.dup() : null),
1694 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001697 InputBindResult startInputLocked(
1698 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001699 IInputMethodClient client, IInputContext inputContext,
1700 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001701 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 // If no method is currently selected, do nothing.
1703 if (mCurMethodId == null) {
1704 return mNoBinding;
1705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 ClientState cs = mClients.get(client.asBinder());
1708 if (cs == null) {
1709 throw new IllegalArgumentException("unknown client "
1710 + client.asBinder());
1711 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001712
Yohei Yukawa74750f22016-03-22 12:54:22 -07001713 if (attribute == null) {
1714 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1715 + " uid=" + cs.uid + " pid=" + cs.pid);
1716 return null;
1717 }
1718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 try {
1720 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1721 // Check with the window manager to make sure this client actually
1722 // has a window with focus. If not, reject. This is thread safe
1723 // because if the focus changes some time before or after, the
1724 // next client receiving focus that has any interest in input will
1725 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001726 if (DEBUG) {
1727 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1728 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 return null;
1731 }
1732 } catch (RemoteException e) {
1733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001734
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001735 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001736 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001737 }
1738
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001739 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001740 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001741 @NonNull EditorInfo attribute, int controlFlags,
1742 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001743 // If no method is currently selected, do nothing.
1744 if (mCurMethodId == null) {
1745 return mNoBinding;
1746 }
1747
Yohei Yukawad57ba672015-06-08 16:39:46 -07001748 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1749 attribute.packageName)) {
1750 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1751 + " uid=" + cs.uid + " package=" + attribute.packageName);
1752 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001753 }
1754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001756 // Was the keyguard locked when switching over to the new client?
1757 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 // If the client is changing, we need to switch over to the new
1759 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001760 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001761 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001762 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763
1764 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001765 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001766 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001767 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
1769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 // Bump up the sequence for this client and attach it.
1772 mCurSeq++;
1773 if (mCurSeq <= 0) mCurSeq = 1;
1774 mCurClient = cs;
1775 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001776 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 // Check if the input method is changing.
1780 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1781 if (cs.curSession != null) {
1782 // Fast case: if we are already connected to the input method,
1783 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001784 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001785 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787 if (mHaveConnection) {
1788 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 // Return to client, and we will get back with it when
1790 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001791 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001792 return new InputBindResult(null, null, mCurId, mCurSeq,
1793 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 } else if (SystemClock.uptimeMillis()
1795 < (mLastBindTime+TIME_TO_RECONNECT)) {
1796 // In this case we have connected to the service, but
1797 // don't yet have its interface. If it hasn't been too
1798 // long since we did the connection, we'll return to
1799 // the client and wait to get the service interface so
1800 // we can report back. If it has been too long, we want
1801 // to fall through so we can try a disconnect/reconnect
1802 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001803 return new InputBindResult(null, null, mCurId, mCurSeq,
1804 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001806 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1807 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
1809 }
1810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001811
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001812 return startInputInnerLocked();
1813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001814
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001815 InputBindResult startInputInnerLocked() {
1816 if (mCurMethodId == null) {
1817 return mNoBinding;
1818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001820 if (!mSystemReady) {
1821 // If the system is not yet ready, we shouldn't be running third
1822 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001823 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1824 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001825 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1828 if (info == null) {
1829 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1830 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001831
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001832 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1835 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001836 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1837 com.android.internal.R.string.input_method_binding_label);
1838 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1839 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001840 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 mLastBindTime = SystemClock.uptimeMillis();
1842 mHaveConnection = true;
1843 mCurId = info.getId();
1844 mCurToken = new Binder();
1845 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001846 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001847 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 } catch (RemoteException e) {
1849 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001850 return new InputBindResult(null, null, mCurId, mCurSeq,
1851 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 } else {
1853 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001854 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 + mCurIntent);
1856 }
1857 return null;
1858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001859
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001860 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001861 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001862 IInputMethodClient client, IInputContext inputContext,
1863 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001864 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001865 if (!calledFromValidUser()) {
1866 return null;
1867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001869 if (DEBUG) {
1870 Slog.v(TAG, "startInput: reason="
1871 + InputMethodClient.getStartInputReason(startInputReason)
1872 + " client = " + client.asBinder()
1873 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001874 + " missingMethods="
1875 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001876 + " attribute=" + attribute
1877 + " controlFlags=#" + Integer.toHexString(controlFlags));
1878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 final long ident = Binder.clearCallingIdentity();
1880 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001881 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1882 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 } finally {
1884 Binder.restoreCallingIdentity(ident);
1885 }
1886 }
1887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001888
satoke7c6998e2011-06-03 17:57:59 +09001889 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 public void finishInput(IInputMethodClient client) {
1891 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001892
satoke7c6998e2011-06-03 17:57:59 +09001893 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 public void onServiceConnected(ComponentName name, IBinder service) {
1895 synchronized (mMethodMap) {
1896 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1897 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001898 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001899 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001900 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001901 return;
1902 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001903 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001904 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1905 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001907 clearClientSessionLocked(mCurClient);
1908 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910 }
1911 }
1912 }
1913
Jeff Brownc28867a2013-03-26 15:42:39 -07001914 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1915 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 synchronized (mMethodMap) {
1917 if (mCurMethod != null && method != null
1918 && mCurMethod.asBinder() == method.asBinder()) {
1919 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001920 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001922 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001923 InputBindResult res = attachNewInputLocked(
1924 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 if (res.method != null) {
1926 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001927 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001929 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
1931 }
1932 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001933
1934 // Session abandoned. Close its associated input channel.
1935 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001937
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001938 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001939 if (mVisibleBound) {
1940 mContext.unbindService(mVisibleConnection);
1941 mVisibleBound = false;
1942 }
1943
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001944 if (mHaveConnection) {
1945 mContext.unbindService(this);
1946 mHaveConnection = false;
1947 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001948
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001949 if (mCurToken != null) {
1950 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001951 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001952 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001953 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001954 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001955 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001956 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001957 } catch (RemoteException e) {
1958 }
1959 mCurToken = null;
1960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001961
The Android Open Source Project10592532009-03-18 17:39:46 -07001962 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001963 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001965
Yohei Yukawa33e81792015-11-17 21:14:42 -08001966 void resetCurrentMethodAndClient(
1967 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001968 mCurMethodId = null;
1969 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001970 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001971 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001972
1973 void requestClientSessionLocked(ClientState cs) {
1974 if (!cs.sessionRequested) {
1975 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1976 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1977 cs.sessionRequested = true;
1978 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1979 MSG_CREATE_SESSION, mCurMethod, channels[1],
1980 new MethodCallback(this, mCurMethod, channels[0])));
1981 }
1982 }
1983
1984 void clearClientSessionLocked(ClientState cs) {
1985 finishSessionLocked(cs.curSession);
1986 cs.curSession = null;
1987 cs.sessionRequested = false;
1988 }
1989
1990 private void finishSessionLocked(SessionState sessionState) {
1991 if (sessionState != null) {
1992 if (sessionState.session != null) {
1993 try {
1994 sessionState.session.finishSession();
1995 } catch (RemoteException e) {
1996 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001997 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001998 }
1999 sessionState.session = null;
2000 }
2001 if (sessionState.channel != null) {
2002 sessionState.channel.dispose();
2003 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002004 }
2005 }
2006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002007
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002008 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 if (mCurMethod != null) {
2010 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002011 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002013
Jeff Brownc28867a2013-03-26 15:42:39 -07002014 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002015 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 mCurMethod = null;
2017 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002018 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002019 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002020 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002021 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002023
satoke7c6998e2011-06-03 17:57:59 +09002024 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002026 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2027 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2028 // which is irregular but can eventually happen for everyone just by continuing using the
2029 // device. Thus it is important to make sure that all the internal states are properly
2030 // refreshed when this method is called back. Running
2031 // adb install -r <APK that implements the current IME>
2032 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002034 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 + " mCurIntent=" + mCurIntent);
2036 if (mCurMethod != null && mCurIntent != null
2037 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002038 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 // We consider this to be a new bind attempt, since the system
2040 // should now try to restart the service for us.
2041 mLastBindTime = SystemClock.uptimeMillis();
2042 mShowRequested = mInputShown;
2043 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002044 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
2046 }
2047 }
2048
satokf9f01002011-05-19 21:31:50 +09002049 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002051 synchronized (mMethodMap) {
2052 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002053 return;
2054 }
2055 final long ident = Binder.clearCallingIdentity();
2056 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002058 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002059 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002060 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002063 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002064 CharSequence contentDescription = null;
2065 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002066 // Use PackageManager to load label
2067 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002068 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002069 mIPackageManager.getApplicationInfo(packageName, 0,
2070 mSettings.getCurrentUserId()));
2071 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002072 /* ignore */
2073 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002074 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002075 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002076 contentDescription != null
2077 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002078 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002081 } finally {
2082 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
2085 }
2086
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002087 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002088 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002089 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09002090 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002091 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002092 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002093 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2094 // When physical keyboard is attached, we show the ime switcher (or notification if
2095 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2096 // exists in the IME switcher dialog. Might be OK to remove this condition once
2097 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2098 return true;
2099 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002100 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2101 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002102 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002103
2104 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2105 final int N = imis.size();
2106 if (N > 2) return true;
2107 if (N < 1) return false;
2108 int nonAuxCount = 0;
2109 int auxCount = 0;
2110 InputMethodSubtype nonAuxSubtype = null;
2111 InputMethodSubtype auxSubtype = null;
2112 for(int i = 0; i < N; ++i) {
2113 final InputMethodInfo imi = imis.get(i);
2114 final List<InputMethodSubtype> subtypes =
2115 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2116 final int subtypeCount = subtypes.size();
2117 if (subtypeCount == 0) {
2118 ++nonAuxCount;
2119 } else {
2120 for (int j = 0; j < subtypeCount; ++j) {
2121 final InputMethodSubtype subtype = subtypes.get(j);
2122 if (!subtype.isAuxiliary()) {
2123 ++nonAuxCount;
2124 nonAuxSubtype = subtype;
2125 } else {
2126 ++auxCount;
2127 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002128 }
2129 }
satok7cfc0ed2011-06-20 21:29:36 +09002130 }
2131 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002132 if (nonAuxCount > 1 || auxCount > 1) {
2133 return true;
2134 } else if (nonAuxCount == 1 && auxCount == 1) {
2135 if (nonAuxSubtype != null && auxSubtype != null
2136 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2137 || auxSubtype.overridesImplicitlyEnabledSubtype()
2138 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2139 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2140 return false;
2141 }
2142 return true;
2143 }
2144 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002145 }
2146
John Spurlocke0980502013-10-25 11:59:29 -04002147 private boolean isKeyguardLocked() {
2148 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2149 }
2150
Yohei Yukawad6475a62017-04-17 10:35:27 -07002151 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002152 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002153 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002154 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2155 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002156 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002157 return;
2158 }
2159
Yohei Yukawa69e68022017-02-13 12:04:50 -08002160 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002161 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002162 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002163 mImeWindowVis = vis;
2164 mBackDisposition = backDisposition;
2165 updateSystemUiLocked(token, vis, backDisposition);
2166 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002167
2168 final boolean dismissImeOnBackKeyPressed;
2169 switch (backDisposition) {
2170 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2171 dismissImeOnBackKeyPressed = true;
2172 break;
2173 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2174 dismissImeOnBackKeyPressed = false;
2175 break;
2176 default:
2177 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2178 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2179 break;
2180 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002181 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2182 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawad6475a62017-04-17 10:35:27 -07002183 dismissImeOnBackKeyPressed, info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002184 }
2185
2186 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2187 synchronized (mMethodMap) {
2188 updateSystemUiLocked(token, vis, backDisposition);
2189 }
2190 }
2191
2192 // Caution! This method is called in this class. Handle multi-user carefully
2193 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2194 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002195 return;
2196 }
2197
2198 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2199 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002200 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002201 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002202 // apply policy for binder calls
2203 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2204 vis = 0;
satok06487a52010-10-29 11:37:18 +09002205 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002206 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2207 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2208 if (mStatusBar != null) {
2209 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2210 needsToShowImeSwitcher);
2211 }
2212 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2213 if (imi != null && needsToShowImeSwitcher) {
2214 // Used to load label
2215 final CharSequence title = mRes.getText(
2216 com.android.internal.R.string.select_input_method);
2217 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2218 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002219 mImeSwitcherNotification.setContentTitle(title)
2220 .setContentText(summary)
2221 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002222 try {
2223 if ((mNotificationManager != null)
2224 && !mIWindowManager.hasNavigationBar()) {
2225 if (DEBUG) {
2226 Slog.d(TAG, "--- show notification: label = " + summary);
2227 }
2228 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002229 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002230 mImeSwitcherNotification.build(), UserHandle.ALL);
2231 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002232 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002233 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002234 }
2235 } else {
2236 if (mNotificationShown && mNotificationManager != null) {
2237 if (DEBUG) {
2238 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002239 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002240 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002241 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002242 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002243 }
satok06487a52010-10-29 11:37:18 +09002244 }
2245 } finally {
2246 Binder.restoreCallingIdentity(ident);
2247 }
2248 }
2249
satoke7c6998e2011-06-03 17:57:59 +09002250 @Override
satokf9f01002011-05-19 21:31:50 +09002251 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002252 if (!calledFromValidUser()) {
2253 return;
2254 }
satokf9f01002011-05-19 21:31:50 +09002255 synchronized (mMethodMap) {
2256 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2257 for (int i = 0; i < spans.length; ++i) {
2258 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002259 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002260 mSecureSuggestionSpans.put(ss, currentImi);
2261 }
2262 }
2263 }
2264 }
2265
satoke7c6998e2011-06-03 17:57:59 +09002266 @Override
satokf9f01002011-05-19 21:31:50 +09002267 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002268 if (!calledFromValidUser()) {
2269 return false;
2270 }
satokf9f01002011-05-19 21:31:50 +09002271 synchronized (mMethodMap) {
2272 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2273 // TODO: Do not send the intent if the process of the targetImi is already dead.
2274 if (targetImi != null) {
2275 final String[] suggestions = span.getSuggestions();
2276 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002277 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002278 final Intent intent = new Intent();
2279 // Ensures that only a class in the original IME package will receive the
2280 // notification.
satok42c5a162011-05-26 16:46:14 +09002281 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002282 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2283 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2284 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2285 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002286 final long ident = Binder.clearCallingIdentity();
2287 try {
2288 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2289 } finally {
2290 Binder.restoreCallingIdentity(ident);
2291 }
satokf9f01002011-05-19 21:31:50 +09002292 return true;
2293 }
2294 }
2295 return false;
2296 }
2297
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002298 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002299 updateInputMethodsFromSettingsLocked(enabledMayChange);
2300 updateKeyboardFromSettingsLocked();
2301 }
2302
2303 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002304 if (enabledMayChange) {
2305 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2306 for (int i=0; i<enabled.size(); i++) {
2307 // We allow the user to select "disabled until used" apps, so if they
2308 // are enabling one of those here we now need to make it enabled.
2309 InputMethodInfo imm = enabled.get(i);
2310 try {
2311 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2312 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2313 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002314 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002315 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002316 if (DEBUG) {
2317 Slog.d(TAG, "Update state(" + imm.getId()
2318 + "): DISABLED_UNTIL_USED -> DEFAULT");
2319 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002320 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2321 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002322 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2323 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002324 }
2325 } catch (RemoteException e) {
2326 }
2327 }
2328 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002329 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2330 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2331 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2332 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002333 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002334 // There is no input method selected, try to choose new applicable input method.
2335 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002336 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002337 }
2338 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002340 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002342 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002343 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 }
satokf3db1af2010-11-23 13:34:33 +09002345 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002346 } else {
2347 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002348 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002350 // Here is not the perfect place to reset the switching controller. Ideally
2351 // mSwitchingController and mSettings should be able to share the same state.
2352 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2353 // the same enabled IMEs list.
2354 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002355
2356 }
2357
2358 public void updateKeyboardFromSettingsLocked() {
2359 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2360 if (mSwitchingDialog != null
2361 && mSwitchingDialogTitleView != null
2362 && mSwitchingDialog.isShowing()) {
2363 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2364 com.android.internal.R.id.hard_keyboard_switch);
2365 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002368
Yohei Yukawab097b822015-12-01 10:43:08 -08002369 private void notifyInputMethodSubtypeChanged(final int userId,
2370 @Nullable final InputMethodInfo inputMethodInfo,
2371 @Nullable final InputMethodSubtype subtype) {
2372 final InputManagerInternal inputManagerInternal =
2373 LocalServices.getService(InputManagerInternal.class);
2374 if (inputManagerInternal != null) {
2375 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2376 }
2377 }
2378
satokab751aa2010-09-14 19:17:36 +09002379 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 InputMethodInfo info = mMethodMap.get(id);
2381 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002382 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002384
satokd81e9502012-05-21 12:58:45 +09002385 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002387 final int subtypeCount = info.getSubtypeCount();
2388 if (subtypeCount <= 0) {
2389 return;
satokcd7cd292010-11-20 15:46:23 +09002390 }
satokd81e9502012-05-21 12:58:45 +09002391 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2392 final InputMethodSubtype newSubtype;
2393 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2394 newSubtype = info.getSubtypeAt(subtypeId);
2395 } else {
2396 // If subtype is null, try to find the most applicable one from
2397 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002398 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002399 }
2400 if (newSubtype == null || oldSubtype == null) {
2401 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2402 + ", new subtype = " + newSubtype);
2403 return;
2404 }
2405 if (newSubtype != oldSubtype) {
2406 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2407 if (mCurMethod != null) {
2408 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002409 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002410 mCurMethod.changeInputMethodSubtype(newSubtype);
2411 } catch (RemoteException e) {
2412 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002413 return;
satokab751aa2010-09-14 19:17:36 +09002414 }
2415 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002416 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 return;
2419 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002420
satokd81e9502012-05-21 12:58:45 +09002421 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 final long ident = Binder.clearCallingIdentity();
2423 try {
satokab751aa2010-09-14 19:17:36 +09002424 // Set a subtype to this input method.
2425 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002426 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2427 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2428 // because mCurMethodId is stored as a history in
2429 // setSelectedInputMethodAndSubtypeLocked().
2430 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002432 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002434 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002436 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002438 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 } finally {
2440 Binder.restoreCallingIdentity(ident);
2441 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002442
2443 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2444 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002446
satok42c5a162011-05-26 16:46:14 +09002447 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002448 public boolean showSoftInput(IInputMethodClient client, int flags,
2449 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002450 if (!calledFromValidUser()) {
2451 return false;
2452 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002453 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 long ident = Binder.clearCallingIdentity();
2455 try {
2456 synchronized (mMethodMap) {
2457 if (mCurClient == null || client == null
2458 || mCurClient.client.asBinder() != client.asBinder()) {
2459 try {
2460 // We need to check if this is the current client with
2461 // focus in the window manager, to allow this call to
2462 // be made before input is started in it.
2463 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002464 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002465 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 }
2467 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002468 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 }
2470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002471
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002472 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002473 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
2475 } finally {
2476 Binder.restoreCallingIdentity(ident);
2477 }
2478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002479
The Android Open Source Project4df24232009-03-05 14:34:35 -08002480 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002482 if (mAccessibilityRequestingNoSoftKeyboard) {
2483 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 }
Anna Galusza9b278112016-01-04 11:37:37 -08002485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2487 mShowExplicitlyRequested = true;
2488 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002489 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2490 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002492
Dianne Hackborncc278702009-09-02 23:07:23 -07002493 if (!mSystemReady) {
2494 return false;
2495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002496
The Android Open Source Project4df24232009-03-05 14:34:35 -08002497 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002499 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002500 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2501 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2502 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002504 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002505 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002506 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002507 mVisibleBound = true;
2508 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002509 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002511 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 // The client has asked to have the input method shown, but
2513 // we have been sitting here too long with a connection to the
2514 // service and no interface received, so let's disconnect/connect
2515 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002516 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002518 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002520 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002521 } else {
2522 if (DEBUG) {
2523 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2524 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002527
The Android Open Source Project4df24232009-03-05 14:34:35 -08002528 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002530
satok42c5a162011-05-26 16:46:14 +09002531 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002532 public boolean hideSoftInput(IInputMethodClient client, int flags,
2533 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002534 if (!calledFromValidUser()) {
2535 return false;
2536 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002537 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 long ident = Binder.clearCallingIdentity();
2539 try {
2540 synchronized (mMethodMap) {
2541 if (mCurClient == null || client == null
2542 || mCurClient.client.asBinder() != client.asBinder()) {
2543 try {
2544 // We need to check if this is the current client with
2545 // focus in the window manager, to allow this call to
2546 // be made before input is started in it.
2547 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002548 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2549 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002550 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 }
2552 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002553 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 }
2555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002556
Joe Onorato8a9b2202010-02-26 18:56:32 -08002557 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002558 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 }
2560 } finally {
2561 Binder.restoreCallingIdentity(ident);
2562 }
2563 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002564
The Android Open Source Project4df24232009-03-05 14:34:35 -08002565 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2567 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002568 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 -08002569 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 }
2571 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002572 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 -08002573 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002575
2576 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2577 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2578 // to be updated with the new value sent from IME process. Even in such a transient state
2579 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2580 // application process as a valid request, and have even promised such a behavior with CTS
2581 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2582 // IMMS#InputShown indicates that the software keyboard is shown.
2583 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2584 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2585 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002586 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002587 if (shouldHideSoftInput) {
2588 // The IME will report its visible state again after the following message finally
2589 // delivered to the IME process as an IPC. Hence the inconsistency between
2590 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2591 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002592 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2593 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2594 res = true;
2595 } else {
2596 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002598 if (mHaveConnection && mVisibleBound) {
2599 mContext.unbindService(mVisibleConnection);
2600 mVisibleBound = false;
2601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 mInputShown = false;
2603 mShowRequested = false;
2604 mShowExplicitlyRequested = false;
2605 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002606 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002608
satok42c5a162011-05-26 16:46:14 +09002609 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002610 public InputBindResult startInputOrWindowGainedFocus(
2611 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2612 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002613 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002614 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002615 if (windowToken != null) {
2616 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002617 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002618 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002619 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2620 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002621 }
2622 }
2623
2624 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002625 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002626 IInputMethodClient client, IBinder windowToken, int controlFlags,
2627 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002628 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2629 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002630 // Needs to check the validity before clearing calling identity
2631 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002632 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 long ident = Binder.clearCallingIdentity();
2634 try {
2635 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002636 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2637 + InputMethodClient.getStartInputReason(startInputReason)
2638 + " client=" + client.asBinder()
2639 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002640 + " missingMethods="
2641 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002642 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002643 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002644 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002645 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002646
Dianne Hackborn7663d802012-02-24 13:08:49 -08002647 ClientState cs = mClients.get(client.asBinder());
2648 if (cs == null) {
2649 throw new IllegalArgumentException("unknown client "
2650 + client.asBinder());
2651 }
2652
2653 try {
2654 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2655 // Check with the window manager to make sure this client actually
2656 // has a window with focus. If not, reject. This is thread safe
2657 // because if the focus changes some time before or after, the
2658 // next client receiving focus that has any interest in input will
2659 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002660 if (DEBUG) {
2661 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2662 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2663 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002664 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002666 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002668
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002669 if (!calledFromValidUser) {
2670 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2671 Slog.w(TAG, "If you want to interect with IME, you need "
2672 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2673 hideCurrentInputLocked(0, null);
2674 return null;
2675 }
2676
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002677 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002678 if (DEBUG) {
2679 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2680 + " attribute=" + attribute + ", token = " + windowToken);
2681 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002682 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002683 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002684 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002685 }
2686 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002687 }
2688 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002689 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002690 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002691
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002692 // Should we auto-show the IME even if the caller has not
2693 // specified what should be done with it?
2694 // We only do this automatically if the window can resize
2695 // to accommodate the IME (so what the user sees will give
2696 // them good context without input information being obscured
2697 // by the IME) or if running on a large screen where there
2698 // is more room for the target window + IME.
2699 final boolean doAutoShow =
2700 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2701 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2702 || mRes.getConfiguration().isLayoutSizeAtLeast(
2703 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002704 final boolean isTextEditor =
2705 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2706
2707 // We want to start input before showing the IME, but after closing
2708 // it. We want to do this after closing it to help the IME disappear
2709 // more quickly (not get stuck behind it initializing itself for the
2710 // new focused input, even if its window wants to hide the IME).
2711 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2714 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002715 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2717 // There is no focus view, and this window will
2718 // be behind any soft input window, so hide the
2719 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002720 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002721 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002723 } else if (isTextEditor && doAutoShow && (softInputMode &
2724 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 // There is a focus view, and we are navigating forward
2726 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002727 // We only do this automatically if the window can resize
2728 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002729 // them good context without input information being obscured
2730 // by the IME) or if running on a large screen where there
2731 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002732 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002733 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002734 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002735 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002736 didStart = true;
2737 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002738 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740 break;
2741 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2742 // Do nothing.
2743 break;
2744 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2745 if ((softInputMode &
2746 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002747 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002748 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
2750 break;
2751 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002752 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002753 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 break;
2755 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2756 if ((softInputMode &
2757 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002758 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002759 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002760 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002761 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002762 didStart = true;
2763 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002764 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 }
2766 break;
2767 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002768 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002769 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002770 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002771 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002772 didStart = true;
2773 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002774 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 break;
2776 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002777
2778 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002779 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002780 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 }
2783 } finally {
2784 Binder.restoreCallingIdentity(ident);
2785 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002786
2787 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002789
satok42c5a162011-05-26 16:46:14 +09002790 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002791 public void showInputMethodPickerFromClient(
2792 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002793 if (!calledFromValidUser()) {
2794 return;
2795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 synchronized (mMethodMap) {
2797 if (mCurClient == null || client == null
2798 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002799 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002800 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802
satok440aab52010-11-25 09:43:11 +09002803 // Always call subtype picker, because subtype picker is a superset of input method
2804 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002805 mHandler.sendMessage(mCaller.obtainMessageI(
2806 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002807 }
2808 }
2809
satok42c5a162011-05-26 16:46:14 +09002810 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002812 if (!calledFromValidUser()) {
2813 return;
2814 }
satok28203512010-11-24 11:06:49 +09002815 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2816 }
2817
satok42c5a162011-05-26 16:46:14 +09002818 @Override
satok28203512010-11-24 11:06:49 +09002819 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002820 if (!calledFromValidUser()) {
2821 return;
2822 }
satok28203512010-11-24 11:06:49 +09002823 synchronized (mMethodMap) {
2824 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002825 setInputMethodWithSubtypeIdLocked(token, id,
2826 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2827 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002828 } else {
2829 setInputMethod(token, id);
2830 }
2831 }
satokab751aa2010-09-14 19:17:36 +09002832 }
2833
satok42c5a162011-05-26 16:46:14 +09002834 @Override
satokb416a712010-11-25 20:42:14 +09002835 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002836 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002837 if (!calledFromValidUser()) {
2838 return;
2839 }
satokb416a712010-11-25 20:42:14 +09002840 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002841 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2842 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002843 }
2844 }
2845
satok4fc87d62011-05-20 16:13:43 +09002846 @Override
satok735cf382010-11-11 20:40:09 +09002847 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002848 if (!calledFromValidUser()) {
2849 return false;
2850 }
satok735cf382010-11-11 20:40:09 +09002851 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002852 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002853 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002854 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002855 lastImi = mMethodMap.get(lastIme.first);
2856 } else {
2857 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002858 }
satok4fc87d62011-05-20 16:13:43 +09002859 String targetLastImiId = null;
2860 int subtypeId = NOT_A_SUBTYPE_ID;
2861 if (lastIme != null && lastImi != null) {
2862 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002863 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002864 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2865 : mCurrentSubtype.hashCode();
2866 // If the last IME is the same as the current IME and the last subtype is not
2867 // defined, there is no need to switch to the last IME.
2868 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2869 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002870 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002871 }
2872 }
2873
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002874 if (TextUtils.isEmpty(targetLastImiId)
2875 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002876 // This is a safety net. If the currentSubtype can't be added to the history
2877 // and the framework couldn't find the last ime, we will make the last ime be
2878 // the most applicable enabled keyboard subtype of the system imes.
2879 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2880 if (enabled != null) {
2881 final int N = enabled.size();
2882 final String locale = mCurrentSubtype == null
2883 ? mRes.getConfiguration().locale.toString()
2884 : mCurrentSubtype.getLocale();
2885 for (int i = 0; i < N; ++i) {
2886 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002887 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002888 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002889 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2890 InputMethodUtils.getSubtypes(imi),
2891 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002892 if (keyboardSubtype != null) {
2893 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002894 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002895 imi, keyboardSubtype.hashCode());
2896 if(keyboardSubtype.getLocale().equals(locale)) {
2897 break;
2898 }
2899 }
2900 }
2901 }
2902 }
2903 }
2904
2905 if (!TextUtils.isEmpty(targetLastImiId)) {
2906 if (DEBUG) {
2907 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2908 + ", from: " + mCurMethodId + ", " + subtypeId);
2909 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002910 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002911 return true;
2912 } else {
2913 return false;
2914 }
satok735cf382010-11-11 20:40:09 +09002915 }
2916 }
2917
satoke7c6998e2011-06-03 17:57:59 +09002918 @Override
satok688bd472012-02-09 20:09:17 +09002919 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002920 if (!calledFromValidUser()) {
2921 return false;
2922 }
satok688bd472012-02-09 20:09:17 +09002923 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002924 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002925 return false;
2926 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002927 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002928 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2929 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002930 if (nextSubtype == null) {
2931 return false;
2932 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002933 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2934 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002935 return true;
2936 }
2937 }
2938
2939 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002940 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2941 if (!calledFromValidUser()) {
2942 return false;
2943 }
2944 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002945 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002946 return false;
2947 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002948 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002949 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2950 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002951 if (nextSubtype == null) {
2952 return false;
2953 }
2954 return true;
2955 }
2956 }
2957
2958 @Override
satok68f1b782011-04-11 14:26:04 +09002959 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002960 if (!calledFromValidUser()) {
2961 return null;
2962 }
satok68f1b782011-04-11 14:26:04 +09002963 synchronized (mMethodMap) {
2964 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2965 // TODO: Handle the case of the last IME with no subtypes
2966 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2967 || TextUtils.isEmpty(lastIme.second)) return null;
2968 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2969 if (lastImi == null) return null;
2970 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002971 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002972 final int lastSubtypeId =
2973 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002974 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2975 return null;
2976 }
2977 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002978 } catch (NumberFormatException e) {
2979 return null;
2980 }
2981 }
2982 }
2983
satoke7c6998e2011-06-03 17:57:59 +09002984 @Override
satokee5e77c2011-09-02 18:50:15 +09002985 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002986 if (!calledFromValidUser()) {
2987 return;
2988 }
satok91e88122011-07-18 11:11:42 +09002989 // By this IPC call, only a process which shares the same uid with the IME can add
2990 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002991 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002992 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002993 if (!mSystemReady) {
2994 return;
2995 }
satok91e88122011-07-18 11:11:42 +09002996 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002997 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002998 final String[] packageInfos;
2999 try {
3000 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3001 } catch (RemoteException e) {
3002 Slog.e(TAG, "Failed to get package infos");
3003 return;
3004 }
satok91e88122011-07-18 11:11:42 +09003005 if (packageInfos != null) {
3006 final int packageNum = packageInfos.length;
3007 for (int i = 0; i < packageNum; ++i) {
3008 if (packageInfos[i].equals(imi.getPackageName())) {
3009 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09003010 final long ident = Binder.clearCallingIdentity();
3011 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003012 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003013 } finally {
3014 Binder.restoreCallingIdentity(ident);
3015 }
satokee5e77c2011-09-02 18:50:15 +09003016 return;
satok91e88122011-07-18 11:11:42 +09003017 }
3018 }
3019 }
satoke7c6998e2011-06-03 17:57:59 +09003020 }
satokee5e77c2011-09-02 18:50:15 +09003021 return;
satoke7c6998e2011-06-03 17:57:59 +09003022 }
3023
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003024 @Override
3025 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003026 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003027 }
3028
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003029 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003030 public void clearLastInputMethodWindowForTransition(IBinder token) {
3031 if (!calledFromValidUser()) {
3032 return;
3033 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003034 synchronized (mMethodMap) {
3035 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003036 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003037 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003038 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003039 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003040 }
3041
3042 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003043 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003044 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003045 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003046 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003047 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003048 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3049 if (DEBUG) {
3050 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3051 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3052 + " actual: " + sequenceNumber);
3053 }
3054 return;
3055 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003056 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3057 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003058 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003059 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003060 }
3061 }
3062
satok28203512010-11-24 11:06:49 +09003063 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003065 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3066 }
3067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003069 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3070 if (token == null) {
3071 if (mContext.checkCallingOrSelfPermission(
3072 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3073 != PackageManager.PERMISSION_GRANTED) {
3074 throw new SecurityException(
3075 "Using null token requires permission "
3076 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003078 } else if (mCurToken != token) {
3079 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3080 + " token: " + token);
3081 return;
3082 }
3083
3084 final long ident = Binder.clearCallingIdentity();
3085 try {
3086 setInputMethodLocked(id, subtypeId);
3087 } finally {
3088 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
3090 }
3091
satok42c5a162011-05-26 16:46:14 +09003092 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003094 if (!calledFromValidUser()) {
3095 return;
3096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003098 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 return;
3100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 long ident = Binder.clearCallingIdentity();
3102 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003103 hideCurrentInputLocked(flags, null);
3104 } finally {
3105 Binder.restoreCallingIdentity(ident);
3106 }
3107 }
3108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003109
satok42c5a162011-05-26 16:46:14 +09003110 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003111 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003112 if (!calledFromValidUser()) {
3113 return;
3114 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003115 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003116 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003117 return;
3118 }
3119 long ident = Binder.clearCallingIdentity();
3120 try {
3121 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 } finally {
3123 Binder.restoreCallingIdentity(ident);
3124 }
3125 }
3126 }
3127
3128 void setEnabledSessionInMainThread(SessionState session) {
3129 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003130 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003132 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003133 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 } catch (RemoteException e) {
3135 }
3136 }
3137 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003138 if (mEnabledSession != null && mEnabledSession.session != null) {
3139 try {
3140 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3141 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3142 } catch (RemoteException e) {
3143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 }
3145 }
3146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003147
satok42c5a162011-05-26 16:46:14 +09003148 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003150 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003152 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003153 final boolean showAuxSubtypes;
3154 switch (msg.arg1) {
3155 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3156 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3157 // implemented so that auxiliary subtypes will be excluded when the soft
3158 // keyboard is invisible.
3159 showAuxSubtypes = mInputShown;
3160 break;
3161 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3162 showAuxSubtypes = true;
3163 break;
3164 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3165 showAuxSubtypes = false;
3166 break;
3167 default:
3168 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3169 return false;
3170 }
3171 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003172 return true;
3173
satok47a44912010-10-06 16:03:58 +09003174 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003175 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003176 return true;
3177
3178 case MSG_SHOW_IM_CONFIG:
3179 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003180 return true;
3181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 case MSG_UNBIND_INPUT:
3185 try {
3186 ((IInputMethod)msg.obj).unbindInput();
3187 } catch (RemoteException e) {
3188 // There is nothing interesting about the method dying.
3189 }
3190 return true;
3191 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003192 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 try {
3194 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3195 } catch (RemoteException e) {
3196 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003197 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 return true;
3199 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003200 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003202 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003203 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003204 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 } catch (RemoteException e) {
3206 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003207 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 return true;
3209 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003210 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003212 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003213 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003214 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 } catch (RemoteException e) {
3216 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003217 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003219 case MSG_HIDE_CURRENT_INPUT_METHOD:
3220 synchronized (mMethodMap) {
3221 hideCurrentInputLocked(0, null);
3222 }
3223 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003225 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003227 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3229 } catch (RemoteException e) {
3230 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003231 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003233 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003234 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003235 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003236 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003238 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003240 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003241 // Dispose the channel if the input method is not local to this process
3242 // because the remote proxy will get its own copy when unparceled.
3243 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003244 channel.dispose();
3245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003247 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003251
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003252 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003253 final int missingMethods = msg.arg1;
3254 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003255 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003256 final IBinder startInputToken = (IBinder) args.arg1;
3257 final SessionState session = (SessionState) args.arg2;
3258 final IInputContext inputContext = (IInputContext) args.arg3;
3259 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003262 session.method.startInput(startInputToken, inputContext, missingMethods,
3263 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 } catch (RemoteException e) {
3265 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003266 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003271
Yohei Yukawa33e81792015-11-17 21:14:42 -08003272 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003274 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 } catch (RemoteException e) {
3276 // There is nothing interesting about the last client dying.
3277 }
3278 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003279 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003280 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003281 IInputMethodClient client = (IInputMethodClient)args.arg1;
3282 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003284 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003286 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003287 } finally {
3288 // Dispose the channel if the input method is not local to this process
3289 // because the remote proxy will get its own copy when unparceled.
3290 if (res.channel != null && Binder.isProxy(client)) {
3291 res.channel.dispose();
3292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003294 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003296 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003297 case MSG_SET_ACTIVE:
3298 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003299 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003300 } catch (RemoteException e) {
3301 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3302 + ((ClientState)msg.obj).pid + " uid "
3303 + ((ClientState)msg.obj).uid);
3304 }
3305 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003306 case MSG_SET_INTERACTIVE:
3307 handleSetInteractive(msg.arg1 != 0);
3308 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003309 case MSG_SWITCH_IME:
3310 handleSwitchInputMethod(msg.arg1 != 0);
3311 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003312 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3313 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003314 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003315 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003316 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003317 } catch (RemoteException e) {
3318 Slog.w(TAG, "Got RemoteException sending "
3319 + "setUserActionNotificationSequenceNumber("
3320 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003321 + clientState.pid + " uid "
3322 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003323 }
3324 return true;
3325 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003326 case MSG_REPORT_FULLSCREEN_MODE: {
3327 final boolean fullscreen = msg.arg1 != 0;
3328 final ClientState clientState = (ClientState)msg.obj;
3329 try {
3330 clientState.client.reportFullscreenMode(fullscreen);
3331 } catch (RemoteException e) {
3332 Slog.w(TAG, "Got RemoteException sending "
3333 + "reportFullscreen(" + fullscreen + ") notification to pid="
3334 + clientState.pid + " uid=" + clientState.uid);
3335 }
3336 return true;
3337 }
satok01038492012-04-09 21:08:27 +09003338
3339 // --------------------------------------------------------------
3340 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003341 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003342 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003343 case MSG_SYSTEM_UNLOCK_USER:
3344 final int userId = msg.arg1;
3345 onUnlockUser(userId);
3346 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 }
3348 return false;
3349 }
3350
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003351 private void handleSetInteractive(final boolean interactive) {
3352 synchronized (mMethodMap) {
3353 mIsInteractive = interactive;
3354 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3355
3356 // Inform the current client of the change in active status
3357 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003358 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3359 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3360 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003361 }
3362 }
3363 }
3364
Yohei Yukawaae61f712015-12-09 13:00:10 -08003365 private void handleSwitchInputMethod(final boolean forwardDirection) {
3366 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003367 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003368 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003369 if (nextSubtype == null) {
3370 return;
3371 }
3372 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003373 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3374 if (newInputMethodInfo == null) {
3375 return;
3376 }
3377 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3378 newInputMethodInfo, mCurrentSubtype);
3379 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003380 if (mSubtypeSwitchedByShortCutToast == null) {
3381 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3382 Toast.LENGTH_SHORT);
3383 } else {
3384 mSubtypeSwitchedByShortCutToast.setText(toastText);
3385 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003386 mSubtypeSwitchedByShortCutToast.show();
3387 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003388 }
3389 }
3390
satokdc9ddae2011-10-06 12:22:36 +09003391 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003392 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3393 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003394 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003395 if (DEBUG) {
3396 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3397 }
satok723a27e2010-11-11 14:58:11 +09003398 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003399 return true;
3400 }
3401
3402 return false;
3403 }
3404
Yohei Yukawa94e33302016-02-12 19:37:03 -08003405 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003406 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003407 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003408 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003409 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003410 if (!mSystemReady) {
3411 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3412 return;
3413 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003414 mMethodList.clear();
3415 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003416 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003417 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003418
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003419 // Use for queryIntentServicesAsUser
3420 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003422 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3423 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3424 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003425 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003427 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3428 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003429
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003430 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003431 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 for (int i = 0; i < services.size(); ++i) {
3433 ResolveInfo ri = services.get(i);
3434 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003435 final String imeId = InputMethodInfo.computeId(ri);
3436 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3437 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 + ": it does not require the permission "
3439 + android.Manifest.permission.BIND_INPUT_METHOD);
3440 continue;
3441 }
3442
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003443 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003445 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 try {
satoke7c6998e2011-06-03 17:57:59 +09003447 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003448 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003449 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003450 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451
3452 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003453 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003455 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003456 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 }
3458 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003459
Yohei Yukawac4e44912017-02-09 19:30:22 -08003460 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3461 // negatives when the package state remains to be the same but only the component state is
3462 // changed.
3463 {
3464 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3465 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3466 // conservative, but it seems we cannot use it for now (Issue 35176630).
3467 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3468 new Intent(InputMethod.SERVICE_INTERFACE),
3469 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3470 final int N = allInputMethodServices.size();
3471 for (int i = 0; i < N; ++i) {
3472 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003473 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3474 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003475 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003476 }
3477 }
3478
Yohei Yukawa859df052016-02-17 07:56:46 -08003479 // TODO: The following code should find better place to live.
3480 if (!resetDefaultEnabledIme) {
3481 boolean enabledImeFound = false;
3482 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3483 final int N = enabledImes.size();
3484 for (int i = 0; i < N; ++i) {
3485 final InputMethodInfo imi = enabledImes.get(i);
3486 if (mMethodList.contains(imi)) {
3487 enabledImeFound = true;
3488 break;
3489 }
3490 }
3491 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003492 if (DEBUG) {
3493 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3494 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003495 resetDefaultEnabledIme = true;
3496 resetSelectedInputMethodAndSubtypeLocked("");
3497 }
3498 }
3499
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003500 if (resetDefaultEnabledIme) {
3501 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003502 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003503 final int N = defaultEnabledIme.size();
3504 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003505 final InputMethodInfo imi = defaultEnabledIme.get(i);
3506 if (DEBUG) {
3507 Slog.d(TAG, "--- enable ime = " + imi);
3508 }
3509 setInputMethodEnabledLocked(imi.getId(), true);
3510 }
3511 }
3512
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003513 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003514 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003515 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003516 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3517 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003518 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003519 }
3520 } else {
3521 // Double check that the default IME is certainly enabled.
3522 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003523 }
3524 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003525 // Here is not the perfect place to reset the switching controller. Ideally
3526 // mSwitchingController and mSettings should be able to share the same state.
3527 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3528 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003529 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003533
satok217f5482010-12-15 05:19:19 +09003534 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003535 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003536 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003537 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3538 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003539 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003540 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003541 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003542 final int userId;
3543 synchronized (mMethodMap) {
3544 userId = mSettings.getCurrentUserId();
3545 }
3546 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003547 }
3548
3549 private void showConfigureInputMethods() {
3550 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3551 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3552 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3553 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003554 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003555 }
3556
satok2c93efc2012-04-02 19:33:47 +09003557 private boolean isScreenLocked() {
3558 return mKeyguardManager != null
3559 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3560 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003561
Seigo Nonaka14e13912015-05-06 21:04:13 -07003562 private void showInputMethodMenu(boolean showAuxSubtypes) {
3563 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564
satok2c93efc2012-04-02 19:33:47 +09003565 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003566
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003567 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003568 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003569 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003570
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003571 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003572 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003573 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3574 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003575 if (immis == null || immis.size() == 0) {
3576 return;
3577 }
3578
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003579 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580
satok688bd472012-02-09 20:09:17 +09003581 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003582 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003583 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003584
satokc3690562012-01-10 20:14:43 +09003585 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003586 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003587 if (currentSubtype != null) {
3588 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003589 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3590 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003591 }
3592 }
3593
Ken Wakasa761eb372011-03-04 19:06:18 +09003594 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003595 mIms = new InputMethodInfo[N];
3596 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003597 int checkedItem = 0;
3598 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003599 final ImeSubtypeListItem item = imList.get(i);
3600 mIms[i] = item.mImi;
3601 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003602 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003603 int subtypeId = mSubtypeIds[i];
3604 if ((subtypeId == NOT_A_SUBTYPE_ID)
3605 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3606 || (subtypeId == lastInputMethodSubtypeId)) {
3607 checkedItem = i;
3608 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003611
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07003612 final Context settingsContext = new ContextThemeWrapper(
3613 ActivityThread.currentActivityThread().getSystemUiContext(),
Alan Viverette505e3ab2014-11-24 15:22:11 -08003614 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3615
3616 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003617 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3618 @Override
3619 public void onCancel(DialogInterface dialog) {
3620 hideInputMethodMenu();
3621 }
3622 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003623
3624 final Context dialogContext = mDialogBuilder.getContext();
3625 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3626 com.android.internal.R.styleable.DialogPreference,
3627 com.android.internal.R.attr.alertDialogStyle, 0);
3628 final Drawable dialogIcon = a.getDrawable(
3629 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3630 a.recycle();
3631
3632 mDialogBuilder.setIcon(dialogIcon);
3633
Yohei Yukawad34e1482016-02-11 08:03:52 -08003634 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003635 final View tv = inflater.inflate(
3636 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3637 mDialogBuilder.setCustomTitle(tv);
3638
3639 // Setup layout for a toggle switch of the hardware keyboard
3640 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003641 mSwitchingDialogTitleView
3642 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003643 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003644 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003645 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003646 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003647 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003648 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3649 @Override
3650 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003651 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003652 // Ensure that the input method dialog is dismissed when changing
3653 // the hardware keyboard state.
3654 hideInputMethodMenu();
3655 }
3656 });
3657
Alan Viverette505e3ab2014-11-24 15:22:11 -08003658 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003659 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3660 final OnClickListener choiceListener = new OnClickListener() {
3661 @Override
3662 public void onClick(final DialogInterface dialog, final int which) {
3663 synchronized (mMethodMap) {
3664 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3665 || mSubtypeIds.length <= which) {
3666 return;
satok01038492012-04-09 21:08:27 +09003667 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003668 final InputMethodInfo im = mIms[which];
3669 int subtypeId = mSubtypeIds[which];
3670 adapter.mCheckedItem = which;
3671 adapter.notifyDataSetChanged();
3672 hideInputMethodMenu();
3673 if (im != null) {
3674 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3675 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003676 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003677 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003679 }
3680 }
3681 };
3682 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003685 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003686 final Window w = mSwitchingDialog.getWindow();
3687 final WindowManager.LayoutParams attrs = w.getAttributes();
3688 w.setType(TYPE_INPUT_METHOD_DIALOG);
3689 // Use an alternate token for the dialog for that window manager can group the token
3690 // with other IME windows based on type vs. grouping based on whichever token happens
3691 // to get selected by the system later on.
3692 attrs.token = mSwitchingDialogToken;
3693 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3694 attrs.setTitle("Select input method");
3695 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003696 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 mSwitchingDialog.show();
3698 }
3699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003700
Ken Wakasa05dbb652011-08-22 15:22:43 +09003701 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3702 private final LayoutInflater mInflater;
3703 private final int mTextViewResourceId;
3704 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003705 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003706 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3707 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3708 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003709
Ken Wakasa05dbb652011-08-22 15:22:43 +09003710 mTextViewResourceId = textViewResourceId;
3711 mItemsList = itemsList;
3712 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003713 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003714 }
3715
3716 @Override
3717 public View getView(int position, View convertView, ViewGroup parent) {
3718 final View view = convertView != null ? convertView
3719 : mInflater.inflate(mTextViewResourceId, null);
3720 if (position < 0 || position >= mItemsList.size()) return view;
3721 final ImeSubtypeListItem item = mItemsList.get(position);
3722 final CharSequence imeName = item.mImeName;
3723 final CharSequence subtypeName = item.mSubtypeName;
3724 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3725 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3726 if (TextUtils.isEmpty(subtypeName)) {
3727 firstTextView.setText(imeName);
3728 secondTextView.setVisibility(View.GONE);
3729 } else {
3730 firstTextView.setText(subtypeName);
3731 secondTextView.setText(imeName);
3732 secondTextView.setVisibility(View.VISIBLE);
3733 }
3734 final RadioButton radioButton =
3735 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3736 radioButton.setChecked(position == mCheckedItem);
3737 return view;
3738 }
3739 }
3740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003742 synchronized (mMethodMap) {
3743 hideInputMethodMenuLocked();
3744 }
3745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003746
The Android Open Source Project10592532009-03-18 17:39:46 -07003747 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003748 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749
The Android Open Source Project10592532009-03-18 17:39:46 -07003750 if (mSwitchingDialog != null) {
3751 mSwitchingDialog.dismiss();
3752 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003754
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003755 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003756 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003757 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003761
satok42c5a162011-05-26 16:46:14 +09003762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003764 // TODO: Make this work even for non-current users?
3765 if (!calledFromValidUser()) {
3766 return false;
3767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 synchronized (mMethodMap) {
3769 if (mContext.checkCallingOrSelfPermission(
3770 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3771 != PackageManager.PERMISSION_GRANTED) {
3772 throw new SecurityException(
3773 "Requires permission "
3774 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3775 }
Anna Galusza9b278112016-01-04 11:37:37 -08003776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 long ident = Binder.clearCallingIdentity();
3778 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003779 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 } finally {
3781 Binder.restoreCallingIdentity(ident);
3782 }
3783 }
3784 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003785
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003786 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3787 // Make sure this is a valid input method.
3788 InputMethodInfo imm = mMethodMap.get(id);
3789 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003790 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003791 }
3792
satokd87c2592010-09-29 11:52:06 +09003793 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3794 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003795
satokd87c2592010-09-29 11:52:06 +09003796 if (enabled) {
3797 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3798 if (pair.first.equals(id)) {
3799 // We are enabling this input method, but it is already enabled.
3800 // Nothing to do. The previous state was enabled.
3801 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003802 }
3803 }
satokd87c2592010-09-29 11:52:06 +09003804 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3805 // Previous state was disabled.
3806 return false;
3807 } else {
3808 StringBuilder builder = new StringBuilder();
3809 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3810 builder, enabledInputMethodsList, id)) {
3811 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003812 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003813 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3814 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3815 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003816 }
3817 // Previous state was enabled.
3818 return true;
3819 } else {
3820 // We are disabling the input method but it is already disabled.
3821 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003822 return false;
3823 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003824 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003825 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003826
satok723a27e2010-11-11 14:58:11 +09003827 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3828 boolean setSubtypeOnly) {
3829 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003830 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003831
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003832 mCurUserActionNotificationSequenceNumber =
3833 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3834 if (DEBUG) {
3835 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3836 + mCurUserActionNotificationSequenceNumber);
3837 }
3838
3839 if (mCurClient != null && mCurClient.client != null) {
3840 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3841 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003842 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003843 }
3844
satok723a27e2010-11-11 14:58:11 +09003845 // Set Subtype here
3846 if (imi == null || subtypeId < 0) {
3847 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003848 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003849 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003850 if (subtypeId < imi.getSubtypeCount()) {
3851 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3852 mSettings.putSelectedSubtype(subtype.hashCode());
3853 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003854 } else {
3855 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003856 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003857 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003858 }
satokab751aa2010-09-14 19:17:36 +09003859 }
satok723a27e2010-11-11 14:58:11 +09003860
Yohei Yukawa68645a62016-02-17 07:54:20 -08003861 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003862 // Set InputMethod here
3863 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3864 }
3865 }
3866
3867 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3868 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3869 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3870 // newDefaultIme is empty when there is no candidate for the selected IME.
3871 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3872 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3873 if (subtypeHashCode != null) {
3874 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003875 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003876 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003877 } catch (NumberFormatException e) {
3878 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3879 }
3880 }
3881 }
3882 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003883 }
3884
satok4e4569d2010-11-19 18:45:53 +09003885 // If there are no selected shortcuts, tries finding the most applicable ones.
3886 private Pair<InputMethodInfo, InputMethodSubtype>
3887 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3888 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3889 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003890 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003891 boolean foundInSystemIME = false;
3892
3893 // Search applicable subtype for each InputMethodInfo
3894 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003895 final String imiId = imi.getId();
3896 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3897 continue;
3898 }
satokcd7cd292010-11-20 15:46:23 +09003899 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003900 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003901 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003902 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003903 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003904 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003905 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003906 }
satokdf31ae62011-01-15 06:19:44 +09003907 // 2. Search by the system locale from enabledSubtypes.
3908 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003909 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003910 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003911 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003912 }
satoka86f5e42011-09-02 17:12:42 +09003913 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003914 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003915 final ArrayList<InputMethodSubtype> subtypesForSearch =
3916 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003917 ? InputMethodUtils.getSubtypes(imi)
3918 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003919 // 4. Search by the current subtype's locale from all subtypes.
3920 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003921 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003922 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003923 }
3924 // 5. Search by the system locale from all subtypes.
3925 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003926 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003927 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003928 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003929 }
satokcd7cd292010-11-20 15:46:23 +09003930 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003931 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003932 // The current input method is the most applicable IME.
3933 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003934 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003935 break;
satok7599a7f2010-12-22 13:45:23 +09003936 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003937 // The system input method is 2nd applicable IME.
3938 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003939 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003940 if ((imi.getServiceInfo().applicationInfo.flags
3941 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3942 foundInSystemIME = true;
3943 }
satok4e4569d2010-11-19 18:45:53 +09003944 }
3945 }
3946 }
3947 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003948 if (mostApplicableIMI != null) {
3949 Slog.w(TAG, "Most applicable shortcut input method was:"
3950 + mostApplicableIMI.getId());
3951 if (mostApplicableSubtype != null) {
3952 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3953 + "," + mostApplicableSubtype.getMode() + ","
3954 + mostApplicableSubtype.getLocale());
3955 }
3956 }
satok4e4569d2010-11-19 18:45:53 +09003957 }
satokcd7cd292010-11-20 15:46:23 +09003958 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003959 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003960 } else {
3961 return null;
3962 }
3963 }
3964
satokab751aa2010-09-14 19:17:36 +09003965 /**
3966 * @return Return the current subtype of this input method.
3967 */
satok42c5a162011-05-26 16:46:14 +09003968 @Override
satokab751aa2010-09-14 19:17:36 +09003969 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003970 // TODO: Make this work even for non-current users?
3971 if (!calledFromValidUser()) {
3972 return null;
3973 }
3974 synchronized (mMethodMap) {
3975 return getCurrentInputMethodSubtypeLocked();
3976 }
3977 }
3978
3979 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003980 if (mCurMethodId == null) {
3981 return null;
3982 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003983 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003984 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3985 if (imi == null || imi.getSubtypeCount() == 0) {
3986 return null;
satok4e4569d2010-11-19 18:45:53 +09003987 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003988 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003989 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3990 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003991 if (subtypeId == NOT_A_SUBTYPE_ID) {
3992 // If there are no selected subtypes, the framework will try to find
3993 // the most applicable subtype from explicitly or implicitly enabled
3994 // subtypes.
3995 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003996 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003997 // If there is only one explicitly or implicitly enabled subtype,
3998 // just returns it.
3999 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4000 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4001 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004002 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004003 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004004 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004005 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004006 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004007 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4008 true);
satok4e4569d2010-11-19 18:45:53 +09004009 }
satok3ef8b292010-11-23 06:06:29 +09004010 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004011 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004012 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004013 }
4014 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004015 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004016 }
4017
satok4e4569d2010-11-19 18:45:53 +09004018 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004019 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004020 @Override
satok4e4569d2010-11-19 18:45:53 +09004021 public List getShortcutInputMethodsAndSubtypes() {
4022 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004023 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004024 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004025 // If there are no selected shortcut subtypes, the framework will try to find
4026 // the most applicable subtype from all subtypes whose mode is
4027 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004028 Pair<InputMethodInfo, InputMethodSubtype> info =
4029 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004030 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004031 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004032 ret.add(info.first);
4033 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004034 }
satok3da92232011-01-11 22:46:30 +09004035 return ret;
satokf3db1af2010-11-23 13:34:33 +09004036 }
satokf3db1af2010-11-23 13:34:33 +09004037 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4038 ret.add(imi);
4039 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4040 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004041 }
4042 }
satokf3db1af2010-11-23 13:34:33 +09004043 return ret;
satok4e4569d2010-11-19 18:45:53 +09004044 }
4045 }
4046
satok42c5a162011-05-26 16:46:14 +09004047 @Override
satokb66d2872010-11-10 01:04:04 +09004048 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004049 // TODO: Make this work even for non-current users?
4050 if (!calledFromValidUser()) {
4051 return false;
4052 }
satokb66d2872010-11-10 01:04:04 +09004053 synchronized (mMethodMap) {
4054 if (subtype != null && mCurMethodId != null) {
4055 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004056 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004057 if (subtypeId != NOT_A_SUBTYPE_ID) {
4058 setInputMethodLocked(mCurMethodId, subtypeId);
4059 return true;
4060 }
4061 }
4062 return false;
4063 }
4064 }
4065
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004066 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004067 private static class InputMethodFileManager {
4068 private static final String SYSTEM_PATH = "system";
4069 private static final String INPUT_METHOD_PATH = "inputmethod";
4070 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4071 private static final String NODE_SUBTYPES = "subtypes";
4072 private static final String NODE_SUBTYPE = "subtype";
4073 private static final String NODE_IMI = "imi";
4074 private static final String ATTR_ID = "id";
4075 private static final String ATTR_LABEL = "label";
4076 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004077 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004078 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004079 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004080 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4081 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4082 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004083 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004084 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4085 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004086 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004087 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004088 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004089 if (methodMap == null) {
4090 throw new NullPointerException("methodMap is null");
4091 }
4092 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004093 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004094 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4095 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004096 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004097 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004098 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4099 }
4100 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4101 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4102 if (!subtypeFile.exists()) {
4103 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004104 writeAdditionalInputMethodSubtypes(
4105 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004106 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004107 readAdditionalInputMethodSubtypes(
4108 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004109 }
4110 }
4111
4112 private void deleteAllInputMethodSubtypes(String imiId) {
4113 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004114 mAdditionalSubtypesMap.remove(imiId);
4115 writeAdditionalInputMethodSubtypes(
4116 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004117 }
4118 }
4119
4120 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004121 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004122 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004123 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004124 final int N = additionalSubtypes.length;
4125 for (int i = 0; i < N; ++i) {
4126 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004127 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004128 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004129 } else {
4130 Slog.w(TAG, "Duplicated subtype definition found: "
4131 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004132 }
4133 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004134 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4135 writeAdditionalInputMethodSubtypes(
4136 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004137 }
4138 }
4139
4140 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4141 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004142 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004143 }
4144 }
4145
4146 private static void writeAdditionalInputMethodSubtypes(
4147 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4148 HashMap<String, InputMethodInfo> methodMap) {
4149 // Safety net for the case that this function is called before methodMap is set.
4150 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4151 FileOutputStream fos = null;
4152 try {
4153 fos = subtypesFile.startWrite();
4154 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004155 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004156 out.startDocument(null, true);
4157 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4158 out.startTag(null, NODE_SUBTYPES);
4159 for (String imiId : allSubtypes.keySet()) {
4160 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4161 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4162 continue;
4163 }
4164 out.startTag(null, NODE_IMI);
4165 out.attribute(null, ATTR_ID, imiId);
4166 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4167 final int N = subtypesList.size();
4168 for (int i = 0; i < N; ++i) {
4169 final InputMethodSubtype subtype = subtypesList.get(i);
4170 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004171 if (subtype.hasSubtypeId()) {
4172 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4173 String.valueOf(subtype.getSubtypeId()));
4174 }
satoke7c6998e2011-06-03 17:57:59 +09004175 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4176 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4177 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004178 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4179 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004180 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4181 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4182 out.attribute(null, ATTR_IS_AUXILIARY,
4183 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004184 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4185 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004186 out.endTag(null, NODE_SUBTYPE);
4187 }
4188 out.endTag(null, NODE_IMI);
4189 }
4190 out.endTag(null, NODE_SUBTYPES);
4191 out.endDocument();
4192 subtypesFile.finishWrite(fos);
4193 } catch (java.io.IOException e) {
4194 Slog.w(TAG, "Error writing subtypes", e);
4195 if (fos != null) {
4196 subtypesFile.failWrite(fos);
4197 }
4198 }
4199 }
4200
4201 private static void readAdditionalInputMethodSubtypes(
4202 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4203 if (allSubtypes == null || subtypesFile == null) return;
4204 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004205 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004206 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004207 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004208 int type = parser.getEventType();
4209 // Skip parsing until START_TAG
4210 while ((type = parser.next()) != XmlPullParser.START_TAG
4211 && type != XmlPullParser.END_DOCUMENT) {}
4212 String firstNodeName = parser.getName();
4213 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4214 throw new XmlPullParserException("Xml doesn't start with subtypes");
4215 }
4216 final int depth =parser.getDepth();
4217 String currentImiId = null;
4218 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4219 while (((type = parser.next()) != XmlPullParser.END_TAG
4220 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4221 if (type != XmlPullParser.START_TAG)
4222 continue;
4223 final String nodeName = parser.getName();
4224 if (NODE_IMI.equals(nodeName)) {
4225 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4226 if (TextUtils.isEmpty(currentImiId)) {
4227 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4228 continue;
4229 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004230 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004231 allSubtypes.put(currentImiId, tempSubtypesArray);
4232 } else if (NODE_SUBTYPE.equals(nodeName)) {
4233 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4234 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4235 continue;
4236 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004237 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004238 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004239 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004240 parser.getAttributeValue(null, ATTR_LABEL));
4241 final String imeSubtypeLocale =
4242 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004243 final String languageTag =
4244 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004245 final String imeSubtypeMode =
4246 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4247 final String imeSubtypeExtraValue =
4248 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004249 final boolean isAuxiliary = "1".equals(String.valueOf(
4250 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004251 final boolean isAsciiCapable = "1".equals(String.valueOf(
4252 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004253 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004254 .setSubtypeNameResId(label)
4255 .setSubtypeIconResId(icon)
4256 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004257 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004258 .setSubtypeMode(imeSubtypeMode)
4259 .setSubtypeExtraValue(imeSubtypeExtraValue)
4260 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004261 .setIsAsciiCapable(isAsciiCapable);
4262 final String subtypeIdString =
4263 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4264 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004265 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004266 }
4267 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004268 }
4269 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004270 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4271 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004272 return;
satoke7c6998e2011-06-03 17:57:59 +09004273 }
4274 }
4275 }
4276
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004277 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4278 @NonNull
4279 private final Handler mHandler;
4280
4281 LocalServiceImpl(@NonNull final Handler handler) {
4282 mHandler = handler;
4283 }
4284
4285 @Override
4286 public void setInteractive(boolean interactive) {
4287 // Do everything in handler so as not to block the caller.
4288 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4289 interactive ? 1 : 0, 0));
4290 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004291
4292 @Override
4293 public void switchInputMethod(boolean forwardDirection) {
4294 // Do everything in handler so as not to block the caller.
4295 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4296 forwardDirection ? 1 : 0, 0));
4297 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004298
4299 @Override
4300 public void hideCurrentInputMethod() {
4301 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4302 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4303 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004304 }
4305
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004306 private static String imeWindowStatusToString(final int imeWindowVis) {
4307 final StringBuilder sb = new StringBuilder();
4308 boolean first = true;
4309 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4310 sb.append("Active");
4311 first = false;
4312 }
4313 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4314 if (!first) {
4315 sb.append("|");
4316 }
4317 sb.append("Visible");
4318 }
4319 return sb.toString();
4320 }
4321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004323 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4324 @Nullable Uri contentUri, @Nullable String packageName) {
4325 if (!calledFromValidUser()) {
4326 return null;
4327 }
4328
4329 if (token == null) {
4330 throw new NullPointerException("token");
4331 }
4332 if (packageName == null) {
4333 throw new NullPointerException("packageName");
4334 }
4335 if (contentUri == null) {
4336 throw new NullPointerException("contentUri");
4337 }
4338 final String contentUriScheme = contentUri.getScheme();
4339 if (!"content".equals(contentUriScheme)) {
4340 throw new InvalidParameterException("contentUri must have content scheme");
4341 }
4342
4343 synchronized (mMethodMap) {
4344 final int uid = Binder.getCallingUid();
4345 if (mCurMethodId == null) {
4346 return null;
4347 }
4348 if (mCurToken != token) {
4349 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4350 + " token=" + token);
4351 return null;
4352 }
4353 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4354 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4355 // nature of our system. Let's compare it with our internal record.
4356 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4357 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4358 + mCurAttribute.packageName + " packageName=" + packageName);
4359 return null;
4360 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004361 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004362 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004363 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004364 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004365 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4366 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4367 imeUserId);
4368 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4369 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4370 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4371 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4372 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4373 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4374 // actually allowed to "uid", which is guaranteed to be the IME's one.
4375 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4376 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004377 }
4378 }
4379
4380 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004381 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4382 if (!calledFromValidUser()) {
4383 return;
4384 }
4385 synchronized (mMethodMap) {
4386 if (!calledWithValidToken(token)) {
4387 return;
4388 }
4389 if (mCurClient != null && mCurClient.client != null) {
4390 mInFullscreenMode = fullscreen;
4391 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4392 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4393 }
4394 }
4395 }
4396
4397 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004399 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400
4401 IInputMethod method;
4402 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004403 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 synchronized (mMethodMap) {
4408 p.println("Current Input Method Manager state:");
4409 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004410 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 for (int i=0; i<N; i++) {
4412 InputMethodInfo info = mMethodList.get(i);
4413 p.println(" InputMethod #" + i + ":");
4414 info.dump(p, " ");
4415 }
4416 p.println(" Clients:");
4417 for (ClientState ci : mClients.values()) {
4418 p.println(" Client " + ci + ":");
4419 p.println(" client=" + ci.client);
4420 p.println(" inputContext=" + ci.inputContext);
4421 p.println(" sessionRequested=" + ci.sessionRequested);
4422 p.println(" curSession=" + ci.curSession);
4423 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004424 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004426 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004427 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4428 + " softInputMode=" +
4429 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4430 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004431 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4433 + " mBoundToMethod=" + mBoundToMethod);
4434 p.println(" mCurToken=" + mCurToken);
4435 p.println(" mCurIntent=" + mCurIntent);
4436 method = mCurMethod;
4437 p.println(" mCurMethod=" + mCurMethod);
4438 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004439 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004440 p.println(" mShowRequested=" + mShowRequested
4441 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4442 + " mShowForced=" + mShowForced
4443 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004444 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004445 p.println(" mCurUserActionNotificationSequenceNumber="
4446 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004447 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004448 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004449 p.println(" mSwitchingController:");
4450 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004451 p.println(" mSettings:");
4452 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004453
4454 p.println(" mStartInputHistory:");
4455 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004457
Jeff Brownb88102f2010-09-08 11:49:43 -07004458 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 pw.flush();
4461 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004462 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4463 } catch (IOException | RemoteException e) {
4464 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004466 } else {
4467 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004469
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004470 if (focusedWindowClient != null && client != focusedWindowClient) {
4471 p.println(" ");
4472 p.println("Warning: Current input method client doesn't match the last focused. "
4473 + "window.");
4474 p.println("Dumping input method client in the last focused window just in case.");
4475 p.println(" ");
4476 pw.flush();
4477 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004478 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4479 } catch (IOException | RemoteException e) {
4480 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004481 }
4482 }
4483
Jeff Brownb88102f2010-09-08 11:49:43 -07004484 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 pw.flush();
4487 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004488 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4489 } catch (IOException | RemoteException e) {
4490 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004492 } else {
4493 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 }
4495 }
4496}