blob: fd7a27f3c1f61c1ec9dbab10993f25571e93e929 [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;
Yohei Yukawa3eaf8672017-02-11 23:40:07 +0000178 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
Yohei Yukawae0733062017-02-09 22:49:35 -0800230 /**
231 * Tracks how many times {@link #mMethodMap} was updated.
232 */
233 @GuardedBy("mMethodMap")
234 private int mMethodMapUpdateCount = 0;
235
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700236 // Used to bring IME service up to visible adjustment while it is being shown.
237 final ServiceConnection mVisibleConnection = new ServiceConnection() {
238 @Override public void onServiceConnected(ComponentName name, IBinder service) {
239 }
240
241 @Override public void onServiceDisconnected(ComponentName name) {
242 }
243 };
244 boolean mVisibleBound = false;
245
satok7cfc0ed2011-06-20 21:29:36 +0900246 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700247 private NotificationManager mNotificationManager;
248 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700249 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400250 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700251 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900252 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900253 private boolean mNotificationShown;
254
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900255 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 final ClientState client;
257 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700258
259 IInputMethodSession session;
260 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 @Override
263 public String toString() {
264 return "SessionState{uid " + client.uid + " pid " + client.pid
265 + " method " + Integer.toHexString(
266 System.identityHashCode(method))
267 + " session " + Integer.toHexString(
268 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700269 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 + "}";
271 }
272
273 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700274 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 client = _client;
276 method = _method;
277 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700278 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
Jeff Brownc28867a2013-03-26 15:42:39 -0700282 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 final IInputMethodClient client;
284 final IInputContext inputContext;
285 final int uid;
286 final int pid;
287 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 boolean sessionRequested;
290 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 @Override
293 public String toString() {
294 return "ClientState{" + Integer.toHexString(
295 System.identityHashCode(this)) + " uid " + uid
296 + " pid " + pid + "}";
297 }
298
299 ClientState(IInputMethodClient _client, IInputContext _inputContext,
300 int _uid, int _pid) {
301 client = _client;
302 inputContext = _inputContext;
303 uid = _uid;
304 pid = _pid;
305 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
306 }
307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700309 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700312 * Set once the system is ready to run third party code.
313 */
314 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700316 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700317 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
318 * method. This is to be synchronized with the secure settings keyed with
319 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
320 *
321 * <p>This can be transiently {@code null} when the system is re-initializing input method
322 * settings, e.g., the system locale is just changed.</p>
323 *
324 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
325 * {@link InputMethodManagerService}.</p>
326 *
327 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700329 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * The current binding sequence number, incremented every time there is
334 * a new bind performed.
335 */
336 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * The client that is currently bound to an input method.
340 */
341 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800344 * The last window token that we confirmed to be focused. This is always updated upon reports
345 * from the input method client. If the window state is already changed before the report is
346 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700347 */
348 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700350 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800351 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
352 *
353 * @see #mCurFocusedWindow
354 */
355 int mCurFocusedWindowSoftInputMode;
356
357 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800358 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
359 */
360 ClientState mCurFocusedWindowClient;
361
362 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 * The input context last provided by the current client.
364 */
365 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700368 * The missing method flags for the input context last provided by the current client.
369 *
370 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
371 */
372 int mCurInputContextMissingMethods;
373
374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 * The attributes last provided by the current client.
376 */
377 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700380 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700382 *
383 * <p>This can be {@code null} when no input method is connected.</p>
384 *
385 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700387 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 /**
satokab751aa2010-09-14 19:17:36 +0900391 * The current subtype of the current input method.
392 */
393 private InputMethodSubtype mCurrentSubtype;
394
satok4e4569d2010-11-19 18:45:53 +0900395 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900396 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700397 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900398
John Spurlocke0980502013-10-25 11:59:29 -0400399 // Was the keyguard locked when this client became current?
400 private boolean mCurClientInKeyguard;
401
satokab751aa2010-09-14 19:17:36 +0900402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 * Set to true if our ServiceConnection is currently actively bound to
404 * a service (whether or not we have gotten its IBinder back yet).
405 */
406 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Set if the client has asked for the input method to be shown.
410 */
411 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * Set if we were explicitly told to show the input method.
415 */
416 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
419 * Set if we were forced to be shown.
420 */
421 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 /**
424 * Set if we last told the input method to show itself.
425 */
426 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800429 * {@code true} if the current input method is in fullscreen mode.
430 */
431 boolean mInFullscreenMode;
432
433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * The Intent used to connect to the current input method.
435 */
436 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 /**
439 * The token we have made for the currently active input method, to
440 * identify it in the future.
441 */
442 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
445 * If non-null, this is the input method service we are currently connected
446 * to.
447 */
448 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
451 * Time that we last initiated a bind to the input method, to determine
452 * if we should try to disconnect and reconnect to it.
453 */
454 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 /**
457 * Have we called mCurMethod.bindInput()?
458 */
459 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 /**
462 * Currently enabled session. Only touched by service thread, not
463 * protected by a lock.
464 */
465 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700468 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700470 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900472 int mCurUserActionNotificationSequenceNumber = 0;
473
Joe Onorato857fd9b2011-01-27 15:08:35 -0800474 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900475
476 /**
477 * A set of status bits regarding the active IME.
478 *
479 * <p>This value is a combination of following two bits:</p>
480 * <dl>
481 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
482 * <dd>
483 * If this bit is ON, connected IME is ready to accept touch/key events.
484 * </dd>
485 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
486 * <dd>
487 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
488 * </dd>
489 * </dl>
490 * <em>Do not update this value outside of setImeWindowStatus.</em>
491 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800492 int mImeWindowVis;
493
Ken Wakasa05dbb652011-08-22 15:22:43 +0900494 private AlertDialog.Builder mDialogBuilder;
495 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700496 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900497 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700498 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900499 private InputMethodInfo[] mIms;
500 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800501 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700502 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800503 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900504 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
505 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500506 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700507 @HardKeyboardBehavior
508 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700511 int mUserId;
512 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700513 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800514 String mLastEnabled = "";
515
Yohei Yukawa81482972015-06-04 00:58:59 -0700516 /**
517 * <em>This constructor must be called within the lock.</em>
518 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 SettingsObserver(Handler handler) {
520 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700521 }
522
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800523 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700524 if (mRegistered && mUserId == userId) {
525 return;
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700528 if (mRegistered) {
529 mContext.getContentResolver().unregisterContentObserver(this);
530 mRegistered = false;
531 }
532 if (mUserId != userId) {
533 mLastEnabled = "";
534 mUserId = userId;
535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700537 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900538 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700539 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900540 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700541 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700542 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700543 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800544 resolver.registerContentObserver(Settings.Secure.getUriFor(
545 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700546 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800548
Michael Wright7b5a96b2014-08-09 19:28:42 -0700549 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800550 final Uri showImeUri = Settings.Secure.getUriFor(
551 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
552 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
553 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700555 if (showImeUri.equals(uri)) {
556 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800557 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
558 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
559 mContext.getContentResolver(),
560 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
561 0, mUserId) == 1;
562 if (mAccessibilityRequestingNoSoftKeyboard) {
563 final boolean showRequested = mShowRequested;
564 hideCurrentInputLocked(0, null);
565 mShowRequested = showRequested;
566 } else if (mShowRequested) {
567 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
568 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700569 } else {
570 boolean enabledChanged = false;
571 String newEnabled = mSettings.getEnabledInputMethodsStr();
572 if (!mLastEnabled.equals(newEnabled)) {
573 mLastEnabled = newEnabled;
574 enabledChanged = true;
575 }
576 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700580
581 @Override
582 public String toString() {
583 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
584 + " mLastEnabled=" + mLastEnabled + "}";
585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800587
Yohei Yukawa79247822017-01-23 15:26:15 -0800588 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900589 @Override
590 public void onReceive(Context context, Intent intent) {
591 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700592 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900593 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700594 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900595 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800596 } else if (Intent.ACTION_USER_ADDED.equals(action)
597 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100598 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800599 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700600 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
601 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
602 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
603 final String prevValue = intent.getStringExtra(
604 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
605 final String newValue = intent.getStringExtra(
606 Intent.EXTRA_SETTING_NEW_VALUE);
607 restoreEnabledInputMethods(mContext, prevValue, newValue);
608 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800609 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800610 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900611 } else {
612 Slog.w(TAG, "Unexpected intent " + intent);
613 }
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800616
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800617 /**
618 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
619 *
620 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
621 * the users. We should ignore this event if this is about any background user's locale.</p>
622 *
623 * <p>Caution: This method must not be called when system is not ready.</p>
624 */
625 void onActionLocaleChanged() {
626 synchronized (mMethodMap) {
627 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
628 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
629 return;
630 }
631 buildInputMethodListLocked(true);
632 // If the locale is changed, needs to reset the default ime
633 resetDefaultImeLocked(mContext);
634 updateFromSettingsLocked(true);
635 mLastSystemLocales = possibleNewLocale;
636 }
637 }
638
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700639 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
640 // does not attempt to validate on the fly with any installed device policy, so must only
641 // be run in the context of initial device setup.
642 //
643 // TODO: Move this method to InputMethodUtils with adding unit tests.
644 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
645 if (DEBUG_RESTORE) {
646 Slog.i(TAG, "Restoring enabled input methods:");
647 Slog.i(TAG, "prev=" + prevValue);
648 Slog.i(TAG, " new=" + newValue);
649 }
650 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900651 ArrayMap<String, ArraySet<String>> prevMap =
652 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
653 ArrayMap<String, ArraySet<String>> newMap =
654 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700655
656 // Merge the restored ime+subtype enabled states into the live state
657 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
658 final String imeId = entry.getKey();
659 ArraySet<String> prevSubtypes = prevMap.get(imeId);
660 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700661 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700662 prevMap.put(imeId, prevSubtypes);
663 }
664 prevSubtypes.addAll(entry.getValue());
665 }
666
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700667 final String mergedImesAndSubtypesString =
668 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700669 if (DEBUG_RESTORE) {
670 Slog.i(TAG, "Merged IME string:");
671 Slog.i(TAG, " " + mergedImesAndSubtypesString);
672 }
673 Settings.Secure.putString(context.getContentResolver(),
674 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
675 }
676
Yohei Yukawac4e44912017-02-09 19:30:22 -0800677 final class MyPackageMonitor extends PackageMonitor {
678 /**
679 * Set of packages to be monitored.
680 *
681 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
682 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
683 * dynamically unless the entire package is updated, which also always triggers package
684 * rescanning.</p>
685 */
686 @GuardedBy("mMethodMap")
687 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
688
689 @GuardedBy("mMethodMap")
690 void clearPackagesToMonitorComponentChangeLocked() {
691 mPackagesToMonitorComponentChange.clear();
692 }
693
694 @GuardedBy("mMethodMap")
695 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
696 mPackagesToMonitorComponentChange.add(packageName);
697 }
698
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900699 private boolean isChangingPackagesOfCurrentUser() {
700 final int userId = getChangingUserId();
701 final boolean retval = userId == mSettings.getCurrentUserId();
702 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900703 if (!retval) {
704 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
705 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900706 }
707 return retval;
708 }
709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800711 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900712 if (!isChangingPackagesOfCurrentUser()) {
713 return false;
714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900716 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 final int N = mMethodList.size();
718 if (curInputMethodId != null) {
719 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800720 InputMethodInfo imi = mMethodList.get(i);
721 if (imi.getId().equals(curInputMethodId)) {
722 for (String pkg : packages) {
723 if (imi.getPackageName().equals(pkg)) {
724 if (!doit) {
725 return true;
726 }
satok723a27e2010-11-11 14:58:11 +0900727 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800728 chooseNewDefaultIMELocked();
729 return true;
730 }
731 }
732 }
733 }
734 }
735 }
736 return false;
737 }
738
739 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800740 public boolean onPackageChanged(String packageName, int uid, String[] components) {
741 // If this package is in the watch list, we want to check it.
742 synchronized (mMethodMap) {
743 return mPackagesToMonitorComponentChange.contains(packageName);
744 }
745 }
746
747 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800748 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900749 if (!isChangingPackagesOfCurrentUser()) {
750 return;
751 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800752 synchronized (mMethodMap) {
753 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800755 final int N = mMethodList.size();
756 if (curInputMethodId != null) {
757 for (int i=0; i<N; i++) {
758 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900759 final String imiId = imi.getId();
760 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800761 curIm = imi;
762 }
satoke7c6998e2011-06-03 17:57:59 +0900763
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800764 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900765 if (isPackageModified(imi.getPackageName())) {
766 mFileManager.deleteAllInputMethodSubtypes(imiId);
767 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800768 if (change == PACKAGE_TEMPORARY_CHANGE
769 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800770 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800771 + imi.getComponent());
772 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774 }
775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
Yohei Yukawa94e33302016-02-12 19:37:03 -0800777 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800781 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800782 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800783 if (change == PACKAGE_TEMPORARY_CHANGE
784 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800785 ServiceInfo si = null;
786 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900787 si = mIPackageManager.getServiceInfo(
788 curIm.getComponent(), 0, mSettings.getCurrentUserId());
789 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800790 }
791 if (si == null) {
792 // Uh oh, current input method is no longer around!
793 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800794 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900795 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800796 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800797 changed = true;
798 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800799 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900800 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800801 }
802 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800803 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800804 }
satokab751aa2010-09-14 19:17:36 +0900805
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800806 if (curIm == null) {
807 // We currently don't have a default input method... is
808 // one now available?
809 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700810 } else if (!changed && isPackageModified(curIm.getPackageName())) {
811 // Even if the current input method is still available, mCurrentSubtype could
812 // be obsolete when the package is modified in practice.
813 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800815
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800816 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800817 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
819 }
820 }
821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800822
Jeff Brownc28867a2013-03-26 15:42:39 -0700823 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900824 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700825 private final IInputMethod mMethod;
826 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
Jeff Brownc28867a2013-03-26 15:42:39 -0700828 MethodCallback(InputMethodManagerService imms, IInputMethod method,
829 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900830 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700831 mMethod = method;
832 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800834
satoke7c6998e2011-06-03 17:57:59 +0900835 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700836 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700837 long ident = Binder.clearCallingIdentity();
838 try {
839 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
840 } finally {
841 Binder.restoreCallingIdentity(ident);
842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800845
satok01038492012-04-09 21:08:27 +0900846 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700847 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900848 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700849 public void onHardKeyboardStatusChange(boolean available) {
850 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
851 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900852 }
853
Michael Wright7b5a96b2014-08-09 19:28:42 -0700854 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900855 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700856 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900857 }
858 synchronized(mMethodMap) {
859 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
860 && mSwitchingDialog.isShowing()) {
861 mSwitchingDialogTitleView.findViewById(
862 com.android.internal.R.id.hard_keyboard_section).setVisibility(
863 available ? View.VISIBLE : View.GONE);
864 }
865 }
866 }
867 }
868
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800869 public static final class Lifecycle extends SystemService {
870 private InputMethodManagerService mService;
871
872 public Lifecycle(Context context) {
873 super(context);
874 mService = new InputMethodManagerService(context);
875 }
876
877 @Override
878 public void onStart() {
879 LocalServices.addService(InputMethodManagerInternal.class,
880 new LocalServiceImpl(mService.mHandler));
881 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
882 }
883
884 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800885 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700886 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800887 // TODO: Dispatch this to a worker thread as needed.
888 mService.onSwitchUser(userHandle);
889 }
890
891 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800892 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700893 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800894 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800895 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
896 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
897 .getService(Context.STATUS_BAR_SERVICE);
898 mService.systemRunning(statusBarService);
899 }
900 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800901
902 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700903 public void onUnlockUser(final @UserIdInt int userHandle) {
904 // Called on ActivityManager thread.
905 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700906 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800907 }
908 }
909
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800910 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800911 synchronized(mMethodMap) {
912 final int currentUserId = mSettings.getCurrentUserId();
913 if (DEBUG) {
914 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
915 }
916 if (userId != currentUserId) {
917 return;
918 }
919 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800920 if (mSystemReady) {
921 // We need to rebuild IMEs.
922 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
923 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
924 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800925 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800926 }
927
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800928 void onSwitchUser(@UserIdInt int userId) {
929 synchronized (mMethodMap) {
930 switchUserLocked(userId);
931 }
932 }
933
Seigo Nonaka7309b122015-08-17 18:34:13 -0700934 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900935 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800937 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700939 // Note: SettingsObserver doesn't register observers in its constructor.
940 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 mIWindowManager = IWindowManager.Stub.asInterface(
942 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700943 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800944 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900945 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 public void executeMessage(Message msg) {
947 handleMessage(msg);
948 }
Mita Yuned218c72012-12-06 17:18:25 -0800949 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800950 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800951 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900952 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700953 mHasFeature = context.getPackageManager().hasSystemFeature(
954 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500955 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700956 mHardKeyboardBehavior = mContext.getResources().getInteger(
957 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900958
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400959 Bundle extras = new Bundle();
960 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
961 mImeSwitcherNotification = new Notification.Builder(mContext)
962 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
963 .setWhen(0)
964 .setOngoing(true)
965 .addExtras(extras)
966 .setCategory(Notification.CATEGORY_SYSTEM)
967 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400968
satok7cfc0ed2011-06-20 21:29:36 +0900969 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900970 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900971
972 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900973
satok7cfc0ed2011-06-20 21:29:36 +0900974 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900975 int userId = 0;
976 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800977 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900978 } catch (RemoteException e) {
979 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
980 }
satok913a8922010-08-26 21:53:41 +0900981
satokd87c2592010-09-29 11:52:06 +0900982 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900983 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800984 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700985
Kenny Guy2a764942014-04-02 13:29:20 +0100986 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900987 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800988 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
989 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900990 }
991
satok5b927c432012-05-01 20:09:34 +0900992 private void resetDefaultImeLocked(Context context) {
993 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800994 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900995 return;
996 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800997 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800998 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800999 if (suitableImes.isEmpty()) {
1000 Slog.i(TAG, "No default found");
1001 return;
satok5b927c432012-05-01 20:09:34 +09001002 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001003 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001004 if (DEBUG) {
1005 Slog.i(TAG, "Default found, using " + defIm.getId());
1006 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001007 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001008 }
1009
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001010 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001011 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1012 + " currentUserId=" + mSettings.getCurrentUserId());
1013
Yohei Yukawa81482972015-06-04 00:58:59 -07001014 // ContentObserver should be registered again when the user is changed
1015 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001016
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001017 // If the system is not ready or the device is not yed unlocked by the user, then we use
1018 // copy-on-write settings.
1019 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001020 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001021 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001022 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001023 // InputMethodFileManager should be reset when the user is changed
1024 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001025 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001026
1027 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1028 + " defaultImiId=" + defaultImiId);
1029
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001030 // For secondary users, the list of enabled IMEs may not have been updated since the
1031 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1032 // not be empty even if the IME has been uninstalled by the primary user.
1033 // Even in such cases, IMMS works fine because it will find the most applicable
1034 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001035 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001036 mLastSystemLocales = mRes.getConfiguration().getLocales();
1037
1038 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1039 if (mSystemReady) {
1040 hideCurrentInputLocked(0, null);
1041 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1042 buildInputMethodListLocked(initialUserSwitch);
1043 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1044 // This is the first time of the user switch and
1045 // set the current ime to the proper one.
1046 resetDefaultImeLocked(mContext);
1047 }
1048 updateFromSettingsLocked(true);
1049 try {
1050 startInputInnerLocked();
1051 } catch (RuntimeException e) {
1052 Slog.w(TAG, "Unexpected exception", e);
1053 }
1054 }
1055
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001056 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001057 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1058 mSettings.getEnabledInputMethodListLocked(), newUserId,
1059 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001060 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001061
1062 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1063 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001064 }
1065
Kenny Guy2a764942014-04-02 13:29:20 +01001066 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001067 mSettings.setCurrentProfileIds(
1068 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001069 }
1070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 @Override
1072 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1073 throws RemoteException {
1074 try {
1075 return super.onTransact(code, data, reply, flags);
1076 } catch (RuntimeException e) {
1077 // The input method manager only throws security exceptions, so let's
1078 // log all others.
1079 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001080 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 throw e;
1083 }
1084 }
1085
Svetoslav Ganova0027152013-06-25 14:59:53 -07001086 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001087 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001088 if (DEBUG) {
1089 Slog.d(TAG, "--- systemReady");
1090 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001091 if (!mSystemReady) {
1092 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001093 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001094 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001095 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001096 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001097 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1098 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001099 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001100 if (mStatusBar != null) {
1101 mStatusBar.setIconVisibility(mSlotIme, false);
1102 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001103 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001104 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1105 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001106 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001107 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001108 mHardKeyboardListener);
1109 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001110
1111 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1112 mSettingsObserver.registerContentObserverLocked(currentUserId);
1113
1114 final IntentFilter broadcastFilter = new IntentFilter();
1115 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1116 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1117 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1118 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1119 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1120 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1121
1122 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1123 resetDefaultImeLocked(mContext);
1124 updateFromSettingsLocked(true);
1125 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1126 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1127 mContext.getBasePackageName());
1128
Dianne Hackborncc278702009-09-02 23:07:23 -07001129 try {
1130 startInputInnerLocked();
1131 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001132 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001133 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001134 }
1135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001138 // ---------------------------------------------------------------------------------------
1139 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1140 // 1) it comes from the system process
1141 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1142 private boolean calledFromValidUser() {
1143 final int uid = Binder.getCallingUid();
1144 final int userId = UserHandle.getUserId(uid);
1145 if (DEBUG) {
1146 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1147 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1148 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001149 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1150 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001151 }
Kenny Guy2a764942014-04-02 13:29:20 +01001152 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001153 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001154 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001155
1156 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1157 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1158 // must not manage background users' states in any functions.
1159 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1160 // by a token.
1161 if (mContext.checkCallingOrSelfPermission(
1162 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1163 == PackageManager.PERMISSION_GRANTED) {
1164 if (DEBUG) {
1165 Slog.d(TAG, "--- Access granted because the calling process has "
1166 + "the INTERACT_ACROSS_USERS_FULL permission");
1167 }
1168 return true;
1169 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001170 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001171 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1172 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001173 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001174 }
1175
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001176
1177 /**
1178 * Returns true iff the caller is identified to be the current input method with the token.
1179 * @param token The window token given to the input method when it was started.
1180 * @return true if and only if non-null valid token is specified.
1181 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001182 private boolean calledWithValidToken(@Nullable IBinder token) {
1183 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1184 if (DEBUG) {
1185 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1186 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1187 }
1188 return false;
1189 }
1190 if (token == null || token != mCurToken) {
1191 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1192 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1193 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001194 return false;
1195 }
1196 return true;
1197 }
1198
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001199 private boolean bindCurrentInputMethodService(
1200 Intent service, ServiceConnection conn, int flags) {
1201 if (service == null || conn == null) {
1202 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1203 return false;
1204 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001205 return mContext.bindServiceAsUser(service, conn, flags,
1206 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 }
1208
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001211 // TODO: Make this work even for non-current users?
1212 if (!calledFromValidUser()) {
1213 return Collections.emptyList();
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001216 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218 }
1219
satoke7c6998e2011-06-03 17:57:59 +09001220 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001222 // TODO: Make this work even for non-current users?
1223 if (!calledFromValidUser()) {
1224 return Collections.emptyList();
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001227 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
1230
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001231 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001232 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001233 * @return enabled subtypes of the specified imi
1234 */
satoke7c6998e2011-06-03 17:57:59 +09001235 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001236 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001237 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001238 // TODO: Make this work even for non-current users?
1239 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001240 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001241 }
satok67ddf9c2010-11-17 09:45:54 +09001242 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001243 final InputMethodInfo imi;
1244 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001245 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001246 } else {
1247 imi = mMethodMap.get(imiId);
1248 }
1249 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001250 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001251 }
1252 return mSettings.getEnabledInputMethodSubtypeListLocked(
1253 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001254 }
1255 }
1256
satoke7c6998e2011-06-03 17:57:59 +09001257 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 public void addClient(IInputMethodClient client,
1259 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001260 if (!calledFromValidUser()) {
1261 return;
1262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 synchronized (mMethodMap) {
1264 mClients.put(client.asBinder(), new ClientState(client,
1265 inputContext, uid, pid));
1266 }
1267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001268
satoke7c6998e2011-06-03 17:57:59 +09001269 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001271 if (!calledFromValidUser()) {
1272 return;
1273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 ClientState cs = mClients.remove(client.asBinder());
1276 if (cs != null) {
1277 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001278 if (mCurClient == cs) {
1279 mCurClient = null;
1280 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001281 if (mCurFocusedWindowClient == cs) {
1282 mCurFocusedWindowClient = null;
1283 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 void executeOrSendMessage(IInterface target, Message msg) {
1289 if (target.asBinder() instanceof Binder) {
1290 mCaller.sendMessage(msg);
1291 } else {
1292 handleMessage(msg);
1293 msg.recycle();
1294 }
1295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001296
Yohei Yukawa33e81792015-11-17 21:14:42 -08001297 void unbindCurrentClientLocked(
1298 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001300 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 + mCurClient.client.asBinder());
1302 if (mBoundToMethod) {
1303 mBoundToMethod = false;
1304 if (mCurMethod != null) {
1305 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1306 MSG_UNBIND_INPUT, mCurMethod));
1307 }
1308 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001309
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001310 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1311 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001312 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1313 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
The Android Open Source Project10592532009-03-18 17:39:46 -07001317 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 private int getImeShowFlags() {
1322 int flags = 0;
1323 if (mShowForced) {
1324 flags |= InputMethod.SHOW_FORCED
1325 | InputMethod.SHOW_EXPLICIT;
1326 } else if (mShowExplicitlyRequested) {
1327 flags |= InputMethod.SHOW_EXPLICIT;
1328 }
1329 return flags;
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 private int getAppShowFlags() {
1333 int flags = 0;
1334 if (mShowForced) {
1335 flags |= InputMethodManager.SHOW_FORCED;
1336 } else if (!mShowExplicitlyRequested) {
1337 flags |= InputMethodManager.SHOW_IMPLICIT;
1338 }
1339 return flags;
1340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001341
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001342 InputBindResult attachNewInputLocked(
1343 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 if (!mBoundToMethod) {
1345 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1346 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1347 mBoundToMethod = true;
1348 }
1349 final SessionState session = mCurClient.curSession;
Yohei Yukawa3eaf8672017-02-11 23:40:07 +00001350 if (initial) {
1351 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1352 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1353 mCurAttribute));
1354 } else {
1355 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1356 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1357 mCurAttribute));
1358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001360 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001361 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001363 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001364 (session.channel != null ? session.channel.dup() : null),
1365 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001368 InputBindResult startInputLocked(
1369 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001370 IInputMethodClient client, IInputContext inputContext,
1371 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001372 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 // If no method is currently selected, do nothing.
1374 if (mCurMethodId == null) {
1375 return mNoBinding;
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 ClientState cs = mClients.get(client.asBinder());
1379 if (cs == null) {
1380 throw new IllegalArgumentException("unknown client "
1381 + client.asBinder());
1382 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001383
Yohei Yukawa74750f22016-03-22 12:54:22 -07001384 if (attribute == null) {
1385 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1386 + " uid=" + cs.uid + " pid=" + cs.pid);
1387 return null;
1388 }
1389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 try {
1391 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1392 // Check with the window manager to make sure this client actually
1393 // has a window with focus. If not, reject. This is thread safe
1394 // because if the focus changes some time before or after, the
1395 // next client receiving focus that has any interest in input will
1396 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001397 if (DEBUG) {
1398 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1399 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 return null;
1402 }
1403 } catch (RemoteException e) {
1404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001406 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001407 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001408 }
1409
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001410 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001411 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001412 @NonNull EditorInfo attribute, int controlFlags,
1413 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001414 // If no method is currently selected, do nothing.
1415 if (mCurMethodId == null) {
1416 return mNoBinding;
1417 }
1418
Yohei Yukawad57ba672015-06-08 16:39:46 -07001419 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1420 attribute.packageName)) {
1421 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1422 + " uid=" + cs.uid + " package=" + attribute.packageName);
1423 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001424 }
1425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001427 // Was the keyguard locked when switching over to the new client?
1428 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 // If the client is changing, we need to switch over to the new
1430 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001431 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001432 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001433 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434
1435 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001436 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001437 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001438 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
1440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 // Bump up the sequence for this client and attach it.
1443 mCurSeq++;
1444 if (mCurSeq <= 0) mCurSeq = 1;
1445 mCurClient = cs;
1446 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001447 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 // Check if the input method is changing.
1451 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1452 if (cs.curSession != null) {
1453 // Fast case: if we are already connected to the input method,
1454 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001455 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001456 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 }
1458 if (mHaveConnection) {
1459 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 // Return to client, and we will get back with it when
1461 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001462 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001463 return new InputBindResult(null, null, mCurId, mCurSeq,
1464 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 } else if (SystemClock.uptimeMillis()
1466 < (mLastBindTime+TIME_TO_RECONNECT)) {
1467 // In this case we have connected to the service, but
1468 // don't yet have its interface. If it hasn't been too
1469 // long since we did the connection, we'll return to
1470 // the client and wait to get the service interface so
1471 // we can report back. If it has been too long, we want
1472 // to fall through so we can try a disconnect/reconnect
1473 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001474 return new InputBindResult(null, null, mCurId, mCurSeq,
1475 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1478 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
1480 }
1481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001482
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001483 return startInputInnerLocked();
1484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001485
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001486 InputBindResult startInputInnerLocked() {
1487 if (mCurMethodId == null) {
1488 return mNoBinding;
1489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001491 if (!mSystemReady) {
1492 // If the system is not yet ready, we shouldn't be running third
1493 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001494 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1495 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1499 if (info == null) {
1500 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001503 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1506 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001507 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1508 com.android.internal.R.string.input_method_binding_label);
1509 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1510 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001511 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001512 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1513 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 mLastBindTime = SystemClock.uptimeMillis();
1515 mHaveConnection = true;
1516 mCurId = info.getId();
1517 mCurToken = new Binder();
1518 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001519 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001520 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 } catch (RemoteException e) {
1522 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001523 return new InputBindResult(null, null, mCurId, mCurSeq,
1524 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 } else {
1526 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 + mCurIntent);
1529 }
1530 return null;
1531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001533 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001534 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001535 IInputMethodClient client, IInputContext inputContext,
1536 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001537 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001538 if (!calledFromValidUser()) {
1539 return null;
1540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001542 if (DEBUG) {
1543 Slog.v(TAG, "startInput: reason="
1544 + InputMethodClient.getStartInputReason(startInputReason)
1545 + " client = " + client.asBinder()
1546 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001547 + " missingMethods="
1548 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001549 + " attribute=" + attribute
1550 + " controlFlags=#" + Integer.toHexString(controlFlags));
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 final long ident = Binder.clearCallingIdentity();
1553 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001554 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1555 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 } finally {
1557 Binder.restoreCallingIdentity(ident);
1558 }
1559 }
1560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
satoke7c6998e2011-06-03 17:57:59 +09001562 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 public void finishInput(IInputMethodClient client) {
1564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
satoke7c6998e2011-06-03 17:57:59 +09001566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 public void onServiceConnected(ComponentName name, IBinder service) {
1568 synchronized (mMethodMap) {
1569 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1570 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001571 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001572 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001573 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001574 return;
1575 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001576 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001577 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1578 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001580 clearClientSessionLocked(mCurClient);
1581 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 }
1584 }
1585 }
1586
Jeff Brownc28867a2013-03-26 15:42:39 -07001587 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1588 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 synchronized (mMethodMap) {
1590 if (mCurMethod != null && method != null
1591 && mCurMethod.asBinder() == method.asBinder()) {
1592 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001593 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001595 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001596 InputBindResult res = attachNewInputLocked(
1597 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if (res.method != null) {
1599 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001600 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001602 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
1604 }
1605 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001606
1607 // Session abandoned. Close its associated input channel.
1608 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001611 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001612 if (mVisibleBound) {
1613 mContext.unbindService(mVisibleConnection);
1614 mVisibleBound = false;
1615 }
1616
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001617 if (mHaveConnection) {
1618 mContext.unbindService(this);
1619 mHaveConnection = false;
1620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001621
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001622 if (mCurToken != null) {
1623 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001624 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001625 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001626 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001627 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001628 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001629 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001630 } catch (RemoteException e) {
1631 }
1632 mCurToken = null;
1633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001634
The Android Open Source Project10592532009-03-18 17:39:46 -07001635 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001636 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
Yohei Yukawa33e81792015-11-17 21:14:42 -08001639 void resetCurrentMethodAndClient(
1640 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001641 mCurMethodId = null;
1642 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001643 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001644 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001645
1646 void requestClientSessionLocked(ClientState cs) {
1647 if (!cs.sessionRequested) {
1648 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1649 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1650 cs.sessionRequested = true;
1651 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1652 MSG_CREATE_SESSION, mCurMethod, channels[1],
1653 new MethodCallback(this, mCurMethod, channels[0])));
1654 }
1655 }
1656
1657 void clearClientSessionLocked(ClientState cs) {
1658 finishSessionLocked(cs.curSession);
1659 cs.curSession = null;
1660 cs.sessionRequested = false;
1661 }
1662
1663 private void finishSessionLocked(SessionState sessionState) {
1664 if (sessionState != null) {
1665 if (sessionState.session != null) {
1666 try {
1667 sessionState.session.finishSession();
1668 } catch (RemoteException e) {
1669 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001670 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001671 }
1672 sessionState.session = null;
1673 }
1674 if (sessionState.channel != null) {
1675 sessionState.channel.dispose();
1676 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001677 }
1678 }
1679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001680
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001681 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (mCurMethod != null) {
1683 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001684 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001686
Jeff Brownc28867a2013-03-26 15:42:39 -07001687 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001688 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 mCurMethod = null;
1690 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001691 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001692 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001693 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001694 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
satoke7c6998e2011-06-03 17:57:59 +09001697 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001699 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1700 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1701 // which is irregular but can eventually happen for everyone just by continuing using the
1702 // device. Thus it is important to make sure that all the internal states are properly
1703 // refreshed when this method is called back. Running
1704 // adb install -r <APK that implements the current IME>
1705 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001707 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 + " mCurIntent=" + mCurIntent);
1709 if (mCurMethod != null && mCurIntent != null
1710 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001711 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 // We consider this to be a new bind attempt, since the system
1713 // should now try to restart the service for us.
1714 mLastBindTime = SystemClock.uptimeMillis();
1715 mShowRequested = mInputShown;
1716 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001717 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719 }
1720 }
1721
satokf9f01002011-05-19 21:31:50 +09001722 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001724 synchronized (mMethodMap) {
1725 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001726 return;
1727 }
1728 final long ident = Binder.clearCallingIdentity();
1729 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001731 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001732 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001733 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001736 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001737 CharSequence contentDescription = null;
1738 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001739 // Use PackageManager to load label
1740 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001741 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001742 mIPackageManager.getApplicationInfo(packageName, 0,
1743 mSettings.getCurrentUserId()));
1744 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001745 /* ignore */
1746 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001747 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001748 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001749 contentDescription != null
1750 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001751 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001754 } finally {
1755 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
1758 }
1759
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001760 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001761 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001762 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001763 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001764 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001765 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001766 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1767 // When physical keyboard is attached, we show the ime switcher (or notification if
1768 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1769 // exists in the IME switcher dialog. Might be OK to remove this condition once
1770 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1771 return true;
1772 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001773 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1774 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001775 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001776
1777 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1778 final int N = imis.size();
1779 if (N > 2) return true;
1780 if (N < 1) return false;
1781 int nonAuxCount = 0;
1782 int auxCount = 0;
1783 InputMethodSubtype nonAuxSubtype = null;
1784 InputMethodSubtype auxSubtype = null;
1785 for(int i = 0; i < N; ++i) {
1786 final InputMethodInfo imi = imis.get(i);
1787 final List<InputMethodSubtype> subtypes =
1788 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1789 final int subtypeCount = subtypes.size();
1790 if (subtypeCount == 0) {
1791 ++nonAuxCount;
1792 } else {
1793 for (int j = 0; j < subtypeCount; ++j) {
1794 final InputMethodSubtype subtype = subtypes.get(j);
1795 if (!subtype.isAuxiliary()) {
1796 ++nonAuxCount;
1797 nonAuxSubtype = subtype;
1798 } else {
1799 ++auxCount;
1800 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001801 }
1802 }
satok7cfc0ed2011-06-20 21:29:36 +09001803 }
1804 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001805 if (nonAuxCount > 1 || auxCount > 1) {
1806 return true;
1807 } else if (nonAuxCount == 1 && auxCount == 1) {
1808 if (nonAuxSubtype != null && auxSubtype != null
1809 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1810 || auxSubtype.overridesImplicitlyEnabledSubtype()
1811 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1812 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1813 return false;
1814 }
1815 return true;
1816 }
1817 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001818 }
1819
John Spurlocke0980502013-10-25 11:59:29 -04001820 private boolean isKeyguardLocked() {
1821 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1822 }
1823
satokdbf29502011-08-25 15:28:23 +09001824 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001825 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001826 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001827 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001828 return;
1829 }
1830
1831 synchronized (mMethodMap) {
1832 mImeWindowVis = vis;
1833 mBackDisposition = backDisposition;
1834 updateSystemUiLocked(token, vis, backDisposition);
1835 }
1836 }
1837
1838 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1839 synchronized (mMethodMap) {
1840 updateSystemUiLocked(token, vis, backDisposition);
1841 }
1842 }
1843
1844 // Caution! This method is called in this class. Handle multi-user carefully
1845 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1846 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001847 return;
1848 }
1849
1850 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1851 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001852 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001853 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001854 // apply policy for binder calls
1855 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1856 vis = 0;
satok06487a52010-10-29 11:37:18 +09001857 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001858 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1859 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1860 if (mStatusBar != null) {
1861 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1862 needsToShowImeSwitcher);
1863 }
1864 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1865 if (imi != null && needsToShowImeSwitcher) {
1866 // Used to load label
1867 final CharSequence title = mRes.getText(
1868 com.android.internal.R.string.select_input_method);
1869 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1870 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001871 mImeSwitcherNotification.setContentTitle(title)
1872 .setContentText(summary)
1873 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001874 try {
1875 if ((mNotificationManager != null)
1876 && !mIWindowManager.hasNavigationBar()) {
1877 if (DEBUG) {
1878 Slog.d(TAG, "--- show notification: label = " + summary);
1879 }
1880 mNotificationManager.notifyAsUser(null,
1881 com.android.internal.R.string.select_input_method,
1882 mImeSwitcherNotification.build(), UserHandle.ALL);
1883 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001884 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001885 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001886 }
1887 } else {
1888 if (mNotificationShown && mNotificationManager != null) {
1889 if (DEBUG) {
1890 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001891 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001892 mNotificationManager.cancelAsUser(null,
1893 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1894 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001895 }
satok06487a52010-10-29 11:37:18 +09001896 }
1897 } finally {
1898 Binder.restoreCallingIdentity(ident);
1899 }
1900 }
1901
satoke7c6998e2011-06-03 17:57:59 +09001902 @Override
satokf9f01002011-05-19 21:31:50 +09001903 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001904 if (!calledFromValidUser()) {
1905 return;
1906 }
satokf9f01002011-05-19 21:31:50 +09001907 synchronized (mMethodMap) {
1908 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1909 for (int i = 0; i < spans.length; ++i) {
1910 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001911 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001912 mSecureSuggestionSpans.put(ss, currentImi);
1913 }
1914 }
1915 }
1916 }
1917
satoke7c6998e2011-06-03 17:57:59 +09001918 @Override
satokf9f01002011-05-19 21:31:50 +09001919 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001920 if (!calledFromValidUser()) {
1921 return false;
1922 }
satokf9f01002011-05-19 21:31:50 +09001923 synchronized (mMethodMap) {
1924 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1925 // TODO: Do not send the intent if the process of the targetImi is already dead.
1926 if (targetImi != null) {
1927 final String[] suggestions = span.getSuggestions();
1928 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001929 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001930 final Intent intent = new Intent();
1931 // Ensures that only a class in the original IME package will receive the
1932 // notification.
satok42c5a162011-05-26 16:46:14 +09001933 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001934 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1935 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1936 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1937 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001938 final long ident = Binder.clearCallingIdentity();
1939 try {
1940 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1941 } finally {
1942 Binder.restoreCallingIdentity(ident);
1943 }
satokf9f01002011-05-19 21:31:50 +09001944 return true;
1945 }
1946 }
1947 return false;
1948 }
1949
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001950 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001951 updateInputMethodsFromSettingsLocked(enabledMayChange);
1952 updateKeyboardFromSettingsLocked();
1953 }
1954
1955 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001956 if (enabledMayChange) {
1957 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1958 for (int i=0; i<enabled.size(); i++) {
1959 // We allow the user to select "disabled until used" apps, so if they
1960 // are enabling one of those here we now need to make it enabled.
1961 InputMethodInfo imm = enabled.get(i);
1962 try {
1963 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1964 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1965 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001966 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001967 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001968 if (DEBUG) {
1969 Slog.d(TAG, "Update state(" + imm.getId()
1970 + "): DISABLED_UNTIL_USED -> DEFAULT");
1971 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001972 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1973 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001974 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1975 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001976 }
1977 } catch (RemoteException e) {
1978 }
1979 }
1980 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001981 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1982 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1983 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1984 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001985 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001986 // There is no input method selected, try to choose new applicable input method.
1987 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001988 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001989 }
1990 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001992 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001994 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001995 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
satokf3db1af2010-11-23 13:34:33 +09001997 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001998 } else {
1999 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002000 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002002 // Here is not the perfect place to reset the switching controller. Ideally
2003 // mSwitchingController and mSettings should be able to share the same state.
2004 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2005 // the same enabled IMEs list.
2006 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002007
2008 }
2009
2010 public void updateKeyboardFromSettingsLocked() {
2011 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2012 if (mSwitchingDialog != null
2013 && mSwitchingDialogTitleView != null
2014 && mSwitchingDialog.isShowing()) {
2015 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2016 com.android.internal.R.id.hard_keyboard_switch);
2017 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002020
Yohei Yukawab097b822015-12-01 10:43:08 -08002021 private void notifyInputMethodSubtypeChanged(final int userId,
2022 @Nullable final InputMethodInfo inputMethodInfo,
2023 @Nullable final InputMethodSubtype subtype) {
2024 final InputManagerInternal inputManagerInternal =
2025 LocalServices.getService(InputManagerInternal.class);
2026 if (inputManagerInternal != null) {
2027 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2028 }
2029 }
2030
satokab751aa2010-09-14 19:17:36 +09002031 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 InputMethodInfo info = mMethodMap.get(id);
2033 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002034 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002036
satokd81e9502012-05-21 12:58:45 +09002037 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002039 final int subtypeCount = info.getSubtypeCount();
2040 if (subtypeCount <= 0) {
2041 return;
satokcd7cd292010-11-20 15:46:23 +09002042 }
satokd81e9502012-05-21 12:58:45 +09002043 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2044 final InputMethodSubtype newSubtype;
2045 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2046 newSubtype = info.getSubtypeAt(subtypeId);
2047 } else {
2048 // If subtype is null, try to find the most applicable one from
2049 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002050 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002051 }
2052 if (newSubtype == null || oldSubtype == null) {
2053 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2054 + ", new subtype = " + newSubtype);
2055 return;
2056 }
2057 if (newSubtype != oldSubtype) {
2058 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2059 if (mCurMethod != null) {
2060 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002061 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002062 mCurMethod.changeInputMethodSubtype(newSubtype);
2063 } catch (RemoteException e) {
2064 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002065 return;
satokab751aa2010-09-14 19:17:36 +09002066 }
2067 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002068 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 return;
2071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002072
satokd81e9502012-05-21 12:58:45 +09002073 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 final long ident = Binder.clearCallingIdentity();
2075 try {
satokab751aa2010-09-14 19:17:36 +09002076 // Set a subtype to this input method.
2077 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002078 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2079 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2080 // because mCurMethodId is stored as a history in
2081 // setSelectedInputMethodAndSubtypeLocked().
2082 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002084 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002086 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002088 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002090 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 } finally {
2092 Binder.restoreCallingIdentity(ident);
2093 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002094
2095 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2096 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
satok42c5a162011-05-26 16:46:14 +09002099 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002100 public boolean showSoftInput(IInputMethodClient client, int flags,
2101 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002102 if (!calledFromValidUser()) {
2103 return false;
2104 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002105 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 long ident = Binder.clearCallingIdentity();
2107 try {
2108 synchronized (mMethodMap) {
2109 if (mCurClient == null || client == null
2110 || mCurClient.client.asBinder() != client.asBinder()) {
2111 try {
2112 // We need to check if this is the current client with
2113 // focus in the window manager, to allow this call to
2114 // be made before input is started in it.
2115 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002116 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002120 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
2122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002123
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002124 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002125 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127 } finally {
2128 Binder.restoreCallingIdentity(ident);
2129 }
2130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002131
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002134 if (mAccessibilityRequestingNoSoftKeyboard) {
2135 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 }
Anna Galusza9b278112016-01-04 11:37:37 -08002137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2139 mShowExplicitlyRequested = true;
2140 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002141 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2142 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002144
Dianne Hackborncc278702009-09-02 23:07:23 -07002145 if (!mSystemReady) {
2146 return false;
2147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002148
The Android Open Source Project4df24232009-03-05 14:34:35 -08002149 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002151 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002152 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2153 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2154 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002156 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002157 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002158 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002159 | Context.BIND_TREAT_LIKE_ACTIVITY
2160 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002161 mVisibleBound = true;
2162 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002163 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002165 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 // The client has asked to have the input method shown, but
2167 // we have been sitting here too long with a connection to the
2168 // service and no interface received, so let's disconnect/connect
2169 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002170 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002172 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002174 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002175 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002176 } else {
2177 if (DEBUG) {
2178 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2179 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002182
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002185
satok42c5a162011-05-26 16:46:14 +09002186 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 public boolean hideSoftInput(IInputMethodClient client, int flags,
2188 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002189 if (!calledFromValidUser()) {
2190 return false;
2191 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002192 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 long ident = Binder.clearCallingIdentity();
2194 try {
2195 synchronized (mMethodMap) {
2196 if (mCurClient == null || client == null
2197 || mCurClient.client.asBinder() != client.asBinder()) {
2198 try {
2199 // We need to check if this is the current client with
2200 // focus in the window manager, to allow this call to
2201 // be made before input is started in it.
2202 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002203 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2204 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002205 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
2207 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002208 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
2210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002211
Joe Onorato8a9b2202010-02-26 18:56:32 -08002212 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002213 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 }
2215 } finally {
2216 Binder.restoreCallingIdentity(ident);
2217 }
2218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002219
The Android Open Source Project4df24232009-03-05 14:34:35 -08002220 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2222 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002223 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 -08002224 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 }
2226 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002227 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 -08002228 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002230
2231 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2232 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2233 // to be updated with the new value sent from IME process. Even in such a transient state
2234 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2235 // application process as a valid request, and have even promised such a behavior with CTS
2236 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2237 // IMMS#InputShown indicates that the software keyboard is shown.
2238 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2239 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2240 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002241 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002242 if (shouldHideSoftInput) {
2243 // The IME will report its visible state again after the following message finally
2244 // delivered to the IME process as an IPC. Hence the inconsistency between
2245 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2246 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002247 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2248 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2249 res = true;
2250 } else {
2251 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002253 if (mHaveConnection && mVisibleBound) {
2254 mContext.unbindService(mVisibleConnection);
2255 mVisibleBound = false;
2256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 mInputShown = false;
2258 mShowRequested = false;
2259 mShowExplicitlyRequested = false;
2260 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002261 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002263
satok42c5a162011-05-26 16:46:14 +09002264 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002265 public InputBindResult startInputOrWindowGainedFocus(
2266 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2267 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002268 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002269 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002270 if (windowToken != null) {
2271 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002272 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002273 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002274 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2275 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002276 }
2277 }
2278
2279 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002280 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002281 IInputMethodClient client, IBinder windowToken, int controlFlags,
2282 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002283 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2284 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002285 // Needs to check the validity before clearing calling identity
2286 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002287 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 long ident = Binder.clearCallingIdentity();
2289 try {
2290 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002291 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2292 + InputMethodClient.getStartInputReason(startInputReason)
2293 + " client=" + client.asBinder()
2294 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002295 + " missingMethods="
2296 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002297 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002298 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002299 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002300 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002301
Dianne Hackborn7663d802012-02-24 13:08:49 -08002302 ClientState cs = mClients.get(client.asBinder());
2303 if (cs == null) {
2304 throw new IllegalArgumentException("unknown client "
2305 + client.asBinder());
2306 }
2307
2308 try {
2309 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2310 // Check with the window manager to make sure this client actually
2311 // has a window with focus. If not, reject. This is thread safe
2312 // because if the focus changes some time before or after, the
2313 // next client receiving focus that has any interest in input will
2314 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002315 if (DEBUG) {
2316 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2317 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2318 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002319 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002321 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002323
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002324 if (!calledFromValidUser) {
2325 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2326 Slog.w(TAG, "If you want to interect with IME, you need "
2327 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2328 hideCurrentInputLocked(0, null);
2329 return null;
2330 }
2331
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002332 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002333 if (DEBUG) {
2334 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2335 + " attribute=" + attribute + ", token = " + windowToken);
2336 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002337 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002338 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002339 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002340 }
2341 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002342 }
2343 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002344 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002345 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002346
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002347 // Should we auto-show the IME even if the caller has not
2348 // specified what should be done with it?
2349 // We only do this automatically if the window can resize
2350 // to accommodate the IME (so what the user sees will give
2351 // them good context without input information being obscured
2352 // by the IME) or if running on a large screen where there
2353 // is more room for the target window + IME.
2354 final boolean doAutoShow =
2355 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2356 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2357 || mRes.getConfiguration().isLayoutSizeAtLeast(
2358 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002359 final boolean isTextEditor =
2360 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2361
2362 // We want to start input before showing the IME, but after closing
2363 // it. We want to do this after closing it to help the IME disappear
2364 // more quickly (not get stuck behind it initializing itself for the
2365 // new focused input, even if its window wants to hide the IME).
2366 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002370 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2372 // There is no focus view, and this window will
2373 // be behind any soft input window, so hide the
2374 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002375 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002376 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002378 } else if (isTextEditor && doAutoShow && (softInputMode &
2379 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 // There is a focus view, and we are navigating forward
2381 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002382 // We only do this automatically if the window can resize
2383 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002384 // them good context without input information being obscured
2385 // by the IME) or if running on a large screen where there
2386 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002387 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002388 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002389 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002390 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002391 didStart = true;
2392 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002393 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 }
2395 break;
2396 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2397 // Do nothing.
2398 break;
2399 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2400 if ((softInputMode &
2401 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002402 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002403 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405 break;
2406 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002407 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002408 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 break;
2410 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2411 if ((softInputMode &
2412 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002413 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002414 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002415 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002416 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002417 didStart = true;
2418 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002419 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
2421 break;
2422 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002423 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002424 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002425 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002426 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002427 didStart = true;
2428 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002429 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 break;
2431 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002432
2433 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002434 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002435 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438 } finally {
2439 Binder.restoreCallingIdentity(ident);
2440 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002441
2442 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002444
satok42c5a162011-05-26 16:46:14 +09002445 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002446 public void showInputMethodPickerFromClient(
2447 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002448 if (!calledFromValidUser()) {
2449 return;
2450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 synchronized (mMethodMap) {
2452 if (mCurClient == null || client == null
2453 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002454 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002455 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 }
2457
satok440aab52010-11-25 09:43:11 +09002458 // Always call subtype picker, because subtype picker is a superset of input method
2459 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002460 mHandler.sendMessage(mCaller.obtainMessageI(
2461 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002462 }
2463 }
2464
satok42c5a162011-05-26 16:46:14 +09002465 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002467 if (!calledFromValidUser()) {
2468 return;
2469 }
satok28203512010-11-24 11:06:49 +09002470 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2471 }
2472
satok42c5a162011-05-26 16:46:14 +09002473 @Override
satok28203512010-11-24 11:06:49 +09002474 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002475 if (!calledFromValidUser()) {
2476 return;
2477 }
satok28203512010-11-24 11:06:49 +09002478 synchronized (mMethodMap) {
2479 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002480 setInputMethodWithSubtypeIdLocked(token, id,
2481 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2482 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002483 } else {
2484 setInputMethod(token, id);
2485 }
2486 }
satokab751aa2010-09-14 19:17:36 +09002487 }
2488
satok42c5a162011-05-26 16:46:14 +09002489 @Override
satokb416a712010-11-25 20:42:14 +09002490 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002491 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002492 if (!calledFromValidUser()) {
2493 return;
2494 }
satokb416a712010-11-25 20:42:14 +09002495 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002496 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2497 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002498 }
2499 }
2500
satok4fc87d62011-05-20 16:13:43 +09002501 @Override
satok735cf382010-11-11 20:40:09 +09002502 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002503 if (!calledFromValidUser()) {
2504 return false;
2505 }
satok735cf382010-11-11 20:40:09 +09002506 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002507 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002508 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002509 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002510 lastImi = mMethodMap.get(lastIme.first);
2511 } else {
2512 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002513 }
satok4fc87d62011-05-20 16:13:43 +09002514 String targetLastImiId = null;
2515 int subtypeId = NOT_A_SUBTYPE_ID;
2516 if (lastIme != null && lastImi != null) {
2517 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002518 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002519 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2520 : mCurrentSubtype.hashCode();
2521 // If the last IME is the same as the current IME and the last subtype is not
2522 // defined, there is no need to switch to the last IME.
2523 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2524 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002525 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002526 }
2527 }
2528
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002529 if (TextUtils.isEmpty(targetLastImiId)
2530 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002531 // This is a safety net. If the currentSubtype can't be added to the history
2532 // and the framework couldn't find the last ime, we will make the last ime be
2533 // the most applicable enabled keyboard subtype of the system imes.
2534 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2535 if (enabled != null) {
2536 final int N = enabled.size();
2537 final String locale = mCurrentSubtype == null
2538 ? mRes.getConfiguration().locale.toString()
2539 : mCurrentSubtype.getLocale();
2540 for (int i = 0; i < N; ++i) {
2541 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002542 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002543 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002544 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2545 InputMethodUtils.getSubtypes(imi),
2546 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002547 if (keyboardSubtype != null) {
2548 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002549 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002550 imi, keyboardSubtype.hashCode());
2551 if(keyboardSubtype.getLocale().equals(locale)) {
2552 break;
2553 }
2554 }
2555 }
2556 }
2557 }
2558 }
2559
2560 if (!TextUtils.isEmpty(targetLastImiId)) {
2561 if (DEBUG) {
2562 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2563 + ", from: " + mCurMethodId + ", " + subtypeId);
2564 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002565 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002566 return true;
2567 } else {
2568 return false;
2569 }
satok735cf382010-11-11 20:40:09 +09002570 }
2571 }
2572
satoke7c6998e2011-06-03 17:57:59 +09002573 @Override
satok688bd472012-02-09 20:09:17 +09002574 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002575 if (!calledFromValidUser()) {
2576 return false;
2577 }
satok688bd472012-02-09 20:09:17 +09002578 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002579 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002580 return false;
2581 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002582 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002583 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2584 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002585 if (nextSubtype == null) {
2586 return false;
2587 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002588 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2589 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002590 return true;
2591 }
2592 }
2593
2594 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002595 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2596 if (!calledFromValidUser()) {
2597 return false;
2598 }
2599 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002600 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002601 return false;
2602 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002603 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002604 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2605 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002606 if (nextSubtype == null) {
2607 return false;
2608 }
2609 return true;
2610 }
2611 }
2612
2613 @Override
satok68f1b782011-04-11 14:26:04 +09002614 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002615 if (!calledFromValidUser()) {
2616 return null;
2617 }
satok68f1b782011-04-11 14:26:04 +09002618 synchronized (mMethodMap) {
2619 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2620 // TODO: Handle the case of the last IME with no subtypes
2621 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2622 || TextUtils.isEmpty(lastIme.second)) return null;
2623 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2624 if (lastImi == null) return null;
2625 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002626 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002627 final int lastSubtypeId =
2628 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002629 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2630 return null;
2631 }
2632 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002633 } catch (NumberFormatException e) {
2634 return null;
2635 }
2636 }
2637 }
2638
satoke7c6998e2011-06-03 17:57:59 +09002639 @Override
satokee5e77c2011-09-02 18:50:15 +09002640 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002641 if (!calledFromValidUser()) {
2642 return;
2643 }
satok91e88122011-07-18 11:11:42 +09002644 // By this IPC call, only a process which shares the same uid with the IME can add
2645 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002646 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002647 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002648 if (!mSystemReady) {
2649 return;
2650 }
satok91e88122011-07-18 11:11:42 +09002651 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002652 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002653 final String[] packageInfos;
2654 try {
2655 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2656 } catch (RemoteException e) {
2657 Slog.e(TAG, "Failed to get package infos");
2658 return;
2659 }
satok91e88122011-07-18 11:11:42 +09002660 if (packageInfos != null) {
2661 final int packageNum = packageInfos.length;
2662 for (int i = 0; i < packageNum; ++i) {
2663 if (packageInfos[i].equals(imi.getPackageName())) {
2664 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002665 final long ident = Binder.clearCallingIdentity();
2666 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002667 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002668 } finally {
2669 Binder.restoreCallingIdentity(ident);
2670 }
satokee5e77c2011-09-02 18:50:15 +09002671 return;
satok91e88122011-07-18 11:11:42 +09002672 }
2673 }
2674 }
satoke7c6998e2011-06-03 17:57:59 +09002675 }
satokee5e77c2011-09-02 18:50:15 +09002676 return;
satoke7c6998e2011-06-03 17:57:59 +09002677 }
2678
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002679 @Override
2680 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002681 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002682 }
2683
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002684 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002685 public void clearLastInputMethodWindowForTransition(IBinder token) {
2686 if (!calledFromValidUser()) {
2687 return;
2688 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002689 synchronized (mMethodMap) {
2690 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002691 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002692 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002693 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002694 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002695 }
2696
2697 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002698 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002699 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002700 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002701 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002702 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002703 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2704 if (DEBUG) {
2705 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2706 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2707 + " actual: " + sequenceNumber);
2708 }
2709 return;
2710 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002711 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2712 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002713 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002714 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002715 }
2716 }
2717
satok28203512010-11-24 11:06:49 +09002718 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002720 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2721 }
2722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002724 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2725 if (token == null) {
2726 if (mContext.checkCallingOrSelfPermission(
2727 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2728 != PackageManager.PERMISSION_GRANTED) {
2729 throw new SecurityException(
2730 "Using null token requires permission "
2731 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002733 } else if (mCurToken != token) {
2734 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2735 + " token: " + token);
2736 return;
2737 }
2738
2739 final long ident = Binder.clearCallingIdentity();
2740 try {
2741 setInputMethodLocked(id, subtypeId);
2742 } finally {
2743 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
2745 }
2746
satok42c5a162011-05-26 16:46:14 +09002747 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002749 if (!calledFromValidUser()) {
2750 return;
2751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002753 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 return;
2755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 long ident = Binder.clearCallingIdentity();
2757 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002758 hideCurrentInputLocked(flags, null);
2759 } finally {
2760 Binder.restoreCallingIdentity(ident);
2761 }
2762 }
2763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002764
satok42c5a162011-05-26 16:46:14 +09002765 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002766 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002767 if (!calledFromValidUser()) {
2768 return;
2769 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002770 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002771 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002772 return;
2773 }
2774 long ident = Binder.clearCallingIdentity();
2775 try {
2776 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 } finally {
2778 Binder.restoreCallingIdentity(ident);
2779 }
2780 }
2781 }
2782
2783 void setEnabledSessionInMainThread(SessionState session) {
2784 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002785 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002787 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002788 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 } catch (RemoteException e) {
2790 }
2791 }
2792 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002793 if (mEnabledSession != null && mEnabledSession.session != null) {
2794 try {
2795 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2796 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2797 } catch (RemoteException e) {
2798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 }
2800 }
2801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002802
satok42c5a162011-05-26 16:46:14 +09002803 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002805 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002807 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002808 final boolean showAuxSubtypes;
2809 switch (msg.arg1) {
2810 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2811 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2812 // implemented so that auxiliary subtypes will be excluded when the soft
2813 // keyboard is invisible.
2814 showAuxSubtypes = mInputShown;
2815 break;
2816 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2817 showAuxSubtypes = true;
2818 break;
2819 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2820 showAuxSubtypes = false;
2821 break;
2822 default:
2823 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2824 return false;
2825 }
2826 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002827 return true;
2828
satok47a44912010-10-06 16:03:58 +09002829 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002830 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002831 return true;
2832
2833 case MSG_SHOW_IM_CONFIG:
2834 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002835 return true;
2836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 case MSG_UNBIND_INPUT:
2840 try {
2841 ((IInputMethod)msg.obj).unbindInput();
2842 } catch (RemoteException e) {
2843 // There is nothing interesting about the method dying.
2844 }
2845 return true;
2846 case MSG_BIND_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 {
2849 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2850 } catch (RemoteException e) {
2851 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002852 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 return true;
2854 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002855 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002857 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002858 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002859 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 } catch (RemoteException e) {
2861 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002862 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 return true;
2864 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002865 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002867 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002868 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002869 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 } catch (RemoteException e) {
2871 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002872 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002874 case MSG_HIDE_CURRENT_INPUT_METHOD:
2875 synchronized (mMethodMap) {
2876 hideCurrentInputLocked(0, null);
2877 }
2878 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002880 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002882 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2884 } catch (RemoteException e) {
2885 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002886 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002888 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002889 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002890 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002891 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002893 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002895 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002896 // Dispose the channel if the input method is not local to this process
2897 // because the remote proxy will get its own copy when unparceled.
2898 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002899 channel.dispose();
2900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002902 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002906
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002907 case MSG_START_INPUT: {
2908 int missingMethods = msg.arg1;
2909 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002911 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002913 session.method.startInput((IInputContext) args.arg2, missingMethods,
Yohei Yukawa3eaf8672017-02-11 23:40:07 +00002914 (EditorInfo) args.arg3);
2915 } catch (RemoteException e) {
2916 }
2917 args.recycle();
2918 return true;
2919 }
2920 case MSG_RESTART_INPUT: {
2921 int missingMethods = msg.arg1;
2922 args = (SomeArgs) msg.obj;
2923 try {
2924 SessionState session = (SessionState) args.arg1;
2925 setEnabledSessionInMainThread(session);
2926 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2927 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 } catch (RemoteException e) {
2929 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002930 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002935
Yohei Yukawa33e81792015-11-17 21:14:42 -08002936 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002938 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 } catch (RemoteException e) {
2940 // There is nothing interesting about the last client dying.
2941 }
2942 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002943 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002944 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002945 IInputMethodClient client = (IInputMethodClient)args.arg1;
2946 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002948 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002950 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002951 } finally {
2952 // Dispose the channel if the input method is not local to this process
2953 // because the remote proxy will get its own copy when unparceled.
2954 if (res.channel != null && Binder.isProxy(client)) {
2955 res.channel.dispose();
2956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002958 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002960 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002961 case MSG_SET_ACTIVE:
2962 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002963 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002964 } catch (RemoteException e) {
2965 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2966 + ((ClientState)msg.obj).pid + " uid "
2967 + ((ClientState)msg.obj).uid);
2968 }
2969 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002970 case MSG_SET_INTERACTIVE:
2971 handleSetInteractive(msg.arg1 != 0);
2972 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002973 case MSG_SWITCH_IME:
2974 handleSwitchInputMethod(msg.arg1 != 0);
2975 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002976 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2977 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002978 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002979 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002980 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002981 } catch (RemoteException e) {
2982 Slog.w(TAG, "Got RemoteException sending "
2983 + "setUserActionNotificationSequenceNumber("
2984 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002985 + clientState.pid + " uid "
2986 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002987 }
2988 return true;
2989 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002990 case MSG_REPORT_FULLSCREEN_MODE: {
2991 final boolean fullscreen = msg.arg1 != 0;
2992 final ClientState clientState = (ClientState)msg.obj;
2993 try {
2994 clientState.client.reportFullscreenMode(fullscreen);
2995 } catch (RemoteException e) {
2996 Slog.w(TAG, "Got RemoteException sending "
2997 + "reportFullscreen(" + fullscreen + ") notification to pid="
2998 + clientState.pid + " uid=" + clientState.uid);
2999 }
3000 return true;
3001 }
satok01038492012-04-09 21:08:27 +09003002
3003 // --------------------------------------------------------------
3004 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003005 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003006 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003007 case MSG_SYSTEM_UNLOCK_USER:
3008 final int userId = msg.arg1;
3009 onUnlockUser(userId);
3010 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 }
3012 return false;
3013 }
3014
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003015 private void handleSetInteractive(final boolean interactive) {
3016 synchronized (mMethodMap) {
3017 mIsInteractive = interactive;
3018 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3019
3020 // Inform the current client of the change in active status
3021 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003022 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3023 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3024 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003025 }
3026 }
3027 }
3028
Yohei Yukawaae61f712015-12-09 13:00:10 -08003029 private void handleSwitchInputMethod(final boolean forwardDirection) {
3030 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003031 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003032 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003033 if (nextSubtype == null) {
3034 return;
3035 }
3036 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003037 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3038 if (newInputMethodInfo == null) {
3039 return;
3040 }
3041 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3042 newInputMethodInfo, mCurrentSubtype);
3043 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003044 if (mSubtypeSwitchedByShortCutToast == null) {
3045 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3046 Toast.LENGTH_SHORT);
3047 } else {
3048 mSubtypeSwitchedByShortCutToast.setText(toastText);
3049 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003050 mSubtypeSwitchedByShortCutToast.show();
3051 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003052 }
3053 }
3054
satokdc9ddae2011-10-06 12:22:36 +09003055 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003056 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3057 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003058 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003059 if (DEBUG) {
3060 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3061 }
satok723a27e2010-11-11 14:58:11 +09003062 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003063 return true;
3064 }
3065
3066 return false;
3067 }
3068
Yohei Yukawa94e33302016-02-12 19:37:03 -08003069 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003070 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003071 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003072 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003073 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003074 if (!mSystemReady) {
3075 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3076 return;
3077 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003078 mMethodList.clear();
3079 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003080 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003081 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003082
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003083 // Use for queryIntentServicesAsUser
3084 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003086 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3087 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3088 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003089 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003091 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3092 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003093
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003094 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003095 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 for (int i = 0; i < services.size(); ++i) {
3097 ResolveInfo ri = services.get(i);
3098 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003099 final String imeId = InputMethodInfo.computeId(ri);
3100 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3101 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 + ": it does not require the permission "
3103 + android.Manifest.permission.BIND_INPUT_METHOD);
3104 continue;
3105 }
3106
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003107 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003109 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 try {
satoke7c6998e2011-06-03 17:57:59 +09003111 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003112 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003113 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003114 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115
3116 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003117 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003119 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003120 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 }
3122 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003123
Yohei Yukawac4e44912017-02-09 19:30:22 -08003124 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3125 // negatives when the package state remains to be the same but only the component state is
3126 // changed.
3127 {
3128 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3129 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3130 // conservative, but it seems we cannot use it for now (Issue 35176630).
3131 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3132 new Intent(InputMethod.SERVICE_INTERFACE),
3133 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3134 final int N = allInputMethodServices.size();
3135 for (int i = 0; i < N; ++i) {
3136 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3137 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3138 continue;
3139 }
3140 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3141 }
3142 }
3143
Yohei Yukawa859df052016-02-17 07:56:46 -08003144 // TODO: The following code should find better place to live.
3145 if (!resetDefaultEnabledIme) {
3146 boolean enabledImeFound = false;
3147 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3148 final int N = enabledImes.size();
3149 for (int i = 0; i < N; ++i) {
3150 final InputMethodInfo imi = enabledImes.get(i);
3151 if (mMethodList.contains(imi)) {
3152 enabledImeFound = true;
3153 break;
3154 }
3155 }
3156 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003157 if (DEBUG) {
3158 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3159 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003160 resetDefaultEnabledIme = true;
3161 resetSelectedInputMethodAndSubtypeLocked("");
3162 }
3163 }
3164
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003165 if (resetDefaultEnabledIme) {
3166 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003167 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003168 final int N = defaultEnabledIme.size();
3169 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003170 final InputMethodInfo imi = defaultEnabledIme.get(i);
3171 if (DEBUG) {
3172 Slog.d(TAG, "--- enable ime = " + imi);
3173 }
3174 setInputMethodEnabledLocked(imi.getId(), true);
3175 }
3176 }
3177
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003178 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003179 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003180 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003181 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3182 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003183 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003184 }
3185 } else {
3186 // Double check that the default IME is certainly enabled.
3187 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003188 }
3189 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003190 // Here is not the perfect place to reset the switching controller. Ideally
3191 // mSwitchingController and mSettings should be able to share the same state.
3192 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3193 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003194 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003198
satok217f5482010-12-15 05:19:19 +09003199 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003200 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003201 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003202 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3203 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003204 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003205 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003206 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003207 final int userId;
3208 synchronized (mMethodMap) {
3209 userId = mSettings.getCurrentUserId();
3210 }
3211 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003212 }
3213
3214 private void showConfigureInputMethods() {
3215 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3216 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3217 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3218 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003219 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003220 }
3221
satok2c93efc2012-04-02 19:33:47 +09003222 private boolean isScreenLocked() {
3223 return mKeyguardManager != null
3224 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3225 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003226
Seigo Nonaka14e13912015-05-06 21:04:13 -07003227 private void showInputMethodMenu(boolean showAuxSubtypes) {
3228 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003231 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003232
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003233 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003234 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003235 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003236
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003237 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003238 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003239 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3240 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003241 if (immis == null || immis.size() == 0) {
3242 return;
3243 }
3244
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003245 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246
satok688bd472012-02-09 20:09:17 +09003247 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003248 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003249 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003250
satokc3690562012-01-10 20:14:43 +09003251 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003252 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003253 if (currentSubtype != null) {
3254 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003255 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3256 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003257 }
3258 }
3259
Ken Wakasa761eb372011-03-04 19:06:18 +09003260 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003261 mIms = new InputMethodInfo[N];
3262 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003263 int checkedItem = 0;
3264 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003265 final ImeSubtypeListItem item = imList.get(i);
3266 mIms[i] = item.mImi;
3267 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003268 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003269 int subtypeId = mSubtypeIds[i];
3270 if ((subtypeId == NOT_A_SUBTYPE_ID)
3271 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3272 || (subtypeId == lastInputMethodSubtypeId)) {
3273 checkedItem = i;
3274 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003277
3278 final Context settingsContext = new ContextThemeWrapper(context,
3279 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3280
3281 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003282 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3283 @Override
3284 public void onCancel(DialogInterface dialog) {
3285 hideInputMethodMenu();
3286 }
3287 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003288
3289 final Context dialogContext = mDialogBuilder.getContext();
3290 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3291 com.android.internal.R.styleable.DialogPreference,
3292 com.android.internal.R.attr.alertDialogStyle, 0);
3293 final Drawable dialogIcon = a.getDrawable(
3294 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3295 a.recycle();
3296
3297 mDialogBuilder.setIcon(dialogIcon);
3298
Yohei Yukawad34e1482016-02-11 08:03:52 -08003299 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003300 final View tv = inflater.inflate(
3301 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3302 mDialogBuilder.setCustomTitle(tv);
3303
3304 // Setup layout for a toggle switch of the hardware keyboard
3305 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003306 mSwitchingDialogTitleView
3307 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003308 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003309 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003310 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003311 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003312 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003313 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3314 @Override
3315 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003316 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003317 // Ensure that the input method dialog is dismissed when changing
3318 // the hardware keyboard state.
3319 hideInputMethodMenu();
3320 }
3321 });
3322
Alan Viverette505e3ab2014-11-24 15:22:11 -08003323 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003324 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3325 final OnClickListener choiceListener = new OnClickListener() {
3326 @Override
3327 public void onClick(final DialogInterface dialog, final int which) {
3328 synchronized (mMethodMap) {
3329 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3330 || mSubtypeIds.length <= which) {
3331 return;
satok01038492012-04-09 21:08:27 +09003332 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003333 final InputMethodInfo im = mIms[which];
3334 int subtypeId = mSubtypeIds[which];
3335 adapter.mCheckedItem = which;
3336 adapter.notifyDataSetChanged();
3337 hideInputMethodMenu();
3338 if (im != null) {
3339 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3340 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003341 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003342 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003344 }
3345 }
3346 };
3347 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003350 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003351 final Window w = mSwitchingDialog.getWindow();
3352 final WindowManager.LayoutParams attrs = w.getAttributes();
3353 w.setType(TYPE_INPUT_METHOD_DIALOG);
3354 // Use an alternate token for the dialog for that window manager can group the token
3355 // with other IME windows based on type vs. grouping based on whichever token happens
3356 // to get selected by the system later on.
3357 attrs.token = mSwitchingDialogToken;
3358 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3359 attrs.setTitle("Select input method");
3360 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003361 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 mSwitchingDialog.show();
3363 }
3364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003365
Ken Wakasa05dbb652011-08-22 15:22:43 +09003366 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3367 private final LayoutInflater mInflater;
3368 private final int mTextViewResourceId;
3369 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003370 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003371 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3372 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3373 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003374
Ken Wakasa05dbb652011-08-22 15:22:43 +09003375 mTextViewResourceId = textViewResourceId;
3376 mItemsList = itemsList;
3377 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003378 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003379 }
3380
3381 @Override
3382 public View getView(int position, View convertView, ViewGroup parent) {
3383 final View view = convertView != null ? convertView
3384 : mInflater.inflate(mTextViewResourceId, null);
3385 if (position < 0 || position >= mItemsList.size()) return view;
3386 final ImeSubtypeListItem item = mItemsList.get(position);
3387 final CharSequence imeName = item.mImeName;
3388 final CharSequence subtypeName = item.mSubtypeName;
3389 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3390 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3391 if (TextUtils.isEmpty(subtypeName)) {
3392 firstTextView.setText(imeName);
3393 secondTextView.setVisibility(View.GONE);
3394 } else {
3395 firstTextView.setText(subtypeName);
3396 secondTextView.setText(imeName);
3397 secondTextView.setVisibility(View.VISIBLE);
3398 }
3399 final RadioButton radioButton =
3400 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3401 radioButton.setChecked(position == mCheckedItem);
3402 return view;
3403 }
3404 }
3405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003407 synchronized (mMethodMap) {
3408 hideInputMethodMenuLocked();
3409 }
3410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003411
The Android Open Source Project10592532009-03-18 17:39:46 -07003412 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003413 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414
The Android Open Source Project10592532009-03-18 17:39:46 -07003415 if (mSwitchingDialog != null) {
3416 mSwitchingDialog.dismiss();
3417 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003419
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003420 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003421 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003422 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003426
satok42c5a162011-05-26 16:46:14 +09003427 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003429 // TODO: Make this work even for non-current users?
3430 if (!calledFromValidUser()) {
3431 return false;
3432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 synchronized (mMethodMap) {
3434 if (mContext.checkCallingOrSelfPermission(
3435 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3436 != PackageManager.PERMISSION_GRANTED) {
3437 throw new SecurityException(
3438 "Requires permission "
3439 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3440 }
Anna Galusza9b278112016-01-04 11:37:37 -08003441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 long ident = Binder.clearCallingIdentity();
3443 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003444 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 } finally {
3446 Binder.restoreCallingIdentity(ident);
3447 }
3448 }
3449 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003450
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003451 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3452 // Make sure this is a valid input method.
3453 InputMethodInfo imm = mMethodMap.get(id);
3454 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003455 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003456 }
3457
satokd87c2592010-09-29 11:52:06 +09003458 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3459 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003460
satokd87c2592010-09-29 11:52:06 +09003461 if (enabled) {
3462 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3463 if (pair.first.equals(id)) {
3464 // We are enabling this input method, but it is already enabled.
3465 // Nothing to do. The previous state was enabled.
3466 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003467 }
3468 }
satokd87c2592010-09-29 11:52:06 +09003469 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3470 // Previous state was disabled.
3471 return false;
3472 } else {
3473 StringBuilder builder = new StringBuilder();
3474 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3475 builder, enabledInputMethodsList, id)) {
3476 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003477 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003478 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3479 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3480 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003481 }
3482 // Previous state was enabled.
3483 return true;
3484 } else {
3485 // We are disabling the input method but it is already disabled.
3486 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003487 return false;
3488 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003489 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003490 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003491
satok723a27e2010-11-11 14:58:11 +09003492 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3493 boolean setSubtypeOnly) {
3494 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003495 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003496
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003497 mCurUserActionNotificationSequenceNumber =
3498 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3499 if (DEBUG) {
3500 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3501 + mCurUserActionNotificationSequenceNumber);
3502 }
3503
3504 if (mCurClient != null && mCurClient.client != null) {
3505 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3506 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003507 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003508 }
3509
satok723a27e2010-11-11 14:58:11 +09003510 // Set Subtype here
3511 if (imi == null || subtypeId < 0) {
3512 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003513 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003514 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003515 if (subtypeId < imi.getSubtypeCount()) {
3516 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3517 mSettings.putSelectedSubtype(subtype.hashCode());
3518 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003519 } else {
3520 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003521 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003522 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003523 }
satokab751aa2010-09-14 19:17:36 +09003524 }
satok723a27e2010-11-11 14:58:11 +09003525
Yohei Yukawa68645a62016-02-17 07:54:20 -08003526 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003527 // Set InputMethod here
3528 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3529 }
3530 }
3531
3532 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3533 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3534 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3535 // newDefaultIme is empty when there is no candidate for the selected IME.
3536 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3537 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3538 if (subtypeHashCode != null) {
3539 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003540 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003541 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003542 } catch (NumberFormatException e) {
3543 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3544 }
3545 }
3546 }
3547 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003548 }
3549
satok4e4569d2010-11-19 18:45:53 +09003550 // If there are no selected shortcuts, tries finding the most applicable ones.
3551 private Pair<InputMethodInfo, InputMethodSubtype>
3552 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3553 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3554 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003555 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003556 boolean foundInSystemIME = false;
3557
3558 // Search applicable subtype for each InputMethodInfo
3559 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003560 final String imiId = imi.getId();
3561 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3562 continue;
3563 }
satokcd7cd292010-11-20 15:46:23 +09003564 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003565 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003566 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003567 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003568 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003569 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003570 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003571 }
satokdf31ae62011-01-15 06:19:44 +09003572 // 2. Search by the system locale from enabledSubtypes.
3573 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003574 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003575 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003576 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003577 }
satoka86f5e42011-09-02 17:12:42 +09003578 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003579 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003580 final ArrayList<InputMethodSubtype> subtypesForSearch =
3581 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003582 ? InputMethodUtils.getSubtypes(imi)
3583 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003584 // 4. Search by the current subtype's locale from all subtypes.
3585 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003586 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003587 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003588 }
3589 // 5. Search by the system locale from all subtypes.
3590 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003591 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003592 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003593 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003594 }
satokcd7cd292010-11-20 15:46:23 +09003595 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003596 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003597 // The current input method is the most applicable IME.
3598 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003599 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003600 break;
satok7599a7f2010-12-22 13:45:23 +09003601 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003602 // The system input method is 2nd applicable IME.
3603 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003604 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003605 if ((imi.getServiceInfo().applicationInfo.flags
3606 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3607 foundInSystemIME = true;
3608 }
satok4e4569d2010-11-19 18:45:53 +09003609 }
3610 }
3611 }
3612 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003613 if (mostApplicableIMI != null) {
3614 Slog.w(TAG, "Most applicable shortcut input method was:"
3615 + mostApplicableIMI.getId());
3616 if (mostApplicableSubtype != null) {
3617 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3618 + "," + mostApplicableSubtype.getMode() + ","
3619 + mostApplicableSubtype.getLocale());
3620 }
3621 }
satok4e4569d2010-11-19 18:45:53 +09003622 }
satokcd7cd292010-11-20 15:46:23 +09003623 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003624 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003625 } else {
3626 return null;
3627 }
3628 }
3629
satokab751aa2010-09-14 19:17:36 +09003630 /**
3631 * @return Return the current subtype of this input method.
3632 */
satok42c5a162011-05-26 16:46:14 +09003633 @Override
satokab751aa2010-09-14 19:17:36 +09003634 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003635 // TODO: Make this work even for non-current users?
3636 if (!calledFromValidUser()) {
3637 return null;
3638 }
3639 synchronized (mMethodMap) {
3640 return getCurrentInputMethodSubtypeLocked();
3641 }
3642 }
3643
3644 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003645 if (mCurMethodId == null) {
3646 return null;
3647 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003648 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003649 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3650 if (imi == null || imi.getSubtypeCount() == 0) {
3651 return null;
satok4e4569d2010-11-19 18:45:53 +09003652 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003653 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003654 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3655 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003656 if (subtypeId == NOT_A_SUBTYPE_ID) {
3657 // If there are no selected subtypes, the framework will try to find
3658 // the most applicable subtype from explicitly or implicitly enabled
3659 // subtypes.
3660 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003661 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003662 // If there is only one explicitly or implicitly enabled subtype,
3663 // just returns it.
3664 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3665 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3666 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003667 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003668 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003669 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003670 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003671 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003672 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3673 true);
satok4e4569d2010-11-19 18:45:53 +09003674 }
satok3ef8b292010-11-23 06:06:29 +09003675 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003676 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003677 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003678 }
3679 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003680 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003681 }
3682
satok4e4569d2010-11-19 18:45:53 +09003683 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003684 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003685 @Override
satok4e4569d2010-11-19 18:45:53 +09003686 public List getShortcutInputMethodsAndSubtypes() {
3687 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003688 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003689 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003690 // If there are no selected shortcut subtypes, the framework will try to find
3691 // the most applicable subtype from all subtypes whose mode is
3692 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003693 Pair<InputMethodInfo, InputMethodSubtype> info =
3694 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003695 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003696 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003697 ret.add(info.first);
3698 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003699 }
satok3da92232011-01-11 22:46:30 +09003700 return ret;
satokf3db1af2010-11-23 13:34:33 +09003701 }
satokf3db1af2010-11-23 13:34:33 +09003702 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3703 ret.add(imi);
3704 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3705 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003706 }
3707 }
satokf3db1af2010-11-23 13:34:33 +09003708 return ret;
satok4e4569d2010-11-19 18:45:53 +09003709 }
3710 }
3711
satok42c5a162011-05-26 16:46:14 +09003712 @Override
satokb66d2872010-11-10 01:04:04 +09003713 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003714 // TODO: Make this work even for non-current users?
3715 if (!calledFromValidUser()) {
3716 return false;
3717 }
satokb66d2872010-11-10 01:04:04 +09003718 synchronized (mMethodMap) {
3719 if (subtype != null && mCurMethodId != null) {
3720 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003721 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003722 if (subtypeId != NOT_A_SUBTYPE_ID) {
3723 setInputMethodLocked(mCurMethodId, subtypeId);
3724 return true;
3725 }
3726 }
3727 return false;
3728 }
3729 }
3730
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003731 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003732 private static class InputMethodFileManager {
3733 private static final String SYSTEM_PATH = "system";
3734 private static final String INPUT_METHOD_PATH = "inputmethod";
3735 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3736 private static final String NODE_SUBTYPES = "subtypes";
3737 private static final String NODE_SUBTYPE = "subtype";
3738 private static final String NODE_IMI = "imi";
3739 private static final String ATTR_ID = "id";
3740 private static final String ATTR_LABEL = "label";
3741 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003742 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003743 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003744 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003745 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3746 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3747 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003748 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003749 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3750 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003751 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003752 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003753 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003754 if (methodMap == null) {
3755 throw new NullPointerException("methodMap is null");
3756 }
3757 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003758 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003759 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3760 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003761 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003762 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003763 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3764 }
3765 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3766 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3767 if (!subtypeFile.exists()) {
3768 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003769 writeAdditionalInputMethodSubtypes(
3770 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003771 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003772 readAdditionalInputMethodSubtypes(
3773 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003774 }
3775 }
3776
3777 private void deleteAllInputMethodSubtypes(String imiId) {
3778 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003779 mAdditionalSubtypesMap.remove(imiId);
3780 writeAdditionalInputMethodSubtypes(
3781 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003782 }
3783 }
3784
3785 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003786 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003787 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003788 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003789 final int N = additionalSubtypes.length;
3790 for (int i = 0; i < N; ++i) {
3791 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003792 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003793 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003794 } else {
3795 Slog.w(TAG, "Duplicated subtype definition found: "
3796 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003797 }
3798 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003799 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3800 writeAdditionalInputMethodSubtypes(
3801 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003802 }
3803 }
3804
3805 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3806 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003807 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003808 }
3809 }
3810
3811 private static void writeAdditionalInputMethodSubtypes(
3812 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3813 HashMap<String, InputMethodInfo> methodMap) {
3814 // Safety net for the case that this function is called before methodMap is set.
3815 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3816 FileOutputStream fos = null;
3817 try {
3818 fos = subtypesFile.startWrite();
3819 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003820 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003821 out.startDocument(null, true);
3822 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3823 out.startTag(null, NODE_SUBTYPES);
3824 for (String imiId : allSubtypes.keySet()) {
3825 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3826 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3827 continue;
3828 }
3829 out.startTag(null, NODE_IMI);
3830 out.attribute(null, ATTR_ID, imiId);
3831 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3832 final int N = subtypesList.size();
3833 for (int i = 0; i < N; ++i) {
3834 final InputMethodSubtype subtype = subtypesList.get(i);
3835 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003836 if (subtype.hasSubtypeId()) {
3837 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3838 String.valueOf(subtype.getSubtypeId()));
3839 }
satoke7c6998e2011-06-03 17:57:59 +09003840 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3841 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3842 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003843 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3844 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003845 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3846 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3847 out.attribute(null, ATTR_IS_AUXILIARY,
3848 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003849 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3850 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003851 out.endTag(null, NODE_SUBTYPE);
3852 }
3853 out.endTag(null, NODE_IMI);
3854 }
3855 out.endTag(null, NODE_SUBTYPES);
3856 out.endDocument();
3857 subtypesFile.finishWrite(fos);
3858 } catch (java.io.IOException e) {
3859 Slog.w(TAG, "Error writing subtypes", e);
3860 if (fos != null) {
3861 subtypesFile.failWrite(fos);
3862 }
3863 }
3864 }
3865
3866 private static void readAdditionalInputMethodSubtypes(
3867 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3868 if (allSubtypes == null || subtypesFile == null) return;
3869 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003870 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003871 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003872 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003873 int type = parser.getEventType();
3874 // Skip parsing until START_TAG
3875 while ((type = parser.next()) != XmlPullParser.START_TAG
3876 && type != XmlPullParser.END_DOCUMENT) {}
3877 String firstNodeName = parser.getName();
3878 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3879 throw new XmlPullParserException("Xml doesn't start with subtypes");
3880 }
3881 final int depth =parser.getDepth();
3882 String currentImiId = null;
3883 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3884 while (((type = parser.next()) != XmlPullParser.END_TAG
3885 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3886 if (type != XmlPullParser.START_TAG)
3887 continue;
3888 final String nodeName = parser.getName();
3889 if (NODE_IMI.equals(nodeName)) {
3890 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3891 if (TextUtils.isEmpty(currentImiId)) {
3892 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3893 continue;
3894 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003895 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003896 allSubtypes.put(currentImiId, tempSubtypesArray);
3897 } else if (NODE_SUBTYPE.equals(nodeName)) {
3898 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3899 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3900 continue;
3901 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003902 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003903 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003904 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003905 parser.getAttributeValue(null, ATTR_LABEL));
3906 final String imeSubtypeLocale =
3907 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003908 final String languageTag =
3909 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003910 final String imeSubtypeMode =
3911 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3912 final String imeSubtypeExtraValue =
3913 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003914 final boolean isAuxiliary = "1".equals(String.valueOf(
3915 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003916 final boolean isAsciiCapable = "1".equals(String.valueOf(
3917 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003918 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003919 .setSubtypeNameResId(label)
3920 .setSubtypeIconResId(icon)
3921 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003922 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003923 .setSubtypeMode(imeSubtypeMode)
3924 .setSubtypeExtraValue(imeSubtypeExtraValue)
3925 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003926 .setIsAsciiCapable(isAsciiCapable);
3927 final String subtypeIdString =
3928 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3929 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003930 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003931 }
3932 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003933 }
3934 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003935 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3936 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003937 return;
satoke7c6998e2011-06-03 17:57:59 +09003938 }
3939 }
3940 }
3941
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003942 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3943 @NonNull
3944 private final Handler mHandler;
3945
3946 LocalServiceImpl(@NonNull final Handler handler) {
3947 mHandler = handler;
3948 }
3949
3950 @Override
3951 public void setInteractive(boolean interactive) {
3952 // Do everything in handler so as not to block the caller.
3953 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3954 interactive ? 1 : 0, 0));
3955 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003956
3957 @Override
3958 public void switchInputMethod(boolean forwardDirection) {
3959 // Do everything in handler so as not to block the caller.
3960 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3961 forwardDirection ? 1 : 0, 0));
3962 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003963
3964 @Override
3965 public void hideCurrentInputMethod() {
3966 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3967 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3968 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003969 }
3970
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003971 private static String imeWindowStatusToString(final int imeWindowVis) {
3972 final StringBuilder sb = new StringBuilder();
3973 boolean first = true;
3974 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3975 sb.append("Active");
3976 first = false;
3977 }
3978 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3979 if (!first) {
3980 sb.append("|");
3981 }
3982 sb.append("Visible");
3983 }
3984 return sb.toString();
3985 }
3986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003988 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3989 @Nullable Uri contentUri, @Nullable String packageName) {
3990 if (!calledFromValidUser()) {
3991 return null;
3992 }
3993
3994 if (token == null) {
3995 throw new NullPointerException("token");
3996 }
3997 if (packageName == null) {
3998 throw new NullPointerException("packageName");
3999 }
4000 if (contentUri == null) {
4001 throw new NullPointerException("contentUri");
4002 }
4003 final String contentUriScheme = contentUri.getScheme();
4004 if (!"content".equals(contentUriScheme)) {
4005 throw new InvalidParameterException("contentUri must have content scheme");
4006 }
4007
4008 synchronized (mMethodMap) {
4009 final int uid = Binder.getCallingUid();
4010 if (mCurMethodId == null) {
4011 return null;
4012 }
4013 if (mCurToken != token) {
4014 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4015 + " token=" + token);
4016 return null;
4017 }
4018 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4019 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4020 // nature of our system. Let's compare it with our internal record.
4021 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4022 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4023 + mCurAttribute.packageName + " packageName=" + packageName);
4024 return null;
4025 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004026 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004027 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004028 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004029 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004030 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4031 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4032 imeUserId);
4033 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4034 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4035 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4036 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4037 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4038 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4039 // actually allowed to "uid", which is guaranteed to be the IME's one.
4040 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4041 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004042 }
4043 }
4044
4045 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004046 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4047 if (!calledFromValidUser()) {
4048 return;
4049 }
4050 synchronized (mMethodMap) {
4051 if (!calledWithValidToken(token)) {
4052 return;
4053 }
4054 if (mCurClient != null && mCurClient.client != null) {
4055 mInFullscreenMode = fullscreen;
4056 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4057 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4058 }
4059 }
4060 }
4061
4062 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4064 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4065 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4068 + Binder.getCallingPid()
4069 + ", uid=" + Binder.getCallingUid());
4070 return;
4071 }
4072
4073 IInputMethod method;
4074 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004075 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 synchronized (mMethodMap) {
4080 p.println("Current Input Method Manager state:");
4081 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004082 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 for (int i=0; i<N; i++) {
4084 InputMethodInfo info = mMethodList.get(i);
4085 p.println(" InputMethod #" + i + ":");
4086 info.dump(p, " ");
4087 }
4088 p.println(" Clients:");
4089 for (ClientState ci : mClients.values()) {
4090 p.println(" Client " + ci + ":");
4091 p.println(" client=" + ci.client);
4092 p.println(" inputContext=" + ci.inputContext);
4093 p.println(" sessionRequested=" + ci.sessionRequested);
4094 p.println(" curSession=" + ci.curSession);
4095 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004096 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004098 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004099 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4100 + " softInputMode=" +
4101 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4102 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004103 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4105 + " mBoundToMethod=" + mBoundToMethod);
4106 p.println(" mCurToken=" + mCurToken);
4107 p.println(" mCurIntent=" + mCurIntent);
4108 method = mCurMethod;
4109 p.println(" mCurMethod=" + mCurMethod);
4110 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004111 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 p.println(" mShowRequested=" + mShowRequested
4113 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4114 + " mShowForced=" + mShowForced
4115 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004116 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004117 p.println(" mCurUserActionNotificationSequenceNumber="
4118 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004119 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004120 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004121 p.println(" mSwitchingController:");
4122 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004123 p.println(" mSettings:");
4124 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004126
Jeff Brownb88102f2010-09-08 11:49:43 -07004127 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 pw.flush();
4130 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004131 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4132 } catch (IOException | RemoteException e) {
4133 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004135 } else {
4136 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004138
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004139 if (focusedWindowClient != null && client != focusedWindowClient) {
4140 p.println(" ");
4141 p.println("Warning: Current input method client doesn't match the last focused. "
4142 + "window.");
4143 p.println("Dumping input method client in the last focused window just in case.");
4144 p.println(" ");
4145 pw.flush();
4146 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004147 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4148 } catch (IOException | RemoteException e) {
4149 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004150 }
4151 }
4152
Jeff Brownb88102f2010-09-08 11:49:43 -07004153 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 pw.flush();
4156 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004157 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4158 } catch (IOException | RemoteException e) {
4159 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004161 } else {
4162 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 }
4164 }
4165}