blob: 3e711ec05c75360182c341454953f09bac14f56d [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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080024import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070025import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090026import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090028import com.android.internal.inputmethod.InputMethodUtils;
29import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070031import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070032import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090033import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.view.IInputContext;
35import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import com.android.internal.view.IInputMethodClient;
37import com.android.internal.view.IInputMethodManager;
38import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070039import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080041import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080042import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
satoke7c6998e2011-06-03 17:57:59 +090044import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090046import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070048import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070049import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070050import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080051import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080052import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070053import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070055import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090056import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090057import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090058import android.app.Notification;
59import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070060import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090061import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080063import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ContentResolver;
65import android.content.Context;
66import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090068import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090070import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070072import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.pm.PackageManager;
75import android.content.pm.ResolveInfo;
76import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070077import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.res.Resources;
79import android.content.res.TypedArray;
80import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080081import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080082import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080083import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070084import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040086import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070087import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090088import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Handler;
90import android.os.IBinder;
91import android.os.IInterface;
92import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070093import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070095import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080097import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.ServiceManager;
99import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900100import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800101import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.provider.Settings;
103import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900104import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700105import android.util.ArrayMap;
106import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700107import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900109import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900110import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.util.PrintWriterPrinter;
112import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900113import android.util.Slog;
114import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900115import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700117import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900118import android.view.LayoutInflater;
119import android.view.View;
120import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700121import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700123import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900124import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700126import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import android.view.inputmethod.InputMethod;
128import android.view.inputmethod.InputMethodInfo;
129import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700130import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900131import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900132import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900133import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900134import android.widget.CompoundButton;
135import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900136import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900137import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900138import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700139import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
satoke7c6998e2011-06-03 17:57:59 +0900141import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900143import java.io.FileInputStream;
144import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.io.IOException;
146import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700147import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100148import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700149import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900151import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.util.HashMap;
153import java.util.List;
154
155/**
156 * This class provides a system service that manages input methods.
157 */
158public class InputMethodManagerService extends IInputMethodManager.Stub
159 implements ServiceConnection, Handler.Callback {
160 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700161 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700162 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700164 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
165 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
166 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 static final int MSG_UNBIND_INPUT = 1000;
169 static final int MSG_BIND_INPUT = 1010;
170 static final int MSG_SHOW_SOFT_INPUT = 1020;
171 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700172 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 static final int MSG_ATTACH_TOKEN = 1040;
174 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_START_INPUT = 2000;
177 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
Yohei Yukawa33e81792015-11-17 21:14:42 -0800179 static final int MSG_UNBIND_CLIENT = 3000;
180 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700181 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700182 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900183 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800184 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
satok01038492012-04-09 21:08:27 +0900186 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
187
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700188 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
189
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700190 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
satokf9f01002011-05-19 21:31:50 +0900192 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
193
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900194 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900195 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900196
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700197 @Retention(SOURCE)
198 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
199 private @interface HardKeyboardBehavior {
200 int WIRELESS_AFFORDANCE = 0;
201 int WIRED_AFFORDANCE = 1;
202 }
satok4e4569d2010-11-19 18:45:53 +0900203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800205 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900207 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700210 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700212 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900213 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900214 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900215 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800216 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900218 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 // All known input methods. mMethodMap also serves as the global
221 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700222 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
223 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900224 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700225 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900226 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700228 // Used to bring IME service up to visible adjustment while it is being shown.
229 final ServiceConnection mVisibleConnection = new ServiceConnection() {
230 @Override public void onServiceConnected(ComponentName name, IBinder service) {
231 }
232
233 @Override public void onServiceDisconnected(ComponentName name) {
234 }
235 };
236 boolean mVisibleBound = false;
237
satok7cfc0ed2011-06-20 21:29:36 +0900238 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700239 private NotificationManager mNotificationManager;
240 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700241 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400242 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700243 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900244 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900245 private boolean mNotificationShown;
246
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900247 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final ClientState client;
249 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700250
251 IInputMethodSession session;
252 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 @Override
255 public String toString() {
256 return "SessionState{uid " + client.uid + " pid " + client.pid
257 + " method " + Integer.toHexString(
258 System.identityHashCode(method))
259 + " session " + Integer.toHexString(
260 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700261 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 + "}";
263 }
264
265 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700266 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 client = _client;
268 method = _method;
269 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700270 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
Jeff Brownc28867a2013-03-26 15:42:39 -0700274 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 final IInputMethodClient client;
276 final IInputContext inputContext;
277 final int uid;
278 final int pid;
279 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 boolean sessionRequested;
282 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 @Override
285 public String toString() {
286 return "ClientState{" + Integer.toHexString(
287 System.identityHashCode(this)) + " uid " + uid
288 + " pid " + pid + "}";
289 }
290
291 ClientState(IInputMethodClient _client, IInputContext _inputContext,
292 int _uid, int _pid) {
293 client = _client;
294 inputContext = _inputContext;
295 uid = _uid;
296 pid = _pid;
297 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
298 }
299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700301 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700304 * Set once the system is ready to run third party code.
305 */
306 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700308 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700309 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
310 * method. This is to be synchronized with the secure settings keyed with
311 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
312 *
313 * <p>This can be transiently {@code null} when the system is re-initializing input method
314 * settings, e.g., the system locale is just changed.</p>
315 *
316 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
317 * {@link InputMethodManagerService}.</p>
318 *
319 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700321 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * The current binding sequence number, incremented every time there is
326 * a new bind performed.
327 */
328 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * The client that is currently bound to an input method.
332 */
333 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800336 * The last window token that we confirmed to be focused. This is always updated upon reports
337 * from the input method client. If the window state is already changed before the report is
338 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700339 */
340 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700342 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800343 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
344 */
345 ClientState mCurFocusedWindowClient;
346
347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 * The input context last provided by the current client.
349 */
350 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700353 * The missing method flags for the input context last provided by the current client.
354 *
355 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
356 */
357 int mCurInputContextMissingMethods;
358
359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * The attributes last provided by the current client.
361 */
362 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700365 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700367 *
368 * <p>This can be {@code null} when no input method is connected.</p>
369 *
370 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700372 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
satokab751aa2010-09-14 19:17:36 +0900376 * The current subtype of the current input method.
377 */
378 private InputMethodSubtype mCurrentSubtype;
379
satok4e4569d2010-11-19 18:45:53 +0900380 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900381 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700382 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900383
John Spurlocke0980502013-10-25 11:59:29 -0400384 // Was the keyguard locked when this client became current?
385 private boolean mCurClientInKeyguard;
386
satokab751aa2010-09-14 19:17:36 +0900387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * Set to true if our ServiceConnection is currently actively bound to
389 * a service (whether or not we have gotten its IBinder back yet).
390 */
391 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
394 * Set if the client has asked for the input method to be shown.
395 */
396 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 /**
399 * Set if we were explicitly told to show the input method.
400 */
401 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
404 * Set if we were forced to be shown.
405 */
406 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Set if we last told the input method to show itself.
410 */
411 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * The Intent used to connect to the current input method.
415 */
416 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
419 * The token we have made for the currently active input method, to
420 * identify it in the future.
421 */
422 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * If non-null, this is the input method service we are currently connected
426 * to.
427 */
428 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * Time that we last initiated a bind to the input method, to determine
432 * if we should try to disconnect and reconnect to it.
433 */
434 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Have we called mCurMethod.bindInput()?
438 */
439 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
442 * Currently enabled session. Only touched by service thread, not
443 * protected by a lock.
444 */
445 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700448 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700450 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900452 int mCurUserActionNotificationSequenceNumber = 0;
453
Joe Onorato857fd9b2011-01-27 15:08:35 -0800454 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900455
456 /**
457 * A set of status bits regarding the active IME.
458 *
459 * <p>This value is a combination of following two bits:</p>
460 * <dl>
461 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
462 * <dd>
463 * If this bit is ON, connected IME is ready to accept touch/key events.
464 * </dd>
465 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
466 * <dd>
467 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
468 * </dd>
469 * </dl>
470 * <em>Do not update this value outside of setImeWindowStatus.</em>
471 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800472 int mImeWindowVis;
473
Ken Wakasa05dbb652011-08-22 15:22:43 +0900474 private AlertDialog.Builder mDialogBuilder;
475 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700476 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900477 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700478 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900479 private InputMethodInfo[] mIms;
480 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800481 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700482 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800483 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900484 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
485 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500486 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700487 @HardKeyboardBehavior
488 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 int mUserId;
492 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700493 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800494 String mLastEnabled = "";
495
Yohei Yukawa81482972015-06-04 00:58:59 -0700496 /**
497 * <em>This constructor must be called within the lock.</em>
498 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 SettingsObserver(Handler handler) {
500 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700501 }
502
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800503 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700504 if (mRegistered && mUserId == userId) {
505 return;
506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700508 if (mRegistered) {
509 mContext.getContentResolver().unregisterContentObserver(this);
510 mRegistered = false;
511 }
512 if (mUserId != userId) {
513 mLastEnabled = "";
514 mUserId = userId;
515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700517 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900518 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700519 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900520 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700521 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700522 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700523 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800524 resolver.registerContentObserver(Settings.Secure.getUriFor(
525 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700526 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800528
Michael Wright7b5a96b2014-08-09 19:28:42 -0700529 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800530 final Uri showImeUri = Settings.Secure.getUriFor(
531 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
532 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
533 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700535 if (showImeUri.equals(uri)) {
536 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800537 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
538 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
539 mContext.getContentResolver(),
540 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
541 0, mUserId) == 1;
542 if (mAccessibilityRequestingNoSoftKeyboard) {
543 final boolean showRequested = mShowRequested;
544 hideCurrentInputLocked(0, null);
545 mShowRequested = showRequested;
546 } else if (mShowRequested) {
547 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
548 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700549 } else {
550 boolean enabledChanged = false;
551 String newEnabled = mSettings.getEnabledInputMethodsStr();
552 if (!mLastEnabled.equals(newEnabled)) {
553 mLastEnabled = newEnabled;
554 enabledChanged = true;
555 }
556 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700560
561 @Override
562 public String toString() {
563 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
564 + " mLastEnabled=" + mLastEnabled + "}";
565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800567
Yohei Yukawa79247822017-01-23 15:26:15 -0800568 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900569 @Override
570 public void onReceive(Context context, Intent intent) {
571 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700572 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900573 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700574 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900575 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800576 } else if (Intent.ACTION_USER_ADDED.equals(action)
577 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100578 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800579 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700580 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
581 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
582 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
583 final String prevValue = intent.getStringExtra(
584 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
585 final String newValue = intent.getStringExtra(
586 Intent.EXTRA_SETTING_NEW_VALUE);
587 restoreEnabledInputMethods(mContext, prevValue, newValue);
588 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800589 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
590 synchronized (mMethodMap) {
591 resetStateIfCurrentLocaleChangedLocked();
592 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900593 } else {
594 Slog.w(TAG, "Unexpected intent " + intent);
595 }
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800598
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700599 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
600 // does not attempt to validate on the fly with any installed device policy, so must only
601 // be run in the context of initial device setup.
602 //
603 // TODO: Move this method to InputMethodUtils with adding unit tests.
604 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
605 if (DEBUG_RESTORE) {
606 Slog.i(TAG, "Restoring enabled input methods:");
607 Slog.i(TAG, "prev=" + prevValue);
608 Slog.i(TAG, " new=" + newValue);
609 }
610 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900611 ArrayMap<String, ArraySet<String>> prevMap =
612 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
613 ArrayMap<String, ArraySet<String>> newMap =
614 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700615
616 // Merge the restored ime+subtype enabled states into the live state
617 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
618 final String imeId = entry.getKey();
619 ArraySet<String> prevSubtypes = prevMap.get(imeId);
620 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700621 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700622 prevMap.put(imeId, prevSubtypes);
623 }
624 prevSubtypes.addAll(entry.getValue());
625 }
626
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700627 final String mergedImesAndSubtypesString =
628 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700629 if (DEBUG_RESTORE) {
630 Slog.i(TAG, "Merged IME string:");
631 Slog.i(TAG, " " + mergedImesAndSubtypesString);
632 }
633 Settings.Secure.putString(context.getContentResolver(),
634 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
635 }
636
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800637 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900638 private boolean isChangingPackagesOfCurrentUser() {
639 final int userId = getChangingUserId();
640 final boolean retval = userId == mSettings.getCurrentUserId();
641 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900642 if (!retval) {
643 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
644 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 }
646 return retval;
647 }
648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800650 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900651 if (!isChangingPackagesOfCurrentUser()) {
652 return false;
653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 final int N = mMethodList.size();
657 if (curInputMethodId != null) {
658 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800659 InputMethodInfo imi = mMethodList.get(i);
660 if (imi.getId().equals(curInputMethodId)) {
661 for (String pkg : packages) {
662 if (imi.getPackageName().equals(pkg)) {
663 if (!doit) {
664 return true;
665 }
satok723a27e2010-11-11 14:58:11 +0900666 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800667 chooseNewDefaultIMELocked();
668 return true;
669 }
670 }
671 }
672 }
673 }
674 }
675 return false;
676 }
677
678 @Override
679 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900680 if (!isChangingPackagesOfCurrentUser()) {
681 return;
682 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800683 synchronized (mMethodMap) {
684 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900685 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800686 final int N = mMethodList.size();
687 if (curInputMethodId != null) {
688 for (int i=0; i<N; i++) {
689 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900690 final String imiId = imi.getId();
691 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692 curIm = imi;
693 }
satoke7c6998e2011-06-03 17:57:59 +0900694
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800695 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900696 if (isPackageModified(imi.getPackageName())) {
697 mFileManager.deleteAllInputMethodSubtypes(imiId);
698 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800699 if (change == PACKAGE_TEMPORARY_CHANGE
700 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800701 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800702 + imi.getComponent());
703 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705 }
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
Yohei Yukawa94e33302016-02-12 19:37:03 -0800708 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800712 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800713 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800714 if (change == PACKAGE_TEMPORARY_CHANGE
715 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800716 ServiceInfo si = null;
717 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900718 si = mIPackageManager.getServiceInfo(
719 curIm.getComponent(), 0, mSettings.getCurrentUserId());
720 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800721 }
722 if (si == null) {
723 // Uh oh, current input method is no longer around!
724 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800725 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900726 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800727 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800728 changed = true;
729 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800730 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900731 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800732 }
733 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800734 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800735 }
satokab751aa2010-09-14 19:17:36 +0900736
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800737 if (curIm == null) {
738 // We currently don't have a default input method... is
739 // one now available?
740 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700741 } else if (!changed && isPackageModified(curIm.getPackageName())) {
742 // Even if the current input method is still available, mCurrentSubtype could
743 // be obsolete when the package is modified in practice.
744 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800747 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800748 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750 }
751 }
752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
Jeff Brownc28867a2013-03-26 15:42:39 -0700754 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900755 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700756 private final IInputMethod mMethod;
757 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800758
Jeff Brownc28867a2013-03-26 15:42:39 -0700759 MethodCallback(InputMethodManagerService imms, IInputMethod method,
760 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900761 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700762 mMethod = method;
763 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800765
satoke7c6998e2011-06-03 17:57:59 +0900766 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700767 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700768 long ident = Binder.clearCallingIdentity();
769 try {
770 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
771 } finally {
772 Binder.restoreCallingIdentity(ident);
773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
satok01038492012-04-09 21:08:27 +0900777 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700778 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900779 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700780 public void onHardKeyboardStatusChange(boolean available) {
781 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
782 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900783 }
784
Michael Wright7b5a96b2014-08-09 19:28:42 -0700785 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900786 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700787 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900788 }
789 synchronized(mMethodMap) {
790 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
791 && mSwitchingDialog.isShowing()) {
792 mSwitchingDialogTitleView.findViewById(
793 com.android.internal.R.id.hard_keyboard_section).setVisibility(
794 available ? View.VISIBLE : View.GONE);
795 }
796 }
797 }
798 }
799
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800800 public static final class Lifecycle extends SystemService {
801 private InputMethodManagerService mService;
802
803 public Lifecycle(Context context) {
804 super(context);
805 mService = new InputMethodManagerService(context);
806 }
807
808 @Override
809 public void onStart() {
810 LocalServices.addService(InputMethodManagerInternal.class,
811 new LocalServiceImpl(mService.mHandler));
812 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
813 }
814
815 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800816 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700817 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800818 // TODO: Dispatch this to a worker thread as needed.
819 mService.onSwitchUser(userHandle);
820 }
821
822 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800823 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700824 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800825 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800826 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
827 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
828 .getService(Context.STATUS_BAR_SERVICE);
829 mService.systemRunning(statusBarService);
830 }
831 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800832
833 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700834 public void onUnlockUser(final @UserIdInt int userHandle) {
835 // Called on ActivityManager thread.
836 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700837 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800838 }
839 }
840
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800841 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800842 synchronized(mMethodMap) {
843 final int currentUserId = mSettings.getCurrentUserId();
844 if (DEBUG) {
845 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
846 }
847 if (userId != currentUserId) {
848 return;
849 }
850 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800851 if (mSystemReady) {
852 // We need to rebuild IMEs.
853 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
854 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
855 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800856 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800857 }
858
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800859 void onSwitchUser(@UserIdInt int userId) {
860 synchronized (mMethodMap) {
861 switchUserLocked(userId);
862 }
863 }
864
Seigo Nonaka7309b122015-08-17 18:34:13 -0700865 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900866 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800868 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700870 // Note: SettingsObserver doesn't register observers in its constructor.
871 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 mIWindowManager = IWindowManager.Stub.asInterface(
873 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700874 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800875 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900876 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 public void executeMessage(Message msg) {
878 handleMessage(msg);
879 }
Mita Yuned218c72012-12-06 17:18:25 -0800880 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800881 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800882 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900883 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700884 mHasFeature = context.getPackageManager().hasSystemFeature(
885 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500886 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700887 mHardKeyboardBehavior = mContext.getResources().getInteger(
888 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900889
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400890 Bundle extras = new Bundle();
891 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
892 mImeSwitcherNotification = new Notification.Builder(mContext)
893 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
894 .setWhen(0)
895 .setOngoing(true)
896 .addExtras(extras)
897 .setCategory(Notification.CATEGORY_SYSTEM)
898 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400899
satok7cfc0ed2011-06-20 21:29:36 +0900900 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900901 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900902
903 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900904
satok7cfc0ed2011-06-20 21:29:36 +0900905 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900906 int userId = 0;
907 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800908 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900909 } catch (RemoteException e) {
910 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
911 }
satok913a8922010-08-26 21:53:41 +0900912
satokd87c2592010-09-29 11:52:06 +0900913 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900914 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800915 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700916
Kenny Guy2a764942014-04-02 13:29:20 +0100917 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900918 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800919 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
920 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900921 }
922
satok5b927c432012-05-01 20:09:34 +0900923 private void resetDefaultImeLocked(Context context) {
924 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800925 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900926 return;
927 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800928 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800929 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800930 if (suitableImes.isEmpty()) {
931 Slog.i(TAG, "No default found");
932 return;
satok5b927c432012-05-01 20:09:34 +0900933 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800934 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -0800935 if (DEBUG) {
936 Slog.i(TAG, "Default found, using " + defIm.getId());
937 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800938 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900939 }
940
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900941 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
942 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 if (!mSystemReady) {
944 // not system ready
945 return;
946 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800947 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800949 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900950 if (!updateOnlyWhenLocaleChanged) {
951 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800952 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900953 }
954 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800955 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800957 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900958 if (!updateOnlyWhenLocaleChanged) {
959 final String selectedImiId = mSettings.getSelectedInputMethod();
960 if (TextUtils.isEmpty(selectedImiId)) {
961 // This is the first time of the user switch and
962 // set the current ime to the proper one.
963 resetDefaultImeLocked(mContext);
964 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900965 } else {
966 // If the locale is changed, needs to reset the default ime
967 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800969 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800970 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 if (!updateOnlyWhenLocaleChanged) {
972 try {
973 startInputInnerLocked();
974 } catch (RuntimeException e) {
975 Slog.w(TAG, "Unexpected exception", e);
976 }
977 }
978 }
979 }
980
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900981 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900982 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
983 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 }
985
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900986 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700987 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
988 + " currentUserId=" + mSettings.getCurrentUserId());
989
Yohei Yukawa81482972015-06-04 00:58:59 -0700990 // ContentObserver should be registered again when the user is changed
991 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800992
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800993 // If the system is not ready or the device is not yed unlocked by the user, then we use
994 // copy-on-write settings.
995 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -0600996 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800997 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +0100998 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900999 // InputMethodFileManager should be reset when the user is changed
1000 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001001 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001002
1003 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1004 + " defaultImiId=" + defaultImiId);
1005
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001006 // For secondary users, the list of enabled IMEs may not have been updated since the
1007 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1008 // not be empty even if the IME has been uninstalled by the primary user.
1009 // Even in such cases, IMMS works fine because it will find the most applicable
1010 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001011 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001012 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001013 initialUserSwitch /* needsToResetDefaultIme */);
1014 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001015 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1016 mSettings.getEnabledInputMethodListLocked(), newUserId,
1017 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001018 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001019
1020 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1021 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001022 }
1023
Kenny Guy2a764942014-04-02 13:29:20 +01001024 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001025 mSettings.setCurrentProfileIds(
1026 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001027 }
1028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 @Override
1030 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1031 throws RemoteException {
1032 try {
1033 return super.onTransact(code, data, reply, flags);
1034 } catch (RuntimeException e) {
1035 // The input method manager only throws security exceptions, so let's
1036 // log all others.
1037 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001038 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040 throw e;
1041 }
1042 }
1043
Svetoslav Ganova0027152013-06-25 14:59:53 -07001044 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001045 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001046 if (DEBUG) {
1047 Slog.d(TAG, "--- systemReady");
1048 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001049 if (!mSystemReady) {
1050 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001051 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001052 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001053 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001054 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001055 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1056 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001057 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001058 if (mStatusBar != null) {
1059 mStatusBar.setIconVisibility(mSlotIme, false);
1060 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001061 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001062 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1063 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001064 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001065 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001066 mHardKeyboardListener);
1067 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001068
1069 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1070 mSettingsObserver.registerContentObserverLocked(currentUserId);
1071
1072 final IntentFilter broadcastFilter = new IntentFilter();
1073 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1074 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1075 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1076 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1077 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1078 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1079
1080 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1081 resetDefaultImeLocked(mContext);
1082 updateFromSettingsLocked(true);
1083 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1084 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1085 mContext.getBasePackageName());
1086
Dianne Hackborncc278702009-09-02 23:07:23 -07001087 try {
1088 startInputInnerLocked();
1089 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001091 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001092 }
1093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001096 // ---------------------------------------------------------------------------------------
1097 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1098 // 1) it comes from the system process
1099 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1100 private boolean calledFromValidUser() {
1101 final int uid = Binder.getCallingUid();
1102 final int userId = UserHandle.getUserId(uid);
1103 if (DEBUG) {
1104 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1105 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1106 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001107 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1108 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001109 }
Kenny Guy2a764942014-04-02 13:29:20 +01001110 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001111 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001113
1114 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1115 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1116 // must not manage background users' states in any functions.
1117 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1118 // by a token.
1119 if (mContext.checkCallingOrSelfPermission(
1120 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1121 == PackageManager.PERMISSION_GRANTED) {
1122 if (DEBUG) {
1123 Slog.d(TAG, "--- Access granted because the calling process has "
1124 + "the INTERACT_ACROSS_USERS_FULL permission");
1125 }
1126 return true;
1127 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001128 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001129 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1130 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001131 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 }
1133
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001134
1135 /**
1136 * Returns true iff the caller is identified to be the current input method with the token.
1137 * @param token The window token given to the input method when it was started.
1138 * @return true if and only if non-null valid token is specified.
1139 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001140 private boolean calledWithValidToken(@Nullable IBinder token) {
1141 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1142 if (DEBUG) {
1143 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1144 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1145 }
1146 return false;
1147 }
1148 if (token == null || token != mCurToken) {
1149 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1150 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1151 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001152 return false;
1153 }
1154 return true;
1155 }
1156
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001157 private boolean bindCurrentInputMethodService(
1158 Intent service, ServiceConnection conn, int flags) {
1159 if (service == null || conn == null) {
1160 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1161 return false;
1162 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001163 return mContext.bindServiceAsUser(service, conn, flags,
1164 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001165 }
1166
satoke7c6998e2011-06-03 17:57:59 +09001167 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001169 // TODO: Make this work even for non-current users?
1170 if (!calledFromValidUser()) {
1171 return Collections.emptyList();
1172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001174 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 }
1177
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 // TODO: Make this work even for non-current users?
1181 if (!calledFromValidUser()) {
1182 return Collections.emptyList();
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001185 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 }
1188
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001189 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001190 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001191 * @return enabled subtypes of the specified imi
1192 */
satoke7c6998e2011-06-03 17:57:59 +09001193 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001194 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001195 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001196 // TODO: Make this work even for non-current users?
1197 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001198 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001199 }
satok67ddf9c2010-11-17 09:45:54 +09001200 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001201 final InputMethodInfo imi;
1202 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001203 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001204 } else {
1205 imi = mMethodMap.get(imiId);
1206 }
1207 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001208 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001209 }
1210 return mSettings.getEnabledInputMethodSubtypeListLocked(
1211 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001212 }
1213 }
1214
satoke7c6998e2011-06-03 17:57:59 +09001215 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 public void addClient(IInputMethodClient client,
1217 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001218 if (!calledFromValidUser()) {
1219 return;
1220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 synchronized (mMethodMap) {
1222 mClients.put(client.asBinder(), new ClientState(client,
1223 inputContext, uid, pid));
1224 }
1225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001226
satoke7c6998e2011-06-03 17:57:59 +09001227 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001229 if (!calledFromValidUser()) {
1230 return;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001233 ClientState cs = mClients.remove(client.asBinder());
1234 if (cs != null) {
1235 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001236 if (mCurClient == cs) {
1237 mCurClient = null;
1238 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001239 if (mCurFocusedWindowClient == cs) {
1240 mCurFocusedWindowClient = null;
1241 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 void executeOrSendMessage(IInterface target, Message msg) {
1247 if (target.asBinder() instanceof Binder) {
1248 mCaller.sendMessage(msg);
1249 } else {
1250 handleMessage(msg);
1251 msg.recycle();
1252 }
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Yohei Yukawa33e81792015-11-17 21:14:42 -08001255 void unbindCurrentClientLocked(
1256 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001258 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 + mCurClient.client.asBinder());
1260 if (mBoundToMethod) {
1261 mBoundToMethod = false;
1262 if (mCurMethod != null) {
1263 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1264 MSG_UNBIND_INPUT, mCurMethod));
1265 }
1266 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001267
1268 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1269 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001270 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1271 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project10592532009-03-18 17:39:46 -07001275 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 private int getImeShowFlags() {
1280 int flags = 0;
1281 if (mShowForced) {
1282 flags |= InputMethod.SHOW_FORCED
1283 | InputMethod.SHOW_EXPLICIT;
1284 } else if (mShowExplicitlyRequested) {
1285 flags |= InputMethod.SHOW_EXPLICIT;
1286 }
1287 return flags;
1288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 private int getAppShowFlags() {
1291 int flags = 0;
1292 if (mShowForced) {
1293 flags |= InputMethodManager.SHOW_FORCED;
1294 } else if (!mShowExplicitlyRequested) {
1295 flags |= InputMethodManager.SHOW_IMPLICIT;
1296 }
1297 return flags;
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
Dianne Hackborn7663d802012-02-24 13:08:49 -08001300 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 if (!mBoundToMethod) {
1302 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1303 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1304 mBoundToMethod = true;
1305 }
1306 final SessionState session = mCurClient.curSession;
1307 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001308 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1309 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1310 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001312 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1313 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1314 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001317 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001318 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001320 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001321 (session.channel != null ? session.channel.dup() : null),
1322 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001324
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001325 InputBindResult startInputLocked(
1326 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001327 IInputMethodClient client, IInputContext inputContext,
1328 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001329 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 // If no method is currently selected, do nothing.
1331 if (mCurMethodId == null) {
1332 return mNoBinding;
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 ClientState cs = mClients.get(client.asBinder());
1336 if (cs == null) {
1337 throw new IllegalArgumentException("unknown client "
1338 + client.asBinder());
1339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
Yohei Yukawa74750f22016-03-22 12:54:22 -07001341 if (attribute == null) {
1342 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1343 + " uid=" + cs.uid + " pid=" + cs.pid);
1344 return null;
1345 }
1346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 try {
1348 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1349 // Check with the window manager to make sure this client actually
1350 // has a window with focus. If not, reject. This is thread safe
1351 // because if the focus changes some time before or after, the
1352 // next client receiving focus that has any interest in input will
1353 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001354 if (DEBUG) {
1355 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1356 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 return null;
1359 }
1360 } catch (RemoteException e) {
1361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001363 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1364 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001365 }
1366
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001367 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001368 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001369 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001370 // If no method is currently selected, do nothing.
1371 if (mCurMethodId == null) {
1372 return mNoBinding;
1373 }
1374
Yohei Yukawad57ba672015-06-08 16:39:46 -07001375 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1376 attribute.packageName)) {
1377 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1378 + " uid=" + cs.uid + " package=" + attribute.packageName);
1379 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001380 }
1381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001383 // Was the keyguard locked when switching over to the new client?
1384 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 // If the client is changing, we need to switch over to the new
1386 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001387 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001388 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001389 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390
1391 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001392 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001393 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001394 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 // Bump up the sequence for this client and attach it.
1399 mCurSeq++;
1400 if (mCurSeq <= 0) mCurSeq = 1;
1401 mCurClient = cs;
1402 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001403 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 // Check if the input method is changing.
1407 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1408 if (cs.curSession != null) {
1409 // Fast case: if we are already connected to the input method,
1410 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001411 return attachNewInputLocked(
1412 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414 if (mHaveConnection) {
1415 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 // Return to client, and we will get back with it when
1417 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001418 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001419 return new InputBindResult(null, null, mCurId, mCurSeq,
1420 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 } else if (SystemClock.uptimeMillis()
1422 < (mLastBindTime+TIME_TO_RECONNECT)) {
1423 // In this case we have connected to the service, but
1424 // don't yet have its interface. If it hasn't been too
1425 // long since we did the connection, we'll return to
1426 // the client and wait to get the service interface so
1427 // we can report back. If it has been too long, we want
1428 // to fall through so we can try a disconnect/reconnect
1429 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001430 return new InputBindResult(null, null, mCurId, mCurSeq,
1431 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1434 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 }
1436 }
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001439 return startInputInnerLocked();
1440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001442 InputBindResult startInputInnerLocked() {
1443 if (mCurMethodId == null) {
1444 return mNoBinding;
1445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001447 if (!mSystemReady) {
1448 // If the system is not yet ready, we shouldn't be running third
1449 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001450 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1451 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1455 if (info == null) {
1456 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001458
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001459 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1462 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001463 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1464 com.android.internal.R.string.input_method_binding_label);
1465 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1466 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001467 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001468 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1469 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 mLastBindTime = SystemClock.uptimeMillis();
1471 mHaveConnection = true;
1472 mCurId = info.getId();
1473 mCurToken = new Binder();
1474 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001475 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001476 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 } catch (RemoteException e) {
1478 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001479 return new InputBindResult(null, null, mCurId, mCurSeq,
1480 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 } else {
1482 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 + mCurIntent);
1485 }
1486 return null;
1487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001488
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001489 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001490 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001491 IInputMethodClient client, IInputContext inputContext,
1492 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001493 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001494 if (!calledFromValidUser()) {
1495 return null;
1496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001498 if (DEBUG) {
1499 Slog.v(TAG, "startInput: reason="
1500 + InputMethodClient.getStartInputReason(startInputReason)
1501 + " client = " + client.asBinder()
1502 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001503 + " missingMethods="
1504 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001505 + " attribute=" + attribute
1506 + " controlFlags=#" + Integer.toHexString(controlFlags));
1507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 final long ident = Binder.clearCallingIdentity();
1509 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001510 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1511 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 } finally {
1513 Binder.restoreCallingIdentity(ident);
1514 }
1515 }
1516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001517
satoke7c6998e2011-06-03 17:57:59 +09001518 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 public void finishInput(IInputMethodClient client) {
1520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
satoke7c6998e2011-06-03 17:57:59 +09001522 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 public void onServiceConnected(ComponentName name, IBinder service) {
1524 synchronized (mMethodMap) {
1525 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1526 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001527 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001528 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001529 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001530 return;
1531 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001532 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001533 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1534 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001536 clearClientSessionLocked(mCurClient);
1537 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539 }
1540 }
1541 }
1542
Jeff Brownc28867a2013-03-26 15:42:39 -07001543 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1544 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 synchronized (mMethodMap) {
1546 if (mCurMethod != null && method != null
1547 && mCurMethod.asBinder() == method.asBinder()) {
1548 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001549 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001551 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001552 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 if (res.method != null) {
1554 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001555 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001557 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559 }
1560 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001561
1562 // Session abandoned. Close its associated input channel.
1563 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001566 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001567 if (mVisibleBound) {
1568 mContext.unbindService(mVisibleConnection);
1569 mVisibleBound = false;
1570 }
1571
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001572 if (mHaveConnection) {
1573 mContext.unbindService(this);
1574 mHaveConnection = false;
1575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001577 if (mCurToken != null) {
1578 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001579 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001580 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001581 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001582 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001583 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001584 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001585 } catch (RemoteException e) {
1586 }
1587 mCurToken = null;
1588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001589
The Android Open Source Project10592532009-03-18 17:39:46 -07001590 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001591 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
Yohei Yukawa33e81792015-11-17 21:14:42 -08001594 void resetCurrentMethodAndClient(
1595 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001596 mCurMethodId = null;
1597 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001598 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001599 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001600
1601 void requestClientSessionLocked(ClientState cs) {
1602 if (!cs.sessionRequested) {
1603 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1604 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1605 cs.sessionRequested = true;
1606 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1607 MSG_CREATE_SESSION, mCurMethod, channels[1],
1608 new MethodCallback(this, mCurMethod, channels[0])));
1609 }
1610 }
1611
1612 void clearClientSessionLocked(ClientState cs) {
1613 finishSessionLocked(cs.curSession);
1614 cs.curSession = null;
1615 cs.sessionRequested = false;
1616 }
1617
1618 private void finishSessionLocked(SessionState sessionState) {
1619 if (sessionState != null) {
1620 if (sessionState.session != null) {
1621 try {
1622 sessionState.session.finishSession();
1623 } catch (RemoteException e) {
1624 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001625 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001626 }
1627 sessionState.session = null;
1628 }
1629 if (sessionState.channel != null) {
1630 sessionState.channel.dispose();
1631 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001632 }
1633 }
1634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001635
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001636 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 if (mCurMethod != null) {
1638 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001639 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001641
Jeff Brownc28867a2013-03-26 15:42:39 -07001642 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001643 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 mCurMethod = null;
1645 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001646 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001647 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001650
satoke7c6998e2011-06-03 17:57:59 +09001651 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001653 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1654 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1655 // which is irregular but can eventually happen for everyone just by continuing using the
1656 // device. Thus it is important to make sure that all the internal states are properly
1657 // refreshed when this method is called back. Running
1658 // adb install -r <APK that implements the current IME>
1659 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001661 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 + " mCurIntent=" + mCurIntent);
1663 if (mCurMethod != null && mCurIntent != null
1664 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001665 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 // We consider this to be a new bind attempt, since the system
1667 // should now try to restart the service for us.
1668 mLastBindTime = SystemClock.uptimeMillis();
1669 mShowRequested = mInputShown;
1670 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001671 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673 }
1674 }
1675
satokf9f01002011-05-19 21:31:50 +09001676 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001678 synchronized (mMethodMap) {
1679 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001680 return;
1681 }
1682 final long ident = Binder.clearCallingIdentity();
1683 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001685 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001686 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001687 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001691 CharSequence contentDescription = null;
1692 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001693 // Use PackageManager to load label
1694 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001695 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001696 mIPackageManager.getApplicationInfo(packageName, 0,
1697 mSettings.getCurrentUserId()));
1698 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001699 /* ignore */
1700 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001701 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001702 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001703 contentDescription != null
1704 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001705 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001708 } finally {
1709 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
1712 }
1713
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001714 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001715 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001716 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001717 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001718 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001719 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001720 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1721 // When physical keyboard is attached, we show the ime switcher (or notification if
1722 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1723 // exists in the IME switcher dialog. Might be OK to remove this condition once
1724 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1725 return true;
1726 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001727 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1728 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001729 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001730
1731 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1732 final int N = imis.size();
1733 if (N > 2) return true;
1734 if (N < 1) return false;
1735 int nonAuxCount = 0;
1736 int auxCount = 0;
1737 InputMethodSubtype nonAuxSubtype = null;
1738 InputMethodSubtype auxSubtype = null;
1739 for(int i = 0; i < N; ++i) {
1740 final InputMethodInfo imi = imis.get(i);
1741 final List<InputMethodSubtype> subtypes =
1742 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1743 final int subtypeCount = subtypes.size();
1744 if (subtypeCount == 0) {
1745 ++nonAuxCount;
1746 } else {
1747 for (int j = 0; j < subtypeCount; ++j) {
1748 final InputMethodSubtype subtype = subtypes.get(j);
1749 if (!subtype.isAuxiliary()) {
1750 ++nonAuxCount;
1751 nonAuxSubtype = subtype;
1752 } else {
1753 ++auxCount;
1754 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001755 }
1756 }
satok7cfc0ed2011-06-20 21:29:36 +09001757 }
1758 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001759 if (nonAuxCount > 1 || auxCount > 1) {
1760 return true;
1761 } else if (nonAuxCount == 1 && auxCount == 1) {
1762 if (nonAuxSubtype != null && auxSubtype != null
1763 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1764 || auxSubtype.overridesImplicitlyEnabledSubtype()
1765 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1766 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1767 return false;
1768 }
1769 return true;
1770 }
1771 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001772 }
1773
John Spurlocke0980502013-10-25 11:59:29 -04001774 private boolean isKeyguardLocked() {
1775 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1776 }
1777
satokdbf29502011-08-25 15:28:23 +09001778 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001779 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001780 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001781 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001782 return;
1783 }
1784
1785 synchronized (mMethodMap) {
1786 mImeWindowVis = vis;
1787 mBackDisposition = backDisposition;
1788 updateSystemUiLocked(token, vis, backDisposition);
1789 }
1790 }
1791
1792 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1793 synchronized (mMethodMap) {
1794 updateSystemUiLocked(token, vis, backDisposition);
1795 }
1796 }
1797
1798 // Caution! This method is called in this class. Handle multi-user carefully
1799 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1800 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001801 return;
1802 }
1803
1804 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1805 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001806 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001807 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001808 // apply policy for binder calls
1809 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1810 vis = 0;
satok06487a52010-10-29 11:37:18 +09001811 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001812 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1813 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1814 if (mStatusBar != null) {
1815 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1816 needsToShowImeSwitcher);
1817 }
1818 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1819 if (imi != null && needsToShowImeSwitcher) {
1820 // Used to load label
1821 final CharSequence title = mRes.getText(
1822 com.android.internal.R.string.select_input_method);
1823 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1824 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001825 mImeSwitcherNotification.setContentTitle(title)
1826 .setContentText(summary)
1827 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001828 try {
1829 if ((mNotificationManager != null)
1830 && !mIWindowManager.hasNavigationBar()) {
1831 if (DEBUG) {
1832 Slog.d(TAG, "--- show notification: label = " + summary);
1833 }
1834 mNotificationManager.notifyAsUser(null,
1835 com.android.internal.R.string.select_input_method,
1836 mImeSwitcherNotification.build(), UserHandle.ALL);
1837 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001838 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001839 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001840 }
1841 } else {
1842 if (mNotificationShown && mNotificationManager != null) {
1843 if (DEBUG) {
1844 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001845 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001846 mNotificationManager.cancelAsUser(null,
1847 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1848 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001849 }
satok06487a52010-10-29 11:37:18 +09001850 }
1851 } finally {
1852 Binder.restoreCallingIdentity(ident);
1853 }
1854 }
1855
satoke7c6998e2011-06-03 17:57:59 +09001856 @Override
satokf9f01002011-05-19 21:31:50 +09001857 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001858 if (!calledFromValidUser()) {
1859 return;
1860 }
satokf9f01002011-05-19 21:31:50 +09001861 synchronized (mMethodMap) {
1862 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1863 for (int i = 0; i < spans.length; ++i) {
1864 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001865 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001866 mSecureSuggestionSpans.put(ss, currentImi);
1867 }
1868 }
1869 }
1870 }
1871
satoke7c6998e2011-06-03 17:57:59 +09001872 @Override
satokf9f01002011-05-19 21:31:50 +09001873 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001874 if (!calledFromValidUser()) {
1875 return false;
1876 }
satokf9f01002011-05-19 21:31:50 +09001877 synchronized (mMethodMap) {
1878 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1879 // TODO: Do not send the intent if the process of the targetImi is already dead.
1880 if (targetImi != null) {
1881 final String[] suggestions = span.getSuggestions();
1882 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001883 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001884 final Intent intent = new Intent();
1885 // Ensures that only a class in the original IME package will receive the
1886 // notification.
satok42c5a162011-05-26 16:46:14 +09001887 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001888 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1889 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1890 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1891 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001892 final long ident = Binder.clearCallingIdentity();
1893 try {
1894 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1895 } finally {
1896 Binder.restoreCallingIdentity(ident);
1897 }
satokf9f01002011-05-19 21:31:50 +09001898 return true;
1899 }
1900 }
1901 return false;
1902 }
1903
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001904 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001905 updateInputMethodsFromSettingsLocked(enabledMayChange);
1906 updateKeyboardFromSettingsLocked();
1907 }
1908
1909 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001910 if (enabledMayChange) {
1911 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1912 for (int i=0; i<enabled.size(); i++) {
1913 // We allow the user to select "disabled until used" apps, so if they
1914 // are enabling one of those here we now need to make it enabled.
1915 InputMethodInfo imm = enabled.get(i);
1916 try {
1917 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1918 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1919 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001920 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001921 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001922 if (DEBUG) {
1923 Slog.d(TAG, "Update state(" + imm.getId()
1924 + "): DISABLED_UNTIL_USED -> DEFAULT");
1925 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001926 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1927 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001928 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1929 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001930 }
1931 } catch (RemoteException e) {
1932 }
1933 }
1934 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001935 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1936 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1937 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1938 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001939 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001940 // There is no input method selected, try to choose new applicable input method.
1941 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001942 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001943 }
1944 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001946 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001948 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001949 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
satokf3db1af2010-11-23 13:34:33 +09001951 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001952 } else {
1953 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001954 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001956 // Here is not the perfect place to reset the switching controller. Ideally
1957 // mSwitchingController and mSettings should be able to share the same state.
1958 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1959 // the same enabled IMEs list.
1960 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001961
1962 }
1963
1964 public void updateKeyboardFromSettingsLocked() {
1965 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1966 if (mSwitchingDialog != null
1967 && mSwitchingDialogTitleView != null
1968 && mSwitchingDialog.isShowing()) {
1969 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1970 com.android.internal.R.id.hard_keyboard_switch);
1971 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001974
Yohei Yukawab097b822015-12-01 10:43:08 -08001975 private void notifyInputMethodSubtypeChanged(final int userId,
1976 @Nullable final InputMethodInfo inputMethodInfo,
1977 @Nullable final InputMethodSubtype subtype) {
1978 final InputManagerInternal inputManagerInternal =
1979 LocalServices.getService(InputManagerInternal.class);
1980 if (inputManagerInternal != null) {
1981 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1982 }
1983 }
1984
satokab751aa2010-09-14 19:17:36 +09001985 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 InputMethodInfo info = mMethodMap.get(id);
1987 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001988 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001990
satokd81e9502012-05-21 12:58:45 +09001991 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001993 final int subtypeCount = info.getSubtypeCount();
1994 if (subtypeCount <= 0) {
1995 return;
satokcd7cd292010-11-20 15:46:23 +09001996 }
satokd81e9502012-05-21 12:58:45 +09001997 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1998 final InputMethodSubtype newSubtype;
1999 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2000 newSubtype = info.getSubtypeAt(subtypeId);
2001 } else {
2002 // If subtype is null, try to find the most applicable one from
2003 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002004 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002005 }
2006 if (newSubtype == null || oldSubtype == null) {
2007 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2008 + ", new subtype = " + newSubtype);
2009 return;
2010 }
2011 if (newSubtype != oldSubtype) {
2012 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2013 if (mCurMethod != null) {
2014 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002015 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002016 mCurMethod.changeInputMethodSubtype(newSubtype);
2017 } catch (RemoteException e) {
2018 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002019 return;
satokab751aa2010-09-14 19:17:36 +09002020 }
2021 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002022 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 return;
2025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002026
satokd81e9502012-05-21 12:58:45 +09002027 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 final long ident = Binder.clearCallingIdentity();
2029 try {
satokab751aa2010-09-14 19:17:36 +09002030 // Set a subtype to this input method.
2031 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002032 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2033 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2034 // because mCurMethodId is stored as a history in
2035 // setSelectedInputMethodAndSubtypeLocked().
2036 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002038 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002040 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002042 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002044 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 } finally {
2046 Binder.restoreCallingIdentity(ident);
2047 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002048
2049 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2050 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002052
satok42c5a162011-05-26 16:46:14 +09002053 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002054 public boolean showSoftInput(IInputMethodClient client, int flags,
2055 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002056 if (!calledFromValidUser()) {
2057 return false;
2058 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002059 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 long ident = Binder.clearCallingIdentity();
2061 try {
2062 synchronized (mMethodMap) {
2063 if (mCurClient == null || client == null
2064 || mCurClient.client.asBinder() != client.asBinder()) {
2065 try {
2066 // We need to check if this is the current client with
2067 // focus in the window manager, to allow this call to
2068 // be made before input is started in it.
2069 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002070 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002071 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
2073 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002074 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002077
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002078 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002079 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
2081 } finally {
2082 Binder.restoreCallingIdentity(ident);
2083 }
2084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002085
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002088 if (mAccessibilityRequestingNoSoftKeyboard) {
2089 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
Anna Galusza9b278112016-01-04 11:37:37 -08002091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2093 mShowExplicitlyRequested = true;
2094 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002095 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2096 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
Dianne Hackborncc278702009-09-02 23:07:23 -07002099 if (!mSystemReady) {
2100 return false;
2101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002102
The Android Open Source Project4df24232009-03-05 14:34:35 -08002103 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002105 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002106 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2107 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2108 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002110 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002111 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002112 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002113 | Context.BIND_TREAT_LIKE_ACTIVITY
2114 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002115 mVisibleBound = true;
2116 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002119 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 // The client has asked to have the input method shown, but
2121 // we have been sitting here too long with a connection to the
2122 // service and no interface received, so let's disconnect/connect
2123 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002124 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002126 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002128 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002129 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002130 } else {
2131 if (DEBUG) {
2132 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2133 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002136
The Android Open Source Project4df24232009-03-05 14:34:35 -08002137 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139
satok42c5a162011-05-26 16:46:14 +09002140 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002141 public boolean hideSoftInput(IInputMethodClient client, int flags,
2142 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002143 if (!calledFromValidUser()) {
2144 return false;
2145 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002146 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 long ident = Binder.clearCallingIdentity();
2148 try {
2149 synchronized (mMethodMap) {
2150 if (mCurClient == null || client == null
2151 || mCurClient.client.asBinder() != client.asBinder()) {
2152 try {
2153 // We need to check if this is the current client with
2154 // focus in the window manager, to allow this call to
2155 // be made before input is started in it.
2156 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002157 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2158 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002159 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
2161 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002162 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
2164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002165
Joe Onorato8a9b2202010-02-26 18:56:32 -08002166 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002167 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
2169 } finally {
2170 Binder.restoreCallingIdentity(ident);
2171 }
2172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002173
The Android Open Source Project4df24232009-03-05 14:34:35 -08002174 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2176 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002177 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 -08002178 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
2180 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002181 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 -08002182 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002184
2185 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2186 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2187 // to be updated with the new value sent from IME process. Even in such a transient state
2188 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2189 // application process as a valid request, and have even promised such a behavior with CTS
2190 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2191 // IMMS#InputShown indicates that the software keyboard is shown.
2192 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2193 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2194 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002195 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002196 if (shouldHideSoftInput) {
2197 // The IME will report its visible state again after the following message finally
2198 // delivered to the IME process as an IPC. Hence the inconsistency between
2199 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2200 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002201 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2202 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2203 res = true;
2204 } else {
2205 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002207 if (mHaveConnection && mVisibleBound) {
2208 mContext.unbindService(mVisibleConnection);
2209 mVisibleBound = false;
2210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 mInputShown = false;
2212 mShowRequested = false;
2213 mShowExplicitlyRequested = false;
2214 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002215 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
satok42c5a162011-05-26 16:46:14 +09002218 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002219 public InputBindResult startInputOrWindowGainedFocus(
2220 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2221 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002222 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002223 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002224 if (windowToken != null) {
2225 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002226 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002227 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002228 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2229 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002230 }
2231 }
2232
2233 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002234 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2235 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002236 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2237 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002238 // Needs to check the validity before clearing calling identity
2239 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002240 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 long ident = Binder.clearCallingIdentity();
2242 try {
2243 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002244 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2245 + InputMethodClient.getStartInputReason(startInputReason)
2246 + " client=" + client.asBinder()
2247 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002248 + " missingMethods="
2249 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002250 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002251 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002253 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002254
Dianne Hackborn7663d802012-02-24 13:08:49 -08002255 ClientState cs = mClients.get(client.asBinder());
2256 if (cs == null) {
2257 throw new IllegalArgumentException("unknown client "
2258 + client.asBinder());
2259 }
2260
2261 try {
2262 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2263 // Check with the window manager to make sure this client actually
2264 // has a window with focus. If not, reject. This is thread safe
2265 // because if the focus changes some time before or after, the
2266 // next client receiving focus that has any interest in input will
2267 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002268 if (DEBUG) {
2269 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2270 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2271 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002272 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002274 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002276
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002277 if (!calledFromValidUser) {
2278 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2279 Slog.w(TAG, "If you want to interect with IME, you need "
2280 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2281 hideCurrentInputLocked(0, null);
2282 return null;
2283 }
2284
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002285 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002286 if (DEBUG) {
2287 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2288 + " attribute=" + attribute + ", token = " + windowToken);
2289 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002290 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002291 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2292 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002293 }
2294 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002295 }
2296 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002297 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002298
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002299 // Should we auto-show the IME even if the caller has not
2300 // specified what should be done with it?
2301 // We only do this automatically if the window can resize
2302 // to accommodate the IME (so what the user sees will give
2303 // them good context without input information being obscured
2304 // by the IME) or if running on a large screen where there
2305 // is more room for the target window + IME.
2306 final boolean doAutoShow =
2307 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2308 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2309 || mRes.getConfiguration().isLayoutSizeAtLeast(
2310 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002311 final boolean isTextEditor =
2312 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2313
2314 // We want to start input before showing the IME, but after closing
2315 // it. We want to do this after closing it to help the IME disappear
2316 // more quickly (not get stuck behind it initializing itself for the
2317 // new focused input, even if its window wants to hide the IME).
2318 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2321 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002322 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2324 // There is no focus view, and this window will
2325 // be behind any soft input window, so hide the
2326 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002327 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002328 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002330 } else if (isTextEditor && doAutoShow && (softInputMode &
2331 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 // There is a focus view, and we are navigating forward
2333 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002334 // We only do this automatically if the window can resize
2335 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002336 // them good context without input information being obscured
2337 // by the IME) or if running on a large screen where there
2338 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002339 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002340 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002341 res = startInputUncheckedLocked(cs, inputContext,
2342 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002343 didStart = true;
2344 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002345 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
2347 break;
2348 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2349 // Do nothing.
2350 break;
2351 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2352 if ((softInputMode &
2353 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002354 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002355 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
2357 break;
2358 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002359 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002360 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 break;
2362 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2363 if ((softInputMode &
2364 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002366 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002367 res = startInputUncheckedLocked(cs, inputContext,
2368 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002369 didStart = true;
2370 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002371 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 }
2373 break;
2374 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002375 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002376 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002377 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2378 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002379 didStart = true;
2380 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002381 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 break;
2383 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002384
2385 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002386 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002387 controlFlags);
2388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390 } finally {
2391 Binder.restoreCallingIdentity(ident);
2392 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002393
2394 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002396
satok42c5a162011-05-26 16:46:14 +09002397 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002398 public void showInputMethodPickerFromClient(
2399 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002400 if (!calledFromValidUser()) {
2401 return;
2402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 synchronized (mMethodMap) {
2404 if (mCurClient == null || client == null
2405 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002406 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002407 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
2409
satok440aab52010-11-25 09:43:11 +09002410 // Always call subtype picker, because subtype picker is a superset of input method
2411 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002412 mHandler.sendMessage(mCaller.obtainMessageI(
2413 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002414 }
2415 }
2416
satok42c5a162011-05-26 16:46:14 +09002417 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002419 if (!calledFromValidUser()) {
2420 return;
2421 }
satok28203512010-11-24 11:06:49 +09002422 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2423 }
2424
satok42c5a162011-05-26 16:46:14 +09002425 @Override
satok28203512010-11-24 11:06:49 +09002426 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002427 if (!calledFromValidUser()) {
2428 return;
2429 }
satok28203512010-11-24 11:06:49 +09002430 synchronized (mMethodMap) {
2431 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002432 setInputMethodWithSubtypeIdLocked(token, id,
2433 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2434 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002435 } else {
2436 setInputMethod(token, id);
2437 }
2438 }
satokab751aa2010-09-14 19:17:36 +09002439 }
2440
satok42c5a162011-05-26 16:46:14 +09002441 @Override
satokb416a712010-11-25 20:42:14 +09002442 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002443 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002444 if (!calledFromValidUser()) {
2445 return;
2446 }
satokb416a712010-11-25 20:42:14 +09002447 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002448 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2449 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002450 }
2451 }
2452
satok4fc87d62011-05-20 16:13:43 +09002453 @Override
satok735cf382010-11-11 20:40:09 +09002454 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002455 if (!calledFromValidUser()) {
2456 return false;
2457 }
satok735cf382010-11-11 20:40:09 +09002458 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002459 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002460 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002461 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002462 lastImi = mMethodMap.get(lastIme.first);
2463 } else {
2464 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002465 }
satok4fc87d62011-05-20 16:13:43 +09002466 String targetLastImiId = null;
2467 int subtypeId = NOT_A_SUBTYPE_ID;
2468 if (lastIme != null && lastImi != null) {
2469 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002470 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002471 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2472 : mCurrentSubtype.hashCode();
2473 // If the last IME is the same as the current IME and the last subtype is not
2474 // defined, there is no need to switch to the last IME.
2475 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2476 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002477 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002478 }
2479 }
2480
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002481 if (TextUtils.isEmpty(targetLastImiId)
2482 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002483 // This is a safety net. If the currentSubtype can't be added to the history
2484 // and the framework couldn't find the last ime, we will make the last ime be
2485 // the most applicable enabled keyboard subtype of the system imes.
2486 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2487 if (enabled != null) {
2488 final int N = enabled.size();
2489 final String locale = mCurrentSubtype == null
2490 ? mRes.getConfiguration().locale.toString()
2491 : mCurrentSubtype.getLocale();
2492 for (int i = 0; i < N; ++i) {
2493 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002494 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002495 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002496 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2497 InputMethodUtils.getSubtypes(imi),
2498 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002499 if (keyboardSubtype != null) {
2500 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002501 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002502 imi, keyboardSubtype.hashCode());
2503 if(keyboardSubtype.getLocale().equals(locale)) {
2504 break;
2505 }
2506 }
2507 }
2508 }
2509 }
2510 }
2511
2512 if (!TextUtils.isEmpty(targetLastImiId)) {
2513 if (DEBUG) {
2514 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2515 + ", from: " + mCurMethodId + ", " + subtypeId);
2516 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002517 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002518 return true;
2519 } else {
2520 return false;
2521 }
satok735cf382010-11-11 20:40:09 +09002522 }
2523 }
2524
satoke7c6998e2011-06-03 17:57:59 +09002525 @Override
satok688bd472012-02-09 20:09:17 +09002526 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002527 if (!calledFromValidUser()) {
2528 return false;
2529 }
satok688bd472012-02-09 20:09:17 +09002530 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002531 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002532 return false;
2533 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002534 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002535 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2536 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002537 if (nextSubtype == null) {
2538 return false;
2539 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002540 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2541 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002542 return true;
2543 }
2544 }
2545
2546 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002547 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2548 if (!calledFromValidUser()) {
2549 return false;
2550 }
2551 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002552 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002553 return false;
2554 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002555 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002556 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2557 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002558 if (nextSubtype == null) {
2559 return false;
2560 }
2561 return true;
2562 }
2563 }
2564
2565 @Override
satok68f1b782011-04-11 14:26:04 +09002566 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002567 if (!calledFromValidUser()) {
2568 return null;
2569 }
satok68f1b782011-04-11 14:26:04 +09002570 synchronized (mMethodMap) {
2571 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2572 // TODO: Handle the case of the last IME with no subtypes
2573 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2574 || TextUtils.isEmpty(lastIme.second)) return null;
2575 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2576 if (lastImi == null) return null;
2577 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002578 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002579 final int lastSubtypeId =
2580 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002581 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2582 return null;
2583 }
2584 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002585 } catch (NumberFormatException e) {
2586 return null;
2587 }
2588 }
2589 }
2590
satoke7c6998e2011-06-03 17:57:59 +09002591 @Override
satokee5e77c2011-09-02 18:50:15 +09002592 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002593 if (!calledFromValidUser()) {
2594 return;
2595 }
satok91e88122011-07-18 11:11:42 +09002596 // By this IPC call, only a process which shares the same uid with the IME can add
2597 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002598 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002599 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002600 if (!mSystemReady) {
2601 return;
2602 }
satok91e88122011-07-18 11:11:42 +09002603 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002604 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002605 final String[] packageInfos;
2606 try {
2607 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2608 } catch (RemoteException e) {
2609 Slog.e(TAG, "Failed to get package infos");
2610 return;
2611 }
satok91e88122011-07-18 11:11:42 +09002612 if (packageInfos != null) {
2613 final int packageNum = packageInfos.length;
2614 for (int i = 0; i < packageNum; ++i) {
2615 if (packageInfos[i].equals(imi.getPackageName())) {
2616 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002617 final long ident = Binder.clearCallingIdentity();
2618 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002619 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002620 } finally {
2621 Binder.restoreCallingIdentity(ident);
2622 }
satokee5e77c2011-09-02 18:50:15 +09002623 return;
satok91e88122011-07-18 11:11:42 +09002624 }
2625 }
2626 }
satoke7c6998e2011-06-03 17:57:59 +09002627 }
satokee5e77c2011-09-02 18:50:15 +09002628 return;
satoke7c6998e2011-06-03 17:57:59 +09002629 }
2630
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002631 @Override
2632 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002633 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002634 }
2635
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002636 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002637 public void clearLastInputMethodWindowForTransition(IBinder token) {
2638 if (!calledFromValidUser()) {
2639 return;
2640 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002641 synchronized (mMethodMap) {
2642 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002643 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002644 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002645 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002646 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002647 }
2648
2649 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002650 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002651 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002652 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002653 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002654 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002655 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2656 if (DEBUG) {
2657 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2658 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2659 + " actual: " + sequenceNumber);
2660 }
2661 return;
2662 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002663 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2664 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002665 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002666 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002667 }
2668 }
2669
satok28203512010-11-24 11:06:49 +09002670 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002672 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2673 }
2674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002676 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2677 if (token == null) {
2678 if (mContext.checkCallingOrSelfPermission(
2679 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2680 != PackageManager.PERMISSION_GRANTED) {
2681 throw new SecurityException(
2682 "Using null token requires permission "
2683 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002685 } else if (mCurToken != token) {
2686 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2687 + " token: " + token);
2688 return;
2689 }
2690
2691 final long ident = Binder.clearCallingIdentity();
2692 try {
2693 setInputMethodLocked(id, subtypeId);
2694 } finally {
2695 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 }
2697 }
2698
satok42c5a162011-05-26 16:46:14 +09002699 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002701 if (!calledFromValidUser()) {
2702 return;
2703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002705 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 return;
2707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 long ident = Binder.clearCallingIdentity();
2709 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002710 hideCurrentInputLocked(flags, null);
2711 } finally {
2712 Binder.restoreCallingIdentity(ident);
2713 }
2714 }
2715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002716
satok42c5a162011-05-26 16:46:14 +09002717 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002718 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002719 if (!calledFromValidUser()) {
2720 return;
2721 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002722 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002723 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002724 return;
2725 }
2726 long ident = Binder.clearCallingIdentity();
2727 try {
2728 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 } finally {
2730 Binder.restoreCallingIdentity(ident);
2731 }
2732 }
2733 }
2734
2735 void setEnabledSessionInMainThread(SessionState session) {
2736 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002737 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002739 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002740 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 } catch (RemoteException e) {
2742 }
2743 }
2744 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002745 if (mEnabledSession != null && mEnabledSession.session != null) {
2746 try {
2747 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2748 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2749 } catch (RemoteException e) {
2750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 }
2752 }
2753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002754
satok42c5a162011-05-26 16:46:14 +09002755 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002757 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002759 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002760 final boolean showAuxSubtypes;
2761 switch (msg.arg1) {
2762 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2763 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2764 // implemented so that auxiliary subtypes will be excluded when the soft
2765 // keyboard is invisible.
2766 showAuxSubtypes = mInputShown;
2767 break;
2768 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2769 showAuxSubtypes = true;
2770 break;
2771 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2772 showAuxSubtypes = false;
2773 break;
2774 default:
2775 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2776 return false;
2777 }
2778 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002779 return true;
2780
satok47a44912010-10-06 16:03:58 +09002781 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002782 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002783 return true;
2784
2785 case MSG_SHOW_IM_CONFIG:
2786 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002787 return true;
2788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 case MSG_UNBIND_INPUT:
2792 try {
2793 ((IInputMethod)msg.obj).unbindInput();
2794 } catch (RemoteException e) {
2795 // There is nothing interesting about the method dying.
2796 }
2797 return true;
2798 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002799 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 try {
2801 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2802 } catch (RemoteException e) {
2803 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002804 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 return true;
2806 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002807 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002809 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002810 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002811 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 } catch (RemoteException e) {
2813 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002814 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 return true;
2816 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002817 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002819 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002820 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002821 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 } catch (RemoteException e) {
2823 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002824 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002826 case MSG_HIDE_CURRENT_INPUT_METHOD:
2827 synchronized (mMethodMap) {
2828 hideCurrentInputLocked(0, null);
2829 }
2830 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002832 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002834 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2836 } catch (RemoteException e) {
2837 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002838 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002840 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002841 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002842 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002843 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002845 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002847 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002848 // Dispose the channel if the input method is not local to this process
2849 // because the remote proxy will get its own copy when unparceled.
2850 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002851 channel.dispose();
2852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002854 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002858
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002859 case MSG_START_INPUT: {
2860 int missingMethods = msg.arg1;
2861 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002863 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002865 session.method.startInput((IInputContext) args.arg2, missingMethods,
2866 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 } catch (RemoteException e) {
2868 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002869 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002871 }
2872 case MSG_RESTART_INPUT: {
2873 int missingMethods = msg.arg1;
2874 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002876 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002878 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2879 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 } catch (RemoteException e) {
2881 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002882 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002884 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002887
Yohei Yukawa33e81792015-11-17 21:14:42 -08002888 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002890 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 } catch (RemoteException e) {
2892 // There is nothing interesting about the last client dying.
2893 }
2894 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002895 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002896 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002897 IInputMethodClient client = (IInputMethodClient)args.arg1;
2898 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002900 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002902 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002903 } finally {
2904 // Dispose the channel if the input method is not local to this process
2905 // because the remote proxy will get its own copy when unparceled.
2906 if (res.channel != null && Binder.isProxy(client)) {
2907 res.channel.dispose();
2908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002910 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002912 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002913 case MSG_SET_ACTIVE:
2914 try {
2915 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2916 } catch (RemoteException e) {
2917 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2918 + ((ClientState)msg.obj).pid + " uid "
2919 + ((ClientState)msg.obj).uid);
2920 }
2921 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002922 case MSG_SET_INTERACTIVE:
2923 handleSetInteractive(msg.arg1 != 0);
2924 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002925 case MSG_SWITCH_IME:
2926 handleSwitchInputMethod(msg.arg1 != 0);
2927 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002928 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2929 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002930 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002931 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002932 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002933 } catch (RemoteException e) {
2934 Slog.w(TAG, "Got RemoteException sending "
2935 + "setUserActionNotificationSequenceNumber("
2936 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002937 + clientState.pid + " uid "
2938 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002939 }
2940 return true;
2941 }
satok01038492012-04-09 21:08:27 +09002942
2943 // --------------------------------------------------------------
2944 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002945 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002946 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002947 case MSG_SYSTEM_UNLOCK_USER:
2948 final int userId = msg.arg1;
2949 onUnlockUser(userId);
2950 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952 return false;
2953 }
2954
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002955 private void handleSetInteractive(final boolean interactive) {
2956 synchronized (mMethodMap) {
2957 mIsInteractive = interactive;
2958 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2959
2960 // Inform the current client of the change in active status
2961 if (mCurClient != null && mCurClient.client != null) {
2962 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2963 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2964 }
2965 }
2966 }
2967
Yohei Yukawaae61f712015-12-09 13:00:10 -08002968 private void handleSwitchInputMethod(final boolean forwardDirection) {
2969 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002970 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002971 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002972 if (nextSubtype == null) {
2973 return;
2974 }
2975 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002976 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2977 if (newInputMethodInfo == null) {
2978 return;
2979 }
2980 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
2981 newInputMethodInfo, mCurrentSubtype);
2982 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07002983 if (mSubtypeSwitchedByShortCutToast == null) {
2984 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
2985 Toast.LENGTH_SHORT);
2986 } else {
2987 mSubtypeSwitchedByShortCutToast.setText(toastText);
2988 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002989 mSubtypeSwitchedByShortCutToast.show();
2990 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08002991 }
2992 }
2993
satokdc9ddae2011-10-06 12:22:36 +09002994 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002995 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2996 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002997 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002998 if (DEBUG) {
2999 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3000 }
satok723a27e2010-11-11 14:58:11 +09003001 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003002 return true;
3003 }
3004
3005 return false;
3006 }
3007
Yohei Yukawa94e33302016-02-12 19:37:03 -08003008 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003009 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003010 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003011 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003012 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003013 if (!mSystemReady) {
3014 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3015 return;
3016 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003017 mMethodList.clear();
3018 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003019
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003020 // Use for queryIntentServicesAsUser
3021 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003023 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3024 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3025 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003026 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003028 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3029 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003030
satoke7c6998e2011-06-03 17:57:59 +09003031 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3032 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 for (int i = 0; i < services.size(); ++i) {
3034 ResolveInfo ri = services.get(i);
3035 ServiceInfo si = ri.serviceInfo;
3036 ComponentName compName = new ComponentName(si.packageName, si.name);
3037 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3038 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003039 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 + ": it does not require the permission "
3041 + android.Manifest.permission.BIND_INPUT_METHOD);
3042 continue;
3043 }
3044
Joe Onorato8a9b2202010-02-26 18:56:32 -08003045 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046
3047 try {
satoke7c6998e2011-06-03 17:57:59 +09003048 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003049 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003050 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003051 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052
3053 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003054 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003056 } catch (Exception e) {
3057 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
3059 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003060
Yohei Yukawa859df052016-02-17 07:56:46 -08003061 // TODO: The following code should find better place to live.
3062 if (!resetDefaultEnabledIme) {
3063 boolean enabledImeFound = false;
3064 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3065 final int N = enabledImes.size();
3066 for (int i = 0; i < N; ++i) {
3067 final InputMethodInfo imi = enabledImes.get(i);
3068 if (mMethodList.contains(imi)) {
3069 enabledImeFound = true;
3070 break;
3071 }
3072 }
3073 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003074 if (DEBUG) {
3075 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3076 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003077 resetDefaultEnabledIme = true;
3078 resetSelectedInputMethodAndSubtypeLocked("");
3079 }
3080 }
3081
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003082 if (resetDefaultEnabledIme) {
3083 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003084 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003085 final int N = defaultEnabledIme.size();
3086 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003087 final InputMethodInfo imi = defaultEnabledIme.get(i);
3088 if (DEBUG) {
3089 Slog.d(TAG, "--- enable ime = " + imi);
3090 }
3091 setInputMethodEnabledLocked(imi.getId(), true);
3092 }
3093 }
3094
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003095 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003096 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003097 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003098 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3099 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003100 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003101 }
3102 } else {
3103 // Double check that the default IME is certainly enabled.
3104 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003105 }
3106 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003107 // Here is not the perfect place to reset the switching controller. Ideally
3108 // mSwitchingController and mSettings should be able to share the same state.
3109 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3110 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003111 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003115
satok217f5482010-12-15 05:19:19 +09003116 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003117 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003118 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003119 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3120 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003121 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003122 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003123 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003124 final int userId;
3125 synchronized (mMethodMap) {
3126 userId = mSettings.getCurrentUserId();
3127 }
3128 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003129 }
3130
3131 private void showConfigureInputMethods() {
3132 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3133 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3134 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3135 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003136 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003137 }
3138
satok2c93efc2012-04-02 19:33:47 +09003139 private boolean isScreenLocked() {
3140 return mKeyguardManager != null
3141 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3142 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003143
Seigo Nonaka14e13912015-05-06 21:04:13 -07003144 private void showInputMethodMenu(boolean showAuxSubtypes) {
3145 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003148 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003149
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003150 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003151 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003152 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003153
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003154 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003155 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003156 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3157 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003158 if (immis == null || immis.size() == 0) {
3159 return;
3160 }
3161
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003162 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163
satok688bd472012-02-09 20:09:17 +09003164 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003165 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003166 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003167
satokc3690562012-01-10 20:14:43 +09003168 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003169 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003170 if (currentSubtype != null) {
3171 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003172 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3173 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003174 }
3175 }
3176
Ken Wakasa761eb372011-03-04 19:06:18 +09003177 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003178 mIms = new InputMethodInfo[N];
3179 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003180 int checkedItem = 0;
3181 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003182 final ImeSubtypeListItem item = imList.get(i);
3183 mIms[i] = item.mImi;
3184 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003185 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003186 int subtypeId = mSubtypeIds[i];
3187 if ((subtypeId == NOT_A_SUBTYPE_ID)
3188 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3189 || (subtypeId == lastInputMethodSubtypeId)) {
3190 checkedItem = i;
3191 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003194
3195 final Context settingsContext = new ContextThemeWrapper(context,
3196 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3197
3198 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003199 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3200 @Override
3201 public void onCancel(DialogInterface dialog) {
3202 hideInputMethodMenu();
3203 }
3204 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003205
3206 final Context dialogContext = mDialogBuilder.getContext();
3207 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3208 com.android.internal.R.styleable.DialogPreference,
3209 com.android.internal.R.attr.alertDialogStyle, 0);
3210 final Drawable dialogIcon = a.getDrawable(
3211 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3212 a.recycle();
3213
3214 mDialogBuilder.setIcon(dialogIcon);
3215
Yohei Yukawad34e1482016-02-11 08:03:52 -08003216 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003217 final View tv = inflater.inflate(
3218 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3219 mDialogBuilder.setCustomTitle(tv);
3220
3221 // Setup layout for a toggle switch of the hardware keyboard
3222 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003223 mSwitchingDialogTitleView
3224 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003225 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003226 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003227 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003228 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003229 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003230 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3231 @Override
3232 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003233 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003234 // Ensure that the input method dialog is dismissed when changing
3235 // the hardware keyboard state.
3236 hideInputMethodMenu();
3237 }
3238 });
3239
Alan Viverette505e3ab2014-11-24 15:22:11 -08003240 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003241 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3242 final OnClickListener choiceListener = new OnClickListener() {
3243 @Override
3244 public void onClick(final DialogInterface dialog, final int which) {
3245 synchronized (mMethodMap) {
3246 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3247 || mSubtypeIds.length <= which) {
3248 return;
satok01038492012-04-09 21:08:27 +09003249 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003250 final InputMethodInfo im = mIms[which];
3251 int subtypeId = mSubtypeIds[which];
3252 adapter.mCheckedItem = which;
3253 adapter.notifyDataSetChanged();
3254 hideInputMethodMenu();
3255 if (im != null) {
3256 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3257 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003258 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003259 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003261 }
3262 }
3263 };
3264 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003267 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003268 final Window w = mSwitchingDialog.getWindow();
3269 final WindowManager.LayoutParams attrs = w.getAttributes();
3270 w.setType(TYPE_INPUT_METHOD_DIALOG);
3271 // Use an alternate token for the dialog for that window manager can group the token
3272 // with other IME windows based on type vs. grouping based on whichever token happens
3273 // to get selected by the system later on.
3274 attrs.token = mSwitchingDialogToken;
3275 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3276 attrs.setTitle("Select input method");
3277 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003278 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 mSwitchingDialog.show();
3280 }
3281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003282
Ken Wakasa05dbb652011-08-22 15:22:43 +09003283 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3284 private final LayoutInflater mInflater;
3285 private final int mTextViewResourceId;
3286 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003287 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003288 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3289 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3290 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003291
Ken Wakasa05dbb652011-08-22 15:22:43 +09003292 mTextViewResourceId = textViewResourceId;
3293 mItemsList = itemsList;
3294 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003295 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003296 }
3297
3298 @Override
3299 public View getView(int position, View convertView, ViewGroup parent) {
3300 final View view = convertView != null ? convertView
3301 : mInflater.inflate(mTextViewResourceId, null);
3302 if (position < 0 || position >= mItemsList.size()) return view;
3303 final ImeSubtypeListItem item = mItemsList.get(position);
3304 final CharSequence imeName = item.mImeName;
3305 final CharSequence subtypeName = item.mSubtypeName;
3306 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3307 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3308 if (TextUtils.isEmpty(subtypeName)) {
3309 firstTextView.setText(imeName);
3310 secondTextView.setVisibility(View.GONE);
3311 } else {
3312 firstTextView.setText(subtypeName);
3313 secondTextView.setText(imeName);
3314 secondTextView.setVisibility(View.VISIBLE);
3315 }
3316 final RadioButton radioButton =
3317 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3318 radioButton.setChecked(position == mCheckedItem);
3319 return view;
3320 }
3321 }
3322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003324 synchronized (mMethodMap) {
3325 hideInputMethodMenuLocked();
3326 }
3327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003328
The Android Open Source Project10592532009-03-18 17:39:46 -07003329 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003330 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331
The Android Open Source Project10592532009-03-18 17:39:46 -07003332 if (mSwitchingDialog != null) {
3333 mSwitchingDialog.dismiss();
3334 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003336
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003337 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003338 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003339 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003343
satok42c5a162011-05-26 16:46:14 +09003344 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003346 // TODO: Make this work even for non-current users?
3347 if (!calledFromValidUser()) {
3348 return false;
3349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 synchronized (mMethodMap) {
3351 if (mContext.checkCallingOrSelfPermission(
3352 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3353 != PackageManager.PERMISSION_GRANTED) {
3354 throw new SecurityException(
3355 "Requires permission "
3356 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3357 }
Anna Galusza9b278112016-01-04 11:37:37 -08003358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 long ident = Binder.clearCallingIdentity();
3360 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003361 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 } finally {
3363 Binder.restoreCallingIdentity(ident);
3364 }
3365 }
3366 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003367
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003368 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3369 // Make sure this is a valid input method.
3370 InputMethodInfo imm = mMethodMap.get(id);
3371 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003372 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003373 }
3374
satokd87c2592010-09-29 11:52:06 +09003375 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3376 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003377
satokd87c2592010-09-29 11:52:06 +09003378 if (enabled) {
3379 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3380 if (pair.first.equals(id)) {
3381 // We are enabling this input method, but it is already enabled.
3382 // Nothing to do. The previous state was enabled.
3383 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003384 }
3385 }
satokd87c2592010-09-29 11:52:06 +09003386 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3387 // Previous state was disabled.
3388 return false;
3389 } else {
3390 StringBuilder builder = new StringBuilder();
3391 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3392 builder, enabledInputMethodsList, id)) {
3393 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003394 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003395 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3396 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3397 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003398 }
3399 // Previous state was enabled.
3400 return true;
3401 } else {
3402 // We are disabling the input method but it is already disabled.
3403 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003404 return false;
3405 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003406 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003407 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003408
satok723a27e2010-11-11 14:58:11 +09003409 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3410 boolean setSubtypeOnly) {
3411 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003412 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003413
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003414 mCurUserActionNotificationSequenceNumber =
3415 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3416 if (DEBUG) {
3417 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3418 + mCurUserActionNotificationSequenceNumber);
3419 }
3420
3421 if (mCurClient != null && mCurClient.client != null) {
3422 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3423 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003424 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003425 }
3426
satok723a27e2010-11-11 14:58:11 +09003427 // Set Subtype here
3428 if (imi == null || subtypeId < 0) {
3429 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003430 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003431 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003432 if (subtypeId < imi.getSubtypeCount()) {
3433 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3434 mSettings.putSelectedSubtype(subtype.hashCode());
3435 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003436 } else {
3437 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003438 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003439 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003440 }
satokab751aa2010-09-14 19:17:36 +09003441 }
satok723a27e2010-11-11 14:58:11 +09003442
Yohei Yukawa68645a62016-02-17 07:54:20 -08003443 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003444 // Set InputMethod here
3445 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3446 }
3447 }
3448
3449 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3450 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3451 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3452 // newDefaultIme is empty when there is no candidate for the selected IME.
3453 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3454 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3455 if (subtypeHashCode != null) {
3456 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003457 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003458 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003459 } catch (NumberFormatException e) {
3460 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3461 }
3462 }
3463 }
3464 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003465 }
3466
satok4e4569d2010-11-19 18:45:53 +09003467 // If there are no selected shortcuts, tries finding the most applicable ones.
3468 private Pair<InputMethodInfo, InputMethodSubtype>
3469 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3470 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3471 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003472 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003473 boolean foundInSystemIME = false;
3474
3475 // Search applicable subtype for each InputMethodInfo
3476 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003477 final String imiId = imi.getId();
3478 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3479 continue;
3480 }
satokcd7cd292010-11-20 15:46:23 +09003481 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003482 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003483 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003484 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003485 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003486 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003487 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003488 }
satokdf31ae62011-01-15 06:19:44 +09003489 // 2. Search by the system locale from enabledSubtypes.
3490 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003491 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003492 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003493 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003494 }
satoka86f5e42011-09-02 17:12:42 +09003495 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003496 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003497 final ArrayList<InputMethodSubtype> subtypesForSearch =
3498 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003499 ? InputMethodUtils.getSubtypes(imi)
3500 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003501 // 4. Search by the current subtype's locale from all subtypes.
3502 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003503 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003504 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003505 }
3506 // 5. Search by the system locale from all subtypes.
3507 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003508 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003509 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003510 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003511 }
satokcd7cd292010-11-20 15:46:23 +09003512 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003513 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003514 // The current input method is the most applicable IME.
3515 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003516 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003517 break;
satok7599a7f2010-12-22 13:45:23 +09003518 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003519 // The system input method is 2nd applicable IME.
3520 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003521 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003522 if ((imi.getServiceInfo().applicationInfo.flags
3523 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3524 foundInSystemIME = true;
3525 }
satok4e4569d2010-11-19 18:45:53 +09003526 }
3527 }
3528 }
3529 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003530 if (mostApplicableIMI != null) {
3531 Slog.w(TAG, "Most applicable shortcut input method was:"
3532 + mostApplicableIMI.getId());
3533 if (mostApplicableSubtype != null) {
3534 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3535 + "," + mostApplicableSubtype.getMode() + ","
3536 + mostApplicableSubtype.getLocale());
3537 }
3538 }
satok4e4569d2010-11-19 18:45:53 +09003539 }
satokcd7cd292010-11-20 15:46:23 +09003540 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003541 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003542 } else {
3543 return null;
3544 }
3545 }
3546
satokab751aa2010-09-14 19:17:36 +09003547 /**
3548 * @return Return the current subtype of this input method.
3549 */
satok42c5a162011-05-26 16:46:14 +09003550 @Override
satokab751aa2010-09-14 19:17:36 +09003551 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003552 // TODO: Make this work even for non-current users?
3553 if (!calledFromValidUser()) {
3554 return null;
3555 }
3556 synchronized (mMethodMap) {
3557 return getCurrentInputMethodSubtypeLocked();
3558 }
3559 }
3560
3561 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003562 if (mCurMethodId == null) {
3563 return null;
3564 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003565 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003566 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3567 if (imi == null || imi.getSubtypeCount() == 0) {
3568 return null;
satok4e4569d2010-11-19 18:45:53 +09003569 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003570 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003571 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3572 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003573 if (subtypeId == NOT_A_SUBTYPE_ID) {
3574 // If there are no selected subtypes, the framework will try to find
3575 // the most applicable subtype from explicitly or implicitly enabled
3576 // subtypes.
3577 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003578 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003579 // If there is only one explicitly or implicitly enabled subtype,
3580 // just returns it.
3581 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3582 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3583 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003584 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003585 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003586 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003587 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003588 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003589 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3590 true);
satok4e4569d2010-11-19 18:45:53 +09003591 }
satok3ef8b292010-11-23 06:06:29 +09003592 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003593 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003594 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003595 }
3596 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003597 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003598 }
3599
satok4e4569d2010-11-19 18:45:53 +09003600 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003601 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003602 @Override
satok4e4569d2010-11-19 18:45:53 +09003603 public List getShortcutInputMethodsAndSubtypes() {
3604 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003605 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003606 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003607 // If there are no selected shortcut subtypes, the framework will try to find
3608 // the most applicable subtype from all subtypes whose mode is
3609 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003610 Pair<InputMethodInfo, InputMethodSubtype> info =
3611 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003612 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003613 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003614 ret.add(info.first);
3615 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003616 }
satok3da92232011-01-11 22:46:30 +09003617 return ret;
satokf3db1af2010-11-23 13:34:33 +09003618 }
satokf3db1af2010-11-23 13:34:33 +09003619 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3620 ret.add(imi);
3621 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3622 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003623 }
3624 }
satokf3db1af2010-11-23 13:34:33 +09003625 return ret;
satok4e4569d2010-11-19 18:45:53 +09003626 }
3627 }
3628
satok42c5a162011-05-26 16:46:14 +09003629 @Override
satokb66d2872010-11-10 01:04:04 +09003630 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003631 // TODO: Make this work even for non-current users?
3632 if (!calledFromValidUser()) {
3633 return false;
3634 }
satokb66d2872010-11-10 01:04:04 +09003635 synchronized (mMethodMap) {
3636 if (subtype != null && mCurMethodId != null) {
3637 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003638 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003639 if (subtypeId != NOT_A_SUBTYPE_ID) {
3640 setInputMethodLocked(mCurMethodId, subtypeId);
3641 return true;
3642 }
3643 }
3644 return false;
3645 }
3646 }
3647
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003648 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003649 private static class InputMethodFileManager {
3650 private static final String SYSTEM_PATH = "system";
3651 private static final String INPUT_METHOD_PATH = "inputmethod";
3652 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3653 private static final String NODE_SUBTYPES = "subtypes";
3654 private static final String NODE_SUBTYPE = "subtype";
3655 private static final String NODE_IMI = "imi";
3656 private static final String ATTR_ID = "id";
3657 private static final String ATTR_LABEL = "label";
3658 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003659 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003660 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003661 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003662 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3663 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3664 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003665 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003666 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3667 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003668 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003669 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003670 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003671 if (methodMap == null) {
3672 throw new NullPointerException("methodMap is null");
3673 }
3674 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003675 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003676 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3677 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003678 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003679 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003680 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3681 }
3682 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3683 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3684 if (!subtypeFile.exists()) {
3685 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003686 writeAdditionalInputMethodSubtypes(
3687 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003688 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003689 readAdditionalInputMethodSubtypes(
3690 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003691 }
3692 }
3693
3694 private void deleteAllInputMethodSubtypes(String imiId) {
3695 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003696 mAdditionalSubtypesMap.remove(imiId);
3697 writeAdditionalInputMethodSubtypes(
3698 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003699 }
3700 }
3701
3702 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003703 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003704 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003705 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003706 final int N = additionalSubtypes.length;
3707 for (int i = 0; i < N; ++i) {
3708 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003709 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003710 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003711 } else {
3712 Slog.w(TAG, "Duplicated subtype definition found: "
3713 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003714 }
3715 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003716 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3717 writeAdditionalInputMethodSubtypes(
3718 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003719 }
3720 }
3721
3722 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3723 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003724 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003725 }
3726 }
3727
3728 private static void writeAdditionalInputMethodSubtypes(
3729 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3730 HashMap<String, InputMethodInfo> methodMap) {
3731 // Safety net for the case that this function is called before methodMap is set.
3732 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3733 FileOutputStream fos = null;
3734 try {
3735 fos = subtypesFile.startWrite();
3736 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003737 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003738 out.startDocument(null, true);
3739 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3740 out.startTag(null, NODE_SUBTYPES);
3741 for (String imiId : allSubtypes.keySet()) {
3742 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3743 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3744 continue;
3745 }
3746 out.startTag(null, NODE_IMI);
3747 out.attribute(null, ATTR_ID, imiId);
3748 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3749 final int N = subtypesList.size();
3750 for (int i = 0; i < N; ++i) {
3751 final InputMethodSubtype subtype = subtypesList.get(i);
3752 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003753 if (subtype.hasSubtypeId()) {
3754 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3755 String.valueOf(subtype.getSubtypeId()));
3756 }
satoke7c6998e2011-06-03 17:57:59 +09003757 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3758 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3759 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003760 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3761 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003762 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3763 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3764 out.attribute(null, ATTR_IS_AUXILIARY,
3765 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003766 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3767 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003768 out.endTag(null, NODE_SUBTYPE);
3769 }
3770 out.endTag(null, NODE_IMI);
3771 }
3772 out.endTag(null, NODE_SUBTYPES);
3773 out.endDocument();
3774 subtypesFile.finishWrite(fos);
3775 } catch (java.io.IOException e) {
3776 Slog.w(TAG, "Error writing subtypes", e);
3777 if (fos != null) {
3778 subtypesFile.failWrite(fos);
3779 }
3780 }
3781 }
3782
3783 private static void readAdditionalInputMethodSubtypes(
3784 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3785 if (allSubtypes == null || subtypesFile == null) return;
3786 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003787 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003788 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003789 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003790 int type = parser.getEventType();
3791 // Skip parsing until START_TAG
3792 while ((type = parser.next()) != XmlPullParser.START_TAG
3793 && type != XmlPullParser.END_DOCUMENT) {}
3794 String firstNodeName = parser.getName();
3795 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3796 throw new XmlPullParserException("Xml doesn't start with subtypes");
3797 }
3798 final int depth =parser.getDepth();
3799 String currentImiId = null;
3800 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3801 while (((type = parser.next()) != XmlPullParser.END_TAG
3802 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3803 if (type != XmlPullParser.START_TAG)
3804 continue;
3805 final String nodeName = parser.getName();
3806 if (NODE_IMI.equals(nodeName)) {
3807 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3808 if (TextUtils.isEmpty(currentImiId)) {
3809 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3810 continue;
3811 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003812 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003813 allSubtypes.put(currentImiId, tempSubtypesArray);
3814 } else if (NODE_SUBTYPE.equals(nodeName)) {
3815 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3816 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3817 continue;
3818 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003819 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003820 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003821 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003822 parser.getAttributeValue(null, ATTR_LABEL));
3823 final String imeSubtypeLocale =
3824 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003825 final String languageTag =
3826 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003827 final String imeSubtypeMode =
3828 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3829 final String imeSubtypeExtraValue =
3830 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003831 final boolean isAuxiliary = "1".equals(String.valueOf(
3832 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003833 final boolean isAsciiCapable = "1".equals(String.valueOf(
3834 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003835 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003836 .setSubtypeNameResId(label)
3837 .setSubtypeIconResId(icon)
3838 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003839 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003840 .setSubtypeMode(imeSubtypeMode)
3841 .setSubtypeExtraValue(imeSubtypeExtraValue)
3842 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003843 .setIsAsciiCapable(isAsciiCapable);
3844 final String subtypeIdString =
3845 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3846 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003847 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003848 }
3849 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003850 }
3851 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003852 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3853 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003854 return;
satoke7c6998e2011-06-03 17:57:59 +09003855 }
3856 }
3857 }
3858
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003859 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3860 @NonNull
3861 private final Handler mHandler;
3862
3863 LocalServiceImpl(@NonNull final Handler handler) {
3864 mHandler = handler;
3865 }
3866
3867 @Override
3868 public void setInteractive(boolean interactive) {
3869 // Do everything in handler so as not to block the caller.
3870 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3871 interactive ? 1 : 0, 0));
3872 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003873
3874 @Override
3875 public void switchInputMethod(boolean forwardDirection) {
3876 // Do everything in handler so as not to block the caller.
3877 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3878 forwardDirection ? 1 : 0, 0));
3879 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003880
3881 @Override
3882 public void hideCurrentInputMethod() {
3883 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3884 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3885 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003886 }
3887
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003888 private static String imeWindowStatusToString(final int imeWindowVis) {
3889 final StringBuilder sb = new StringBuilder();
3890 boolean first = true;
3891 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3892 sb.append("Active");
3893 first = false;
3894 }
3895 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3896 if (!first) {
3897 sb.append("|");
3898 }
3899 sb.append("Visible");
3900 }
3901 return sb.toString();
3902 }
3903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003905 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3906 @Nullable Uri contentUri, @Nullable String packageName) {
3907 if (!calledFromValidUser()) {
3908 return null;
3909 }
3910
3911 if (token == null) {
3912 throw new NullPointerException("token");
3913 }
3914 if (packageName == null) {
3915 throw new NullPointerException("packageName");
3916 }
3917 if (contentUri == null) {
3918 throw new NullPointerException("contentUri");
3919 }
3920 final String contentUriScheme = contentUri.getScheme();
3921 if (!"content".equals(contentUriScheme)) {
3922 throw new InvalidParameterException("contentUri must have content scheme");
3923 }
3924
3925 synchronized (mMethodMap) {
3926 final int uid = Binder.getCallingUid();
3927 if (mCurMethodId == null) {
3928 return null;
3929 }
3930 if (mCurToken != token) {
3931 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3932 + " token=" + token);
3933 return null;
3934 }
3935 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3936 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3937 // nature of our system. Let's compare it with our internal record.
3938 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3939 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3940 + mCurAttribute.packageName + " packageName=" + packageName);
3941 return null;
3942 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003943 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003944 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003945 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003946 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003947 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
3948 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
3949 imeUserId);
3950 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
3951 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
3952 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
3953 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
3954 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
3955 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
3956 // actually allowed to "uid", which is guaranteed to be the IME's one.
3957 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
3958 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07003959 }
3960 }
3961
3962 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3964 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3965 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3968 + Binder.getCallingPid()
3969 + ", uid=" + Binder.getCallingUid());
3970 return;
3971 }
3972
3973 IInputMethod method;
3974 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003975 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 synchronized (mMethodMap) {
3980 p.println("Current Input Method Manager state:");
3981 int N = mMethodList.size();
3982 p.println(" Input Methods:");
3983 for (int i=0; i<N; i++) {
3984 InputMethodInfo info = mMethodList.get(i);
3985 p.println(" InputMethod #" + i + ":");
3986 info.dump(p, " ");
3987 }
3988 p.println(" Clients:");
3989 for (ClientState ci : mClients.values()) {
3990 p.println(" Client " + ci + ":");
3991 p.println(" client=" + ci.client);
3992 p.println(" inputContext=" + ci.inputContext);
3993 p.println(" sessionRequested=" + ci.sessionRequested);
3994 p.println(" curSession=" + ci.curSession);
3995 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003996 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003998 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3999 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004000 focusedWindowClient = mCurFocusedWindowClient;
4001 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4003 + " mBoundToMethod=" + mBoundToMethod);
4004 p.println(" mCurToken=" + mCurToken);
4005 p.println(" mCurIntent=" + mCurIntent);
4006 method = mCurMethod;
4007 p.println(" mCurMethod=" + mCurMethod);
4008 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004009 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 p.println(" mShowRequested=" + mShowRequested
4011 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4012 + " mShowForced=" + mShowForced
4013 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004014 p.println(" mCurUserActionNotificationSequenceNumber="
4015 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004016 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004017 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004018 p.println(" mSwitchingController:");
4019 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004020 p.println(" mSettings:");
4021 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004023
Jeff Brownb88102f2010-09-08 11:49:43 -07004024 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 pw.flush();
4027 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004028 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4029 } catch (IOException | RemoteException e) {
4030 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004032 } else {
4033 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004035
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004036 if (focusedWindowClient != null && client != focusedWindowClient) {
4037 p.println(" ");
4038 p.println("Warning: Current input method client doesn't match the last focused. "
4039 + "window.");
4040 p.println("Dumping input method client in the last focused window just in case.");
4041 p.println(" ");
4042 pw.flush();
4043 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004044 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4045 } catch (IOException | RemoteException e) {
4046 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004047 }
4048 }
4049
Jeff Brownb88102f2010-09-08 11:49:43 -07004050 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 pw.flush();
4053 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004054 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4055 } catch (IOException | RemoteException e) {
4056 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004058 } else {
4059 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
4061 }
4062}