blob: f0f50f07fccab9019b916d3e9bb10557999fbca8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070032import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070033import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090034import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import com.android.internal.view.IInputContext;
36import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import com.android.internal.view.IInputMethodClient;
38import com.android.internal.view.IInputMethodManager;
39import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070040import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080042import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080043import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
satoke7c6998e2011-06-03 17:57:59 +090045import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090047import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070049import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070050import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070051import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080052import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080053import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070054import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070056import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090057import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090058import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090059import android.app.Notification;
60import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070061import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090062import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080064import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.ContentResolver;
66import android.content.Context;
67import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090069import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090071import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070073import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090074import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.pm.PackageManager;
76import android.content.pm.ResolveInfo;
77import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070078import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.content.res.Resources;
80import android.content.res.TypedArray;
81import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080082import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080083import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080084import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070085import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040087import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070088import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090089import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.Handler;
91import android.os.IBinder;
92import android.os.IInterface;
93import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070094import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070096import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080098import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.os.ServiceManager;
100import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900101import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800102import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.provider.Settings;
104import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900105import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700106import android.util.ArrayMap;
107import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700108import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900110import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900111import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.util.PrintWriterPrinter;
113import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900114import android.util.Slog;
115import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900116import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700118import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900119import android.view.LayoutInflater;
120import android.view.View;
121import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700122import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700124import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900125import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700127import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import android.view.inputmethod.InputMethod;
129import android.view.inputmethod.InputMethodInfo;
130import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700131import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900132import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900133import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900134import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900135import android.widget.CompoundButton;
136import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900137import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900138import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900139import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700140import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
satoke7c6998e2011-06-03 17:57:59 +0900142import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900144import java.io.FileInputStream;
145import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146import java.io.IOException;
147import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700148import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100149import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700150import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900152import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153import java.util.HashMap;
154import java.util.List;
155
156/**
157 * This class provides a system service that manages input methods.
158 */
159public class InputMethodManagerService extends IInputMethodManager.Stub
160 implements ServiceConnection, Handler.Callback {
161 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700162 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700163 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700165 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
166 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
167 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 static final int MSG_UNBIND_INPUT = 1000;
170 static final int MSG_BIND_INPUT = 1010;
171 static final int MSG_SHOW_SOFT_INPUT = 1020;
172 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700173 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 static final int MSG_ATTACH_TOKEN = 1040;
175 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 static final int MSG_START_INPUT = 2000;
178 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
Yohei Yukawa33e81792015-11-17 21:14:42 -0800180 static final int MSG_UNBIND_CLIENT = 3000;
181 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700182 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700183 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900184 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800185 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800186 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
satok01038492012-04-09 21:08:27 +0900188 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
189
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700190 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
191
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700192 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
satokf9f01002011-05-19 21:31:50 +0900194 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
195
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900196 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900197 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900198
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700199 @Retention(SOURCE)
200 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
201 private @interface HardKeyboardBehavior {
202 int WIRELESS_AFFORDANCE = 0;
203 int WIRED_AFFORDANCE = 1;
204 }
satok4e4569d2010-11-19 18:45:53 +0900205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800207 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900209 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700212 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700214 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900215 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900216 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900217 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800218 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900220 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 // All known input methods. mMethodMap also serves as the global
223 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700224 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
225 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900226 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700227 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900228 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700230 // Used to bring IME service up to visible adjustment while it is being shown.
231 final ServiceConnection mVisibleConnection = new ServiceConnection() {
232 @Override public void onServiceConnected(ComponentName name, IBinder service) {
233 }
234
235 @Override public void onServiceDisconnected(ComponentName name) {
236 }
237 };
238 boolean mVisibleBound = false;
239
satok7cfc0ed2011-06-20 21:29:36 +0900240 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700241 private NotificationManager mNotificationManager;
242 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700243 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400244 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700245 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900246 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900247 private boolean mNotificationShown;
248
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900249 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final ClientState client;
251 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700252
253 IInputMethodSession session;
254 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 @Override
257 public String toString() {
258 return "SessionState{uid " + client.uid + " pid " + client.pid
259 + " method " + Integer.toHexString(
260 System.identityHashCode(method))
261 + " session " + Integer.toHexString(
262 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700263 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 + "}";
265 }
266
267 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700268 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 client = _client;
270 method = _method;
271 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700272 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
Jeff Brownc28867a2013-03-26 15:42:39 -0700276 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 final IInputMethodClient client;
278 final IInputContext inputContext;
279 final int uid;
280 final int pid;
281 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 boolean sessionRequested;
284 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 @Override
287 public String toString() {
288 return "ClientState{" + Integer.toHexString(
289 System.identityHashCode(this)) + " uid " + uid
290 + " pid " + pid + "}";
291 }
292
293 ClientState(IInputMethodClient _client, IInputContext _inputContext,
294 int _uid, int _pid) {
295 client = _client;
296 inputContext = _inputContext;
297 uid = _uid;
298 pid = _pid;
299 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
300 }
301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700303 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700306 * Set once the system is ready to run third party code.
307 */
308 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700310 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700311 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
312 * method. This is to be synchronized with the secure settings keyed with
313 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
314 *
315 * <p>This can be transiently {@code null} when the system is re-initializing input method
316 * settings, e.g., the system locale is just changed.</p>
317 *
318 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
319 * {@link InputMethodManagerService}.</p>
320 *
321 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700323 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * The current binding sequence number, incremented every time there is
328 * a new bind performed.
329 */
330 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * The client that is currently bound to an input method.
334 */
335 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800338 * The last window token that we confirmed to be focused. This is always updated upon reports
339 * from the input method client. If the window state is already changed before the report is
340 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700341 */
342 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700344 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800345 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
346 */
347 ClientState mCurFocusedWindowClient;
348
349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * The input context last provided by the current client.
351 */
352 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700355 * The missing method flags for the input context last provided by the current client.
356 *
357 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
358 */
359 int mCurInputContextMissingMethods;
360
361 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 * The attributes last provided by the current client.
363 */
364 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700367 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700369 *
370 * <p>This can be {@code null} when no input method is connected.</p>
371 *
372 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700374 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 /**
satokab751aa2010-09-14 19:17:36 +0900378 * The current subtype of the current input method.
379 */
380 private InputMethodSubtype mCurrentSubtype;
381
satok4e4569d2010-11-19 18:45:53 +0900382 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900383 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700384 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900385
John Spurlocke0980502013-10-25 11:59:29 -0400386 // Was the keyguard locked when this client became current?
387 private boolean mCurClientInKeyguard;
388
satokab751aa2010-09-14 19:17:36 +0900389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * Set to true if our ServiceConnection is currently actively bound to
391 * a service (whether or not we have gotten its IBinder back yet).
392 */
393 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 /**
396 * Set if the client has asked for the input method to be shown.
397 */
398 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 /**
401 * Set if we were explicitly told to show the input method.
402 */
403 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 /**
406 * Set if we were forced to be shown.
407 */
408 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 /**
411 * Set if we last told the input method to show itself.
412 */
413 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800416 * {@code true} if the current input method is in fullscreen mode.
417 */
418 boolean mInFullscreenMode;
419
420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 * The Intent used to connect to the current input method.
422 */
423 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * The token we have made for the currently active input method, to
427 * identify it in the future.
428 */
429 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 /**
432 * If non-null, this is the input method service we are currently connected
433 * to.
434 */
435 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 /**
438 * Time that we last initiated a bind to the input method, to determine
439 * if we should try to disconnect and reconnect to it.
440 */
441 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 /**
444 * Have we called mCurMethod.bindInput()?
445 */
446 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 /**
449 * Currently enabled session. Only touched by service thread, not
450 * protected by a lock.
451 */
452 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700455 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700457 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900459 int mCurUserActionNotificationSequenceNumber = 0;
460
Joe Onorato857fd9b2011-01-27 15:08:35 -0800461 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900462
463 /**
464 * A set of status bits regarding the active IME.
465 *
466 * <p>This value is a combination of following two bits:</p>
467 * <dl>
468 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
469 * <dd>
470 * If this bit is ON, connected IME is ready to accept touch/key events.
471 * </dd>
472 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
473 * <dd>
474 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
475 * </dd>
476 * </dl>
477 * <em>Do not update this value outside of setImeWindowStatus.</em>
478 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800479 int mImeWindowVis;
480
Ken Wakasa05dbb652011-08-22 15:22:43 +0900481 private AlertDialog.Builder mDialogBuilder;
482 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700483 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900484 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700485 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900486 private InputMethodInfo[] mIms;
487 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800488 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700489 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800490 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900491 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
492 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500493 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700494 @HardKeyboardBehavior
495 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700498 int mUserId;
499 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700500 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800501 String mLastEnabled = "";
502
Yohei Yukawa81482972015-06-04 00:58:59 -0700503 /**
504 * <em>This constructor must be called within the lock.</em>
505 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 SettingsObserver(Handler handler) {
507 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700508 }
509
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800510 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700511 if (mRegistered && mUserId == userId) {
512 return;
513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700515 if (mRegistered) {
516 mContext.getContentResolver().unregisterContentObserver(this);
517 mRegistered = false;
518 }
519 if (mUserId != userId) {
520 mLastEnabled = "";
521 mUserId = userId;
522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700524 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900525 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700526 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900527 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700528 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700529 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700530 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800531 resolver.registerContentObserver(Settings.Secure.getUriFor(
532 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700533 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800535
Michael Wright7b5a96b2014-08-09 19:28:42 -0700536 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800537 final Uri showImeUri = Settings.Secure.getUriFor(
538 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
539 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
540 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700542 if (showImeUri.equals(uri)) {
543 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800544 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
545 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
546 mContext.getContentResolver(),
547 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
548 0, mUserId) == 1;
549 if (mAccessibilityRequestingNoSoftKeyboard) {
550 final boolean showRequested = mShowRequested;
551 hideCurrentInputLocked(0, null);
552 mShowRequested = showRequested;
553 } else if (mShowRequested) {
554 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
555 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700556 } else {
557 boolean enabledChanged = false;
558 String newEnabled = mSettings.getEnabledInputMethodsStr();
559 if (!mLastEnabled.equals(newEnabled)) {
560 mLastEnabled = newEnabled;
561 enabledChanged = true;
562 }
563 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700567
568 @Override
569 public String toString() {
570 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
571 + " mLastEnabled=" + mLastEnabled + "}";
572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
Yohei Yukawa79247822017-01-23 15:26:15 -0800575 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900576 @Override
577 public void onReceive(Context context, Intent intent) {
578 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700579 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900580 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700581 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900582 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800583 } else if (Intent.ACTION_USER_ADDED.equals(action)
584 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100585 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800586 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700587 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
588 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
589 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
590 final String prevValue = intent.getStringExtra(
591 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
592 final String newValue = intent.getStringExtra(
593 Intent.EXTRA_SETTING_NEW_VALUE);
594 restoreEnabledInputMethods(mContext, prevValue, newValue);
595 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800596 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
597 synchronized (mMethodMap) {
598 resetStateIfCurrentLocaleChangedLocked();
599 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900600 } else {
601 Slog.w(TAG, "Unexpected intent " + intent);
602 }
603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800605
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700606 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
607 // does not attempt to validate on the fly with any installed device policy, so must only
608 // be run in the context of initial device setup.
609 //
610 // TODO: Move this method to InputMethodUtils with adding unit tests.
611 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
612 if (DEBUG_RESTORE) {
613 Slog.i(TAG, "Restoring enabled input methods:");
614 Slog.i(TAG, "prev=" + prevValue);
615 Slog.i(TAG, " new=" + newValue);
616 }
617 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900618 ArrayMap<String, ArraySet<String>> prevMap =
619 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
620 ArrayMap<String, ArraySet<String>> newMap =
621 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700622
623 // Merge the restored ime+subtype enabled states into the live state
624 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
625 final String imeId = entry.getKey();
626 ArraySet<String> prevSubtypes = prevMap.get(imeId);
627 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700628 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700629 prevMap.put(imeId, prevSubtypes);
630 }
631 prevSubtypes.addAll(entry.getValue());
632 }
633
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700634 final String mergedImesAndSubtypesString =
635 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700636 if (DEBUG_RESTORE) {
637 Slog.i(TAG, "Merged IME string:");
638 Slog.i(TAG, " " + mergedImesAndSubtypesString);
639 }
640 Settings.Secure.putString(context.getContentResolver(),
641 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
642 }
643
Yohei Yukawac4e44912017-02-09 19:30:22 -0800644 final class MyPackageMonitor extends PackageMonitor {
645 /**
646 * Set of packages to be monitored.
647 *
648 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
649 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
650 * dynamically unless the entire package is updated, which also always triggers package
651 * rescanning.</p>
652 */
653 @GuardedBy("mMethodMap")
654 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
655
656 @GuardedBy("mMethodMap")
657 void clearPackagesToMonitorComponentChangeLocked() {
658 mPackagesToMonitorComponentChange.clear();
659 }
660
661 @GuardedBy("mMethodMap")
662 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
663 mPackagesToMonitorComponentChange.add(packageName);
664 }
665
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900666 private boolean isChangingPackagesOfCurrentUser() {
667 final int userId = getChangingUserId();
668 final boolean retval = userId == mSettings.getCurrentUserId();
669 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900670 if (!retval) {
671 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
672 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900673 }
674 return retval;
675 }
676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800678 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900679 if (!isChangingPackagesOfCurrentUser()) {
680 return false;
681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900683 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 final int N = mMethodList.size();
685 if (curInputMethodId != null) {
686 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800687 InputMethodInfo imi = mMethodList.get(i);
688 if (imi.getId().equals(curInputMethodId)) {
689 for (String pkg : packages) {
690 if (imi.getPackageName().equals(pkg)) {
691 if (!doit) {
692 return true;
693 }
satok723a27e2010-11-11 14:58:11 +0900694 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800695 chooseNewDefaultIMELocked();
696 return true;
697 }
698 }
699 }
700 }
701 }
702 }
703 return false;
704 }
705
706 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800707 public boolean onPackageChanged(String packageName, int uid, String[] components) {
708 // If this package is in the watch list, we want to check it.
709 synchronized (mMethodMap) {
710 return mPackagesToMonitorComponentChange.contains(packageName);
711 }
712 }
713
714 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800715 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900716 if (!isChangingPackagesOfCurrentUser()) {
717 return;
718 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800719 synchronized (mMethodMap) {
720 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900721 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800722 final int N = mMethodList.size();
723 if (curInputMethodId != null) {
724 for (int i=0; i<N; i++) {
725 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900726 final String imiId = imi.getId();
727 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800728 curIm = imi;
729 }
satoke7c6998e2011-06-03 17:57:59 +0900730
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800731 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900732 if (isPackageModified(imi.getPackageName())) {
733 mFileManager.deleteAllInputMethodSubtypes(imiId);
734 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800735 if (change == PACKAGE_TEMPORARY_CHANGE
736 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800737 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800738 + imi.getComponent());
739 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
741 }
742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
Yohei Yukawa94e33302016-02-12 19:37:03 -0800744 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800747
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800748 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800749 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800750 if (change == PACKAGE_TEMPORARY_CHANGE
751 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800752 ServiceInfo si = null;
753 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 si = mIPackageManager.getServiceInfo(
755 curIm.getComponent(), 0, mSettings.getCurrentUserId());
756 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800757 }
758 if (si == null) {
759 // Uh oh, current input method is no longer around!
760 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900762 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800763 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800764 changed = true;
765 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800766 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900767 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800768 }
769 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800770 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800771 }
satokab751aa2010-09-14 19:17:36 +0900772
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800773 if (curIm == null) {
774 // We currently don't have a default input method... is
775 // one now available?
776 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700777 } else if (!changed && isPackageModified(curIm.getPackageName())) {
778 // Even if the current input method is still available, mCurrentSubtype could
779 // be obsolete when the package is modified in practice.
780 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800783 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800784 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786 }
787 }
788 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800789
Jeff Brownc28867a2013-03-26 15:42:39 -0700790 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900791 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700792 private final IInputMethod mMethod;
793 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800794
Jeff Brownc28867a2013-03-26 15:42:39 -0700795 MethodCallback(InputMethodManagerService imms, IInputMethod method,
796 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900797 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700798 mMethod = method;
799 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800801
satoke7c6998e2011-06-03 17:57:59 +0900802 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700803 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700804 long ident = Binder.clearCallingIdentity();
805 try {
806 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
807 } finally {
808 Binder.restoreCallingIdentity(ident);
809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800812
satok01038492012-04-09 21:08:27 +0900813 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700814 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900815 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700816 public void onHardKeyboardStatusChange(boolean available) {
817 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
818 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900819 }
820
Michael Wright7b5a96b2014-08-09 19:28:42 -0700821 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900822 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700823 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900824 }
825 synchronized(mMethodMap) {
826 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
827 && mSwitchingDialog.isShowing()) {
828 mSwitchingDialogTitleView.findViewById(
829 com.android.internal.R.id.hard_keyboard_section).setVisibility(
830 available ? View.VISIBLE : View.GONE);
831 }
832 }
833 }
834 }
835
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800836 public static final class Lifecycle extends SystemService {
837 private InputMethodManagerService mService;
838
839 public Lifecycle(Context context) {
840 super(context);
841 mService = new InputMethodManagerService(context);
842 }
843
844 @Override
845 public void onStart() {
846 LocalServices.addService(InputMethodManagerInternal.class,
847 new LocalServiceImpl(mService.mHandler));
848 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
849 }
850
851 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800852 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700853 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800854 // TODO: Dispatch this to a worker thread as needed.
855 mService.onSwitchUser(userHandle);
856 }
857
858 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800859 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700860 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800861 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800862 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
863 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
864 .getService(Context.STATUS_BAR_SERVICE);
865 mService.systemRunning(statusBarService);
866 }
867 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800868
869 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700870 public void onUnlockUser(final @UserIdInt int userHandle) {
871 // Called on ActivityManager thread.
872 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700873 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800874 }
875 }
876
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800877 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800878 synchronized(mMethodMap) {
879 final int currentUserId = mSettings.getCurrentUserId();
880 if (DEBUG) {
881 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
882 }
883 if (userId != currentUserId) {
884 return;
885 }
886 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800887 if (mSystemReady) {
888 // We need to rebuild IMEs.
889 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
890 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
891 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800892 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800893 }
894
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800895 void onSwitchUser(@UserIdInt int userId) {
896 synchronized (mMethodMap) {
897 switchUserLocked(userId);
898 }
899 }
900
Seigo Nonaka7309b122015-08-17 18:34:13 -0700901 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900902 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800904 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700906 // Note: SettingsObserver doesn't register observers in its constructor.
907 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 mIWindowManager = IWindowManager.Stub.asInterface(
909 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700910 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800911 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900912 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 public void executeMessage(Message msg) {
914 handleMessage(msg);
915 }
Mita Yuned218c72012-12-06 17:18:25 -0800916 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800917 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800918 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900919 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700920 mHasFeature = context.getPackageManager().hasSystemFeature(
921 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500922 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700923 mHardKeyboardBehavior = mContext.getResources().getInteger(
924 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900925
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400926 Bundle extras = new Bundle();
927 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
928 mImeSwitcherNotification = new Notification.Builder(mContext)
929 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
930 .setWhen(0)
931 .setOngoing(true)
932 .addExtras(extras)
933 .setCategory(Notification.CATEGORY_SYSTEM)
934 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400935
satok7cfc0ed2011-06-20 21:29:36 +0900936 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900937 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900938
939 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900940
satok7cfc0ed2011-06-20 21:29:36 +0900941 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900942 int userId = 0;
943 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800944 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900945 } catch (RemoteException e) {
946 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
947 }
satok913a8922010-08-26 21:53:41 +0900948
satokd87c2592010-09-29 11:52:06 +0900949 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900950 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800951 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700952
Kenny Guy2a764942014-04-02 13:29:20 +0100953 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900954 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800955 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
956 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900957 }
958
satok5b927c432012-05-01 20:09:34 +0900959 private void resetDefaultImeLocked(Context context) {
960 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800961 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900962 return;
963 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800964 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800965 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800966 if (suitableImes.isEmpty()) {
967 Slog.i(TAG, "No default found");
968 return;
satok5b927c432012-05-01 20:09:34 +0900969 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800970 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -0800971 if (DEBUG) {
972 Slog.i(TAG, "Default found, using " + defIm.getId());
973 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800974 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900975 }
976
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900977 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
978 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900979 if (!mSystemReady) {
980 // not system ready
981 return;
982 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800983 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800985 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 if (!updateOnlyWhenLocaleChanged) {
987 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800988 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 }
990 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800991 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900992 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800993 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900994 if (!updateOnlyWhenLocaleChanged) {
995 final String selectedImiId = mSettings.getSelectedInputMethod();
996 if (TextUtils.isEmpty(selectedImiId)) {
997 // This is the first time of the user switch and
998 // set the current ime to the proper one.
999 resetDefaultImeLocked(mContext);
1000 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001001 } else {
1002 // If the locale is changed, needs to reset the default ime
1003 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001005 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001006 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001007 if (!updateOnlyWhenLocaleChanged) {
1008 try {
1009 startInputInnerLocked();
1010 } catch (RuntimeException e) {
1011 Slog.w(TAG, "Unexpected exception", e);
1012 }
1013 }
1014 }
1015 }
1016
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001017 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001018 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1019 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001020 }
1021
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001022 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001023 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1024 + " currentUserId=" + mSettings.getCurrentUserId());
1025
Yohei Yukawa81482972015-06-04 00:58:59 -07001026 // ContentObserver should be registered again when the user is changed
1027 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001028
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001029 // If the system is not ready or the device is not yed unlocked by the user, then we use
1030 // copy-on-write settings.
1031 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001032 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001033 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001034 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001035 // InputMethodFileManager should be reset when the user is changed
1036 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001037 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001038
1039 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1040 + " defaultImiId=" + defaultImiId);
1041
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001042 // For secondary users, the list of enabled IMEs may not have been updated since the
1043 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1044 // not be empty even if the IME has been uninstalled by the primary user.
1045 // Even in such cases, IMMS works fine because it will find the most applicable
1046 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001047 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001048 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001049 initialUserSwitch /* needsToResetDefaultIme */);
1050 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001051 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1052 mSettings.getEnabledInputMethodListLocked(), newUserId,
1053 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001054 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001055
1056 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1057 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001058 }
1059
Kenny Guy2a764942014-04-02 13:29:20 +01001060 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001061 mSettings.setCurrentProfileIds(
1062 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001063 }
1064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 @Override
1066 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1067 throws RemoteException {
1068 try {
1069 return super.onTransact(code, data, reply, flags);
1070 } catch (RuntimeException e) {
1071 // The input method manager only throws security exceptions, so let's
1072 // log all others.
1073 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001074 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076 throw e;
1077 }
1078 }
1079
Svetoslav Ganova0027152013-06-25 14:59:53 -07001080 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001081 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001082 if (DEBUG) {
1083 Slog.d(TAG, "--- systemReady");
1084 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001085 if (!mSystemReady) {
1086 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001087 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001088 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001089 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001090 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001091 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1092 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001093 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001094 if (mStatusBar != null) {
1095 mStatusBar.setIconVisibility(mSlotIme, false);
1096 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001097 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001098 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1099 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001100 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001101 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001102 mHardKeyboardListener);
1103 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001104
1105 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1106 mSettingsObserver.registerContentObserverLocked(currentUserId);
1107
1108 final IntentFilter broadcastFilter = new IntentFilter();
1109 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1110 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1111 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1112 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1113 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1114 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1115
1116 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1117 resetDefaultImeLocked(mContext);
1118 updateFromSettingsLocked(true);
1119 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1120 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1121 mContext.getBasePackageName());
1122
Dianne Hackborncc278702009-09-02 23:07:23 -07001123 try {
1124 startInputInnerLocked();
1125 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001126 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001127 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001128 }
1129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001131
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 // ---------------------------------------------------------------------------------------
1133 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1134 // 1) it comes from the system process
1135 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1136 private boolean calledFromValidUser() {
1137 final int uid = Binder.getCallingUid();
1138 final int userId = UserHandle.getUserId(uid);
1139 if (DEBUG) {
1140 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1141 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1142 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001143 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1144 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001145 }
Kenny Guy2a764942014-04-02 13:29:20 +01001146 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001147 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001148 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001149
1150 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1151 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1152 // must not manage background users' states in any functions.
1153 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1154 // by a token.
1155 if (mContext.checkCallingOrSelfPermission(
1156 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1157 == PackageManager.PERMISSION_GRANTED) {
1158 if (DEBUG) {
1159 Slog.d(TAG, "--- Access granted because the calling process has "
1160 + "the INTERACT_ACROSS_USERS_FULL permission");
1161 }
1162 return true;
1163 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001164 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001165 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1166 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001167 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001168 }
1169
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001170
1171 /**
1172 * Returns true iff the caller is identified to be the current input method with the token.
1173 * @param token The window token given to the input method when it was started.
1174 * @return true if and only if non-null valid token is specified.
1175 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001176 private boolean calledWithValidToken(@Nullable IBinder token) {
1177 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1178 if (DEBUG) {
1179 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1180 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1181 }
1182 return false;
1183 }
1184 if (token == null || token != mCurToken) {
1185 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1186 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1187 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001188 return false;
1189 }
1190 return true;
1191 }
1192
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001193 private boolean bindCurrentInputMethodService(
1194 Intent service, ServiceConnection conn, int flags) {
1195 if (service == null || conn == null) {
1196 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1197 return false;
1198 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001199 return mContext.bindServiceAsUser(service, conn, flags,
1200 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001201 }
1202
satoke7c6998e2011-06-03 17:57:59 +09001203 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001205 // TODO: Make this work even for non-current users?
1206 if (!calledFromValidUser()) {
1207 return Collections.emptyList();
1208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001210 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 }
1213
satoke7c6998e2011-06-03 17:57:59 +09001214 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001216 // TODO: Make this work even for non-current users?
1217 if (!calledFromValidUser()) {
1218 return Collections.emptyList();
1219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001221 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223 }
1224
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001225 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001226 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001227 * @return enabled subtypes of the specified imi
1228 */
satoke7c6998e2011-06-03 17:57:59 +09001229 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001230 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001231 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001232 // TODO: Make this work even for non-current users?
1233 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001234 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001235 }
satok67ddf9c2010-11-17 09:45:54 +09001236 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001237 final InputMethodInfo imi;
1238 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001239 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001240 } else {
1241 imi = mMethodMap.get(imiId);
1242 }
1243 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001244 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001245 }
1246 return mSettings.getEnabledInputMethodSubtypeListLocked(
1247 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001248 }
1249 }
1250
satoke7c6998e2011-06-03 17:57:59 +09001251 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 public void addClient(IInputMethodClient client,
1253 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001254 if (!calledFromValidUser()) {
1255 return;
1256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 synchronized (mMethodMap) {
1258 mClients.put(client.asBinder(), new ClientState(client,
1259 inputContext, uid, pid));
1260 }
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
satoke7c6998e2011-06-03 17:57:59 +09001263 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001265 if (!calledFromValidUser()) {
1266 return;
1267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001269 ClientState cs = mClients.remove(client.asBinder());
1270 if (cs != null) {
1271 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001272 if (mCurClient == cs) {
1273 mCurClient = null;
1274 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001275 if (mCurFocusedWindowClient == cs) {
1276 mCurFocusedWindowClient = null;
1277 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 void executeOrSendMessage(IInterface target, Message msg) {
1283 if (target.asBinder() instanceof Binder) {
1284 mCaller.sendMessage(msg);
1285 } else {
1286 handleMessage(msg);
1287 msg.recycle();
1288 }
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
Yohei Yukawa33e81792015-11-17 21:14:42 -08001291 void unbindCurrentClientLocked(
1292 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001294 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 + mCurClient.client.asBinder());
1296 if (mBoundToMethod) {
1297 mBoundToMethod = false;
1298 if (mCurMethod != null) {
1299 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1300 MSG_UNBIND_INPUT, mCurMethod));
1301 }
1302 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001303
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001304 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1305 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001306 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1307 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001310
The Android Open Source Project10592532009-03-18 17:39:46 -07001311 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 private int getImeShowFlags() {
1316 int flags = 0;
1317 if (mShowForced) {
1318 flags |= InputMethod.SHOW_FORCED
1319 | InputMethod.SHOW_EXPLICIT;
1320 } else if (mShowExplicitlyRequested) {
1321 flags |= InputMethod.SHOW_EXPLICIT;
1322 }
1323 return flags;
1324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 private int getAppShowFlags() {
1327 int flags = 0;
1328 if (mShowForced) {
1329 flags |= InputMethodManager.SHOW_FORCED;
1330 } else if (!mShowExplicitlyRequested) {
1331 flags |= InputMethodManager.SHOW_IMPLICIT;
1332 }
1333 return flags;
1334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001336 InputBindResult attachNewInputLocked(
1337 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 if (!mBoundToMethod) {
1339 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1340 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1341 mBoundToMethod = true;
1342 }
1343 final SessionState session = mCurClient.curSession;
1344 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001345 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1346 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1347 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001349 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1350 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1351 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
1353 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001354 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001355 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001357 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001358 (session.channel != null ? session.channel.dup() : null),
1359 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001361
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001362 InputBindResult startInputLocked(
1363 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001364 IInputMethodClient client, IInputContext inputContext,
1365 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001366 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 // If no method is currently selected, do nothing.
1368 if (mCurMethodId == null) {
1369 return mNoBinding;
1370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 ClientState cs = mClients.get(client.asBinder());
1373 if (cs == null) {
1374 throw new IllegalArgumentException("unknown client "
1375 + client.asBinder());
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Yohei Yukawa74750f22016-03-22 12:54:22 -07001378 if (attribute == null) {
1379 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1380 + " uid=" + cs.uid + " pid=" + cs.pid);
1381 return null;
1382 }
1383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 try {
1385 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1386 // Check with the window manager to make sure this client actually
1387 // has a window with focus. If not, reject. This is thread safe
1388 // because if the focus changes some time before or after, the
1389 // next client receiving focus that has any interest in input will
1390 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001391 if (DEBUG) {
1392 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1393 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 return null;
1396 }
1397 } catch (RemoteException e) {
1398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001400 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001401 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001402 }
1403
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001404 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001405 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001406 @NonNull EditorInfo attribute, int controlFlags,
1407 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001408 // If no method is currently selected, do nothing.
1409 if (mCurMethodId == null) {
1410 return mNoBinding;
1411 }
1412
Yohei Yukawad57ba672015-06-08 16:39:46 -07001413 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1414 attribute.packageName)) {
1415 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1416 + " uid=" + cs.uid + " package=" + attribute.packageName);
1417 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001418 }
1419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001421 // Was the keyguard locked when switching over to the new client?
1422 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 // If the client is changing, we need to switch over to the new
1424 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001425 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001426 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001427 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428
1429 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001430 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001431 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001432 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 }
1434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 // Bump up the sequence for this client and attach it.
1437 mCurSeq++;
1438 if (mCurSeq <= 0) mCurSeq = 1;
1439 mCurClient = cs;
1440 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001441 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 // Check if the input method is changing.
1445 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1446 if (cs.curSession != null) {
1447 // Fast case: if we are already connected to the input method,
1448 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001449 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001450 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
1452 if (mHaveConnection) {
1453 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 // Return to client, and we will get back with it when
1455 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001456 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001457 return new InputBindResult(null, null, mCurId, mCurSeq,
1458 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 } else if (SystemClock.uptimeMillis()
1460 < (mLastBindTime+TIME_TO_RECONNECT)) {
1461 // In this case we have connected to the service, but
1462 // don't yet have its interface. If it hasn't been too
1463 // long since we did the connection, we'll return to
1464 // the client and wait to get the service interface so
1465 // we can report back. If it has been too long, we want
1466 // to fall through so we can try a disconnect/reconnect
1467 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001468 return new InputBindResult(null, null, mCurId, mCurSeq,
1469 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1472 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 }
1474 }
1475 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001477 return startInputInnerLocked();
1478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001479
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001480 InputBindResult startInputInnerLocked() {
1481 if (mCurMethodId == null) {
1482 return mNoBinding;
1483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001485 if (!mSystemReady) {
1486 // If the system is not yet ready, we shouldn't be running third
1487 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001488 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1489 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1493 if (info == null) {
1494 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001497 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1500 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001501 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1502 com.android.internal.R.string.input_method_binding_label);
1503 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1504 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001506 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1507 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 mLastBindTime = SystemClock.uptimeMillis();
1509 mHaveConnection = true;
1510 mCurId = info.getId();
1511 mCurToken = new Binder();
1512 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001513 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001514 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 } catch (RemoteException e) {
1516 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001517 return new InputBindResult(null, null, mCurId, mCurSeq,
1518 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 } else {
1520 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 + mCurIntent);
1523 }
1524 return null;
1525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001526
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001527 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001528 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001529 IInputMethodClient client, IInputContext inputContext,
1530 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001531 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001532 if (!calledFromValidUser()) {
1533 return null;
1534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001536 if (DEBUG) {
1537 Slog.v(TAG, "startInput: reason="
1538 + InputMethodClient.getStartInputReason(startInputReason)
1539 + " client = " + client.asBinder()
1540 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001541 + " missingMethods="
1542 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001543 + " attribute=" + attribute
1544 + " controlFlags=#" + Integer.toHexString(controlFlags));
1545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 final long ident = Binder.clearCallingIdentity();
1547 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001548 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1549 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 } finally {
1551 Binder.restoreCallingIdentity(ident);
1552 }
1553 }
1554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001555
satoke7c6998e2011-06-03 17:57:59 +09001556 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 public void finishInput(IInputMethodClient client) {
1558 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001559
satoke7c6998e2011-06-03 17:57:59 +09001560 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 public void onServiceConnected(ComponentName name, IBinder service) {
1562 synchronized (mMethodMap) {
1563 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1564 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001565 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001566 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001567 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001568 return;
1569 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001570 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001571 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1572 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001574 clearClientSessionLocked(mCurClient);
1575 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 }
1577 }
1578 }
1579 }
1580
Jeff Brownc28867a2013-03-26 15:42:39 -07001581 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1582 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 synchronized (mMethodMap) {
1584 if (mCurMethod != null && method != null
1585 && mCurMethod.asBinder() == method.asBinder()) {
1586 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001587 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001589 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001590 InputBindResult res = attachNewInputLocked(
1591 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 if (res.method != null) {
1593 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001594 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001596 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
1598 }
1599 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001600
1601 // Session abandoned. Close its associated input channel.
1602 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001605 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001606 if (mVisibleBound) {
1607 mContext.unbindService(mVisibleConnection);
1608 mVisibleBound = false;
1609 }
1610
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001611 if (mHaveConnection) {
1612 mContext.unbindService(this);
1613 mHaveConnection = false;
1614 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001615
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001616 if (mCurToken != null) {
1617 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001618 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001619 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001620 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001621 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001622 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001623 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001624 } catch (RemoteException e) {
1625 }
1626 mCurToken = null;
1627 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001628
The Android Open Source Project10592532009-03-18 17:39:46 -07001629 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001630 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001631 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001632
Yohei Yukawa33e81792015-11-17 21:14:42 -08001633 void resetCurrentMethodAndClient(
1634 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001635 mCurMethodId = null;
1636 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001637 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001638 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001639
1640 void requestClientSessionLocked(ClientState cs) {
1641 if (!cs.sessionRequested) {
1642 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1643 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1644 cs.sessionRequested = true;
1645 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1646 MSG_CREATE_SESSION, mCurMethod, channels[1],
1647 new MethodCallback(this, mCurMethod, channels[0])));
1648 }
1649 }
1650
1651 void clearClientSessionLocked(ClientState cs) {
1652 finishSessionLocked(cs.curSession);
1653 cs.curSession = null;
1654 cs.sessionRequested = false;
1655 }
1656
1657 private void finishSessionLocked(SessionState sessionState) {
1658 if (sessionState != null) {
1659 if (sessionState.session != null) {
1660 try {
1661 sessionState.session.finishSession();
1662 } catch (RemoteException e) {
1663 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001664 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001665 }
1666 sessionState.session = null;
1667 }
1668 if (sessionState.channel != null) {
1669 sessionState.channel.dispose();
1670 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001671 }
1672 }
1673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001674
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001675 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 if (mCurMethod != null) {
1677 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001678 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001680
Jeff Brownc28867a2013-03-26 15:42:39 -07001681 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001682 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 mCurMethod = null;
1684 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001685 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001686 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001687 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001688 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001690
satoke7c6998e2011-06-03 17:57:59 +09001691 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001693 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1694 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1695 // which is irregular but can eventually happen for everyone just by continuing using the
1696 // device. Thus it is important to make sure that all the internal states are properly
1697 // refreshed when this method is called back. Running
1698 // adb install -r <APK that implements the current IME>
1699 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001701 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 + " mCurIntent=" + mCurIntent);
1703 if (mCurMethod != null && mCurIntent != null
1704 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001705 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 // We consider this to be a new bind attempt, since the system
1707 // should now try to restart the service for us.
1708 mLastBindTime = SystemClock.uptimeMillis();
1709 mShowRequested = mInputShown;
1710 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001711 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
1713 }
1714 }
1715
satokf9f01002011-05-19 21:31:50 +09001716 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001718 synchronized (mMethodMap) {
1719 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001720 return;
1721 }
1722 final long ident = Binder.clearCallingIdentity();
1723 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001725 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001726 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001727 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001730 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001731 CharSequence contentDescription = null;
1732 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001733 // Use PackageManager to load label
1734 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001735 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001736 mIPackageManager.getApplicationInfo(packageName, 0,
1737 mSettings.getCurrentUserId()));
1738 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001739 /* ignore */
1740 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001741 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001742 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001743 contentDescription != null
1744 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001745 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001748 } finally {
1749 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752 }
1753
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001754 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001755 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001756 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001757 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001758 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001759 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001760 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1761 // When physical keyboard is attached, we show the ime switcher (or notification if
1762 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1763 // exists in the IME switcher dialog. Might be OK to remove this condition once
1764 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1765 return true;
1766 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001767 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1768 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001769 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001770
1771 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1772 final int N = imis.size();
1773 if (N > 2) return true;
1774 if (N < 1) return false;
1775 int nonAuxCount = 0;
1776 int auxCount = 0;
1777 InputMethodSubtype nonAuxSubtype = null;
1778 InputMethodSubtype auxSubtype = null;
1779 for(int i = 0; i < N; ++i) {
1780 final InputMethodInfo imi = imis.get(i);
1781 final List<InputMethodSubtype> subtypes =
1782 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1783 final int subtypeCount = subtypes.size();
1784 if (subtypeCount == 0) {
1785 ++nonAuxCount;
1786 } else {
1787 for (int j = 0; j < subtypeCount; ++j) {
1788 final InputMethodSubtype subtype = subtypes.get(j);
1789 if (!subtype.isAuxiliary()) {
1790 ++nonAuxCount;
1791 nonAuxSubtype = subtype;
1792 } else {
1793 ++auxCount;
1794 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001795 }
1796 }
satok7cfc0ed2011-06-20 21:29:36 +09001797 }
1798 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001799 if (nonAuxCount > 1 || auxCount > 1) {
1800 return true;
1801 } else if (nonAuxCount == 1 && auxCount == 1) {
1802 if (nonAuxSubtype != null && auxSubtype != null
1803 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1804 || auxSubtype.overridesImplicitlyEnabledSubtype()
1805 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1806 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1807 return false;
1808 }
1809 return true;
1810 }
1811 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001812 }
1813
John Spurlocke0980502013-10-25 11:59:29 -04001814 private boolean isKeyguardLocked() {
1815 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1816 }
1817
satokdbf29502011-08-25 15:28:23 +09001818 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001819 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001820 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001821 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001822 return;
1823 }
1824
1825 synchronized (mMethodMap) {
1826 mImeWindowVis = vis;
1827 mBackDisposition = backDisposition;
1828 updateSystemUiLocked(token, vis, backDisposition);
1829 }
1830 }
1831
1832 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1833 synchronized (mMethodMap) {
1834 updateSystemUiLocked(token, vis, backDisposition);
1835 }
1836 }
1837
1838 // Caution! This method is called in this class. Handle multi-user carefully
1839 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1840 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001841 return;
1842 }
1843
1844 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1845 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001846 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001847 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001848 // apply policy for binder calls
1849 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1850 vis = 0;
satok06487a52010-10-29 11:37:18 +09001851 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001852 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1853 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1854 if (mStatusBar != null) {
1855 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1856 needsToShowImeSwitcher);
1857 }
1858 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1859 if (imi != null && needsToShowImeSwitcher) {
1860 // Used to load label
1861 final CharSequence title = mRes.getText(
1862 com.android.internal.R.string.select_input_method);
1863 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1864 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001865 mImeSwitcherNotification.setContentTitle(title)
1866 .setContentText(summary)
1867 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001868 try {
1869 if ((mNotificationManager != null)
1870 && !mIWindowManager.hasNavigationBar()) {
1871 if (DEBUG) {
1872 Slog.d(TAG, "--- show notification: label = " + summary);
1873 }
1874 mNotificationManager.notifyAsUser(null,
1875 com.android.internal.R.string.select_input_method,
1876 mImeSwitcherNotification.build(), UserHandle.ALL);
1877 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001878 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001879 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001880 }
1881 } else {
1882 if (mNotificationShown && mNotificationManager != null) {
1883 if (DEBUG) {
1884 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001885 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001886 mNotificationManager.cancelAsUser(null,
1887 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1888 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001889 }
satok06487a52010-10-29 11:37:18 +09001890 }
1891 } finally {
1892 Binder.restoreCallingIdentity(ident);
1893 }
1894 }
1895
satoke7c6998e2011-06-03 17:57:59 +09001896 @Override
satokf9f01002011-05-19 21:31:50 +09001897 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001898 if (!calledFromValidUser()) {
1899 return;
1900 }
satokf9f01002011-05-19 21:31:50 +09001901 synchronized (mMethodMap) {
1902 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1903 for (int i = 0; i < spans.length; ++i) {
1904 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001905 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001906 mSecureSuggestionSpans.put(ss, currentImi);
1907 }
1908 }
1909 }
1910 }
1911
satoke7c6998e2011-06-03 17:57:59 +09001912 @Override
satokf9f01002011-05-19 21:31:50 +09001913 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001914 if (!calledFromValidUser()) {
1915 return false;
1916 }
satokf9f01002011-05-19 21:31:50 +09001917 synchronized (mMethodMap) {
1918 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1919 // TODO: Do not send the intent if the process of the targetImi is already dead.
1920 if (targetImi != null) {
1921 final String[] suggestions = span.getSuggestions();
1922 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001923 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001924 final Intent intent = new Intent();
1925 // Ensures that only a class in the original IME package will receive the
1926 // notification.
satok42c5a162011-05-26 16:46:14 +09001927 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001928 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1929 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1930 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1931 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001932 final long ident = Binder.clearCallingIdentity();
1933 try {
1934 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1935 } finally {
1936 Binder.restoreCallingIdentity(ident);
1937 }
satokf9f01002011-05-19 21:31:50 +09001938 return true;
1939 }
1940 }
1941 return false;
1942 }
1943
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001944 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001945 updateInputMethodsFromSettingsLocked(enabledMayChange);
1946 updateKeyboardFromSettingsLocked();
1947 }
1948
1949 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001950 if (enabledMayChange) {
1951 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1952 for (int i=0; i<enabled.size(); i++) {
1953 // We allow the user to select "disabled until used" apps, so if they
1954 // are enabling one of those here we now need to make it enabled.
1955 InputMethodInfo imm = enabled.get(i);
1956 try {
1957 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1958 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1959 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001960 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001961 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001962 if (DEBUG) {
1963 Slog.d(TAG, "Update state(" + imm.getId()
1964 + "): DISABLED_UNTIL_USED -> DEFAULT");
1965 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001966 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1967 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001968 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1969 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001970 }
1971 } catch (RemoteException e) {
1972 }
1973 }
1974 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001975 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1976 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1977 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1978 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001979 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001980 // There is no input method selected, try to choose new applicable input method.
1981 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001982 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001983 }
1984 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001986 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001989 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
satokf3db1af2010-11-23 13:34:33 +09001991 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001992 } else {
1993 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001994 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001996 // Here is not the perfect place to reset the switching controller. Ideally
1997 // mSwitchingController and mSettings should be able to share the same state.
1998 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1999 // the same enabled IMEs list.
2000 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002001
2002 }
2003
2004 public void updateKeyboardFromSettingsLocked() {
2005 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2006 if (mSwitchingDialog != null
2007 && mSwitchingDialogTitleView != null
2008 && mSwitchingDialog.isShowing()) {
2009 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2010 com.android.internal.R.id.hard_keyboard_switch);
2011 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002014
Yohei Yukawab097b822015-12-01 10:43:08 -08002015 private void notifyInputMethodSubtypeChanged(final int userId,
2016 @Nullable final InputMethodInfo inputMethodInfo,
2017 @Nullable final InputMethodSubtype subtype) {
2018 final InputManagerInternal inputManagerInternal =
2019 LocalServices.getService(InputManagerInternal.class);
2020 if (inputManagerInternal != null) {
2021 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2022 }
2023 }
2024
satokab751aa2010-09-14 19:17:36 +09002025 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 InputMethodInfo info = mMethodMap.get(id);
2027 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002028 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002030
satokd81e9502012-05-21 12:58:45 +09002031 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002033 final int subtypeCount = info.getSubtypeCount();
2034 if (subtypeCount <= 0) {
2035 return;
satokcd7cd292010-11-20 15:46:23 +09002036 }
satokd81e9502012-05-21 12:58:45 +09002037 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2038 final InputMethodSubtype newSubtype;
2039 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2040 newSubtype = info.getSubtypeAt(subtypeId);
2041 } else {
2042 // If subtype is null, try to find the most applicable one from
2043 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002044 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002045 }
2046 if (newSubtype == null || oldSubtype == null) {
2047 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2048 + ", new subtype = " + newSubtype);
2049 return;
2050 }
2051 if (newSubtype != oldSubtype) {
2052 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2053 if (mCurMethod != null) {
2054 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002055 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002056 mCurMethod.changeInputMethodSubtype(newSubtype);
2057 } catch (RemoteException e) {
2058 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002059 return;
satokab751aa2010-09-14 19:17:36 +09002060 }
2061 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002062 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 return;
2065 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002066
satokd81e9502012-05-21 12:58:45 +09002067 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 final long ident = Binder.clearCallingIdentity();
2069 try {
satokab751aa2010-09-14 19:17:36 +09002070 // Set a subtype to this input method.
2071 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002072 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2073 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2074 // because mCurMethodId is stored as a history in
2075 // setSelectedInputMethodAndSubtypeLocked().
2076 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002078 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002080 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002082 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002084 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 } finally {
2086 Binder.restoreCallingIdentity(ident);
2087 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002088
2089 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2090 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
satok42c5a162011-05-26 16:46:14 +09002093 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002094 public boolean showSoftInput(IInputMethodClient client, int flags,
2095 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002096 if (!calledFromValidUser()) {
2097 return false;
2098 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002099 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 long ident = Binder.clearCallingIdentity();
2101 try {
2102 synchronized (mMethodMap) {
2103 if (mCurClient == null || client == null
2104 || mCurClient.client.asBinder() != client.asBinder()) {
2105 try {
2106 // We need to check if this is the current client with
2107 // focus in the window manager, to allow this call to
2108 // be made before input is started in it.
2109 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002110 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
2113 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002114 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 }
2116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002117
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002118 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002119 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
2121 } finally {
2122 Binder.restoreCallingIdentity(ident);
2123 }
2124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002125
The Android Open Source Project4df24232009-03-05 14:34:35 -08002126 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002128 if (mAccessibilityRequestingNoSoftKeyboard) {
2129 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 }
Anna Galusza9b278112016-01-04 11:37:37 -08002131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2133 mShowExplicitlyRequested = true;
2134 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002135 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2136 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002138
Dianne Hackborncc278702009-09-02 23:07:23 -07002139 if (!mSystemReady) {
2140 return false;
2141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
The Android Open Source Project4df24232009-03-05 14:34:35 -08002143 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002145 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002146 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2147 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2148 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002150 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002151 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002152 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002153 | Context.BIND_TREAT_LIKE_ACTIVITY
2154 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002155 mVisibleBound = true;
2156 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002157 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002159 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 // The client has asked to have the input method shown, but
2161 // we have been sitting here too long with a connection to the
2162 // service and no interface received, so let's disconnect/connect
2163 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002164 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002166 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002168 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002169 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002170 } else {
2171 if (DEBUG) {
2172 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2173 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002176
The Android Open Source Project4df24232009-03-05 14:34:35 -08002177 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002179
satok42c5a162011-05-26 16:46:14 +09002180 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002181 public boolean hideSoftInput(IInputMethodClient client, int flags,
2182 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002183 if (!calledFromValidUser()) {
2184 return false;
2185 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002186 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 long ident = Binder.clearCallingIdentity();
2188 try {
2189 synchronized (mMethodMap) {
2190 if (mCurClient == null || client == null
2191 || mCurClient.client.asBinder() != client.asBinder()) {
2192 try {
2193 // We need to check if this is the current client with
2194 // focus in the window manager, to allow this call to
2195 // be made before input is started in it.
2196 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002197 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2198 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002199 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
2201 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002202 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 }
2204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002205
Joe Onorato8a9b2202010-02-26 18:56:32 -08002206 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002207 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 }
2209 } finally {
2210 Binder.restoreCallingIdentity(ident);
2211 }
2212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002213
The Android Open Source Project4df24232009-03-05 14:34:35 -08002214 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2216 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002217 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 -08002218 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
2220 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002221 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 -08002222 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002224
2225 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2226 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2227 // to be updated with the new value sent from IME process. Even in such a transient state
2228 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2229 // application process as a valid request, and have even promised such a behavior with CTS
2230 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2231 // IMMS#InputShown indicates that the software keyboard is shown.
2232 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2233 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2234 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002235 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002236 if (shouldHideSoftInput) {
2237 // The IME will report its visible state again after the following message finally
2238 // delivered to the IME process as an IPC. Hence the inconsistency between
2239 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2240 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002241 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2242 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2243 res = true;
2244 } else {
2245 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002247 if (mHaveConnection && mVisibleBound) {
2248 mContext.unbindService(mVisibleConnection);
2249 mVisibleBound = false;
2250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 mInputShown = false;
2252 mShowRequested = false;
2253 mShowExplicitlyRequested = false;
2254 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002255 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002257
satok42c5a162011-05-26 16:46:14 +09002258 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002259 public InputBindResult startInputOrWindowGainedFocus(
2260 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2261 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002262 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002263 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002264 if (windowToken != null) {
2265 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002266 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002267 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002268 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2269 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002270 }
2271 }
2272
2273 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002274 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2275 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002276 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2277 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002278 // Needs to check the validity before clearing calling identity
2279 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002280 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 long ident = Binder.clearCallingIdentity();
2282 try {
2283 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002284 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2285 + InputMethodClient.getStartInputReason(startInputReason)
2286 + " client=" + client.asBinder()
2287 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002288 + " missingMethods="
2289 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002290 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002291 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002293 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002294
Dianne Hackborn7663d802012-02-24 13:08:49 -08002295 ClientState cs = mClients.get(client.asBinder());
2296 if (cs == null) {
2297 throw new IllegalArgumentException("unknown client "
2298 + client.asBinder());
2299 }
2300
2301 try {
2302 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2303 // Check with the window manager to make sure this client actually
2304 // has a window with focus. If not, reject. This is thread safe
2305 // because if the focus changes some time before or after, the
2306 // next client receiving focus that has any interest in input will
2307 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002308 if (DEBUG) {
2309 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2310 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2311 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002312 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002314 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002316
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002317 if (!calledFromValidUser) {
2318 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2319 Slog.w(TAG, "If you want to interect with IME, you need "
2320 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2321 hideCurrentInputLocked(0, null);
2322 return null;
2323 }
2324
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002325 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002326 if (DEBUG) {
2327 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2328 + " attribute=" + attribute + ", token = " + windowToken);
2329 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002330 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002331 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002332 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002333 }
2334 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002335 }
2336 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002337 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002338
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002339 // Should we auto-show the IME even if the caller has not
2340 // specified what should be done with it?
2341 // We only do this automatically if the window can resize
2342 // to accommodate the IME (so what the user sees will give
2343 // them good context without input information being obscured
2344 // by the IME) or if running on a large screen where there
2345 // is more room for the target window + IME.
2346 final boolean doAutoShow =
2347 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2348 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2349 || mRes.getConfiguration().isLayoutSizeAtLeast(
2350 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002351 final boolean isTextEditor =
2352 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2353
2354 // We want to start input before showing the IME, but after closing
2355 // it. We want to do this after closing it to help the IME disappear
2356 // more quickly (not get stuck behind it initializing itself for the
2357 // new focused input, even if its window wants to hide the IME).
2358 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2361 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002362 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2364 // There is no focus view, and this window will
2365 // be behind any soft input window, so hide the
2366 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002367 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002368 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002370 } else if (isTextEditor && doAutoShow && (softInputMode &
2371 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 // There is a focus view, and we are navigating forward
2373 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002374 // We only do this automatically if the window can resize
2375 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002376 // them good context without input information being obscured
2377 // by the IME) or if running on a large screen where there
2378 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002379 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002380 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002381 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002382 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002383 didStart = true;
2384 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002385 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 }
2387 break;
2388 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2389 // Do nothing.
2390 break;
2391 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2392 if ((softInputMode &
2393 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002394 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002395 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 }
2397 break;
2398 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002399 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002400 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 break;
2402 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2403 if ((softInputMode &
2404 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002405 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002406 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002407 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002408 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002409 didStart = true;
2410 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002411 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
2413 break;
2414 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002415 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002416 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002417 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002418 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002419 didStart = true;
2420 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002421 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 break;
2423 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002424
2425 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002426 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002427 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430 } finally {
2431 Binder.restoreCallingIdentity(ident);
2432 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002433
2434 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002436
satok42c5a162011-05-26 16:46:14 +09002437 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002438 public void showInputMethodPickerFromClient(
2439 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002440 if (!calledFromValidUser()) {
2441 return;
2442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 synchronized (mMethodMap) {
2444 if (mCurClient == null || client == null
2445 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002446 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002447 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 }
2449
satok440aab52010-11-25 09:43:11 +09002450 // Always call subtype picker, because subtype picker is a superset of input method
2451 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002452 mHandler.sendMessage(mCaller.obtainMessageI(
2453 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002454 }
2455 }
2456
satok42c5a162011-05-26 16:46:14 +09002457 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002459 if (!calledFromValidUser()) {
2460 return;
2461 }
satok28203512010-11-24 11:06:49 +09002462 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2463 }
2464
satok42c5a162011-05-26 16:46:14 +09002465 @Override
satok28203512010-11-24 11:06:49 +09002466 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002467 if (!calledFromValidUser()) {
2468 return;
2469 }
satok28203512010-11-24 11:06:49 +09002470 synchronized (mMethodMap) {
2471 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002472 setInputMethodWithSubtypeIdLocked(token, id,
2473 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2474 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002475 } else {
2476 setInputMethod(token, id);
2477 }
2478 }
satokab751aa2010-09-14 19:17:36 +09002479 }
2480
satok42c5a162011-05-26 16:46:14 +09002481 @Override
satokb416a712010-11-25 20:42:14 +09002482 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002483 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002484 if (!calledFromValidUser()) {
2485 return;
2486 }
satokb416a712010-11-25 20:42:14 +09002487 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002488 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2489 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002490 }
2491 }
2492
satok4fc87d62011-05-20 16:13:43 +09002493 @Override
satok735cf382010-11-11 20:40:09 +09002494 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002495 if (!calledFromValidUser()) {
2496 return false;
2497 }
satok735cf382010-11-11 20:40:09 +09002498 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002499 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002500 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002501 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002502 lastImi = mMethodMap.get(lastIme.first);
2503 } else {
2504 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002505 }
satok4fc87d62011-05-20 16:13:43 +09002506 String targetLastImiId = null;
2507 int subtypeId = NOT_A_SUBTYPE_ID;
2508 if (lastIme != null && lastImi != null) {
2509 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002510 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002511 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2512 : mCurrentSubtype.hashCode();
2513 // If the last IME is the same as the current IME and the last subtype is not
2514 // defined, there is no need to switch to the last IME.
2515 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2516 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002517 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002518 }
2519 }
2520
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002521 if (TextUtils.isEmpty(targetLastImiId)
2522 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002523 // This is a safety net. If the currentSubtype can't be added to the history
2524 // and the framework couldn't find the last ime, we will make the last ime be
2525 // the most applicable enabled keyboard subtype of the system imes.
2526 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2527 if (enabled != null) {
2528 final int N = enabled.size();
2529 final String locale = mCurrentSubtype == null
2530 ? mRes.getConfiguration().locale.toString()
2531 : mCurrentSubtype.getLocale();
2532 for (int i = 0; i < N; ++i) {
2533 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002534 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002535 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002536 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2537 InputMethodUtils.getSubtypes(imi),
2538 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002539 if (keyboardSubtype != null) {
2540 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002541 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002542 imi, keyboardSubtype.hashCode());
2543 if(keyboardSubtype.getLocale().equals(locale)) {
2544 break;
2545 }
2546 }
2547 }
2548 }
2549 }
2550 }
2551
2552 if (!TextUtils.isEmpty(targetLastImiId)) {
2553 if (DEBUG) {
2554 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2555 + ", from: " + mCurMethodId + ", " + subtypeId);
2556 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002557 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002558 return true;
2559 } else {
2560 return false;
2561 }
satok735cf382010-11-11 20:40:09 +09002562 }
2563 }
2564
satoke7c6998e2011-06-03 17:57:59 +09002565 @Override
satok688bd472012-02-09 20:09:17 +09002566 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002567 if (!calledFromValidUser()) {
2568 return false;
2569 }
satok688bd472012-02-09 20:09:17 +09002570 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002571 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002572 return false;
2573 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002574 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002575 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2576 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002577 if (nextSubtype == null) {
2578 return false;
2579 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002580 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2581 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002582 return true;
2583 }
2584 }
2585
2586 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002587 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2588 if (!calledFromValidUser()) {
2589 return false;
2590 }
2591 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002592 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002593 return false;
2594 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002595 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002596 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2597 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002598 if (nextSubtype == null) {
2599 return false;
2600 }
2601 return true;
2602 }
2603 }
2604
2605 @Override
satok68f1b782011-04-11 14:26:04 +09002606 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002607 if (!calledFromValidUser()) {
2608 return null;
2609 }
satok68f1b782011-04-11 14:26:04 +09002610 synchronized (mMethodMap) {
2611 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2612 // TODO: Handle the case of the last IME with no subtypes
2613 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2614 || TextUtils.isEmpty(lastIme.second)) return null;
2615 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2616 if (lastImi == null) return null;
2617 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002618 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002619 final int lastSubtypeId =
2620 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002621 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2622 return null;
2623 }
2624 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002625 } catch (NumberFormatException e) {
2626 return null;
2627 }
2628 }
2629 }
2630
satoke7c6998e2011-06-03 17:57:59 +09002631 @Override
satokee5e77c2011-09-02 18:50:15 +09002632 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002633 if (!calledFromValidUser()) {
2634 return;
2635 }
satok91e88122011-07-18 11:11:42 +09002636 // By this IPC call, only a process which shares the same uid with the IME can add
2637 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002638 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002639 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002640 if (!mSystemReady) {
2641 return;
2642 }
satok91e88122011-07-18 11:11:42 +09002643 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002644 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002645 final String[] packageInfos;
2646 try {
2647 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2648 } catch (RemoteException e) {
2649 Slog.e(TAG, "Failed to get package infos");
2650 return;
2651 }
satok91e88122011-07-18 11:11:42 +09002652 if (packageInfos != null) {
2653 final int packageNum = packageInfos.length;
2654 for (int i = 0; i < packageNum; ++i) {
2655 if (packageInfos[i].equals(imi.getPackageName())) {
2656 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002657 final long ident = Binder.clearCallingIdentity();
2658 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002659 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002660 } finally {
2661 Binder.restoreCallingIdentity(ident);
2662 }
satokee5e77c2011-09-02 18:50:15 +09002663 return;
satok91e88122011-07-18 11:11:42 +09002664 }
2665 }
2666 }
satoke7c6998e2011-06-03 17:57:59 +09002667 }
satokee5e77c2011-09-02 18:50:15 +09002668 return;
satoke7c6998e2011-06-03 17:57:59 +09002669 }
2670
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002671 @Override
2672 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002673 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002674 }
2675
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002676 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002677 public void clearLastInputMethodWindowForTransition(IBinder token) {
2678 if (!calledFromValidUser()) {
2679 return;
2680 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002681 synchronized (mMethodMap) {
2682 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002683 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002684 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002685 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002686 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002687 }
2688
2689 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002690 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002691 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002692 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002693 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002694 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002695 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2696 if (DEBUG) {
2697 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2698 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2699 + " actual: " + sequenceNumber);
2700 }
2701 return;
2702 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002703 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2704 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002705 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002706 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002707 }
2708 }
2709
satok28203512010-11-24 11:06:49 +09002710 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002712 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2713 }
2714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002716 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2717 if (token == null) {
2718 if (mContext.checkCallingOrSelfPermission(
2719 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2720 != PackageManager.PERMISSION_GRANTED) {
2721 throw new SecurityException(
2722 "Using null token requires permission "
2723 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002725 } else if (mCurToken != token) {
2726 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2727 + " token: " + token);
2728 return;
2729 }
2730
2731 final long ident = Binder.clearCallingIdentity();
2732 try {
2733 setInputMethodLocked(id, subtypeId);
2734 } finally {
2735 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 }
2737 }
2738
satok42c5a162011-05-26 16:46:14 +09002739 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002741 if (!calledFromValidUser()) {
2742 return;
2743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002745 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 return;
2747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 long ident = Binder.clearCallingIdentity();
2749 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002750 hideCurrentInputLocked(flags, null);
2751 } finally {
2752 Binder.restoreCallingIdentity(ident);
2753 }
2754 }
2755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002756
satok42c5a162011-05-26 16:46:14 +09002757 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002758 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002759 if (!calledFromValidUser()) {
2760 return;
2761 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002762 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002763 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002764 return;
2765 }
2766 long ident = Binder.clearCallingIdentity();
2767 try {
2768 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 } finally {
2770 Binder.restoreCallingIdentity(ident);
2771 }
2772 }
2773 }
2774
2775 void setEnabledSessionInMainThread(SessionState session) {
2776 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002777 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002779 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002780 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 } catch (RemoteException e) {
2782 }
2783 }
2784 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002785 if (mEnabledSession != null && mEnabledSession.session != null) {
2786 try {
2787 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2788 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2789 } catch (RemoteException e) {
2790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
2792 }
2793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002794
satok42c5a162011-05-26 16:46:14 +09002795 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002797 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002799 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002800 final boolean showAuxSubtypes;
2801 switch (msg.arg1) {
2802 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2803 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2804 // implemented so that auxiliary subtypes will be excluded when the soft
2805 // keyboard is invisible.
2806 showAuxSubtypes = mInputShown;
2807 break;
2808 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2809 showAuxSubtypes = true;
2810 break;
2811 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2812 showAuxSubtypes = false;
2813 break;
2814 default:
2815 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2816 return false;
2817 }
2818 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002819 return true;
2820
satok47a44912010-10-06 16:03:58 +09002821 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002822 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002823 return true;
2824
2825 case MSG_SHOW_IM_CONFIG:
2826 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002827 return true;
2828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 case MSG_UNBIND_INPUT:
2832 try {
2833 ((IInputMethod)msg.obj).unbindInput();
2834 } catch (RemoteException e) {
2835 // There is nothing interesting about the method dying.
2836 }
2837 return true;
2838 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002839 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 try {
2841 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2842 } catch (RemoteException e) {
2843 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002844 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 return true;
2846 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002847 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002849 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002850 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002851 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 } catch (RemoteException e) {
2853 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002854 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 return true;
2856 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002857 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002859 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002860 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002861 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 } catch (RemoteException e) {
2863 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002864 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002866 case MSG_HIDE_CURRENT_INPUT_METHOD:
2867 synchronized (mMethodMap) {
2868 hideCurrentInputLocked(0, null);
2869 }
2870 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002872 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002874 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2876 } catch (RemoteException e) {
2877 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002878 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002880 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002881 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002882 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002883 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002885 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002887 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002888 // Dispose the channel if the input method is not local to this process
2889 // because the remote proxy will get its own copy when unparceled.
2890 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002891 channel.dispose();
2892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002894 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002898
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002899 case MSG_START_INPUT: {
2900 int missingMethods = msg.arg1;
2901 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002903 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002905 session.method.startInput((IInputContext) args.arg2, missingMethods,
2906 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 } catch (RemoteException e) {
2908 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002909 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002911 }
2912 case MSG_RESTART_INPUT: {
2913 int missingMethods = msg.arg1;
2914 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002916 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002918 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2919 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 } catch (RemoteException e) {
2921 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002922 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002924 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002927
Yohei Yukawa33e81792015-11-17 21:14:42 -08002928 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002930 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 } catch (RemoteException e) {
2932 // There is nothing interesting about the last client dying.
2933 }
2934 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002935 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002936 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002937 IInputMethodClient client = (IInputMethodClient)args.arg1;
2938 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002940 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002942 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002943 } finally {
2944 // Dispose the channel if the input method is not local to this process
2945 // because the remote proxy will get its own copy when unparceled.
2946 if (res.channel != null && Binder.isProxy(client)) {
2947 res.channel.dispose();
2948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002950 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002952 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002953 case MSG_SET_ACTIVE:
2954 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002955 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002956 } catch (RemoteException e) {
2957 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2958 + ((ClientState)msg.obj).pid + " uid "
2959 + ((ClientState)msg.obj).uid);
2960 }
2961 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002962 case MSG_SET_INTERACTIVE:
2963 handleSetInteractive(msg.arg1 != 0);
2964 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002965 case MSG_SWITCH_IME:
2966 handleSwitchInputMethod(msg.arg1 != 0);
2967 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002968 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2969 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002970 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002971 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002972 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002973 } catch (RemoteException e) {
2974 Slog.w(TAG, "Got RemoteException sending "
2975 + "setUserActionNotificationSequenceNumber("
2976 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002977 + clientState.pid + " uid "
2978 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002979 }
2980 return true;
2981 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002982 case MSG_REPORT_FULLSCREEN_MODE: {
2983 final boolean fullscreen = msg.arg1 != 0;
2984 final ClientState clientState = (ClientState)msg.obj;
2985 try {
2986 clientState.client.reportFullscreenMode(fullscreen);
2987 } catch (RemoteException e) {
2988 Slog.w(TAG, "Got RemoteException sending "
2989 + "reportFullscreen(" + fullscreen + ") notification to pid="
2990 + clientState.pid + " uid=" + clientState.uid);
2991 }
2992 return true;
2993 }
satok01038492012-04-09 21:08:27 +09002994
2995 // --------------------------------------------------------------
2996 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002997 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002998 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002999 case MSG_SYSTEM_UNLOCK_USER:
3000 final int userId = msg.arg1;
3001 onUnlockUser(userId);
3002 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 }
3004 return false;
3005 }
3006
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003007 private void handleSetInteractive(final boolean interactive) {
3008 synchronized (mMethodMap) {
3009 mIsInteractive = interactive;
3010 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3011
3012 // Inform the current client of the change in active status
3013 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003014 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3015 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3016 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003017 }
3018 }
3019 }
3020
Yohei Yukawaae61f712015-12-09 13:00:10 -08003021 private void handleSwitchInputMethod(final boolean forwardDirection) {
3022 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003023 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003024 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003025 if (nextSubtype == null) {
3026 return;
3027 }
3028 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003029 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3030 if (newInputMethodInfo == null) {
3031 return;
3032 }
3033 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3034 newInputMethodInfo, mCurrentSubtype);
3035 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003036 if (mSubtypeSwitchedByShortCutToast == null) {
3037 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3038 Toast.LENGTH_SHORT);
3039 } else {
3040 mSubtypeSwitchedByShortCutToast.setText(toastText);
3041 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003042 mSubtypeSwitchedByShortCutToast.show();
3043 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003044 }
3045 }
3046
satokdc9ddae2011-10-06 12:22:36 +09003047 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003048 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3049 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003050 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003051 if (DEBUG) {
3052 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3053 }
satok723a27e2010-11-11 14:58:11 +09003054 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003055 return true;
3056 }
3057
3058 return false;
3059 }
3060
Yohei Yukawa94e33302016-02-12 19:37:03 -08003061 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003062 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003063 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003064 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003065 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003066 if (!mSystemReady) {
3067 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3068 return;
3069 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003070 mMethodList.clear();
3071 mMethodMap.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -08003072 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003073
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003074 // Use for queryIntentServicesAsUser
3075 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003077 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3078 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3079 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003080 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003082 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3083 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003084
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003085 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003086 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 for (int i = 0; i < services.size(); ++i) {
3088 ResolveInfo ri = services.get(i);
3089 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003090 final String imeId = InputMethodInfo.computeId(ri);
3091 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3092 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 + ": it does not require the permission "
3094 + android.Manifest.permission.BIND_INPUT_METHOD);
3095 continue;
3096 }
3097
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003098 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003100 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 try {
satoke7c6998e2011-06-03 17:57:59 +09003102 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003103 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003104 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003105 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106
3107 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003108 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003110 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003111 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 }
3113 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003114
Yohei Yukawac4e44912017-02-09 19:30:22 -08003115 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3116 // negatives when the package state remains to be the same but only the component state is
3117 // changed.
3118 {
3119 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3120 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3121 // conservative, but it seems we cannot use it for now (Issue 35176630).
3122 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3123 new Intent(InputMethod.SERVICE_INTERFACE),
3124 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3125 final int N = allInputMethodServices.size();
3126 for (int i = 0; i < N; ++i) {
3127 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3128 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3129 continue;
3130 }
3131 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3132 }
3133 }
3134
Yohei Yukawa859df052016-02-17 07:56:46 -08003135 // TODO: The following code should find better place to live.
3136 if (!resetDefaultEnabledIme) {
3137 boolean enabledImeFound = false;
3138 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3139 final int N = enabledImes.size();
3140 for (int i = 0; i < N; ++i) {
3141 final InputMethodInfo imi = enabledImes.get(i);
3142 if (mMethodList.contains(imi)) {
3143 enabledImeFound = true;
3144 break;
3145 }
3146 }
3147 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003148 if (DEBUG) {
3149 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3150 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003151 resetDefaultEnabledIme = true;
3152 resetSelectedInputMethodAndSubtypeLocked("");
3153 }
3154 }
3155
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003156 if (resetDefaultEnabledIme) {
3157 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003158 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003159 final int N = defaultEnabledIme.size();
3160 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003161 final InputMethodInfo imi = defaultEnabledIme.get(i);
3162 if (DEBUG) {
3163 Slog.d(TAG, "--- enable ime = " + imi);
3164 }
3165 setInputMethodEnabledLocked(imi.getId(), true);
3166 }
3167 }
3168
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003169 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003170 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003171 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003172 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3173 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003174 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003175 }
3176 } else {
3177 // Double check that the default IME is certainly enabled.
3178 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003179 }
3180 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003181 // Here is not the perfect place to reset the switching controller. Ideally
3182 // mSwitchingController and mSettings should be able to share the same state.
3183 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3184 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003185 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003189
satok217f5482010-12-15 05:19:19 +09003190 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003191 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003192 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003193 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3194 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003195 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003196 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003197 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003198 final int userId;
3199 synchronized (mMethodMap) {
3200 userId = mSettings.getCurrentUserId();
3201 }
3202 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003203 }
3204
3205 private void showConfigureInputMethods() {
3206 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3207 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3208 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3209 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003210 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003211 }
3212
satok2c93efc2012-04-02 19:33:47 +09003213 private boolean isScreenLocked() {
3214 return mKeyguardManager != null
3215 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3216 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003217
Seigo Nonaka14e13912015-05-06 21:04:13 -07003218 private void showInputMethodMenu(boolean showAuxSubtypes) {
3219 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003222 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003223
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003224 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003225 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003226 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003227
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003228 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003229 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003230 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3231 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003232 if (immis == null || immis.size() == 0) {
3233 return;
3234 }
3235
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003236 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237
satok688bd472012-02-09 20:09:17 +09003238 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003239 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003240 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003241
satokc3690562012-01-10 20:14:43 +09003242 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003243 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003244 if (currentSubtype != null) {
3245 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003246 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3247 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003248 }
3249 }
3250
Ken Wakasa761eb372011-03-04 19:06:18 +09003251 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003252 mIms = new InputMethodInfo[N];
3253 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003254 int checkedItem = 0;
3255 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003256 final ImeSubtypeListItem item = imList.get(i);
3257 mIms[i] = item.mImi;
3258 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003259 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003260 int subtypeId = mSubtypeIds[i];
3261 if ((subtypeId == NOT_A_SUBTYPE_ID)
3262 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3263 || (subtypeId == lastInputMethodSubtypeId)) {
3264 checkedItem = i;
3265 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003268
3269 final Context settingsContext = new ContextThemeWrapper(context,
3270 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3271
3272 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003273 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3274 @Override
3275 public void onCancel(DialogInterface dialog) {
3276 hideInputMethodMenu();
3277 }
3278 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003279
3280 final Context dialogContext = mDialogBuilder.getContext();
3281 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3282 com.android.internal.R.styleable.DialogPreference,
3283 com.android.internal.R.attr.alertDialogStyle, 0);
3284 final Drawable dialogIcon = a.getDrawable(
3285 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3286 a.recycle();
3287
3288 mDialogBuilder.setIcon(dialogIcon);
3289
Yohei Yukawad34e1482016-02-11 08:03:52 -08003290 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003291 final View tv = inflater.inflate(
3292 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3293 mDialogBuilder.setCustomTitle(tv);
3294
3295 // Setup layout for a toggle switch of the hardware keyboard
3296 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003297 mSwitchingDialogTitleView
3298 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003299 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003300 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003301 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003302 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003303 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003304 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3305 @Override
3306 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003307 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003308 // Ensure that the input method dialog is dismissed when changing
3309 // the hardware keyboard state.
3310 hideInputMethodMenu();
3311 }
3312 });
3313
Alan Viverette505e3ab2014-11-24 15:22:11 -08003314 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003315 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3316 final OnClickListener choiceListener = new OnClickListener() {
3317 @Override
3318 public void onClick(final DialogInterface dialog, final int which) {
3319 synchronized (mMethodMap) {
3320 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3321 || mSubtypeIds.length <= which) {
3322 return;
satok01038492012-04-09 21:08:27 +09003323 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003324 final InputMethodInfo im = mIms[which];
3325 int subtypeId = mSubtypeIds[which];
3326 adapter.mCheckedItem = which;
3327 adapter.notifyDataSetChanged();
3328 hideInputMethodMenu();
3329 if (im != null) {
3330 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3331 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003332 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003333 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003335 }
3336 }
3337 };
3338 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003341 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003342 final Window w = mSwitchingDialog.getWindow();
3343 final WindowManager.LayoutParams attrs = w.getAttributes();
3344 w.setType(TYPE_INPUT_METHOD_DIALOG);
3345 // Use an alternate token for the dialog for that window manager can group the token
3346 // with other IME windows based on type vs. grouping based on whichever token happens
3347 // to get selected by the system later on.
3348 attrs.token = mSwitchingDialogToken;
3349 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3350 attrs.setTitle("Select input method");
3351 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003352 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 mSwitchingDialog.show();
3354 }
3355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003356
Ken Wakasa05dbb652011-08-22 15:22:43 +09003357 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3358 private final LayoutInflater mInflater;
3359 private final int mTextViewResourceId;
3360 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003361 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003362 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3363 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3364 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003365
Ken Wakasa05dbb652011-08-22 15:22:43 +09003366 mTextViewResourceId = textViewResourceId;
3367 mItemsList = itemsList;
3368 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003369 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003370 }
3371
3372 @Override
3373 public View getView(int position, View convertView, ViewGroup parent) {
3374 final View view = convertView != null ? convertView
3375 : mInflater.inflate(mTextViewResourceId, null);
3376 if (position < 0 || position >= mItemsList.size()) return view;
3377 final ImeSubtypeListItem item = mItemsList.get(position);
3378 final CharSequence imeName = item.mImeName;
3379 final CharSequence subtypeName = item.mSubtypeName;
3380 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3381 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3382 if (TextUtils.isEmpty(subtypeName)) {
3383 firstTextView.setText(imeName);
3384 secondTextView.setVisibility(View.GONE);
3385 } else {
3386 firstTextView.setText(subtypeName);
3387 secondTextView.setText(imeName);
3388 secondTextView.setVisibility(View.VISIBLE);
3389 }
3390 final RadioButton radioButton =
3391 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3392 radioButton.setChecked(position == mCheckedItem);
3393 return view;
3394 }
3395 }
3396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003398 synchronized (mMethodMap) {
3399 hideInputMethodMenuLocked();
3400 }
3401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003402
The Android Open Source Project10592532009-03-18 17:39:46 -07003403 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003404 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405
The Android Open Source Project10592532009-03-18 17:39:46 -07003406 if (mSwitchingDialog != null) {
3407 mSwitchingDialog.dismiss();
3408 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003410
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003411 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003412 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003413 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003417
satok42c5a162011-05-26 16:46:14 +09003418 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003420 // TODO: Make this work even for non-current users?
3421 if (!calledFromValidUser()) {
3422 return false;
3423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 synchronized (mMethodMap) {
3425 if (mContext.checkCallingOrSelfPermission(
3426 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3427 != PackageManager.PERMISSION_GRANTED) {
3428 throw new SecurityException(
3429 "Requires permission "
3430 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3431 }
Anna Galusza9b278112016-01-04 11:37:37 -08003432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 long ident = Binder.clearCallingIdentity();
3434 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003435 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 } finally {
3437 Binder.restoreCallingIdentity(ident);
3438 }
3439 }
3440 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003441
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003442 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3443 // Make sure this is a valid input method.
3444 InputMethodInfo imm = mMethodMap.get(id);
3445 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003446 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003447 }
3448
satokd87c2592010-09-29 11:52:06 +09003449 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3450 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003451
satokd87c2592010-09-29 11:52:06 +09003452 if (enabled) {
3453 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3454 if (pair.first.equals(id)) {
3455 // We are enabling this input method, but it is already enabled.
3456 // Nothing to do. The previous state was enabled.
3457 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003458 }
3459 }
satokd87c2592010-09-29 11:52:06 +09003460 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3461 // Previous state was disabled.
3462 return false;
3463 } else {
3464 StringBuilder builder = new StringBuilder();
3465 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3466 builder, enabledInputMethodsList, id)) {
3467 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003468 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003469 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3470 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3471 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003472 }
3473 // Previous state was enabled.
3474 return true;
3475 } else {
3476 // We are disabling the input method but it is already disabled.
3477 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003478 return false;
3479 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003480 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003481 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003482
satok723a27e2010-11-11 14:58:11 +09003483 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3484 boolean setSubtypeOnly) {
3485 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003486 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003487
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003488 mCurUserActionNotificationSequenceNumber =
3489 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3490 if (DEBUG) {
3491 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3492 + mCurUserActionNotificationSequenceNumber);
3493 }
3494
3495 if (mCurClient != null && mCurClient.client != null) {
3496 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3497 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003498 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003499 }
3500
satok723a27e2010-11-11 14:58:11 +09003501 // Set Subtype here
3502 if (imi == null || subtypeId < 0) {
3503 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003504 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003505 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003506 if (subtypeId < imi.getSubtypeCount()) {
3507 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3508 mSettings.putSelectedSubtype(subtype.hashCode());
3509 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003510 } else {
3511 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003512 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003513 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003514 }
satokab751aa2010-09-14 19:17:36 +09003515 }
satok723a27e2010-11-11 14:58:11 +09003516
Yohei Yukawa68645a62016-02-17 07:54:20 -08003517 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003518 // Set InputMethod here
3519 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3520 }
3521 }
3522
3523 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3524 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3525 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3526 // newDefaultIme is empty when there is no candidate for the selected IME.
3527 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3528 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3529 if (subtypeHashCode != null) {
3530 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003531 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003532 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003533 } catch (NumberFormatException e) {
3534 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3535 }
3536 }
3537 }
3538 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003539 }
3540
satok4e4569d2010-11-19 18:45:53 +09003541 // If there are no selected shortcuts, tries finding the most applicable ones.
3542 private Pair<InputMethodInfo, InputMethodSubtype>
3543 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3544 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3545 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003546 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003547 boolean foundInSystemIME = false;
3548
3549 // Search applicable subtype for each InputMethodInfo
3550 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003551 final String imiId = imi.getId();
3552 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3553 continue;
3554 }
satokcd7cd292010-11-20 15:46:23 +09003555 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003556 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003557 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003558 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003559 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003560 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003561 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003562 }
satokdf31ae62011-01-15 06:19:44 +09003563 // 2. Search by the system locale from enabledSubtypes.
3564 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003565 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003566 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003567 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003568 }
satoka86f5e42011-09-02 17:12:42 +09003569 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003570 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003571 final ArrayList<InputMethodSubtype> subtypesForSearch =
3572 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003573 ? InputMethodUtils.getSubtypes(imi)
3574 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003575 // 4. Search by the current subtype's locale from all subtypes.
3576 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003577 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003578 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003579 }
3580 // 5. Search by the system locale from all subtypes.
3581 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003582 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003583 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003584 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003585 }
satokcd7cd292010-11-20 15:46:23 +09003586 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003587 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003588 // The current input method is the most applicable IME.
3589 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003590 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003591 break;
satok7599a7f2010-12-22 13:45:23 +09003592 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003593 // The system input method is 2nd applicable IME.
3594 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003595 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003596 if ((imi.getServiceInfo().applicationInfo.flags
3597 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3598 foundInSystemIME = true;
3599 }
satok4e4569d2010-11-19 18:45:53 +09003600 }
3601 }
3602 }
3603 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003604 if (mostApplicableIMI != null) {
3605 Slog.w(TAG, "Most applicable shortcut input method was:"
3606 + mostApplicableIMI.getId());
3607 if (mostApplicableSubtype != null) {
3608 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3609 + "," + mostApplicableSubtype.getMode() + ","
3610 + mostApplicableSubtype.getLocale());
3611 }
3612 }
satok4e4569d2010-11-19 18:45:53 +09003613 }
satokcd7cd292010-11-20 15:46:23 +09003614 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003615 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003616 } else {
3617 return null;
3618 }
3619 }
3620
satokab751aa2010-09-14 19:17:36 +09003621 /**
3622 * @return Return the current subtype of this input method.
3623 */
satok42c5a162011-05-26 16:46:14 +09003624 @Override
satokab751aa2010-09-14 19:17:36 +09003625 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003626 // TODO: Make this work even for non-current users?
3627 if (!calledFromValidUser()) {
3628 return null;
3629 }
3630 synchronized (mMethodMap) {
3631 return getCurrentInputMethodSubtypeLocked();
3632 }
3633 }
3634
3635 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003636 if (mCurMethodId == null) {
3637 return null;
3638 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003639 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003640 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3641 if (imi == null || imi.getSubtypeCount() == 0) {
3642 return null;
satok4e4569d2010-11-19 18:45:53 +09003643 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003644 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003645 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3646 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003647 if (subtypeId == NOT_A_SUBTYPE_ID) {
3648 // If there are no selected subtypes, the framework will try to find
3649 // the most applicable subtype from explicitly or implicitly enabled
3650 // subtypes.
3651 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003652 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003653 // If there is only one explicitly or implicitly enabled subtype,
3654 // just returns it.
3655 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3656 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3657 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003658 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003659 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003660 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003661 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003662 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003663 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3664 true);
satok4e4569d2010-11-19 18:45:53 +09003665 }
satok3ef8b292010-11-23 06:06:29 +09003666 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003667 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003668 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003669 }
3670 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003671 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003672 }
3673
satok4e4569d2010-11-19 18:45:53 +09003674 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003675 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003676 @Override
satok4e4569d2010-11-19 18:45:53 +09003677 public List getShortcutInputMethodsAndSubtypes() {
3678 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003679 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003680 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003681 // If there are no selected shortcut subtypes, the framework will try to find
3682 // the most applicable subtype from all subtypes whose mode is
3683 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003684 Pair<InputMethodInfo, InputMethodSubtype> info =
3685 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003686 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003687 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003688 ret.add(info.first);
3689 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003690 }
satok3da92232011-01-11 22:46:30 +09003691 return ret;
satokf3db1af2010-11-23 13:34:33 +09003692 }
satokf3db1af2010-11-23 13:34:33 +09003693 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3694 ret.add(imi);
3695 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3696 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003697 }
3698 }
satokf3db1af2010-11-23 13:34:33 +09003699 return ret;
satok4e4569d2010-11-19 18:45:53 +09003700 }
3701 }
3702
satok42c5a162011-05-26 16:46:14 +09003703 @Override
satokb66d2872010-11-10 01:04:04 +09003704 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003705 // TODO: Make this work even for non-current users?
3706 if (!calledFromValidUser()) {
3707 return false;
3708 }
satokb66d2872010-11-10 01:04:04 +09003709 synchronized (mMethodMap) {
3710 if (subtype != null && mCurMethodId != null) {
3711 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003712 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003713 if (subtypeId != NOT_A_SUBTYPE_ID) {
3714 setInputMethodLocked(mCurMethodId, subtypeId);
3715 return true;
3716 }
3717 }
3718 return false;
3719 }
3720 }
3721
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003722 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003723 private static class InputMethodFileManager {
3724 private static final String SYSTEM_PATH = "system";
3725 private static final String INPUT_METHOD_PATH = "inputmethod";
3726 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3727 private static final String NODE_SUBTYPES = "subtypes";
3728 private static final String NODE_SUBTYPE = "subtype";
3729 private static final String NODE_IMI = "imi";
3730 private static final String ATTR_ID = "id";
3731 private static final String ATTR_LABEL = "label";
3732 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003733 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003734 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003735 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003736 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3737 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3738 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003739 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003740 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3741 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003742 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003743 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003744 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003745 if (methodMap == null) {
3746 throw new NullPointerException("methodMap is null");
3747 }
3748 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003749 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003750 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3751 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003752 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003753 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003754 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3755 }
3756 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3757 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3758 if (!subtypeFile.exists()) {
3759 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003760 writeAdditionalInputMethodSubtypes(
3761 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003762 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003763 readAdditionalInputMethodSubtypes(
3764 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003765 }
3766 }
3767
3768 private void deleteAllInputMethodSubtypes(String imiId) {
3769 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003770 mAdditionalSubtypesMap.remove(imiId);
3771 writeAdditionalInputMethodSubtypes(
3772 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003773 }
3774 }
3775
3776 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003777 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003778 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003779 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003780 final int N = additionalSubtypes.length;
3781 for (int i = 0; i < N; ++i) {
3782 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003783 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003784 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003785 } else {
3786 Slog.w(TAG, "Duplicated subtype definition found: "
3787 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003788 }
3789 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003790 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3791 writeAdditionalInputMethodSubtypes(
3792 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003793 }
3794 }
3795
3796 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3797 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003798 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003799 }
3800 }
3801
3802 private static void writeAdditionalInputMethodSubtypes(
3803 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3804 HashMap<String, InputMethodInfo> methodMap) {
3805 // Safety net for the case that this function is called before methodMap is set.
3806 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3807 FileOutputStream fos = null;
3808 try {
3809 fos = subtypesFile.startWrite();
3810 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003811 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003812 out.startDocument(null, true);
3813 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3814 out.startTag(null, NODE_SUBTYPES);
3815 for (String imiId : allSubtypes.keySet()) {
3816 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3817 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3818 continue;
3819 }
3820 out.startTag(null, NODE_IMI);
3821 out.attribute(null, ATTR_ID, imiId);
3822 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3823 final int N = subtypesList.size();
3824 for (int i = 0; i < N; ++i) {
3825 final InputMethodSubtype subtype = subtypesList.get(i);
3826 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003827 if (subtype.hasSubtypeId()) {
3828 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3829 String.valueOf(subtype.getSubtypeId()));
3830 }
satoke7c6998e2011-06-03 17:57:59 +09003831 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3832 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3833 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003834 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3835 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003836 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3837 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3838 out.attribute(null, ATTR_IS_AUXILIARY,
3839 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003840 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3841 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003842 out.endTag(null, NODE_SUBTYPE);
3843 }
3844 out.endTag(null, NODE_IMI);
3845 }
3846 out.endTag(null, NODE_SUBTYPES);
3847 out.endDocument();
3848 subtypesFile.finishWrite(fos);
3849 } catch (java.io.IOException e) {
3850 Slog.w(TAG, "Error writing subtypes", e);
3851 if (fos != null) {
3852 subtypesFile.failWrite(fos);
3853 }
3854 }
3855 }
3856
3857 private static void readAdditionalInputMethodSubtypes(
3858 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3859 if (allSubtypes == null || subtypesFile == null) return;
3860 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003861 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003862 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003863 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003864 int type = parser.getEventType();
3865 // Skip parsing until START_TAG
3866 while ((type = parser.next()) != XmlPullParser.START_TAG
3867 && type != XmlPullParser.END_DOCUMENT) {}
3868 String firstNodeName = parser.getName();
3869 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3870 throw new XmlPullParserException("Xml doesn't start with subtypes");
3871 }
3872 final int depth =parser.getDepth();
3873 String currentImiId = null;
3874 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3875 while (((type = parser.next()) != XmlPullParser.END_TAG
3876 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3877 if (type != XmlPullParser.START_TAG)
3878 continue;
3879 final String nodeName = parser.getName();
3880 if (NODE_IMI.equals(nodeName)) {
3881 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3882 if (TextUtils.isEmpty(currentImiId)) {
3883 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3884 continue;
3885 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003886 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003887 allSubtypes.put(currentImiId, tempSubtypesArray);
3888 } else if (NODE_SUBTYPE.equals(nodeName)) {
3889 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3890 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3891 continue;
3892 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003893 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003894 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003895 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003896 parser.getAttributeValue(null, ATTR_LABEL));
3897 final String imeSubtypeLocale =
3898 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003899 final String languageTag =
3900 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003901 final String imeSubtypeMode =
3902 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3903 final String imeSubtypeExtraValue =
3904 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003905 final boolean isAuxiliary = "1".equals(String.valueOf(
3906 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003907 final boolean isAsciiCapable = "1".equals(String.valueOf(
3908 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003909 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003910 .setSubtypeNameResId(label)
3911 .setSubtypeIconResId(icon)
3912 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003913 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003914 .setSubtypeMode(imeSubtypeMode)
3915 .setSubtypeExtraValue(imeSubtypeExtraValue)
3916 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003917 .setIsAsciiCapable(isAsciiCapable);
3918 final String subtypeIdString =
3919 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3920 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003921 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003922 }
3923 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003924 }
3925 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003926 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3927 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003928 return;
satoke7c6998e2011-06-03 17:57:59 +09003929 }
3930 }
3931 }
3932
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003933 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3934 @NonNull
3935 private final Handler mHandler;
3936
3937 LocalServiceImpl(@NonNull final Handler handler) {
3938 mHandler = handler;
3939 }
3940
3941 @Override
3942 public void setInteractive(boolean interactive) {
3943 // Do everything in handler so as not to block the caller.
3944 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3945 interactive ? 1 : 0, 0));
3946 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003947
3948 @Override
3949 public void switchInputMethod(boolean forwardDirection) {
3950 // Do everything in handler so as not to block the caller.
3951 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3952 forwardDirection ? 1 : 0, 0));
3953 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003954
3955 @Override
3956 public void hideCurrentInputMethod() {
3957 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3958 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3959 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003960 }
3961
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003962 private static String imeWindowStatusToString(final int imeWindowVis) {
3963 final StringBuilder sb = new StringBuilder();
3964 boolean first = true;
3965 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3966 sb.append("Active");
3967 first = false;
3968 }
3969 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3970 if (!first) {
3971 sb.append("|");
3972 }
3973 sb.append("Visible");
3974 }
3975 return sb.toString();
3976 }
3977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003979 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3980 @Nullable Uri contentUri, @Nullable String packageName) {
3981 if (!calledFromValidUser()) {
3982 return null;
3983 }
3984
3985 if (token == null) {
3986 throw new NullPointerException("token");
3987 }
3988 if (packageName == null) {
3989 throw new NullPointerException("packageName");
3990 }
3991 if (contentUri == null) {
3992 throw new NullPointerException("contentUri");
3993 }
3994 final String contentUriScheme = contentUri.getScheme();
3995 if (!"content".equals(contentUriScheme)) {
3996 throw new InvalidParameterException("contentUri must have content scheme");
3997 }
3998
3999 synchronized (mMethodMap) {
4000 final int uid = Binder.getCallingUid();
4001 if (mCurMethodId == null) {
4002 return null;
4003 }
4004 if (mCurToken != token) {
4005 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4006 + " token=" + token);
4007 return null;
4008 }
4009 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4010 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4011 // nature of our system. Let's compare it with our internal record.
4012 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4013 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4014 + mCurAttribute.packageName + " packageName=" + packageName);
4015 return null;
4016 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004017 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004018 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004019 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004020 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004021 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4022 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4023 imeUserId);
4024 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4025 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4026 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4027 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4028 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4029 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4030 // actually allowed to "uid", which is guaranteed to be the IME's one.
4031 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4032 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004033 }
4034 }
4035
4036 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004037 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4038 if (!calledFromValidUser()) {
4039 return;
4040 }
4041 synchronized (mMethodMap) {
4042 if (!calledWithValidToken(token)) {
4043 return;
4044 }
4045 if (mCurClient != null && mCurClient.client != null) {
4046 mInFullscreenMode = fullscreen;
4047 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4048 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4049 }
4050 }
4051 }
4052
4053 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4055 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4056 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4059 + Binder.getCallingPid()
4060 + ", uid=" + Binder.getCallingUid());
4061 return;
4062 }
4063
4064 IInputMethod method;
4065 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004066 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 synchronized (mMethodMap) {
4071 p.println("Current Input Method Manager state:");
4072 int N = mMethodList.size();
4073 p.println(" Input Methods:");
4074 for (int i=0; i<N; i++) {
4075 InputMethodInfo info = mMethodList.get(i);
4076 p.println(" InputMethod #" + i + ":");
4077 info.dump(p, " ");
4078 }
4079 p.println(" Clients:");
4080 for (ClientState ci : mClients.values()) {
4081 p.println(" Client " + ci + ":");
4082 p.println(" client=" + ci.client);
4083 p.println(" inputContext=" + ci.inputContext);
4084 p.println(" sessionRequested=" + ci.sessionRequested);
4085 p.println(" curSession=" + ci.curSession);
4086 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004087 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004089 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4090 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004091 focusedWindowClient = mCurFocusedWindowClient;
4092 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4094 + " mBoundToMethod=" + mBoundToMethod);
4095 p.println(" mCurToken=" + mCurToken);
4096 p.println(" mCurIntent=" + mCurIntent);
4097 method = mCurMethod;
4098 p.println(" mCurMethod=" + mCurMethod);
4099 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004100 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 p.println(" mShowRequested=" + mShowRequested
4102 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4103 + " mShowForced=" + mShowForced
4104 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004105 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004106 p.println(" mCurUserActionNotificationSequenceNumber="
4107 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004108 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004109 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004110 p.println(" mSwitchingController:");
4111 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004112 p.println(" mSettings:");
4113 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004115
Jeff Brownb88102f2010-09-08 11:49:43 -07004116 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 pw.flush();
4119 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004120 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4121 } catch (IOException | RemoteException e) {
4122 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004124 } else {
4125 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004127
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004128 if (focusedWindowClient != null && client != focusedWindowClient) {
4129 p.println(" ");
4130 p.println("Warning: Current input method client doesn't match the last focused. "
4131 + "window.");
4132 p.println("Dumping input method client in the last focused window just in case.");
4133 p.println(" ");
4134 pw.flush();
4135 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004136 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4137 } catch (IOException | RemoteException e) {
4138 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004139 }
4140 }
4141
Jeff Brownb88102f2010-09-08 11:49:43 -07004142 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 pw.flush();
4145 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004146 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4147 } catch (IOException | RemoteException e) {
4148 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004150 } else {
4151 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
4153 }
4154}