blob: 2b3d8d4e1fb14d68980368d6276d4b5bd7fc738f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070032import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070033import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090034import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import com.android.internal.view.IInputContext;
36import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import com.android.internal.view.IInputMethodClient;
38import com.android.internal.view.IInputMethodManager;
39import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070040import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080042import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080043import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
satoke7c6998e2011-06-03 17:57:59 +090045import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090047import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070049import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070050import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070051import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080052import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080053import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070054import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070056import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090057import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090058import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090059import android.app.Notification;
60import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070061import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090062import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080064import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.ContentResolver;
66import android.content.Context;
67import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090069import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090071import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070073import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090074import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.pm.PackageManager;
76import android.content.pm.ResolveInfo;
77import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070078import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.content.res.Resources;
80import android.content.res.TypedArray;
81import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080082import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080083import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080084import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070085import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040087import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070088import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090089import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.Handler;
91import android.os.IBinder;
92import android.os.IInterface;
93import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070094import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070096import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080098import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.os.ServiceManager;
100import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900101import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800102import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.provider.Settings;
104import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900105import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700106import android.util.ArrayMap;
107import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700108import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900110import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900111import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.util.PrintWriterPrinter;
113import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900114import android.util.Slog;
115import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900116import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700118import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900119import android.view.LayoutInflater;
120import android.view.View;
121import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700122import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700124import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900125import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700127import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import android.view.inputmethod.InputMethod;
129import android.view.inputmethod.InputMethodInfo;
130import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700131import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900132import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900133import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900134import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900135import android.widget.CompoundButton;
136import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900137import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900138import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900139import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700140import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
satoke7c6998e2011-06-03 17:57:59 +0900142import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900144import java.io.FileInputStream;
145import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146import java.io.IOException;
147import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700148import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100149import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700150import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900152import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153import java.util.HashMap;
154import java.util.List;
155
156/**
157 * This class provides a system service that manages input methods.
158 */
159public class InputMethodManagerService extends IInputMethodManager.Stub
160 implements ServiceConnection, Handler.Callback {
161 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700162 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700163 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700165 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
166 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
167 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 static final int MSG_UNBIND_INPUT = 1000;
170 static final int MSG_BIND_INPUT = 1010;
171 static final int MSG_SHOW_SOFT_INPUT = 1020;
172 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700173 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 static final int MSG_ATTACH_TOKEN = 1040;
175 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 static final int MSG_START_INPUT = 2000;
178 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
Yohei Yukawa33e81792015-11-17 21:14:42 -0800180 static final int MSG_UNBIND_CLIENT = 3000;
181 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700182 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700183 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900184 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800185 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800186 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
satok01038492012-04-09 21:08:27 +0900188 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
189
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700190 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
191
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700192 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
satokf9f01002011-05-19 21:31:50 +0900194 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
195
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900196 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900197 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900198
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700199 @Retention(SOURCE)
200 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
201 private @interface HardKeyboardBehavior {
202 int WIRELESS_AFFORDANCE = 0;
203 int WIRED_AFFORDANCE = 1;
204 }
satok4e4569d2010-11-19 18:45:53 +0900205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800207 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900209 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700212 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700214 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900215 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900216 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900217 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800218 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900220 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 // All known input methods. mMethodMap also serves as the global
223 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700224 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
225 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900226 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700227 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900228 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
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 Yukawae39d4ed2015-11-19 03:38:49 -0800351 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
352 */
353 ClientState mCurFocusedWindowClient;
354
355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 * The input context last provided by the current client.
357 */
358 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700361 * The missing method flags for the input context last provided by the current client.
362 *
363 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
364 */
365 int mCurInputContextMissingMethods;
366
367 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 * The attributes last provided by the current client.
369 */
370 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700373 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700375 *
376 * <p>This can be {@code null} when no input method is connected.</p>
377 *
378 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700380 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /**
satokab751aa2010-09-14 19:17:36 +0900384 * The current subtype of the current input method.
385 */
386 private InputMethodSubtype mCurrentSubtype;
387
satok4e4569d2010-11-19 18:45:53 +0900388 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900389 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700390 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900391
John Spurlocke0980502013-10-25 11:59:29 -0400392 // Was the keyguard locked when this client became current?
393 private boolean mCurClientInKeyguard;
394
satokab751aa2010-09-14 19:17:36 +0900395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 * Set to true if our ServiceConnection is currently actively bound to
397 * a service (whether or not we have gotten its IBinder back yet).
398 */
399 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 /**
402 * Set if the client has asked for the input method to be shown.
403 */
404 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
407 * Set if we were explicitly told to show the input method.
408 */
409 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /**
412 * Set if we were forced to be shown.
413 */
414 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 /**
417 * Set if we last told the input method to show itself.
418 */
419 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800422 * {@code true} if the current input method is in fullscreen mode.
423 */
424 boolean mInFullscreenMode;
425
426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * The Intent used to connect to the current input method.
428 */
429 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 /**
432 * The token we have made for the currently active input method, to
433 * identify it in the future.
434 */
435 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 /**
438 * If non-null, this is the input method service we are currently connected
439 * to.
440 */
441 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 /**
444 * Time that we last initiated a bind to the input method, to determine
445 * if we should try to disconnect and reconnect to it.
446 */
447 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 /**
450 * Have we called mCurMethod.bindInput()?
451 */
452 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 /**
455 * Currently enabled session. Only touched by service thread, not
456 * protected by a lock.
457 */
458 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700461 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700463 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800464
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900465 int mCurUserActionNotificationSequenceNumber = 0;
466
Joe Onorato857fd9b2011-01-27 15:08:35 -0800467 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900468
469 /**
470 * A set of status bits regarding the active IME.
471 *
472 * <p>This value is a combination of following two bits:</p>
473 * <dl>
474 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
475 * <dd>
476 * If this bit is ON, connected IME is ready to accept touch/key events.
477 * </dd>
478 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
479 * <dd>
480 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
481 * </dd>
482 * </dl>
483 * <em>Do not update this value outside of setImeWindowStatus.</em>
484 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800485 int mImeWindowVis;
486
Ken Wakasa05dbb652011-08-22 15:22:43 +0900487 private AlertDialog.Builder mDialogBuilder;
488 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700489 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900490 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700491 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900492 private InputMethodInfo[] mIms;
493 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800494 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700495 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800496 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900497 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
498 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500499 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700500 @HardKeyboardBehavior
501 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700504 int mUserId;
505 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700506 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800507 String mLastEnabled = "";
508
Yohei Yukawa81482972015-06-04 00:58:59 -0700509 /**
510 * <em>This constructor must be called within the lock.</em>
511 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 SettingsObserver(Handler handler) {
513 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700514 }
515
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800516 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700517 if (mRegistered && mUserId == userId) {
518 return;
519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700521 if (mRegistered) {
522 mContext.getContentResolver().unregisterContentObserver(this);
523 mRegistered = false;
524 }
525 if (mUserId != userId) {
526 mLastEnabled = "";
527 mUserId = userId;
528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700530 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900531 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700532 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900533 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700534 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700535 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700536 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800537 resolver.registerContentObserver(Settings.Secure.getUriFor(
538 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700539 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
Michael Wright7b5a96b2014-08-09 19:28:42 -0700542 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800543 final Uri showImeUri = Settings.Secure.getUriFor(
544 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
545 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
546 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700548 if (showImeUri.equals(uri)) {
549 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800550 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
551 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
552 mContext.getContentResolver(),
553 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
554 0, mUserId) == 1;
555 if (mAccessibilityRequestingNoSoftKeyboard) {
556 final boolean showRequested = mShowRequested;
557 hideCurrentInputLocked(0, null);
558 mShowRequested = showRequested;
559 } else if (mShowRequested) {
560 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
561 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700562 } else {
563 boolean enabledChanged = false;
564 String newEnabled = mSettings.getEnabledInputMethodsStr();
565 if (!mLastEnabled.equals(newEnabled)) {
566 mLastEnabled = newEnabled;
567 enabledChanged = true;
568 }
569 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700573
574 @Override
575 public String toString() {
576 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
577 + " mLastEnabled=" + mLastEnabled + "}";
578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
Yohei Yukawa79247822017-01-23 15:26:15 -0800581 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900582 @Override
583 public void onReceive(Context context, Intent intent) {
584 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700585 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900586 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700587 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900588 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800589 } else if (Intent.ACTION_USER_ADDED.equals(action)
590 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100591 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800592 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700593 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
594 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
595 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
596 final String prevValue = intent.getStringExtra(
597 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
598 final String newValue = intent.getStringExtra(
599 Intent.EXTRA_SETTING_NEW_VALUE);
600 restoreEnabledInputMethods(mContext, prevValue, newValue);
601 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800602 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800603 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900604 } else {
605 Slog.w(TAG, "Unexpected intent " + intent);
606 }
607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800609
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800610 /**
611 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
612 *
613 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
614 * the users. We should ignore this event if this is about any background user's locale.</p>
615 *
616 * <p>Caution: This method must not be called when system is not ready.</p>
617 */
618 void onActionLocaleChanged() {
619 synchronized (mMethodMap) {
620 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
621 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
622 return;
623 }
624 buildInputMethodListLocked(true);
625 // If the locale is changed, needs to reset the default ime
626 resetDefaultImeLocked(mContext);
627 updateFromSettingsLocked(true);
628 mLastSystemLocales = possibleNewLocale;
629 }
630 }
631
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700632 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
633 // does not attempt to validate on the fly with any installed device policy, so must only
634 // be run in the context of initial device setup.
635 //
636 // TODO: Move this method to InputMethodUtils with adding unit tests.
637 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
638 if (DEBUG_RESTORE) {
639 Slog.i(TAG, "Restoring enabled input methods:");
640 Slog.i(TAG, "prev=" + prevValue);
641 Slog.i(TAG, " new=" + newValue);
642 }
643 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900644 ArrayMap<String, ArraySet<String>> prevMap =
645 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
646 ArrayMap<String, ArraySet<String>> newMap =
647 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700648
649 // Merge the restored ime+subtype enabled states into the live state
650 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
651 final String imeId = entry.getKey();
652 ArraySet<String> prevSubtypes = prevMap.get(imeId);
653 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700654 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700655 prevMap.put(imeId, prevSubtypes);
656 }
657 prevSubtypes.addAll(entry.getValue());
658 }
659
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700660 final String mergedImesAndSubtypesString =
661 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700662 if (DEBUG_RESTORE) {
663 Slog.i(TAG, "Merged IME string:");
664 Slog.i(TAG, " " + mergedImesAndSubtypesString);
665 }
666 Settings.Secure.putString(context.getContentResolver(),
667 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
668 }
669
Yohei Yukawac4e44912017-02-09 19:30:22 -0800670 final class MyPackageMonitor extends PackageMonitor {
671 /**
672 * Set of packages to be monitored.
673 *
674 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
675 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
676 * dynamically unless the entire package is updated, which also always triggers package
677 * rescanning.</p>
678 */
679 @GuardedBy("mMethodMap")
680 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
681
682 @GuardedBy("mMethodMap")
683 void clearPackagesToMonitorComponentChangeLocked() {
684 mPackagesToMonitorComponentChange.clear();
685 }
686
687 @GuardedBy("mMethodMap")
688 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
689 mPackagesToMonitorComponentChange.add(packageName);
690 }
691
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900692 private boolean isChangingPackagesOfCurrentUser() {
693 final int userId = getChangingUserId();
694 final boolean retval = userId == mSettings.getCurrentUserId();
695 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900696 if (!retval) {
697 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
698 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900699 }
700 return retval;
701 }
702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800704 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900705 if (!isChangingPackagesOfCurrentUser()) {
706 return false;
707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900709 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 final int N = mMethodList.size();
711 if (curInputMethodId != null) {
712 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800713 InputMethodInfo imi = mMethodList.get(i);
714 if (imi.getId().equals(curInputMethodId)) {
715 for (String pkg : packages) {
716 if (imi.getPackageName().equals(pkg)) {
717 if (!doit) {
718 return true;
719 }
satok723a27e2010-11-11 14:58:11 +0900720 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800721 chooseNewDefaultIMELocked();
722 return true;
723 }
724 }
725 }
726 }
727 }
728 }
729 return false;
730 }
731
732 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800733 public boolean onPackageChanged(String packageName, int uid, String[] components) {
734 // If this package is in the watch list, we want to check it.
735 synchronized (mMethodMap) {
736 return mPackagesToMonitorComponentChange.contains(packageName);
737 }
738 }
739
740 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800741 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900742 if (!isChangingPackagesOfCurrentUser()) {
743 return;
744 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800745 synchronized (mMethodMap) {
746 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900747 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800748 final int N = mMethodList.size();
749 if (curInputMethodId != null) {
750 for (int i=0; i<N; i++) {
751 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900752 final String imiId = imi.getId();
753 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800754 curIm = imi;
755 }
satoke7c6998e2011-06-03 17:57:59 +0900756
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800757 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900758 if (isPackageModified(imi.getPackageName())) {
759 mFileManager.deleteAllInputMethodSubtypes(imiId);
760 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800761 if (change == PACKAGE_TEMPORARY_CHANGE
762 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800763 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800764 + imi.getComponent());
765 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767 }
768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800769
Yohei Yukawa94e33302016-02-12 19:37:03 -0800770 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800774 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800775 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800776 if (change == PACKAGE_TEMPORARY_CHANGE
777 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800778 ServiceInfo si = null;
779 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900780 si = mIPackageManager.getServiceInfo(
781 curIm.getComponent(), 0, mSettings.getCurrentUserId());
782 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800783 }
784 if (si == null) {
785 // Uh oh, current input method is no longer around!
786 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800787 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900788 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800789 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800790 changed = true;
791 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800792 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900793 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800794 }
795 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800796 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800797 }
satokab751aa2010-09-14 19:17:36 +0900798
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800799 if (curIm == null) {
800 // We currently don't have a default input method... is
801 // one now available?
802 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700803 } else if (!changed && isPackageModified(curIm.getPackageName())) {
804 // Even if the current input method is still available, mCurrentSubtype could
805 // be obsolete when the package is modified in practice.
806 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800809 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800810 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 }
812 }
813 }
814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800815
Jeff Brownc28867a2013-03-26 15:42:39 -0700816 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900817 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700818 private final IInputMethod mMethod;
819 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800820
Jeff Brownc28867a2013-03-26 15:42:39 -0700821 MethodCallback(InputMethodManagerService imms, IInputMethod method,
822 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900823 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700824 mMethod = method;
825 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
satoke7c6998e2011-06-03 17:57:59 +0900828 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700829 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700830 long ident = Binder.clearCallingIdentity();
831 try {
832 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
833 } finally {
834 Binder.restoreCallingIdentity(ident);
835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800838
satok01038492012-04-09 21:08:27 +0900839 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700840 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900841 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700842 public void onHardKeyboardStatusChange(boolean available) {
843 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
844 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900845 }
846
Michael Wright7b5a96b2014-08-09 19:28:42 -0700847 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900848 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700849 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900850 }
851 synchronized(mMethodMap) {
852 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
853 && mSwitchingDialog.isShowing()) {
854 mSwitchingDialogTitleView.findViewById(
855 com.android.internal.R.id.hard_keyboard_section).setVisibility(
856 available ? View.VISIBLE : View.GONE);
857 }
858 }
859 }
860 }
861
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800862 public static final class Lifecycle extends SystemService {
863 private InputMethodManagerService mService;
864
865 public Lifecycle(Context context) {
866 super(context);
867 mService = new InputMethodManagerService(context);
868 }
869
870 @Override
871 public void onStart() {
872 LocalServices.addService(InputMethodManagerInternal.class,
873 new LocalServiceImpl(mService.mHandler));
874 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
875 }
876
877 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800878 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700879 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800880 // TODO: Dispatch this to a worker thread as needed.
881 mService.onSwitchUser(userHandle);
882 }
883
884 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800885 public void onBootPhase(int phase) {
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.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800888 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
889 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
890 .getService(Context.STATUS_BAR_SERVICE);
891 mService.systemRunning(statusBarService);
892 }
893 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800894
895 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700896 public void onUnlockUser(final @UserIdInt int userHandle) {
897 // Called on ActivityManager thread.
898 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700899 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800900 }
901 }
902
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800903 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800904 synchronized(mMethodMap) {
905 final int currentUserId = mSettings.getCurrentUserId();
906 if (DEBUG) {
907 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
908 }
909 if (userId != currentUserId) {
910 return;
911 }
912 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800913 if (mSystemReady) {
914 // We need to rebuild IMEs.
915 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
916 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
917 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800918 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800919 }
920
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800921 void onSwitchUser(@UserIdInt int userId) {
922 synchronized (mMethodMap) {
923 switchUserLocked(userId);
924 }
925 }
926
Seigo Nonaka7309b122015-08-17 18:34:13 -0700927 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900928 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800930 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700932 // Note: SettingsObserver doesn't register observers in its constructor.
933 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 mIWindowManager = IWindowManager.Stub.asInterface(
935 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700936 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800937 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900938 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 public void executeMessage(Message msg) {
940 handleMessage(msg);
941 }
Mita Yuned218c72012-12-06 17:18:25 -0800942 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800943 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800944 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900945 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700946 mHasFeature = context.getPackageManager().hasSystemFeature(
947 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500948 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700949 mHardKeyboardBehavior = mContext.getResources().getInteger(
950 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900951
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400952 Bundle extras = new Bundle();
953 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
954 mImeSwitcherNotification = new Notification.Builder(mContext)
955 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
956 .setWhen(0)
957 .setOngoing(true)
958 .addExtras(extras)
959 .setCategory(Notification.CATEGORY_SYSTEM)
960 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400961
satok7cfc0ed2011-06-20 21:29:36 +0900962 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900963 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900964
965 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900966
satok7cfc0ed2011-06-20 21:29:36 +0900967 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 int userId = 0;
969 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800970 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 } catch (RemoteException e) {
972 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
973 }
satok913a8922010-08-26 21:53:41 +0900974
satokd87c2592010-09-29 11:52:06 +0900975 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900976 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800977 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700978
Kenny Guy2a764942014-04-02 13:29:20 +0100979 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900980 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800981 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
982 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900983 }
984
satok5b927c432012-05-01 20:09:34 +0900985 private void resetDefaultImeLocked(Context context) {
986 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800987 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900988 return;
989 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800990 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800991 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800992 if (suitableImes.isEmpty()) {
993 Slog.i(TAG, "No default found");
994 return;
satok5b927c432012-05-01 20:09:34 +0900995 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800996 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -0800997 if (DEBUG) {
998 Slog.i(TAG, "Default found, using " + defIm.getId());
999 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001000 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001001 }
1002
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001003 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001004 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1005 + " currentUserId=" + mSettings.getCurrentUserId());
1006
Yohei Yukawa81482972015-06-04 00:58:59 -07001007 // ContentObserver should be registered again when the user is changed
1008 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001009
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001010 // If the system is not ready or the device is not yed unlocked by the user, then we use
1011 // copy-on-write settings.
1012 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001013 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001014 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001015 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001016 // InputMethodFileManager should be reset when the user is changed
1017 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001018 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001019
1020 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1021 + " defaultImiId=" + defaultImiId);
1022
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001023 // For secondary users, the list of enabled IMEs may not have been updated since the
1024 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1025 // not be empty even if the IME has been uninstalled by the primary user.
1026 // Even in such cases, IMMS works fine because it will find the most applicable
1027 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001028 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001029 mLastSystemLocales = mRes.getConfiguration().getLocales();
1030
1031 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1032 if (mSystemReady) {
1033 hideCurrentInputLocked(0, null);
1034 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1035 buildInputMethodListLocked(initialUserSwitch);
1036 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1037 // This is the first time of the user switch and
1038 // set the current ime to the proper one.
1039 resetDefaultImeLocked(mContext);
1040 }
1041 updateFromSettingsLocked(true);
1042 try {
1043 startInputInnerLocked();
1044 } catch (RuntimeException e) {
1045 Slog.w(TAG, "Unexpected exception", e);
1046 }
1047 }
1048
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001049 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001050 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1051 mSettings.getEnabledInputMethodListLocked(), newUserId,
1052 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001053 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001054
1055 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1056 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001057 }
1058
Kenny Guy2a764942014-04-02 13:29:20 +01001059 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001060 mSettings.setCurrentProfileIds(
1061 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 @Override
1065 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1066 throws RemoteException {
1067 try {
1068 return super.onTransact(code, data, reply, flags);
1069 } catch (RuntimeException e) {
1070 // The input method manager only throws security exceptions, so let's
1071 // log all others.
1072 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001073 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075 throw e;
1076 }
1077 }
1078
Svetoslav Ganova0027152013-06-25 14:59:53 -07001079 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001080 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001081 if (DEBUG) {
1082 Slog.d(TAG, "--- systemReady");
1083 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001084 if (!mSystemReady) {
1085 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001086 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001087 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001088 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001089 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001090 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1091 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001092 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001093 if (mStatusBar != null) {
1094 mStatusBar.setIconVisibility(mSlotIme, false);
1095 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001096 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001097 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1098 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001099 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001100 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001101 mHardKeyboardListener);
1102 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001103
1104 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1105 mSettingsObserver.registerContentObserverLocked(currentUserId);
1106
1107 final IntentFilter broadcastFilter = new IntentFilter();
1108 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1109 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1110 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1111 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1112 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1113 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1114
1115 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1116 resetDefaultImeLocked(mContext);
1117 updateFromSettingsLocked(true);
1118 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1119 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1120 mContext.getBasePackageName());
1121
Dianne Hackborncc278702009-09-02 23:07:23 -07001122 try {
1123 startInputInnerLocked();
1124 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001125 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001126 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001127 }
1128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001130
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 // ---------------------------------------------------------------------------------------
1132 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1133 // 1) it comes from the system process
1134 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1135 private boolean calledFromValidUser() {
1136 final int uid = Binder.getCallingUid();
1137 final int userId = UserHandle.getUserId(uid);
1138 if (DEBUG) {
1139 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1140 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1141 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001142 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1143 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001144 }
Kenny Guy2a764942014-04-02 13:29:20 +01001145 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001146 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001147 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001148
1149 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1150 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1151 // must not manage background users' states in any functions.
1152 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1153 // by a token.
1154 if (mContext.checkCallingOrSelfPermission(
1155 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1156 == PackageManager.PERMISSION_GRANTED) {
1157 if (DEBUG) {
1158 Slog.d(TAG, "--- Access granted because the calling process has "
1159 + "the INTERACT_ACROSS_USERS_FULL permission");
1160 }
1161 return true;
1162 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001163 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001164 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1165 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001166 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001167 }
1168
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001169
1170 /**
1171 * Returns true iff the caller is identified to be the current input method with the token.
1172 * @param token The window token given to the input method when it was started.
1173 * @return true if and only if non-null valid token is specified.
1174 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001175 private boolean calledWithValidToken(@Nullable IBinder token) {
1176 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1177 if (DEBUG) {
1178 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1179 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1180 }
1181 return false;
1182 }
1183 if (token == null || token != mCurToken) {
1184 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1185 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1186 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001187 return false;
1188 }
1189 return true;
1190 }
1191
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001192 private boolean bindCurrentInputMethodService(
1193 Intent service, ServiceConnection conn, int flags) {
1194 if (service == null || conn == null) {
1195 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1196 return false;
1197 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001198 return mContext.bindServiceAsUser(service, conn, flags,
1199 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001200 }
1201
satoke7c6998e2011-06-03 17:57:59 +09001202 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001204 // TODO: Make this work even for non-current users?
1205 if (!calledFromValidUser()) {
1206 return Collections.emptyList();
1207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001209 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 }
1212
satoke7c6998e2011-06-03 17:57:59 +09001213 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001215 // TODO: Make this work even for non-current users?
1216 if (!calledFromValidUser()) {
1217 return Collections.emptyList();
1218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001220 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222 }
1223
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001224 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001225 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001226 * @return enabled subtypes of the specified imi
1227 */
satoke7c6998e2011-06-03 17:57:59 +09001228 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001229 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001230 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001231 // TODO: Make this work even for non-current users?
1232 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001233 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001234 }
satok67ddf9c2010-11-17 09:45:54 +09001235 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001236 final InputMethodInfo imi;
1237 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001238 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001239 } else {
1240 imi = mMethodMap.get(imiId);
1241 }
1242 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001243 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001244 }
1245 return mSettings.getEnabledInputMethodSubtypeListLocked(
1246 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001247 }
1248 }
1249
satoke7c6998e2011-06-03 17:57:59 +09001250 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public void addClient(IInputMethodClient client,
1252 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001253 if (!calledFromValidUser()) {
1254 return;
1255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 synchronized (mMethodMap) {
1257 mClients.put(client.asBinder(), new ClientState(client,
1258 inputContext, uid, pid));
1259 }
1260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
satoke7c6998e2011-06-03 17:57:59 +09001262 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001264 if (!calledFromValidUser()) {
1265 return;
1266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001268 ClientState cs = mClients.remove(client.asBinder());
1269 if (cs != null) {
1270 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001271 if (mCurClient == cs) {
1272 mCurClient = null;
1273 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001274 if (mCurFocusedWindowClient == cs) {
1275 mCurFocusedWindowClient = null;
1276 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 }
1279 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 void executeOrSendMessage(IInterface target, Message msg) {
1282 if (target.asBinder() instanceof Binder) {
1283 mCaller.sendMessage(msg);
1284 } else {
1285 handleMessage(msg);
1286 msg.recycle();
1287 }
1288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
Yohei Yukawa33e81792015-11-17 21:14:42 -08001290 void unbindCurrentClientLocked(
1291 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001293 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 + mCurClient.client.asBinder());
1295 if (mBoundToMethod) {
1296 mBoundToMethod = false;
1297 if (mCurMethod != null) {
1298 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1299 MSG_UNBIND_INPUT, mCurMethod));
1300 }
1301 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001302
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001303 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1304 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001305 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1306 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309
The Android Open Source Project10592532009-03-18 17:39:46 -07001310 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 private int getImeShowFlags() {
1315 int flags = 0;
1316 if (mShowForced) {
1317 flags |= InputMethod.SHOW_FORCED
1318 | InputMethod.SHOW_EXPLICIT;
1319 } else if (mShowExplicitlyRequested) {
1320 flags |= InputMethod.SHOW_EXPLICIT;
1321 }
1322 return flags;
1323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 private int getAppShowFlags() {
1326 int flags = 0;
1327 if (mShowForced) {
1328 flags |= InputMethodManager.SHOW_FORCED;
1329 } else if (!mShowExplicitlyRequested) {
1330 flags |= InputMethodManager.SHOW_IMPLICIT;
1331 }
1332 return flags;
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001335 InputBindResult attachNewInputLocked(
1336 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 if (!mBoundToMethod) {
1338 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1339 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1340 mBoundToMethod = true;
1341 }
1342 final SessionState session = mCurClient.curSession;
1343 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001344 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1345 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1346 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001348 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1349 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1350 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
1352 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001353 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001354 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001356 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001357 (session.channel != null ? session.channel.dup() : null),
1358 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001360
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001361 InputBindResult startInputLocked(
1362 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001363 IInputMethodClient client, IInputContext inputContext,
1364 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001365 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 // If no method is currently selected, do nothing.
1367 if (mCurMethodId == null) {
1368 return mNoBinding;
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 ClientState cs = mClients.get(client.asBinder());
1372 if (cs == null) {
1373 throw new IllegalArgumentException("unknown client "
1374 + client.asBinder());
1375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
Yohei Yukawa74750f22016-03-22 12:54:22 -07001377 if (attribute == null) {
1378 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1379 + " uid=" + cs.uid + " pid=" + cs.pid);
1380 return null;
1381 }
1382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 try {
1384 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1385 // Check with the window manager to make sure this client actually
1386 // has a window with focus. If not, reject. This is thread safe
1387 // because if the focus changes some time before or after, the
1388 // next client receiving focus that has any interest in input will
1389 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001390 if (DEBUG) {
1391 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1392 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 return null;
1395 }
1396 } catch (RemoteException e) {
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001399 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001400 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001401 }
1402
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001403 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001404 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001405 @NonNull EditorInfo attribute, int controlFlags,
1406 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001407 // If no method is currently selected, do nothing.
1408 if (mCurMethodId == null) {
1409 return mNoBinding;
1410 }
1411
Yohei Yukawad57ba672015-06-08 16:39:46 -07001412 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1413 attribute.packageName)) {
1414 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1415 + " uid=" + cs.uid + " package=" + attribute.packageName);
1416 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001417 }
1418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001420 // Was the keyguard locked when switching over to the new client?
1421 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 // If the client is changing, we need to switch over to the new
1423 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001424 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001425 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001426 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427
1428 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001429 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001430 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001431 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 }
1433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 // Bump up the sequence for this client and attach it.
1436 mCurSeq++;
1437 if (mCurSeq <= 0) mCurSeq = 1;
1438 mCurClient = cs;
1439 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001440 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 // Check if the input method is changing.
1444 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1445 if (cs.curSession != null) {
1446 // Fast case: if we are already connected to the input method,
1447 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001448 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001449 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 if (mHaveConnection) {
1452 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 // Return to client, and we will get back with it when
1454 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001455 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001456 return new InputBindResult(null, null, mCurId, mCurSeq,
1457 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 } else if (SystemClock.uptimeMillis()
1459 < (mLastBindTime+TIME_TO_RECONNECT)) {
1460 // In this case we have connected to the service, but
1461 // don't yet have its interface. If it hasn't been too
1462 // long since we did the connection, we'll return to
1463 // the client and wait to get the service interface so
1464 // we can report back. If it has been too long, we want
1465 // to fall through so we can try a disconnect/reconnect
1466 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001467 return new InputBindResult(null, null, mCurId, mCurSeq,
1468 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001470 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1471 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 }
1473 }
1474 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001475
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001476 return startInputInnerLocked();
1477 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001478
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001479 InputBindResult startInputInnerLocked() {
1480 if (mCurMethodId == null) {
1481 return mNoBinding;
1482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001483
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001484 if (!mSystemReady) {
1485 // If the system is not yet ready, we shouldn't be running third
1486 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001487 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1488 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1492 if (info == null) {
1493 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001495
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001496 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1499 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001500 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1501 com.android.internal.R.string.input_method_binding_label);
1502 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1503 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001504 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001505 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1506 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 mLastBindTime = SystemClock.uptimeMillis();
1508 mHaveConnection = true;
1509 mCurId = info.getId();
1510 mCurToken = new Binder();
1511 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001512 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001513 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 } catch (RemoteException e) {
1515 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001516 return new InputBindResult(null, null, mCurId, mCurSeq,
1517 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 } else {
1519 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001520 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 + mCurIntent);
1522 }
1523 return null;
1524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001526 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001527 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001528 IInputMethodClient client, IInputContext inputContext,
1529 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001530 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001531 if (!calledFromValidUser()) {
1532 return null;
1533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001535 if (DEBUG) {
1536 Slog.v(TAG, "startInput: reason="
1537 + InputMethodClient.getStartInputReason(startInputReason)
1538 + " client = " + client.asBinder()
1539 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001540 + " missingMethods="
1541 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001542 + " attribute=" + attribute
1543 + " controlFlags=#" + Integer.toHexString(controlFlags));
1544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 final long ident = Binder.clearCallingIdentity();
1546 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001547 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1548 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 } finally {
1550 Binder.restoreCallingIdentity(ident);
1551 }
1552 }
1553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001554
satoke7c6998e2011-06-03 17:57:59 +09001555 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 public void finishInput(IInputMethodClient client) {
1557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001558
satoke7c6998e2011-06-03 17:57:59 +09001559 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 public void onServiceConnected(ComponentName name, IBinder service) {
1561 synchronized (mMethodMap) {
1562 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1563 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001564 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001565 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001566 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001567 return;
1568 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001569 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001570 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1571 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001573 clearClientSessionLocked(mCurClient);
1574 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576 }
1577 }
1578 }
1579
Jeff Brownc28867a2013-03-26 15:42:39 -07001580 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1581 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 synchronized (mMethodMap) {
1583 if (mCurMethod != null && method != null
1584 && mCurMethod.asBinder() == method.asBinder()) {
1585 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001586 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001588 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001589 InputBindResult res = attachNewInputLocked(
1590 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 if (res.method != null) {
1592 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001593 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001595 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
1597 }
1598 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001599
1600 // Session abandoned. Close its associated input channel.
1601 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001603
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001604 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001605 if (mVisibleBound) {
1606 mContext.unbindService(mVisibleConnection);
1607 mVisibleBound = false;
1608 }
1609
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001610 if (mHaveConnection) {
1611 mContext.unbindService(this);
1612 mHaveConnection = false;
1613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001615 if (mCurToken != null) {
1616 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001617 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001618 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001619 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001620 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001621 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001622 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001623 } catch (RemoteException e) {
1624 }
1625 mCurToken = null;
1626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001627
The Android Open Source Project10592532009-03-18 17:39:46 -07001628 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001629 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631
Yohei Yukawa33e81792015-11-17 21:14:42 -08001632 void resetCurrentMethodAndClient(
1633 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001634 mCurMethodId = null;
1635 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001636 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001637 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001638
1639 void requestClientSessionLocked(ClientState cs) {
1640 if (!cs.sessionRequested) {
1641 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1642 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1643 cs.sessionRequested = true;
1644 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1645 MSG_CREATE_SESSION, mCurMethod, channels[1],
1646 new MethodCallback(this, mCurMethod, channels[0])));
1647 }
1648 }
1649
1650 void clearClientSessionLocked(ClientState cs) {
1651 finishSessionLocked(cs.curSession);
1652 cs.curSession = null;
1653 cs.sessionRequested = false;
1654 }
1655
1656 private void finishSessionLocked(SessionState sessionState) {
1657 if (sessionState != null) {
1658 if (sessionState.session != null) {
1659 try {
1660 sessionState.session.finishSession();
1661 } catch (RemoteException e) {
1662 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001663 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001664 }
1665 sessionState.session = null;
1666 }
1667 if (sessionState.channel != null) {
1668 sessionState.channel.dispose();
1669 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001670 }
1671 }
1672 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001673
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001674 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if (mCurMethod != null) {
1676 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001677 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001679
Jeff Brownc28867a2013-03-26 15:42:39 -07001680 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001681 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 mCurMethod = null;
1683 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001684 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001685 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001686 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001687 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001689
satoke7c6998e2011-06-03 17:57:59 +09001690 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001692 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1693 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1694 // which is irregular but can eventually happen for everyone just by continuing using the
1695 // device. Thus it is important to make sure that all the internal states are properly
1696 // refreshed when this method is called back. Running
1697 // adb install -r <APK that implements the current IME>
1698 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 + " mCurIntent=" + mCurIntent);
1702 if (mCurMethod != null && mCurIntent != null
1703 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001704 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 // We consider this to be a new bind attempt, since the system
1706 // should now try to restart the service for us.
1707 mLastBindTime = SystemClock.uptimeMillis();
1708 mShowRequested = mInputShown;
1709 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001710 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
1712 }
1713 }
1714
satokf9f01002011-05-19 21:31:50 +09001715 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001717 synchronized (mMethodMap) {
1718 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001719 return;
1720 }
1721 final long ident = Binder.clearCallingIdentity();
1722 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001724 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001725 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001726 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001729 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001730 CharSequence contentDescription = null;
1731 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001732 // Use PackageManager to load label
1733 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001734 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001735 mIPackageManager.getApplicationInfo(packageName, 0,
1736 mSettings.getCurrentUserId()));
1737 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001738 /* ignore */
1739 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001740 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001741 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001742 contentDescription != null
1743 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001744 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001747 } finally {
1748 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751 }
1752
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001753 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001754 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001755 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001756 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001757 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001758 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001759 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1760 // When physical keyboard is attached, we show the ime switcher (or notification if
1761 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1762 // exists in the IME switcher dialog. Might be OK to remove this condition once
1763 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1764 return true;
1765 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001766 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1767 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001768 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001769
1770 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1771 final int N = imis.size();
1772 if (N > 2) return true;
1773 if (N < 1) return false;
1774 int nonAuxCount = 0;
1775 int auxCount = 0;
1776 InputMethodSubtype nonAuxSubtype = null;
1777 InputMethodSubtype auxSubtype = null;
1778 for(int i = 0; i < N; ++i) {
1779 final InputMethodInfo imi = imis.get(i);
1780 final List<InputMethodSubtype> subtypes =
1781 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1782 final int subtypeCount = subtypes.size();
1783 if (subtypeCount == 0) {
1784 ++nonAuxCount;
1785 } else {
1786 for (int j = 0; j < subtypeCount; ++j) {
1787 final InputMethodSubtype subtype = subtypes.get(j);
1788 if (!subtype.isAuxiliary()) {
1789 ++nonAuxCount;
1790 nonAuxSubtype = subtype;
1791 } else {
1792 ++auxCount;
1793 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001794 }
1795 }
satok7cfc0ed2011-06-20 21:29:36 +09001796 }
1797 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001798 if (nonAuxCount > 1 || auxCount > 1) {
1799 return true;
1800 } else if (nonAuxCount == 1 && auxCount == 1) {
1801 if (nonAuxSubtype != null && auxSubtype != null
1802 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1803 || auxSubtype.overridesImplicitlyEnabledSubtype()
1804 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1805 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1806 return false;
1807 }
1808 return true;
1809 }
1810 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001811 }
1812
John Spurlocke0980502013-10-25 11:59:29 -04001813 private boolean isKeyguardLocked() {
1814 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1815 }
1816
satokdbf29502011-08-25 15:28:23 +09001817 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001818 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001819 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001820 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001821 return;
1822 }
1823
1824 synchronized (mMethodMap) {
1825 mImeWindowVis = vis;
1826 mBackDisposition = backDisposition;
1827 updateSystemUiLocked(token, vis, backDisposition);
1828 }
1829 }
1830
1831 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1832 synchronized (mMethodMap) {
1833 updateSystemUiLocked(token, vis, backDisposition);
1834 }
1835 }
1836
1837 // Caution! This method is called in this class. Handle multi-user carefully
1838 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1839 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001840 return;
1841 }
1842
1843 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1844 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001845 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001846 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001847 // apply policy for binder calls
1848 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1849 vis = 0;
satok06487a52010-10-29 11:37:18 +09001850 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001851 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1852 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1853 if (mStatusBar != null) {
1854 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1855 needsToShowImeSwitcher);
1856 }
1857 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1858 if (imi != null && needsToShowImeSwitcher) {
1859 // Used to load label
1860 final CharSequence title = mRes.getText(
1861 com.android.internal.R.string.select_input_method);
1862 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1863 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001864 mImeSwitcherNotification.setContentTitle(title)
1865 .setContentText(summary)
1866 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001867 try {
1868 if ((mNotificationManager != null)
1869 && !mIWindowManager.hasNavigationBar()) {
1870 if (DEBUG) {
1871 Slog.d(TAG, "--- show notification: label = " + summary);
1872 }
1873 mNotificationManager.notifyAsUser(null,
1874 com.android.internal.R.string.select_input_method,
1875 mImeSwitcherNotification.build(), UserHandle.ALL);
1876 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001877 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001878 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001879 }
1880 } else {
1881 if (mNotificationShown && mNotificationManager != null) {
1882 if (DEBUG) {
1883 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001884 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001885 mNotificationManager.cancelAsUser(null,
1886 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1887 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001888 }
satok06487a52010-10-29 11:37:18 +09001889 }
1890 } finally {
1891 Binder.restoreCallingIdentity(ident);
1892 }
1893 }
1894
satoke7c6998e2011-06-03 17:57:59 +09001895 @Override
satokf9f01002011-05-19 21:31:50 +09001896 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001897 if (!calledFromValidUser()) {
1898 return;
1899 }
satokf9f01002011-05-19 21:31:50 +09001900 synchronized (mMethodMap) {
1901 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1902 for (int i = 0; i < spans.length; ++i) {
1903 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001904 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001905 mSecureSuggestionSpans.put(ss, currentImi);
1906 }
1907 }
1908 }
1909 }
1910
satoke7c6998e2011-06-03 17:57:59 +09001911 @Override
satokf9f01002011-05-19 21:31:50 +09001912 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001913 if (!calledFromValidUser()) {
1914 return false;
1915 }
satokf9f01002011-05-19 21:31:50 +09001916 synchronized (mMethodMap) {
1917 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1918 // TODO: Do not send the intent if the process of the targetImi is already dead.
1919 if (targetImi != null) {
1920 final String[] suggestions = span.getSuggestions();
1921 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001922 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001923 final Intent intent = new Intent();
1924 // Ensures that only a class in the original IME package will receive the
1925 // notification.
satok42c5a162011-05-26 16:46:14 +09001926 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001927 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1928 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1929 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1930 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001931 final long ident = Binder.clearCallingIdentity();
1932 try {
1933 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1934 } finally {
1935 Binder.restoreCallingIdentity(ident);
1936 }
satokf9f01002011-05-19 21:31:50 +09001937 return true;
1938 }
1939 }
1940 return false;
1941 }
1942
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001943 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001944 updateInputMethodsFromSettingsLocked(enabledMayChange);
1945 updateKeyboardFromSettingsLocked();
1946 }
1947
1948 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001949 if (enabledMayChange) {
1950 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1951 for (int i=0; i<enabled.size(); i++) {
1952 // We allow the user to select "disabled until used" apps, so if they
1953 // are enabling one of those here we now need to make it enabled.
1954 InputMethodInfo imm = enabled.get(i);
1955 try {
1956 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1957 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1958 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001959 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001960 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001961 if (DEBUG) {
1962 Slog.d(TAG, "Update state(" + imm.getId()
1963 + "): DISABLED_UNTIL_USED -> DEFAULT");
1964 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001965 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1966 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001967 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1968 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001969 }
1970 } catch (RemoteException e) {
1971 }
1972 }
1973 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001974 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1975 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1976 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1977 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001978 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001979 // There is no input method selected, try to choose new applicable input method.
1980 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001981 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001982 }
1983 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001985 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001988 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
satokf3db1af2010-11-23 13:34:33 +09001990 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001991 } else {
1992 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001993 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001995 // Here is not the perfect place to reset the switching controller. Ideally
1996 // mSwitchingController and mSettings should be able to share the same state.
1997 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1998 // the same enabled IMEs list.
1999 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002000
2001 }
2002
2003 public void updateKeyboardFromSettingsLocked() {
2004 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2005 if (mSwitchingDialog != null
2006 && mSwitchingDialogTitleView != null
2007 && mSwitchingDialog.isShowing()) {
2008 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2009 com.android.internal.R.id.hard_keyboard_switch);
2010 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
Yohei Yukawab097b822015-12-01 10:43:08 -08002014 private void notifyInputMethodSubtypeChanged(final int userId,
2015 @Nullable final InputMethodInfo inputMethodInfo,
2016 @Nullable final InputMethodSubtype subtype) {
2017 final InputManagerInternal inputManagerInternal =
2018 LocalServices.getService(InputManagerInternal.class);
2019 if (inputManagerInternal != null) {
2020 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2021 }
2022 }
2023
satokab751aa2010-09-14 19:17:36 +09002024 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 InputMethodInfo info = mMethodMap.get(id);
2026 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002027 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002029
satokd81e9502012-05-21 12:58:45 +09002030 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002032 final int subtypeCount = info.getSubtypeCount();
2033 if (subtypeCount <= 0) {
2034 return;
satokcd7cd292010-11-20 15:46:23 +09002035 }
satokd81e9502012-05-21 12:58:45 +09002036 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2037 final InputMethodSubtype newSubtype;
2038 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2039 newSubtype = info.getSubtypeAt(subtypeId);
2040 } else {
2041 // If subtype is null, try to find the most applicable one from
2042 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002043 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002044 }
2045 if (newSubtype == null || oldSubtype == null) {
2046 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2047 + ", new subtype = " + newSubtype);
2048 return;
2049 }
2050 if (newSubtype != oldSubtype) {
2051 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2052 if (mCurMethod != null) {
2053 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002054 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002055 mCurMethod.changeInputMethodSubtype(newSubtype);
2056 } catch (RemoteException e) {
2057 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002058 return;
satokab751aa2010-09-14 19:17:36 +09002059 }
2060 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002061 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 return;
2064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002065
satokd81e9502012-05-21 12:58:45 +09002066 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 final long ident = Binder.clearCallingIdentity();
2068 try {
satokab751aa2010-09-14 19:17:36 +09002069 // Set a subtype to this input method.
2070 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002071 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2072 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2073 // because mCurMethodId is stored as a history in
2074 // setSelectedInputMethodAndSubtypeLocked().
2075 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002077 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002079 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002081 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002083 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 } finally {
2085 Binder.restoreCallingIdentity(ident);
2086 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002087
2088 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2089 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002091
satok42c5a162011-05-26 16:46:14 +09002092 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002093 public boolean showSoftInput(IInputMethodClient client, int flags,
2094 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002095 if (!calledFromValidUser()) {
2096 return false;
2097 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002098 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 long ident = Binder.clearCallingIdentity();
2100 try {
2101 synchronized (mMethodMap) {
2102 if (mCurClient == null || client == null
2103 || mCurClient.client.asBinder() != client.asBinder()) {
2104 try {
2105 // We need to check if this is the current client with
2106 // focus in the window manager, to allow this call to
2107 // be made before input is started in it.
2108 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002109 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002110 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002113 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 }
2115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002116
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002117 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002118 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 }
2120 } finally {
2121 Binder.restoreCallingIdentity(ident);
2122 }
2123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002124
The Android Open Source Project4df24232009-03-05 14:34:35 -08002125 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002127 if (mAccessibilityRequestingNoSoftKeyboard) {
2128 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
Anna Galusza9b278112016-01-04 11:37:37 -08002130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2132 mShowExplicitlyRequested = true;
2133 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002134 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2135 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002137
Dianne Hackborncc278702009-09-02 23:07:23 -07002138 if (!mSystemReady) {
2139 return false;
2140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002141
The Android Open Source Project4df24232009-03-05 14:34:35 -08002142 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002144 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002145 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2146 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2147 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002149 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002150 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002151 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002152 | Context.BIND_TREAT_LIKE_ACTIVITY
2153 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002154 mVisibleBound = true;
2155 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002156 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002158 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 // The client has asked to have the input method shown, but
2160 // we have been sitting here too long with a connection to the
2161 // service and no interface received, so let's disconnect/connect
2162 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002163 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002165 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002167 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002168 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002169 } else {
2170 if (DEBUG) {
2171 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2172 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002175
The Android Open Source Project4df24232009-03-05 14:34:35 -08002176 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002178
satok42c5a162011-05-26 16:46:14 +09002179 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002180 public boolean hideSoftInput(IInputMethodClient client, int flags,
2181 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002182 if (!calledFromValidUser()) {
2183 return false;
2184 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002185 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 long ident = Binder.clearCallingIdentity();
2187 try {
2188 synchronized (mMethodMap) {
2189 if (mCurClient == null || client == null
2190 || mCurClient.client.asBinder() != client.asBinder()) {
2191 try {
2192 // We need to check if this is the current client with
2193 // focus in the window manager, to allow this call to
2194 // be made before input is started in it.
2195 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002196 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2197 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002198 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
2200 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002201 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
2203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002204
Joe Onorato8a9b2202010-02-26 18:56:32 -08002205 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002206 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 }
2208 } finally {
2209 Binder.restoreCallingIdentity(ident);
2210 }
2211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002212
The Android Open Source Project4df24232009-03-05 14:34:35 -08002213 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2215 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002216 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 -08002217 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 }
2219 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002220 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 -08002221 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002223
2224 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2225 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2226 // to be updated with the new value sent from IME process. Even in such a transient state
2227 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2228 // application process as a valid request, and have even promised such a behavior with CTS
2229 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2230 // IMMS#InputShown indicates that the software keyboard is shown.
2231 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2232 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2233 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002234 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002235 if (shouldHideSoftInput) {
2236 // The IME will report its visible state again after the following message finally
2237 // delivered to the IME process as an IPC. Hence the inconsistency between
2238 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2239 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002240 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2241 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2242 res = true;
2243 } else {
2244 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002246 if (mHaveConnection && mVisibleBound) {
2247 mContext.unbindService(mVisibleConnection);
2248 mVisibleBound = false;
2249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 mInputShown = false;
2251 mShowRequested = false;
2252 mShowExplicitlyRequested = false;
2253 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002254 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002256
satok42c5a162011-05-26 16:46:14 +09002257 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002258 public InputBindResult startInputOrWindowGainedFocus(
2259 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2260 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002261 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002262 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002263 if (windowToken != null) {
2264 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002265 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002266 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002267 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2268 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002269 }
2270 }
2271
2272 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002273 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2274 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002275 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2276 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002277 // Needs to check the validity before clearing calling identity
2278 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002279 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 long ident = Binder.clearCallingIdentity();
2281 try {
2282 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002283 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2284 + InputMethodClient.getStartInputReason(startInputReason)
2285 + " client=" + client.asBinder()
2286 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002287 + " missingMethods="
2288 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002289 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002290 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002292 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002293
Dianne Hackborn7663d802012-02-24 13:08:49 -08002294 ClientState cs = mClients.get(client.asBinder());
2295 if (cs == null) {
2296 throw new IllegalArgumentException("unknown client "
2297 + client.asBinder());
2298 }
2299
2300 try {
2301 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2302 // Check with the window manager to make sure this client actually
2303 // has a window with focus. If not, reject. This is thread safe
2304 // because if the focus changes some time before or after, the
2305 // next client receiving focus that has any interest in input will
2306 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002307 if (DEBUG) {
2308 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2309 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2310 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002311 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002313 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002315
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002316 if (!calledFromValidUser) {
2317 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2318 Slog.w(TAG, "If you want to interect with IME, you need "
2319 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2320 hideCurrentInputLocked(0, null);
2321 return null;
2322 }
2323
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002324 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002325 if (DEBUG) {
2326 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2327 + " attribute=" + attribute + ", token = " + windowToken);
2328 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002329 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002330 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002331 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002332 }
2333 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002334 }
2335 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002336 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002337
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002338 // Should we auto-show the IME even if the caller has not
2339 // specified what should be done with it?
2340 // We only do this automatically if the window can resize
2341 // to accommodate the IME (so what the user sees will give
2342 // them good context without input information being obscured
2343 // by the IME) or if running on a large screen where there
2344 // is more room for the target window + IME.
2345 final boolean doAutoShow =
2346 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2347 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2348 || mRes.getConfiguration().isLayoutSizeAtLeast(
2349 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002350 final boolean isTextEditor =
2351 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2352
2353 // We want to start input before showing the IME, but after closing
2354 // it. We want to do this after closing it to help the IME disappear
2355 // more quickly (not get stuck behind it initializing itself for the
2356 // new focused input, even if its window wants to hide the IME).
2357 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2360 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002361 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2363 // There is no focus view, and this window will
2364 // be behind any soft input window, so hide the
2365 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002366 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002367 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002369 } else if (isTextEditor && doAutoShow && (softInputMode &
2370 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 // There is a focus view, and we are navigating forward
2372 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002373 // We only do this automatically if the window can resize
2374 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002375 // them good context without input information being obscured
2376 // by the IME) or if running on a large screen where there
2377 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002378 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002379 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002380 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002381 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002382 didStart = true;
2383 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002384 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
2386 break;
2387 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2388 // Do nothing.
2389 break;
2390 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2391 if ((softInputMode &
2392 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002393 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002394 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 }
2396 break;
2397 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002398 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002399 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 break;
2401 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2402 if ((softInputMode &
2403 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002404 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002405 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002406 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002407 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002408 didStart = true;
2409 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002410 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
2412 break;
2413 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002414 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002415 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002416 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002417 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002418 didStart = true;
2419 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002420 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 break;
2422 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002423
2424 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002425 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002426 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 }
2429 } finally {
2430 Binder.restoreCallingIdentity(ident);
2431 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002432
2433 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002435
satok42c5a162011-05-26 16:46:14 +09002436 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002437 public void showInputMethodPickerFromClient(
2438 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002439 if (!calledFromValidUser()) {
2440 return;
2441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 synchronized (mMethodMap) {
2443 if (mCurClient == null || client == null
2444 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002445 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002446 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448
satok440aab52010-11-25 09:43:11 +09002449 // Always call subtype picker, because subtype picker is a superset of input method
2450 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002451 mHandler.sendMessage(mCaller.obtainMessageI(
2452 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002453 }
2454 }
2455
satok42c5a162011-05-26 16:46:14 +09002456 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002458 if (!calledFromValidUser()) {
2459 return;
2460 }
satok28203512010-11-24 11:06:49 +09002461 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2462 }
2463
satok42c5a162011-05-26 16:46:14 +09002464 @Override
satok28203512010-11-24 11:06:49 +09002465 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002466 if (!calledFromValidUser()) {
2467 return;
2468 }
satok28203512010-11-24 11:06:49 +09002469 synchronized (mMethodMap) {
2470 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002471 setInputMethodWithSubtypeIdLocked(token, id,
2472 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2473 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002474 } else {
2475 setInputMethod(token, id);
2476 }
2477 }
satokab751aa2010-09-14 19:17:36 +09002478 }
2479
satok42c5a162011-05-26 16:46:14 +09002480 @Override
satokb416a712010-11-25 20:42:14 +09002481 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002482 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002483 if (!calledFromValidUser()) {
2484 return;
2485 }
satokb416a712010-11-25 20:42:14 +09002486 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002487 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2488 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002489 }
2490 }
2491
satok4fc87d62011-05-20 16:13:43 +09002492 @Override
satok735cf382010-11-11 20:40:09 +09002493 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002494 if (!calledFromValidUser()) {
2495 return false;
2496 }
satok735cf382010-11-11 20:40:09 +09002497 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002498 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002499 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002500 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002501 lastImi = mMethodMap.get(lastIme.first);
2502 } else {
2503 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002504 }
satok4fc87d62011-05-20 16:13:43 +09002505 String targetLastImiId = null;
2506 int subtypeId = NOT_A_SUBTYPE_ID;
2507 if (lastIme != null && lastImi != null) {
2508 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002509 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002510 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2511 : mCurrentSubtype.hashCode();
2512 // If the last IME is the same as the current IME and the last subtype is not
2513 // defined, there is no need to switch to the last IME.
2514 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2515 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002516 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002517 }
2518 }
2519
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002520 if (TextUtils.isEmpty(targetLastImiId)
2521 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002522 // This is a safety net. If the currentSubtype can't be added to the history
2523 // and the framework couldn't find the last ime, we will make the last ime be
2524 // the most applicable enabled keyboard subtype of the system imes.
2525 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2526 if (enabled != null) {
2527 final int N = enabled.size();
2528 final String locale = mCurrentSubtype == null
2529 ? mRes.getConfiguration().locale.toString()
2530 : mCurrentSubtype.getLocale();
2531 for (int i = 0; i < N; ++i) {
2532 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002533 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002534 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002535 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2536 InputMethodUtils.getSubtypes(imi),
2537 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002538 if (keyboardSubtype != null) {
2539 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002540 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002541 imi, keyboardSubtype.hashCode());
2542 if(keyboardSubtype.getLocale().equals(locale)) {
2543 break;
2544 }
2545 }
2546 }
2547 }
2548 }
2549 }
2550
2551 if (!TextUtils.isEmpty(targetLastImiId)) {
2552 if (DEBUG) {
2553 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2554 + ", from: " + mCurMethodId + ", " + subtypeId);
2555 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002556 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002557 return true;
2558 } else {
2559 return false;
2560 }
satok735cf382010-11-11 20:40:09 +09002561 }
2562 }
2563
satoke7c6998e2011-06-03 17:57:59 +09002564 @Override
satok688bd472012-02-09 20:09:17 +09002565 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002566 if (!calledFromValidUser()) {
2567 return false;
2568 }
satok688bd472012-02-09 20:09:17 +09002569 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002570 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002571 return false;
2572 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002573 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002574 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2575 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002576 if (nextSubtype == null) {
2577 return false;
2578 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002579 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2580 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002581 return true;
2582 }
2583 }
2584
2585 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002586 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2587 if (!calledFromValidUser()) {
2588 return false;
2589 }
2590 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002591 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002592 return false;
2593 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002594 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002595 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2596 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002597 if (nextSubtype == null) {
2598 return false;
2599 }
2600 return true;
2601 }
2602 }
2603
2604 @Override
satok68f1b782011-04-11 14:26:04 +09002605 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002606 if (!calledFromValidUser()) {
2607 return null;
2608 }
satok68f1b782011-04-11 14:26:04 +09002609 synchronized (mMethodMap) {
2610 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2611 // TODO: Handle the case of the last IME with no subtypes
2612 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2613 || TextUtils.isEmpty(lastIme.second)) return null;
2614 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2615 if (lastImi == null) return null;
2616 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002617 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002618 final int lastSubtypeId =
2619 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002620 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2621 return null;
2622 }
2623 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002624 } catch (NumberFormatException e) {
2625 return null;
2626 }
2627 }
2628 }
2629
satoke7c6998e2011-06-03 17:57:59 +09002630 @Override
satokee5e77c2011-09-02 18:50:15 +09002631 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002632 if (!calledFromValidUser()) {
2633 return;
2634 }
satok91e88122011-07-18 11:11:42 +09002635 // By this IPC call, only a process which shares the same uid with the IME can add
2636 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002637 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002638 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002639 if (!mSystemReady) {
2640 return;
2641 }
satok91e88122011-07-18 11:11:42 +09002642 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002643 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002644 final String[] packageInfos;
2645 try {
2646 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2647 } catch (RemoteException e) {
2648 Slog.e(TAG, "Failed to get package infos");
2649 return;
2650 }
satok91e88122011-07-18 11:11:42 +09002651 if (packageInfos != null) {
2652 final int packageNum = packageInfos.length;
2653 for (int i = 0; i < packageNum; ++i) {
2654 if (packageInfos[i].equals(imi.getPackageName())) {
2655 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002656 final long ident = Binder.clearCallingIdentity();
2657 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002658 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002659 } finally {
2660 Binder.restoreCallingIdentity(ident);
2661 }
satokee5e77c2011-09-02 18:50:15 +09002662 return;
satok91e88122011-07-18 11:11:42 +09002663 }
2664 }
2665 }
satoke7c6998e2011-06-03 17:57:59 +09002666 }
satokee5e77c2011-09-02 18:50:15 +09002667 return;
satoke7c6998e2011-06-03 17:57:59 +09002668 }
2669
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002670 @Override
2671 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002672 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002673 }
2674
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002675 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002676 public void clearLastInputMethodWindowForTransition(IBinder token) {
2677 if (!calledFromValidUser()) {
2678 return;
2679 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002680 synchronized (mMethodMap) {
2681 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002682 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002683 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002684 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002685 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002686 }
2687
2688 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002689 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002690 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002691 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002692 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002693 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002694 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2695 if (DEBUG) {
2696 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2697 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2698 + " actual: " + sequenceNumber);
2699 }
2700 return;
2701 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002702 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2703 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002704 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002705 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002706 }
2707 }
2708
satok28203512010-11-24 11:06:49 +09002709 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002711 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2712 }
2713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002715 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2716 if (token == null) {
2717 if (mContext.checkCallingOrSelfPermission(
2718 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2719 != PackageManager.PERMISSION_GRANTED) {
2720 throw new SecurityException(
2721 "Using null token requires permission "
2722 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002724 } else if (mCurToken != token) {
2725 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2726 + " token: " + token);
2727 return;
2728 }
2729
2730 final long ident = Binder.clearCallingIdentity();
2731 try {
2732 setInputMethodLocked(id, subtypeId);
2733 } finally {
2734 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
2736 }
2737
satok42c5a162011-05-26 16:46:14 +09002738 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002740 if (!calledFromValidUser()) {
2741 return;
2742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002744 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 return;
2746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 long ident = Binder.clearCallingIdentity();
2748 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002749 hideCurrentInputLocked(flags, null);
2750 } finally {
2751 Binder.restoreCallingIdentity(ident);
2752 }
2753 }
2754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002755
satok42c5a162011-05-26 16:46:14 +09002756 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002757 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002758 if (!calledFromValidUser()) {
2759 return;
2760 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002761 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002762 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002763 return;
2764 }
2765 long ident = Binder.clearCallingIdentity();
2766 try {
2767 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 } finally {
2769 Binder.restoreCallingIdentity(ident);
2770 }
2771 }
2772 }
2773
2774 void setEnabledSessionInMainThread(SessionState session) {
2775 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002776 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002778 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002779 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 } catch (RemoteException e) {
2781 }
2782 }
2783 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002784 if (mEnabledSession != null && mEnabledSession.session != null) {
2785 try {
2786 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2787 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2788 } catch (RemoteException e) {
2789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
2791 }
2792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002793
satok42c5a162011-05-26 16:46:14 +09002794 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002796 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002798 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002799 final boolean showAuxSubtypes;
2800 switch (msg.arg1) {
2801 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2802 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2803 // implemented so that auxiliary subtypes will be excluded when the soft
2804 // keyboard is invisible.
2805 showAuxSubtypes = mInputShown;
2806 break;
2807 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2808 showAuxSubtypes = true;
2809 break;
2810 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2811 showAuxSubtypes = false;
2812 break;
2813 default:
2814 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2815 return false;
2816 }
2817 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002818 return true;
2819
satok47a44912010-10-06 16:03:58 +09002820 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002821 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002822 return true;
2823
2824 case MSG_SHOW_IM_CONFIG:
2825 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002826 return true;
2827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 case MSG_UNBIND_INPUT:
2831 try {
2832 ((IInputMethod)msg.obj).unbindInput();
2833 } catch (RemoteException e) {
2834 // There is nothing interesting about the method dying.
2835 }
2836 return true;
2837 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002838 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 try {
2840 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2841 } catch (RemoteException e) {
2842 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002843 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 return true;
2845 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002846 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002848 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002849 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002850 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 } catch (RemoteException e) {
2852 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002853 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 return true;
2855 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002856 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002858 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002859 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002860 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 } catch (RemoteException e) {
2862 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002863 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002865 case MSG_HIDE_CURRENT_INPUT_METHOD:
2866 synchronized (mMethodMap) {
2867 hideCurrentInputLocked(0, null);
2868 }
2869 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002871 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002873 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2875 } catch (RemoteException e) {
2876 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002877 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002879 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002880 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002881 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002882 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002884 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002886 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002887 // Dispose the channel if the input method is not local to this process
2888 // because the remote proxy will get its own copy when unparceled.
2889 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002890 channel.dispose();
2891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002893 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002897
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002898 case MSG_START_INPUT: {
2899 int missingMethods = msg.arg1;
2900 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002902 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002904 session.method.startInput((IInputContext) args.arg2, missingMethods,
2905 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 } catch (RemoteException e) {
2907 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002908 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002910 }
2911 case MSG_RESTART_INPUT: {
2912 int missingMethods = msg.arg1;
2913 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002915 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002917 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2918 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 } catch (RemoteException e) {
2920 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002921 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002926
Yohei Yukawa33e81792015-11-17 21:14:42 -08002927 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002929 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 } catch (RemoteException e) {
2931 // There is nothing interesting about the last client dying.
2932 }
2933 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002934 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002935 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002936 IInputMethodClient client = (IInputMethodClient)args.arg1;
2937 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002939 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002941 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002942 } finally {
2943 // Dispose the channel if the input method is not local to this process
2944 // because the remote proxy will get its own copy when unparceled.
2945 if (res.channel != null && Binder.isProxy(client)) {
2946 res.channel.dispose();
2947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002949 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002951 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002952 case MSG_SET_ACTIVE:
2953 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002954 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002955 } catch (RemoteException e) {
2956 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2957 + ((ClientState)msg.obj).pid + " uid "
2958 + ((ClientState)msg.obj).uid);
2959 }
2960 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002961 case MSG_SET_INTERACTIVE:
2962 handleSetInteractive(msg.arg1 != 0);
2963 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002964 case MSG_SWITCH_IME:
2965 handleSwitchInputMethod(msg.arg1 != 0);
2966 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002967 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2968 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002969 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002970 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002971 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002972 } catch (RemoteException e) {
2973 Slog.w(TAG, "Got RemoteException sending "
2974 + "setUserActionNotificationSequenceNumber("
2975 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002976 + clientState.pid + " uid "
2977 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002978 }
2979 return true;
2980 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002981 case MSG_REPORT_FULLSCREEN_MODE: {
2982 final boolean fullscreen = msg.arg1 != 0;
2983 final ClientState clientState = (ClientState)msg.obj;
2984 try {
2985 clientState.client.reportFullscreenMode(fullscreen);
2986 } catch (RemoteException e) {
2987 Slog.w(TAG, "Got RemoteException sending "
2988 + "reportFullscreen(" + fullscreen + ") notification to pid="
2989 + clientState.pid + " uid=" + clientState.uid);
2990 }
2991 return true;
2992 }
satok01038492012-04-09 21:08:27 +09002993
2994 // --------------------------------------------------------------
2995 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002996 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002997 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002998 case MSG_SYSTEM_UNLOCK_USER:
2999 final int userId = msg.arg1;
3000 onUnlockUser(userId);
3001 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 }
3003 return false;
3004 }
3005
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003006 private void handleSetInteractive(final boolean interactive) {
3007 synchronized (mMethodMap) {
3008 mIsInteractive = interactive;
3009 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3010
3011 // Inform the current client of the change in active status
3012 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003013 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3014 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3015 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003016 }
3017 }
3018 }
3019
Yohei Yukawaae61f712015-12-09 13:00:10 -08003020 private void handleSwitchInputMethod(final boolean forwardDirection) {
3021 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003022 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003023 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003024 if (nextSubtype == null) {
3025 return;
3026 }
3027 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003028 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3029 if (newInputMethodInfo == null) {
3030 return;
3031 }
3032 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3033 newInputMethodInfo, mCurrentSubtype);
3034 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003035 if (mSubtypeSwitchedByShortCutToast == null) {
3036 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3037 Toast.LENGTH_SHORT);
3038 } else {
3039 mSubtypeSwitchedByShortCutToast.setText(toastText);
3040 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003041 mSubtypeSwitchedByShortCutToast.show();
3042 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003043 }
3044 }
3045
satokdc9ddae2011-10-06 12:22:36 +09003046 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003047 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3048 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003049 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003050 if (DEBUG) {
3051 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3052 }
satok723a27e2010-11-11 14:58:11 +09003053 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003054 return true;
3055 }
3056
3057 return false;
3058 }
3059
Yohei Yukawa94e33302016-02-12 19:37:03 -08003060 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003061 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003062 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003063 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003064 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003065 if (!mSystemReady) {
3066 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3067 return;
3068 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003069 mMethodList.clear();
3070 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003071 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003072 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003073
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003074 // Use for queryIntentServicesAsUser
3075 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003077 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3078 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3079 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003080 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003082 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3083 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003084
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003085 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003086 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 for (int i = 0; i < services.size(); ++i) {
3088 ResolveInfo ri = services.get(i);
3089 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003090 final String imeId = InputMethodInfo.computeId(ri);
3091 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3092 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 + ": it does not require the permission "
3094 + android.Manifest.permission.BIND_INPUT_METHOD);
3095 continue;
3096 }
3097
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003098 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003100 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 try {
satoke7c6998e2011-06-03 17:57:59 +09003102 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003103 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003104 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003105 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106
3107 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003108 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003110 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003111 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 }
3113 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003114
Yohei Yukawac4e44912017-02-09 19:30:22 -08003115 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3116 // negatives when the package state remains to be the same but only the component state is
3117 // changed.
3118 {
3119 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3120 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3121 // conservative, but it seems we cannot use it for now (Issue 35176630).
3122 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3123 new Intent(InputMethod.SERVICE_INTERFACE),
3124 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3125 final int N = allInputMethodServices.size();
3126 for (int i = 0; i < N; ++i) {
3127 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3128 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3129 continue;
3130 }
3131 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3132 }
3133 }
3134
Yohei Yukawa859df052016-02-17 07:56:46 -08003135 // TODO: The following code should find better place to live.
3136 if (!resetDefaultEnabledIme) {
3137 boolean enabledImeFound = false;
3138 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3139 final int N = enabledImes.size();
3140 for (int i = 0; i < N; ++i) {
3141 final InputMethodInfo imi = enabledImes.get(i);
3142 if (mMethodList.contains(imi)) {
3143 enabledImeFound = true;
3144 break;
3145 }
3146 }
3147 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003148 if (DEBUG) {
3149 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3150 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003151 resetDefaultEnabledIme = true;
3152 resetSelectedInputMethodAndSubtypeLocked("");
3153 }
3154 }
3155
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003156 if (resetDefaultEnabledIme) {
3157 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003158 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003159 final int N = defaultEnabledIme.size();
3160 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003161 final InputMethodInfo imi = defaultEnabledIme.get(i);
3162 if (DEBUG) {
3163 Slog.d(TAG, "--- enable ime = " + imi);
3164 }
3165 setInputMethodEnabledLocked(imi.getId(), true);
3166 }
3167 }
3168
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003169 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003170 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003171 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003172 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3173 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003174 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003175 }
3176 } else {
3177 // Double check that the default IME is certainly enabled.
3178 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003179 }
3180 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003181 // Here is not the perfect place to reset the switching controller. Ideally
3182 // mSwitchingController and mSettings should be able to share the same state.
3183 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3184 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003185 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003189
satok217f5482010-12-15 05:19:19 +09003190 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003191 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003192 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003193 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3194 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003195 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003196 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003197 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003198 final int userId;
3199 synchronized (mMethodMap) {
3200 userId = mSettings.getCurrentUserId();
3201 }
3202 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003203 }
3204
3205 private void showConfigureInputMethods() {
3206 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3207 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3208 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3209 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003210 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003211 }
3212
satok2c93efc2012-04-02 19:33:47 +09003213 private boolean isScreenLocked() {
3214 return mKeyguardManager != null
3215 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3216 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003217
Seigo Nonaka14e13912015-05-06 21:04:13 -07003218 private void showInputMethodMenu(boolean showAuxSubtypes) {
3219 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003222 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003223
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003224 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003225 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003226 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003227
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003228 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003229 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003230 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3231 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003232 if (immis == null || immis.size() == 0) {
3233 return;
3234 }
3235
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003236 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237
satok688bd472012-02-09 20:09:17 +09003238 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003239 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003240 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003241
satokc3690562012-01-10 20:14:43 +09003242 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003243 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003244 if (currentSubtype != null) {
3245 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003246 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3247 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003248 }
3249 }
3250
Ken Wakasa761eb372011-03-04 19:06:18 +09003251 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003252 mIms = new InputMethodInfo[N];
3253 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003254 int checkedItem = 0;
3255 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003256 final ImeSubtypeListItem item = imList.get(i);
3257 mIms[i] = item.mImi;
3258 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003259 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003260 int subtypeId = mSubtypeIds[i];
3261 if ((subtypeId == NOT_A_SUBTYPE_ID)
3262 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3263 || (subtypeId == lastInputMethodSubtypeId)) {
3264 checkedItem = i;
3265 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003268
3269 final Context settingsContext = new ContextThemeWrapper(context,
3270 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3271
3272 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003273 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3274 @Override
3275 public void onCancel(DialogInterface dialog) {
3276 hideInputMethodMenu();
3277 }
3278 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003279
3280 final Context dialogContext = mDialogBuilder.getContext();
3281 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3282 com.android.internal.R.styleable.DialogPreference,
3283 com.android.internal.R.attr.alertDialogStyle, 0);
3284 final Drawable dialogIcon = a.getDrawable(
3285 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3286 a.recycle();
3287
3288 mDialogBuilder.setIcon(dialogIcon);
3289
Yohei Yukawad34e1482016-02-11 08:03:52 -08003290 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003291 final View tv = inflater.inflate(
3292 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3293 mDialogBuilder.setCustomTitle(tv);
3294
3295 // Setup layout for a toggle switch of the hardware keyboard
3296 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003297 mSwitchingDialogTitleView
3298 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003299 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003300 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003301 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003302 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003303 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003304 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3305 @Override
3306 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003307 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003308 // Ensure that the input method dialog is dismissed when changing
3309 // the hardware keyboard state.
3310 hideInputMethodMenu();
3311 }
3312 });
3313
Alan Viverette505e3ab2014-11-24 15:22:11 -08003314 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003315 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3316 final OnClickListener choiceListener = new OnClickListener() {
3317 @Override
3318 public void onClick(final DialogInterface dialog, final int which) {
3319 synchronized (mMethodMap) {
3320 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3321 || mSubtypeIds.length <= which) {
3322 return;
satok01038492012-04-09 21:08:27 +09003323 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003324 final InputMethodInfo im = mIms[which];
3325 int subtypeId = mSubtypeIds[which];
3326 adapter.mCheckedItem = which;
3327 adapter.notifyDataSetChanged();
3328 hideInputMethodMenu();
3329 if (im != null) {
3330 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3331 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003332 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003333 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003335 }
3336 }
3337 };
3338 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003341 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003342 final Window w = mSwitchingDialog.getWindow();
3343 final WindowManager.LayoutParams attrs = w.getAttributes();
3344 w.setType(TYPE_INPUT_METHOD_DIALOG);
3345 // Use an alternate token for the dialog for that window manager can group the token
3346 // with other IME windows based on type vs. grouping based on whichever token happens
3347 // to get selected by the system later on.
3348 attrs.token = mSwitchingDialogToken;
3349 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3350 attrs.setTitle("Select input method");
3351 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003352 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 mSwitchingDialog.show();
3354 }
3355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003356
Ken Wakasa05dbb652011-08-22 15:22:43 +09003357 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3358 private final LayoutInflater mInflater;
3359 private final int mTextViewResourceId;
3360 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003361 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003362 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3363 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3364 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003365
Ken Wakasa05dbb652011-08-22 15:22:43 +09003366 mTextViewResourceId = textViewResourceId;
3367 mItemsList = itemsList;
3368 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003369 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003370 }
3371
3372 @Override
3373 public View getView(int position, View convertView, ViewGroup parent) {
3374 final View view = convertView != null ? convertView
3375 : mInflater.inflate(mTextViewResourceId, null);
3376 if (position < 0 || position >= mItemsList.size()) return view;
3377 final ImeSubtypeListItem item = mItemsList.get(position);
3378 final CharSequence imeName = item.mImeName;
3379 final CharSequence subtypeName = item.mSubtypeName;
3380 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3381 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3382 if (TextUtils.isEmpty(subtypeName)) {
3383 firstTextView.setText(imeName);
3384 secondTextView.setVisibility(View.GONE);
3385 } else {
3386 firstTextView.setText(subtypeName);
3387 secondTextView.setText(imeName);
3388 secondTextView.setVisibility(View.VISIBLE);
3389 }
3390 final RadioButton radioButton =
3391 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3392 radioButton.setChecked(position == mCheckedItem);
3393 return view;
3394 }
3395 }
3396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003398 synchronized (mMethodMap) {
3399 hideInputMethodMenuLocked();
3400 }
3401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003402
The Android Open Source Project10592532009-03-18 17:39:46 -07003403 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003404 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405
The Android Open Source Project10592532009-03-18 17:39:46 -07003406 if (mSwitchingDialog != null) {
3407 mSwitchingDialog.dismiss();
3408 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003410
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003411 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003412 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003413 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003417
satok42c5a162011-05-26 16:46:14 +09003418 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003420 // TODO: Make this work even for non-current users?
3421 if (!calledFromValidUser()) {
3422 return false;
3423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 synchronized (mMethodMap) {
3425 if (mContext.checkCallingOrSelfPermission(
3426 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3427 != PackageManager.PERMISSION_GRANTED) {
3428 throw new SecurityException(
3429 "Requires permission "
3430 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3431 }
Anna Galusza9b278112016-01-04 11:37:37 -08003432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 long ident = Binder.clearCallingIdentity();
3434 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003435 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 } finally {
3437 Binder.restoreCallingIdentity(ident);
3438 }
3439 }
3440 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003441
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003442 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3443 // Make sure this is a valid input method.
3444 InputMethodInfo imm = mMethodMap.get(id);
3445 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003446 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003447 }
3448
satokd87c2592010-09-29 11:52:06 +09003449 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3450 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003451
satokd87c2592010-09-29 11:52:06 +09003452 if (enabled) {
3453 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3454 if (pair.first.equals(id)) {
3455 // We are enabling this input method, but it is already enabled.
3456 // Nothing to do. The previous state was enabled.
3457 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003458 }
3459 }
satokd87c2592010-09-29 11:52:06 +09003460 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3461 // Previous state was disabled.
3462 return false;
3463 } else {
3464 StringBuilder builder = new StringBuilder();
3465 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3466 builder, enabledInputMethodsList, id)) {
3467 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003468 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003469 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3470 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3471 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003472 }
3473 // Previous state was enabled.
3474 return true;
3475 } else {
3476 // We are disabling the input method but it is already disabled.
3477 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003478 return false;
3479 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003480 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003481 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003482
satok723a27e2010-11-11 14:58:11 +09003483 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3484 boolean setSubtypeOnly) {
3485 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003486 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003487
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003488 mCurUserActionNotificationSequenceNumber =
3489 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3490 if (DEBUG) {
3491 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3492 + mCurUserActionNotificationSequenceNumber);
3493 }
3494
3495 if (mCurClient != null && mCurClient.client != null) {
3496 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3497 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003498 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003499 }
3500
satok723a27e2010-11-11 14:58:11 +09003501 // Set Subtype here
3502 if (imi == null || subtypeId < 0) {
3503 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003504 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003505 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003506 if (subtypeId < imi.getSubtypeCount()) {
3507 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3508 mSettings.putSelectedSubtype(subtype.hashCode());
3509 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003510 } else {
3511 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003512 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003513 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003514 }
satokab751aa2010-09-14 19:17:36 +09003515 }
satok723a27e2010-11-11 14:58:11 +09003516
Yohei Yukawa68645a62016-02-17 07:54:20 -08003517 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003518 // Set InputMethod here
3519 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3520 }
3521 }
3522
3523 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3524 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3525 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3526 // newDefaultIme is empty when there is no candidate for the selected IME.
3527 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3528 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3529 if (subtypeHashCode != null) {
3530 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003531 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003532 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003533 } catch (NumberFormatException e) {
3534 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3535 }
3536 }
3537 }
3538 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003539 }
3540
satok4e4569d2010-11-19 18:45:53 +09003541 // If there are no selected shortcuts, tries finding the most applicable ones.
3542 private Pair<InputMethodInfo, InputMethodSubtype>
3543 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3544 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3545 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003546 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003547 boolean foundInSystemIME = false;
3548
3549 // Search applicable subtype for each InputMethodInfo
3550 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003551 final String imiId = imi.getId();
3552 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3553 continue;
3554 }
satokcd7cd292010-11-20 15:46:23 +09003555 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003556 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003557 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003558 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003559 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003560 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003561 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003562 }
satokdf31ae62011-01-15 06:19:44 +09003563 // 2. Search by the system locale from enabledSubtypes.
3564 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003565 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003566 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003567 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003568 }
satoka86f5e42011-09-02 17:12:42 +09003569 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003570 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003571 final ArrayList<InputMethodSubtype> subtypesForSearch =
3572 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003573 ? InputMethodUtils.getSubtypes(imi)
3574 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003575 // 4. Search by the current subtype's locale from all subtypes.
3576 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003577 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003578 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003579 }
3580 // 5. Search by the system locale from all subtypes.
3581 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003582 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003583 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003584 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003585 }
satokcd7cd292010-11-20 15:46:23 +09003586 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003587 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003588 // The current input method is the most applicable IME.
3589 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003590 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003591 break;
satok7599a7f2010-12-22 13:45:23 +09003592 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003593 // The system input method is 2nd applicable IME.
3594 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003595 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003596 if ((imi.getServiceInfo().applicationInfo.flags
3597 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3598 foundInSystemIME = true;
3599 }
satok4e4569d2010-11-19 18:45:53 +09003600 }
3601 }
3602 }
3603 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003604 if (mostApplicableIMI != null) {
3605 Slog.w(TAG, "Most applicable shortcut input method was:"
3606 + mostApplicableIMI.getId());
3607 if (mostApplicableSubtype != null) {
3608 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3609 + "," + mostApplicableSubtype.getMode() + ","
3610 + mostApplicableSubtype.getLocale());
3611 }
3612 }
satok4e4569d2010-11-19 18:45:53 +09003613 }
satokcd7cd292010-11-20 15:46:23 +09003614 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003615 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003616 } else {
3617 return null;
3618 }
3619 }
3620
satokab751aa2010-09-14 19:17:36 +09003621 /**
3622 * @return Return the current subtype of this input method.
3623 */
satok42c5a162011-05-26 16:46:14 +09003624 @Override
satokab751aa2010-09-14 19:17:36 +09003625 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003626 // TODO: Make this work even for non-current users?
3627 if (!calledFromValidUser()) {
3628 return null;
3629 }
3630 synchronized (mMethodMap) {
3631 return getCurrentInputMethodSubtypeLocked();
3632 }
3633 }
3634
3635 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003636 if (mCurMethodId == null) {
3637 return null;
3638 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003639 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003640 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3641 if (imi == null || imi.getSubtypeCount() == 0) {
3642 return null;
satok4e4569d2010-11-19 18:45:53 +09003643 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003644 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003645 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3646 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003647 if (subtypeId == NOT_A_SUBTYPE_ID) {
3648 // If there are no selected subtypes, the framework will try to find
3649 // the most applicable subtype from explicitly or implicitly enabled
3650 // subtypes.
3651 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003652 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003653 // If there is only one explicitly or implicitly enabled subtype,
3654 // just returns it.
3655 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3656 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3657 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003658 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003659 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003660 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003661 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003662 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003663 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3664 true);
satok4e4569d2010-11-19 18:45:53 +09003665 }
satok3ef8b292010-11-23 06:06:29 +09003666 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003667 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003668 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003669 }
3670 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003671 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003672 }
3673
satok4e4569d2010-11-19 18:45:53 +09003674 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003675 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003676 @Override
satok4e4569d2010-11-19 18:45:53 +09003677 public List getShortcutInputMethodsAndSubtypes() {
3678 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003679 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003680 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003681 // If there are no selected shortcut subtypes, the framework will try to find
3682 // the most applicable subtype from all subtypes whose mode is
3683 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003684 Pair<InputMethodInfo, InputMethodSubtype> info =
3685 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003686 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003687 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003688 ret.add(info.first);
3689 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003690 }
satok3da92232011-01-11 22:46:30 +09003691 return ret;
satokf3db1af2010-11-23 13:34:33 +09003692 }
satokf3db1af2010-11-23 13:34:33 +09003693 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3694 ret.add(imi);
3695 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3696 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003697 }
3698 }
satokf3db1af2010-11-23 13:34:33 +09003699 return ret;
satok4e4569d2010-11-19 18:45:53 +09003700 }
3701 }
3702
satok42c5a162011-05-26 16:46:14 +09003703 @Override
satokb66d2872010-11-10 01:04:04 +09003704 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003705 // TODO: Make this work even for non-current users?
3706 if (!calledFromValidUser()) {
3707 return false;
3708 }
satokb66d2872010-11-10 01:04:04 +09003709 synchronized (mMethodMap) {
3710 if (subtype != null && mCurMethodId != null) {
3711 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003712 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003713 if (subtypeId != NOT_A_SUBTYPE_ID) {
3714 setInputMethodLocked(mCurMethodId, subtypeId);
3715 return true;
3716 }
3717 }
3718 return false;
3719 }
3720 }
3721
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003722 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003723 private static class InputMethodFileManager {
3724 private static final String SYSTEM_PATH = "system";
3725 private static final String INPUT_METHOD_PATH = "inputmethod";
3726 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3727 private static final String NODE_SUBTYPES = "subtypes";
3728 private static final String NODE_SUBTYPE = "subtype";
3729 private static final String NODE_IMI = "imi";
3730 private static final String ATTR_ID = "id";
3731 private static final String ATTR_LABEL = "label";
3732 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003733 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003734 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003735 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003736 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3737 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3738 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003739 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003740 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3741 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003742 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003743 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003744 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003745 if (methodMap == null) {
3746 throw new NullPointerException("methodMap is null");
3747 }
3748 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003749 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003750 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3751 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003752 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003753 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003754 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3755 }
3756 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3757 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3758 if (!subtypeFile.exists()) {
3759 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003760 writeAdditionalInputMethodSubtypes(
3761 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003762 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003763 readAdditionalInputMethodSubtypes(
3764 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003765 }
3766 }
3767
3768 private void deleteAllInputMethodSubtypes(String imiId) {
3769 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003770 mAdditionalSubtypesMap.remove(imiId);
3771 writeAdditionalInputMethodSubtypes(
3772 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003773 }
3774 }
3775
3776 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003777 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003778 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003779 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003780 final int N = additionalSubtypes.length;
3781 for (int i = 0; i < N; ++i) {
3782 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003783 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003784 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003785 } else {
3786 Slog.w(TAG, "Duplicated subtype definition found: "
3787 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003788 }
3789 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003790 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3791 writeAdditionalInputMethodSubtypes(
3792 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003793 }
3794 }
3795
3796 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3797 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003798 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003799 }
3800 }
3801
3802 private static void writeAdditionalInputMethodSubtypes(
3803 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3804 HashMap<String, InputMethodInfo> methodMap) {
3805 // Safety net for the case that this function is called before methodMap is set.
3806 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3807 FileOutputStream fos = null;
3808 try {
3809 fos = subtypesFile.startWrite();
3810 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003811 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003812 out.startDocument(null, true);
3813 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3814 out.startTag(null, NODE_SUBTYPES);
3815 for (String imiId : allSubtypes.keySet()) {
3816 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3817 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3818 continue;
3819 }
3820 out.startTag(null, NODE_IMI);
3821 out.attribute(null, ATTR_ID, imiId);
3822 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3823 final int N = subtypesList.size();
3824 for (int i = 0; i < N; ++i) {
3825 final InputMethodSubtype subtype = subtypesList.get(i);
3826 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003827 if (subtype.hasSubtypeId()) {
3828 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3829 String.valueOf(subtype.getSubtypeId()));
3830 }
satoke7c6998e2011-06-03 17:57:59 +09003831 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3832 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3833 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003834 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3835 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003836 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3837 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3838 out.attribute(null, ATTR_IS_AUXILIARY,
3839 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003840 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3841 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003842 out.endTag(null, NODE_SUBTYPE);
3843 }
3844 out.endTag(null, NODE_IMI);
3845 }
3846 out.endTag(null, NODE_SUBTYPES);
3847 out.endDocument();
3848 subtypesFile.finishWrite(fos);
3849 } catch (java.io.IOException e) {
3850 Slog.w(TAG, "Error writing subtypes", e);
3851 if (fos != null) {
3852 subtypesFile.failWrite(fos);
3853 }
3854 }
3855 }
3856
3857 private static void readAdditionalInputMethodSubtypes(
3858 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3859 if (allSubtypes == null || subtypesFile == null) return;
3860 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003861 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003862 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003863 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003864 int type = parser.getEventType();
3865 // Skip parsing until START_TAG
3866 while ((type = parser.next()) != XmlPullParser.START_TAG
3867 && type != XmlPullParser.END_DOCUMENT) {}
3868 String firstNodeName = parser.getName();
3869 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3870 throw new XmlPullParserException("Xml doesn't start with subtypes");
3871 }
3872 final int depth =parser.getDepth();
3873 String currentImiId = null;
3874 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3875 while (((type = parser.next()) != XmlPullParser.END_TAG
3876 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3877 if (type != XmlPullParser.START_TAG)
3878 continue;
3879 final String nodeName = parser.getName();
3880 if (NODE_IMI.equals(nodeName)) {
3881 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3882 if (TextUtils.isEmpty(currentImiId)) {
3883 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3884 continue;
3885 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003886 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003887 allSubtypes.put(currentImiId, tempSubtypesArray);
3888 } else if (NODE_SUBTYPE.equals(nodeName)) {
3889 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3890 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3891 continue;
3892 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003893 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003894 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003895 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003896 parser.getAttributeValue(null, ATTR_LABEL));
3897 final String imeSubtypeLocale =
3898 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003899 final String languageTag =
3900 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003901 final String imeSubtypeMode =
3902 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3903 final String imeSubtypeExtraValue =
3904 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003905 final boolean isAuxiliary = "1".equals(String.valueOf(
3906 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003907 final boolean isAsciiCapable = "1".equals(String.valueOf(
3908 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003909 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003910 .setSubtypeNameResId(label)
3911 .setSubtypeIconResId(icon)
3912 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003913 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003914 .setSubtypeMode(imeSubtypeMode)
3915 .setSubtypeExtraValue(imeSubtypeExtraValue)
3916 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003917 .setIsAsciiCapable(isAsciiCapable);
3918 final String subtypeIdString =
3919 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3920 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003921 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003922 }
3923 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003924 }
3925 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003926 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3927 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003928 return;
satoke7c6998e2011-06-03 17:57:59 +09003929 }
3930 }
3931 }
3932
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003933 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3934 @NonNull
3935 private final Handler mHandler;
3936
3937 LocalServiceImpl(@NonNull final Handler handler) {
3938 mHandler = handler;
3939 }
3940
3941 @Override
3942 public void setInteractive(boolean interactive) {
3943 // Do everything in handler so as not to block the caller.
3944 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3945 interactive ? 1 : 0, 0));
3946 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003947
3948 @Override
3949 public void switchInputMethod(boolean forwardDirection) {
3950 // Do everything in handler so as not to block the caller.
3951 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3952 forwardDirection ? 1 : 0, 0));
3953 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003954
3955 @Override
3956 public void hideCurrentInputMethod() {
3957 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3958 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3959 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003960 }
3961
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003962 private static String imeWindowStatusToString(final int imeWindowVis) {
3963 final StringBuilder sb = new StringBuilder();
3964 boolean first = true;
3965 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3966 sb.append("Active");
3967 first = false;
3968 }
3969 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3970 if (!first) {
3971 sb.append("|");
3972 }
3973 sb.append("Visible");
3974 }
3975 return sb.toString();
3976 }
3977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003979 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3980 @Nullable Uri contentUri, @Nullable String packageName) {
3981 if (!calledFromValidUser()) {
3982 return null;
3983 }
3984
3985 if (token == null) {
3986 throw new NullPointerException("token");
3987 }
3988 if (packageName == null) {
3989 throw new NullPointerException("packageName");
3990 }
3991 if (contentUri == null) {
3992 throw new NullPointerException("contentUri");
3993 }
3994 final String contentUriScheme = contentUri.getScheme();
3995 if (!"content".equals(contentUriScheme)) {
3996 throw new InvalidParameterException("contentUri must have content scheme");
3997 }
3998
3999 synchronized (mMethodMap) {
4000 final int uid = Binder.getCallingUid();
4001 if (mCurMethodId == null) {
4002 return null;
4003 }
4004 if (mCurToken != token) {
4005 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4006 + " token=" + token);
4007 return null;
4008 }
4009 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4010 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4011 // nature of our system. Let's compare it with our internal record.
4012 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4013 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4014 + mCurAttribute.packageName + " packageName=" + packageName);
4015 return null;
4016 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004017 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004018 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004019 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004020 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004021 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4022 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4023 imeUserId);
4024 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4025 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4026 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4027 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4028 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4029 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4030 // actually allowed to "uid", which is guaranteed to be the IME's one.
4031 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4032 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004033 }
4034 }
4035
4036 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004037 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4038 if (!calledFromValidUser()) {
4039 return;
4040 }
4041 synchronized (mMethodMap) {
4042 if (!calledWithValidToken(token)) {
4043 return;
4044 }
4045 if (mCurClient != null && mCurClient.client != null) {
4046 mInFullscreenMode = fullscreen;
4047 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4048 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4049 }
4050 }
4051 }
4052
4053 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4055 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4056 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4059 + Binder.getCallingPid()
4060 + ", uid=" + Binder.getCallingUid());
4061 return;
4062 }
4063
4064 IInputMethod method;
4065 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004066 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 synchronized (mMethodMap) {
4071 p.println("Current Input Method Manager state:");
4072 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004073 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 for (int i=0; i<N; i++) {
4075 InputMethodInfo info = mMethodList.get(i);
4076 p.println(" InputMethod #" + i + ":");
4077 info.dump(p, " ");
4078 }
4079 p.println(" Clients:");
4080 for (ClientState ci : mClients.values()) {
4081 p.println(" Client " + ci + ":");
4082 p.println(" client=" + ci.client);
4083 p.println(" inputContext=" + ci.inputContext);
4084 p.println(" sessionRequested=" + ci.sessionRequested);
4085 p.println(" curSession=" + ci.curSession);
4086 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004087 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004089 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4090 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004091 focusedWindowClient = mCurFocusedWindowClient;
4092 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4094 + " mBoundToMethod=" + mBoundToMethod);
4095 p.println(" mCurToken=" + mCurToken);
4096 p.println(" mCurIntent=" + mCurIntent);
4097 method = mCurMethod;
4098 p.println(" mCurMethod=" + mCurMethod);
4099 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004100 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 p.println(" mShowRequested=" + mShowRequested
4102 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4103 + " mShowForced=" + mShowForced
4104 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004105 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004106 p.println(" mCurUserActionNotificationSequenceNumber="
4107 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004108 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004109 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004110 p.println(" mSwitchingController:");
4111 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004112 p.println(" mSettings:");
4113 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004115
Jeff Brownb88102f2010-09-08 11:49:43 -07004116 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 pw.flush();
4119 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004120 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4121 } catch (IOException | RemoteException e) {
4122 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004124 } else {
4125 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004127
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004128 if (focusedWindowClient != null && client != focusedWindowClient) {
4129 p.println(" ");
4130 p.println("Warning: Current input method client doesn't match the last focused. "
4131 + "window.");
4132 p.println("Dumping input method client in the last focused window just in case.");
4133 p.println(" ");
4134 pw.flush();
4135 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004136 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4137 } catch (IOException | RemoteException e) {
4138 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004139 }
4140 }
4141
Jeff Brownb88102f2010-09-08 11:49:43 -07004142 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 pw.flush();
4145 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004146 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4147 } catch (IOException | RemoteException e) {
4148 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004150 } else {
4151 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
4153 }
4154}