blob: 121bc3c02d523b5a25c725e19a057a07b5c688c6 [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)) {
603 synchronized (mMethodMap) {
604 resetStateIfCurrentLocaleChangedLocked();
605 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900606 } else {
607 Slog.w(TAG, "Unexpected intent " + intent);
608 }
609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800611
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700612 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
613 // does not attempt to validate on the fly with any installed device policy, so must only
614 // be run in the context of initial device setup.
615 //
616 // TODO: Move this method to InputMethodUtils with adding unit tests.
617 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
618 if (DEBUG_RESTORE) {
619 Slog.i(TAG, "Restoring enabled input methods:");
620 Slog.i(TAG, "prev=" + prevValue);
621 Slog.i(TAG, " new=" + newValue);
622 }
623 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900624 ArrayMap<String, ArraySet<String>> prevMap =
625 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
626 ArrayMap<String, ArraySet<String>> newMap =
627 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700628
629 // Merge the restored ime+subtype enabled states into the live state
630 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
631 final String imeId = entry.getKey();
632 ArraySet<String> prevSubtypes = prevMap.get(imeId);
633 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700634 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700635 prevMap.put(imeId, prevSubtypes);
636 }
637 prevSubtypes.addAll(entry.getValue());
638 }
639
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700640 final String mergedImesAndSubtypesString =
641 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700642 if (DEBUG_RESTORE) {
643 Slog.i(TAG, "Merged IME string:");
644 Slog.i(TAG, " " + mergedImesAndSubtypesString);
645 }
646 Settings.Secure.putString(context.getContentResolver(),
647 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
648 }
649
Yohei Yukawac4e44912017-02-09 19:30:22 -0800650 final class MyPackageMonitor extends PackageMonitor {
651 /**
652 * Set of packages to be monitored.
653 *
654 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
655 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
656 * dynamically unless the entire package is updated, which also always triggers package
657 * rescanning.</p>
658 */
659 @GuardedBy("mMethodMap")
660 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
661
662 @GuardedBy("mMethodMap")
663 void clearPackagesToMonitorComponentChangeLocked() {
664 mPackagesToMonitorComponentChange.clear();
665 }
666
667 @GuardedBy("mMethodMap")
668 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
669 mPackagesToMonitorComponentChange.add(packageName);
670 }
671
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900672 private boolean isChangingPackagesOfCurrentUser() {
673 final int userId = getChangingUserId();
674 final boolean retval = userId == mSettings.getCurrentUserId();
675 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900676 if (!retval) {
677 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
678 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900679 }
680 return retval;
681 }
682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800684 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900685 if (!isChangingPackagesOfCurrentUser()) {
686 return false;
687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900689 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 final int N = mMethodList.size();
691 if (curInputMethodId != null) {
692 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800693 InputMethodInfo imi = mMethodList.get(i);
694 if (imi.getId().equals(curInputMethodId)) {
695 for (String pkg : packages) {
696 if (imi.getPackageName().equals(pkg)) {
697 if (!doit) {
698 return true;
699 }
satok723a27e2010-11-11 14:58:11 +0900700 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800701 chooseNewDefaultIMELocked();
702 return true;
703 }
704 }
705 }
706 }
707 }
708 }
709 return false;
710 }
711
712 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800713 public boolean onPackageChanged(String packageName, int uid, String[] components) {
714 // If this package is in the watch list, we want to check it.
715 synchronized (mMethodMap) {
716 return mPackagesToMonitorComponentChange.contains(packageName);
717 }
718 }
719
720 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800721 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900722 if (!isChangingPackagesOfCurrentUser()) {
723 return;
724 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800725 synchronized (mMethodMap) {
726 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900727 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800728 final int N = mMethodList.size();
729 if (curInputMethodId != null) {
730 for (int i=0; i<N; i++) {
731 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900732 final String imiId = imi.getId();
733 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800734 curIm = imi;
735 }
satoke7c6998e2011-06-03 17:57:59 +0900736
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800737 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900738 if (isPackageModified(imi.getPackageName())) {
739 mFileManager.deleteAllInputMethodSubtypes(imiId);
740 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800741 if (change == PACKAGE_TEMPORARY_CHANGE
742 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800743 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800744 + imi.getComponent());
745 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
Yohei Yukawa94e33302016-02-12 19:37:03 -0800750 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800754 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800755 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800756 if (change == PACKAGE_TEMPORARY_CHANGE
757 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800758 ServiceInfo si = null;
759 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900760 si = mIPackageManager.getServiceInfo(
761 curIm.getComponent(), 0, mSettings.getCurrentUserId());
762 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800763 }
764 if (si == null) {
765 // Uh oh, current input method is no longer around!
766 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800767 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900768 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800769 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800770 changed = true;
771 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800772 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900773 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800774 }
775 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800776 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800777 }
satokab751aa2010-09-14 19:17:36 +0900778
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800779 if (curIm == null) {
780 // We currently don't have a default input method... is
781 // one now available?
782 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700783 } else if (!changed && isPackageModified(curIm.getPackageName())) {
784 // Even if the current input method is still available, mCurrentSubtype could
785 // be obsolete when the package is modified in practice.
786 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800789 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800790 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792 }
793 }
794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800795
Jeff Brownc28867a2013-03-26 15:42:39 -0700796 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900797 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700798 private final IInputMethod mMethod;
799 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800800
Jeff Brownc28867a2013-03-26 15:42:39 -0700801 MethodCallback(InputMethodManagerService imms, IInputMethod method,
802 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900803 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700804 mMethod = method;
805 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807
satoke7c6998e2011-06-03 17:57:59 +0900808 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700809 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700810 long ident = Binder.clearCallingIdentity();
811 try {
812 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
813 } finally {
814 Binder.restoreCallingIdentity(ident);
815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800818
satok01038492012-04-09 21:08:27 +0900819 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700820 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900821 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700822 public void onHardKeyboardStatusChange(boolean available) {
823 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
824 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900825 }
826
Michael Wright7b5a96b2014-08-09 19:28:42 -0700827 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900828 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700829 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900830 }
831 synchronized(mMethodMap) {
832 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
833 && mSwitchingDialog.isShowing()) {
834 mSwitchingDialogTitleView.findViewById(
835 com.android.internal.R.id.hard_keyboard_section).setVisibility(
836 available ? View.VISIBLE : View.GONE);
837 }
838 }
839 }
840 }
841
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800842 public static final class Lifecycle extends SystemService {
843 private InputMethodManagerService mService;
844
845 public Lifecycle(Context context) {
846 super(context);
847 mService = new InputMethodManagerService(context);
848 }
849
850 @Override
851 public void onStart() {
852 LocalServices.addService(InputMethodManagerInternal.class,
853 new LocalServiceImpl(mService.mHandler));
854 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
855 }
856
857 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800858 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700859 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800860 // TODO: Dispatch this to a worker thread as needed.
861 mService.onSwitchUser(userHandle);
862 }
863
864 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800865 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700866 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800867 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800868 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
869 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
870 .getService(Context.STATUS_BAR_SERVICE);
871 mService.systemRunning(statusBarService);
872 }
873 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800874
875 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700876 public void onUnlockUser(final @UserIdInt int userHandle) {
877 // Called on ActivityManager thread.
878 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700879 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800880 }
881 }
882
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800883 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800884 synchronized(mMethodMap) {
885 final int currentUserId = mSettings.getCurrentUserId();
886 if (DEBUG) {
887 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
888 }
889 if (userId != currentUserId) {
890 return;
891 }
892 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800893 if (mSystemReady) {
894 // We need to rebuild IMEs.
895 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
896 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
897 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800898 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800899 }
900
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800901 void onSwitchUser(@UserIdInt int userId) {
902 synchronized (mMethodMap) {
903 switchUserLocked(userId);
904 }
905 }
906
Seigo Nonaka7309b122015-08-17 18:34:13 -0700907 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800910 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700912 // Note: SettingsObserver doesn't register observers in its constructor.
913 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 mIWindowManager = IWindowManager.Stub.asInterface(
915 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700916 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800917 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900918 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 public void executeMessage(Message msg) {
920 handleMessage(msg);
921 }
Mita Yuned218c72012-12-06 17:18:25 -0800922 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800923 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800924 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900925 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700926 mHasFeature = context.getPackageManager().hasSystemFeature(
927 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500928 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700929 mHardKeyboardBehavior = mContext.getResources().getInteger(
930 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900931
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400932 Bundle extras = new Bundle();
933 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
934 mImeSwitcherNotification = new Notification.Builder(mContext)
935 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
936 .setWhen(0)
937 .setOngoing(true)
938 .addExtras(extras)
939 .setCategory(Notification.CATEGORY_SYSTEM)
940 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400941
satok7cfc0ed2011-06-20 21:29:36 +0900942 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900943 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900944
945 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900946
satok7cfc0ed2011-06-20 21:29:36 +0900947 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 int userId = 0;
949 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800950 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900951 } catch (RemoteException e) {
952 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
953 }
satok913a8922010-08-26 21:53:41 +0900954
satokd87c2592010-09-29 11:52:06 +0900955 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900956 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800957 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700958
Kenny Guy2a764942014-04-02 13:29:20 +0100959 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900960 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800961 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
962 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900963 }
964
satok5b927c432012-05-01 20:09:34 +0900965 private void resetDefaultImeLocked(Context context) {
966 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800967 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900968 return;
969 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800970 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800971 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800972 if (suitableImes.isEmpty()) {
973 Slog.i(TAG, "No default found");
974 return;
satok5b927c432012-05-01 20:09:34 +0900975 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800976 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -0800977 if (DEBUG) {
978 Slog.i(TAG, "Default found, using " + defIm.getId());
979 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800980 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900981 }
982
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900983 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
984 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 if (!mSystemReady) {
986 // not system ready
987 return;
988 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800989 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800991 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900992 if (!updateOnlyWhenLocaleChanged) {
993 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800994 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900995 }
996 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800997 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800999 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001000 if (!updateOnlyWhenLocaleChanged) {
1001 final String selectedImiId = mSettings.getSelectedInputMethod();
1002 if (TextUtils.isEmpty(selectedImiId)) {
1003 // This is the first time of the user switch and
1004 // set the current ime to the proper one.
1005 resetDefaultImeLocked(mContext);
1006 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001007 } else {
1008 // If the locale is changed, needs to reset the default ime
1009 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001010 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001011 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001012 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001013 if (!updateOnlyWhenLocaleChanged) {
1014 try {
1015 startInputInnerLocked();
1016 } catch (RuntimeException e) {
1017 Slog.w(TAG, "Unexpected exception", e);
1018 }
1019 }
1020 }
1021 }
1022
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001023 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001024 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1025 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001026 }
1027
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001028 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001029 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1030 + " currentUserId=" + mSettings.getCurrentUserId());
1031
Yohei Yukawa81482972015-06-04 00:58:59 -07001032 // ContentObserver should be registered again when the user is changed
1033 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001034
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001035 // If the system is not ready or the device is not yed unlocked by the user, then we use
1036 // copy-on-write settings.
1037 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001038 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001039 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001040 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001041 // InputMethodFileManager should be reset when the user is changed
1042 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001043 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001044
1045 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1046 + " defaultImiId=" + defaultImiId);
1047
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001048 // For secondary users, the list of enabled IMEs may not have been updated since the
1049 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1050 // not be empty even if the IME has been uninstalled by the primary user.
1051 // Even in such cases, IMMS works fine because it will find the most applicable
1052 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001053 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001054 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001055 initialUserSwitch /* needsToResetDefaultIme */);
1056 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001057 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1058 mSettings.getEnabledInputMethodListLocked(), newUserId,
1059 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001060 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001061
1062 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1063 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001064 }
1065
Kenny Guy2a764942014-04-02 13:29:20 +01001066 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001067 mSettings.setCurrentProfileIds(
1068 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001069 }
1070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 @Override
1072 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1073 throws RemoteException {
1074 try {
1075 return super.onTransact(code, data, reply, flags);
1076 } catch (RuntimeException e) {
1077 // The input method manager only throws security exceptions, so let's
1078 // log all others.
1079 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001080 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 throw e;
1083 }
1084 }
1085
Svetoslav Ganova0027152013-06-25 14:59:53 -07001086 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001087 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001088 if (DEBUG) {
1089 Slog.d(TAG, "--- systemReady");
1090 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001091 if (!mSystemReady) {
1092 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001093 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001094 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001095 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001096 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001097 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1098 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001099 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001100 if (mStatusBar != null) {
1101 mStatusBar.setIconVisibility(mSlotIme, false);
1102 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001103 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001104 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1105 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001106 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001107 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001108 mHardKeyboardListener);
1109 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001110
1111 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1112 mSettingsObserver.registerContentObserverLocked(currentUserId);
1113
1114 final IntentFilter broadcastFilter = new IntentFilter();
1115 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1116 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1117 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1118 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1119 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1120 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1121
1122 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1123 resetDefaultImeLocked(mContext);
1124 updateFromSettingsLocked(true);
1125 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1126 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1127 mContext.getBasePackageName());
1128
Dianne Hackborncc278702009-09-02 23:07:23 -07001129 try {
1130 startInputInnerLocked();
1131 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001132 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001133 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001134 }
1135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001138 // ---------------------------------------------------------------------------------------
1139 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1140 // 1) it comes from the system process
1141 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1142 private boolean calledFromValidUser() {
1143 final int uid = Binder.getCallingUid();
1144 final int userId = UserHandle.getUserId(uid);
1145 if (DEBUG) {
1146 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1147 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1148 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001149 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1150 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001151 }
Kenny Guy2a764942014-04-02 13:29:20 +01001152 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001153 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001154 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001155
1156 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1157 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1158 // must not manage background users' states in any functions.
1159 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1160 // by a token.
1161 if (mContext.checkCallingOrSelfPermission(
1162 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1163 == PackageManager.PERMISSION_GRANTED) {
1164 if (DEBUG) {
1165 Slog.d(TAG, "--- Access granted because the calling process has "
1166 + "the INTERACT_ACROSS_USERS_FULL permission");
1167 }
1168 return true;
1169 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001170 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001171 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1172 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001173 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001174 }
1175
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001176
1177 /**
1178 * Returns true iff the caller is identified to be the current input method with the token.
1179 * @param token The window token given to the input method when it was started.
1180 * @return true if and only if non-null valid token is specified.
1181 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001182 private boolean calledWithValidToken(@Nullable IBinder token) {
1183 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1184 if (DEBUG) {
1185 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1186 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1187 }
1188 return false;
1189 }
1190 if (token == null || token != mCurToken) {
1191 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1192 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1193 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001194 return false;
1195 }
1196 return true;
1197 }
1198
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001199 private boolean bindCurrentInputMethodService(
1200 Intent service, ServiceConnection conn, int flags) {
1201 if (service == null || conn == null) {
1202 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1203 return false;
1204 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001205 return mContext.bindServiceAsUser(service, conn, flags,
1206 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 }
1208
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001211 // TODO: Make this work even for non-current users?
1212 if (!calledFromValidUser()) {
1213 return Collections.emptyList();
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001216 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218 }
1219
satoke7c6998e2011-06-03 17:57:59 +09001220 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001222 // TODO: Make this work even for non-current users?
1223 if (!calledFromValidUser()) {
1224 return Collections.emptyList();
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001227 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
1230
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001231 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001232 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001233 * @return enabled subtypes of the specified imi
1234 */
satoke7c6998e2011-06-03 17:57:59 +09001235 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001236 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001237 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001238 // TODO: Make this work even for non-current users?
1239 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001240 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001241 }
satok67ddf9c2010-11-17 09:45:54 +09001242 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001243 final InputMethodInfo imi;
1244 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001245 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001246 } else {
1247 imi = mMethodMap.get(imiId);
1248 }
1249 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001250 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001251 }
1252 return mSettings.getEnabledInputMethodSubtypeListLocked(
1253 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001254 }
1255 }
1256
satoke7c6998e2011-06-03 17:57:59 +09001257 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 public void addClient(IInputMethodClient client,
1259 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001260 if (!calledFromValidUser()) {
1261 return;
1262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 synchronized (mMethodMap) {
1264 mClients.put(client.asBinder(), new ClientState(client,
1265 inputContext, uid, pid));
1266 }
1267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001268
satoke7c6998e2011-06-03 17:57:59 +09001269 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001271 if (!calledFromValidUser()) {
1272 return;
1273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 ClientState cs = mClients.remove(client.asBinder());
1276 if (cs != null) {
1277 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001278 if (mCurClient == cs) {
1279 mCurClient = null;
1280 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001281 if (mCurFocusedWindowClient == cs) {
1282 mCurFocusedWindowClient = null;
1283 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 void executeOrSendMessage(IInterface target, Message msg) {
1289 if (target.asBinder() instanceof Binder) {
1290 mCaller.sendMessage(msg);
1291 } else {
1292 handleMessage(msg);
1293 msg.recycle();
1294 }
1295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001296
Yohei Yukawa33e81792015-11-17 21:14:42 -08001297 void unbindCurrentClientLocked(
1298 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001300 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 + mCurClient.client.asBinder());
1302 if (mBoundToMethod) {
1303 mBoundToMethod = false;
1304 if (mCurMethod != null) {
1305 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1306 MSG_UNBIND_INPUT, mCurMethod));
1307 }
1308 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001309
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001310 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1311 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001312 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1313 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
The Android Open Source Project10592532009-03-18 17:39:46 -07001317 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 }
1319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 private int getImeShowFlags() {
1322 int flags = 0;
1323 if (mShowForced) {
1324 flags |= InputMethod.SHOW_FORCED
1325 | InputMethod.SHOW_EXPLICIT;
1326 } else if (mShowExplicitlyRequested) {
1327 flags |= InputMethod.SHOW_EXPLICIT;
1328 }
1329 return flags;
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 private int getAppShowFlags() {
1333 int flags = 0;
1334 if (mShowForced) {
1335 flags |= InputMethodManager.SHOW_FORCED;
1336 } else if (!mShowExplicitlyRequested) {
1337 flags |= InputMethodManager.SHOW_IMPLICIT;
1338 }
1339 return flags;
1340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001341
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001342 InputBindResult attachNewInputLocked(
1343 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 if (!mBoundToMethod) {
1345 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1346 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1347 mBoundToMethod = true;
1348 }
1349 final SessionState session = mCurClient.curSession;
1350 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001351 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1352 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1353 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001355 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1356 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1357 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001360 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001361 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001363 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001364 (session.channel != null ? session.channel.dup() : null),
1365 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001368 InputBindResult startInputLocked(
1369 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001370 IInputMethodClient client, IInputContext inputContext,
1371 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001372 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 // If no method is currently selected, do nothing.
1374 if (mCurMethodId == null) {
1375 return mNoBinding;
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 ClientState cs = mClients.get(client.asBinder());
1379 if (cs == null) {
1380 throw new IllegalArgumentException("unknown client "
1381 + client.asBinder());
1382 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001383
Yohei Yukawa74750f22016-03-22 12:54:22 -07001384 if (attribute == null) {
1385 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1386 + " uid=" + cs.uid + " pid=" + cs.pid);
1387 return null;
1388 }
1389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 try {
1391 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1392 // Check with the window manager to make sure this client actually
1393 // has a window with focus. If not, reject. This is thread safe
1394 // because if the focus changes some time before or after, the
1395 // next client receiving focus that has any interest in input will
1396 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001397 if (DEBUG) {
1398 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1399 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 return null;
1402 }
1403 } catch (RemoteException e) {
1404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001406 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001407 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001408 }
1409
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001410 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001411 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001412 @NonNull EditorInfo attribute, int controlFlags,
1413 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001414 // If no method is currently selected, do nothing.
1415 if (mCurMethodId == null) {
1416 return mNoBinding;
1417 }
1418
Yohei Yukawad57ba672015-06-08 16:39:46 -07001419 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1420 attribute.packageName)) {
1421 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1422 + " uid=" + cs.uid + " package=" + attribute.packageName);
1423 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001424 }
1425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001427 // Was the keyguard locked when switching over to the new client?
1428 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 // If the client is changing, we need to switch over to the new
1430 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001431 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001432 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001433 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434
1435 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001436 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001437 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001438 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
1440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 // Bump up the sequence for this client and attach it.
1443 mCurSeq++;
1444 if (mCurSeq <= 0) mCurSeq = 1;
1445 mCurClient = cs;
1446 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001447 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 // Check if the input method is changing.
1451 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1452 if (cs.curSession != null) {
1453 // Fast case: if we are already connected to the input method,
1454 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001455 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001456 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 }
1458 if (mHaveConnection) {
1459 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 // Return to client, and we will get back with it when
1461 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001462 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001463 return new InputBindResult(null, null, mCurId, mCurSeq,
1464 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 } else if (SystemClock.uptimeMillis()
1466 < (mLastBindTime+TIME_TO_RECONNECT)) {
1467 // In this case we have connected to the service, but
1468 // don't yet have its interface. If it hasn't been too
1469 // long since we did the connection, we'll return to
1470 // the client and wait to get the service interface so
1471 // we can report back. If it has been too long, we want
1472 // to fall through so we can try a disconnect/reconnect
1473 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001474 return new InputBindResult(null, null, mCurId, mCurSeq,
1475 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1478 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
1480 }
1481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001482
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001483 return startInputInnerLocked();
1484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001485
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001486 InputBindResult startInputInnerLocked() {
1487 if (mCurMethodId == null) {
1488 return mNoBinding;
1489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001491 if (!mSystemReady) {
1492 // If the system is not yet ready, we shouldn't be running third
1493 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001494 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1495 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1499 if (info == null) {
1500 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001503 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1506 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001507 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1508 com.android.internal.R.string.input_method_binding_label);
1509 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1510 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001511 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001512 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1513 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 mLastBindTime = SystemClock.uptimeMillis();
1515 mHaveConnection = true;
1516 mCurId = info.getId();
1517 mCurToken = new Binder();
1518 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001519 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001520 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 } catch (RemoteException e) {
1522 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001523 return new InputBindResult(null, null, mCurId, mCurSeq,
1524 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 } else {
1526 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 + mCurIntent);
1529 }
1530 return null;
1531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001533 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001534 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001535 IInputMethodClient client, IInputContext inputContext,
1536 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001537 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001538 if (!calledFromValidUser()) {
1539 return null;
1540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001542 if (DEBUG) {
1543 Slog.v(TAG, "startInput: reason="
1544 + InputMethodClient.getStartInputReason(startInputReason)
1545 + " client = " + client.asBinder()
1546 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001547 + " missingMethods="
1548 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001549 + " attribute=" + attribute
1550 + " controlFlags=#" + Integer.toHexString(controlFlags));
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 final long ident = Binder.clearCallingIdentity();
1553 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001554 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1555 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 } finally {
1557 Binder.restoreCallingIdentity(ident);
1558 }
1559 }
1560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
satoke7c6998e2011-06-03 17:57:59 +09001562 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 public void finishInput(IInputMethodClient client) {
1564 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001565
satoke7c6998e2011-06-03 17:57:59 +09001566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 public void onServiceConnected(ComponentName name, IBinder service) {
1568 synchronized (mMethodMap) {
1569 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1570 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001571 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001572 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001573 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001574 return;
1575 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001576 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001577 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1578 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001580 clearClientSessionLocked(mCurClient);
1581 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 }
1584 }
1585 }
1586
Jeff Brownc28867a2013-03-26 15:42:39 -07001587 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1588 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 synchronized (mMethodMap) {
1590 if (mCurMethod != null && method != null
1591 && mCurMethod.asBinder() == method.asBinder()) {
1592 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001593 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001595 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001596 InputBindResult res = attachNewInputLocked(
1597 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if (res.method != null) {
1599 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001600 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001602 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
1604 }
1605 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001606
1607 // Session abandoned. Close its associated input channel.
1608 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001611 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001612 if (mVisibleBound) {
1613 mContext.unbindService(mVisibleConnection);
1614 mVisibleBound = false;
1615 }
1616
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001617 if (mHaveConnection) {
1618 mContext.unbindService(this);
1619 mHaveConnection = false;
1620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001621
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001622 if (mCurToken != null) {
1623 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001624 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001625 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001626 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001627 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001628 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001629 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001630 } catch (RemoteException e) {
1631 }
1632 mCurToken = null;
1633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001634
The Android Open Source Project10592532009-03-18 17:39:46 -07001635 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001636 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
Yohei Yukawa33e81792015-11-17 21:14:42 -08001639 void resetCurrentMethodAndClient(
1640 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001641 mCurMethodId = null;
1642 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001643 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001644 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001645
1646 void requestClientSessionLocked(ClientState cs) {
1647 if (!cs.sessionRequested) {
1648 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1649 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1650 cs.sessionRequested = true;
1651 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1652 MSG_CREATE_SESSION, mCurMethod, channels[1],
1653 new MethodCallback(this, mCurMethod, channels[0])));
1654 }
1655 }
1656
1657 void clearClientSessionLocked(ClientState cs) {
1658 finishSessionLocked(cs.curSession);
1659 cs.curSession = null;
1660 cs.sessionRequested = false;
1661 }
1662
1663 private void finishSessionLocked(SessionState sessionState) {
1664 if (sessionState != null) {
1665 if (sessionState.session != null) {
1666 try {
1667 sessionState.session.finishSession();
1668 } catch (RemoteException e) {
1669 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001670 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001671 }
1672 sessionState.session = null;
1673 }
1674 if (sessionState.channel != null) {
1675 sessionState.channel.dispose();
1676 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001677 }
1678 }
1679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001680
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001681 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (mCurMethod != null) {
1683 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001684 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001686
Jeff Brownc28867a2013-03-26 15:42:39 -07001687 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001688 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 mCurMethod = null;
1690 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001691 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001692 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001693 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001694 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
satoke7c6998e2011-06-03 17:57:59 +09001697 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001699 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1700 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1701 // which is irregular but can eventually happen for everyone just by continuing using the
1702 // device. Thus it is important to make sure that all the internal states are properly
1703 // refreshed when this method is called back. Running
1704 // adb install -r <APK that implements the current IME>
1705 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001707 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 + " mCurIntent=" + mCurIntent);
1709 if (mCurMethod != null && mCurIntent != null
1710 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001711 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 // We consider this to be a new bind attempt, since the system
1713 // should now try to restart the service for us.
1714 mLastBindTime = SystemClock.uptimeMillis();
1715 mShowRequested = mInputShown;
1716 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001717 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719 }
1720 }
1721
satokf9f01002011-05-19 21:31:50 +09001722 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001724 synchronized (mMethodMap) {
1725 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001726 return;
1727 }
1728 final long ident = Binder.clearCallingIdentity();
1729 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001731 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001732 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001733 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001736 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001737 CharSequence contentDescription = null;
1738 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001739 // Use PackageManager to load label
1740 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001741 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001742 mIPackageManager.getApplicationInfo(packageName, 0,
1743 mSettings.getCurrentUserId()));
1744 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001745 /* ignore */
1746 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001747 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001748 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001749 contentDescription != null
1750 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001751 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001754 } finally {
1755 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
1758 }
1759
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001760 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001761 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001762 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001763 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001764 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001765 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001766 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1767 // When physical keyboard is attached, we show the ime switcher (or notification if
1768 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1769 // exists in the IME switcher dialog. Might be OK to remove this condition once
1770 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1771 return true;
1772 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001773 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1774 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001775 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001776
1777 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1778 final int N = imis.size();
1779 if (N > 2) return true;
1780 if (N < 1) return false;
1781 int nonAuxCount = 0;
1782 int auxCount = 0;
1783 InputMethodSubtype nonAuxSubtype = null;
1784 InputMethodSubtype auxSubtype = null;
1785 for(int i = 0; i < N; ++i) {
1786 final InputMethodInfo imi = imis.get(i);
1787 final List<InputMethodSubtype> subtypes =
1788 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1789 final int subtypeCount = subtypes.size();
1790 if (subtypeCount == 0) {
1791 ++nonAuxCount;
1792 } else {
1793 for (int j = 0; j < subtypeCount; ++j) {
1794 final InputMethodSubtype subtype = subtypes.get(j);
1795 if (!subtype.isAuxiliary()) {
1796 ++nonAuxCount;
1797 nonAuxSubtype = subtype;
1798 } else {
1799 ++auxCount;
1800 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001801 }
1802 }
satok7cfc0ed2011-06-20 21:29:36 +09001803 }
1804 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001805 if (nonAuxCount > 1 || auxCount > 1) {
1806 return true;
1807 } else if (nonAuxCount == 1 && auxCount == 1) {
1808 if (nonAuxSubtype != null && auxSubtype != null
1809 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1810 || auxSubtype.overridesImplicitlyEnabledSubtype()
1811 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1812 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1813 return false;
1814 }
1815 return true;
1816 }
1817 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001818 }
1819
John Spurlocke0980502013-10-25 11:59:29 -04001820 private boolean isKeyguardLocked() {
1821 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1822 }
1823
satokdbf29502011-08-25 15:28:23 +09001824 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001825 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001826 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001827 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001828 return;
1829 }
1830
1831 synchronized (mMethodMap) {
1832 mImeWindowVis = vis;
1833 mBackDisposition = backDisposition;
1834 updateSystemUiLocked(token, vis, backDisposition);
1835 }
1836 }
1837
1838 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1839 synchronized (mMethodMap) {
1840 updateSystemUiLocked(token, vis, backDisposition);
1841 }
1842 }
1843
1844 // Caution! This method is called in this class. Handle multi-user carefully
1845 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1846 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001847 return;
1848 }
1849
1850 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1851 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001852 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001853 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001854 // apply policy for binder calls
1855 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1856 vis = 0;
satok06487a52010-10-29 11:37:18 +09001857 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001858 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1859 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1860 if (mStatusBar != null) {
1861 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1862 needsToShowImeSwitcher);
1863 }
1864 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1865 if (imi != null && needsToShowImeSwitcher) {
1866 // Used to load label
1867 final CharSequence title = mRes.getText(
1868 com.android.internal.R.string.select_input_method);
1869 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1870 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001871 mImeSwitcherNotification.setContentTitle(title)
1872 .setContentText(summary)
1873 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001874 try {
1875 if ((mNotificationManager != null)
1876 && !mIWindowManager.hasNavigationBar()) {
1877 if (DEBUG) {
1878 Slog.d(TAG, "--- show notification: label = " + summary);
1879 }
1880 mNotificationManager.notifyAsUser(null,
1881 com.android.internal.R.string.select_input_method,
1882 mImeSwitcherNotification.build(), UserHandle.ALL);
1883 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001884 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001885 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001886 }
1887 } else {
1888 if (mNotificationShown && mNotificationManager != null) {
1889 if (DEBUG) {
1890 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001891 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001892 mNotificationManager.cancelAsUser(null,
1893 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1894 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001895 }
satok06487a52010-10-29 11:37:18 +09001896 }
1897 } finally {
1898 Binder.restoreCallingIdentity(ident);
1899 }
1900 }
1901
satoke7c6998e2011-06-03 17:57:59 +09001902 @Override
satokf9f01002011-05-19 21:31:50 +09001903 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001904 if (!calledFromValidUser()) {
1905 return;
1906 }
satokf9f01002011-05-19 21:31:50 +09001907 synchronized (mMethodMap) {
1908 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1909 for (int i = 0; i < spans.length; ++i) {
1910 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001911 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001912 mSecureSuggestionSpans.put(ss, currentImi);
1913 }
1914 }
1915 }
1916 }
1917
satoke7c6998e2011-06-03 17:57:59 +09001918 @Override
satokf9f01002011-05-19 21:31:50 +09001919 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001920 if (!calledFromValidUser()) {
1921 return false;
1922 }
satokf9f01002011-05-19 21:31:50 +09001923 synchronized (mMethodMap) {
1924 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1925 // TODO: Do not send the intent if the process of the targetImi is already dead.
1926 if (targetImi != null) {
1927 final String[] suggestions = span.getSuggestions();
1928 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001929 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001930 final Intent intent = new Intent();
1931 // Ensures that only a class in the original IME package will receive the
1932 // notification.
satok42c5a162011-05-26 16:46:14 +09001933 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001934 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1935 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1936 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1937 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001938 final long ident = Binder.clearCallingIdentity();
1939 try {
1940 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1941 } finally {
1942 Binder.restoreCallingIdentity(ident);
1943 }
satokf9f01002011-05-19 21:31:50 +09001944 return true;
1945 }
1946 }
1947 return false;
1948 }
1949
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001950 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001951 updateInputMethodsFromSettingsLocked(enabledMayChange);
1952 updateKeyboardFromSettingsLocked();
1953 }
1954
1955 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001956 if (enabledMayChange) {
1957 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1958 for (int i=0; i<enabled.size(); i++) {
1959 // We allow the user to select "disabled until used" apps, so if they
1960 // are enabling one of those here we now need to make it enabled.
1961 InputMethodInfo imm = enabled.get(i);
1962 try {
1963 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1964 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1965 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001966 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001967 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001968 if (DEBUG) {
1969 Slog.d(TAG, "Update state(" + imm.getId()
1970 + "): DISABLED_UNTIL_USED -> DEFAULT");
1971 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001972 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1973 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001974 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1975 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001976 }
1977 } catch (RemoteException e) {
1978 }
1979 }
1980 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001981 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1982 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1983 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1984 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001985 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001986 // There is no input method selected, try to choose new applicable input method.
1987 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001988 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001989 }
1990 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001992 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001994 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001995 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
satokf3db1af2010-11-23 13:34:33 +09001997 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001998 } else {
1999 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002000 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002002 // Here is not the perfect place to reset the switching controller. Ideally
2003 // mSwitchingController and mSettings should be able to share the same state.
2004 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2005 // the same enabled IMEs list.
2006 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002007
2008 }
2009
2010 public void updateKeyboardFromSettingsLocked() {
2011 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2012 if (mSwitchingDialog != null
2013 && mSwitchingDialogTitleView != null
2014 && mSwitchingDialog.isShowing()) {
2015 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2016 com.android.internal.R.id.hard_keyboard_switch);
2017 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002020
Yohei Yukawab097b822015-12-01 10:43:08 -08002021 private void notifyInputMethodSubtypeChanged(final int userId,
2022 @Nullable final InputMethodInfo inputMethodInfo,
2023 @Nullable final InputMethodSubtype subtype) {
2024 final InputManagerInternal inputManagerInternal =
2025 LocalServices.getService(InputManagerInternal.class);
2026 if (inputManagerInternal != null) {
2027 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2028 }
2029 }
2030
satokab751aa2010-09-14 19:17:36 +09002031 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 InputMethodInfo info = mMethodMap.get(id);
2033 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002034 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002036
satokd81e9502012-05-21 12:58:45 +09002037 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002039 final int subtypeCount = info.getSubtypeCount();
2040 if (subtypeCount <= 0) {
2041 return;
satokcd7cd292010-11-20 15:46:23 +09002042 }
satokd81e9502012-05-21 12:58:45 +09002043 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2044 final InputMethodSubtype newSubtype;
2045 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2046 newSubtype = info.getSubtypeAt(subtypeId);
2047 } else {
2048 // If subtype is null, try to find the most applicable one from
2049 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002050 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002051 }
2052 if (newSubtype == null || oldSubtype == null) {
2053 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2054 + ", new subtype = " + newSubtype);
2055 return;
2056 }
2057 if (newSubtype != oldSubtype) {
2058 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2059 if (mCurMethod != null) {
2060 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002061 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002062 mCurMethod.changeInputMethodSubtype(newSubtype);
2063 } catch (RemoteException e) {
2064 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002065 return;
satokab751aa2010-09-14 19:17:36 +09002066 }
2067 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002068 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 return;
2071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002072
satokd81e9502012-05-21 12:58:45 +09002073 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 final long ident = Binder.clearCallingIdentity();
2075 try {
satokab751aa2010-09-14 19:17:36 +09002076 // Set a subtype to this input method.
2077 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002078 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2079 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2080 // because mCurMethodId is stored as a history in
2081 // setSelectedInputMethodAndSubtypeLocked().
2082 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002084 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002086 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002088 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002090 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 } finally {
2092 Binder.restoreCallingIdentity(ident);
2093 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002094
2095 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2096 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
satok42c5a162011-05-26 16:46:14 +09002099 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002100 public boolean showSoftInput(IInputMethodClient client, int flags,
2101 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002102 if (!calledFromValidUser()) {
2103 return false;
2104 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002105 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 long ident = Binder.clearCallingIdentity();
2107 try {
2108 synchronized (mMethodMap) {
2109 if (mCurClient == null || client == null
2110 || mCurClient.client.asBinder() != client.asBinder()) {
2111 try {
2112 // We need to check if this is the current client with
2113 // focus in the window manager, to allow this call to
2114 // be made before input is started in it.
2115 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002116 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002120 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
2122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002123
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002124 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002125 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127 } finally {
2128 Binder.restoreCallingIdentity(ident);
2129 }
2130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002131
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002134 if (mAccessibilityRequestingNoSoftKeyboard) {
2135 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 }
Anna Galusza9b278112016-01-04 11:37:37 -08002137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2139 mShowExplicitlyRequested = true;
2140 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002141 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2142 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002144
Dianne Hackborncc278702009-09-02 23:07:23 -07002145 if (!mSystemReady) {
2146 return false;
2147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002148
The Android Open Source Project4df24232009-03-05 14:34:35 -08002149 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002151 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002152 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2153 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2154 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002156 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002157 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002158 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002159 | Context.BIND_TREAT_LIKE_ACTIVITY
2160 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002161 mVisibleBound = true;
2162 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002163 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002165 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 // The client has asked to have the input method shown, but
2167 // we have been sitting here too long with a connection to the
2168 // service and no interface received, so let's disconnect/connect
2169 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002170 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002172 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002174 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002175 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002176 } else {
2177 if (DEBUG) {
2178 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2179 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002182
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002185
satok42c5a162011-05-26 16:46:14 +09002186 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 public boolean hideSoftInput(IInputMethodClient client, int flags,
2188 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002189 if (!calledFromValidUser()) {
2190 return false;
2191 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002192 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 long ident = Binder.clearCallingIdentity();
2194 try {
2195 synchronized (mMethodMap) {
2196 if (mCurClient == null || client == null
2197 || mCurClient.client.asBinder() != client.asBinder()) {
2198 try {
2199 // We need to check if this is the current client with
2200 // focus in the window manager, to allow this call to
2201 // be made before input is started in it.
2202 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002203 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2204 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002205 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
2207 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002208 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
2210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002211
Joe Onorato8a9b2202010-02-26 18:56:32 -08002212 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002213 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 }
2215 } finally {
2216 Binder.restoreCallingIdentity(ident);
2217 }
2218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002219
The Android Open Source Project4df24232009-03-05 14:34:35 -08002220 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2222 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002223 if (DEBUG) Slog.v(TAG, "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002224 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 }
2226 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002227 if (DEBUG) Slog.v(TAG, "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002228 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002230
2231 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2232 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2233 // to be updated with the new value sent from IME process. Even in such a transient state
2234 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2235 // application process as a valid request, and have even promised such a behavior with CTS
2236 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2237 // IMMS#InputShown indicates that the software keyboard is shown.
2238 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2239 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2240 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002241 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002242 if (shouldHideSoftInput) {
2243 // The IME will report its visible state again after the following message finally
2244 // delivered to the IME process as an IPC. Hence the inconsistency between
2245 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2246 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002247 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2248 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2249 res = true;
2250 } else {
2251 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002253 if (mHaveConnection && mVisibleBound) {
2254 mContext.unbindService(mVisibleConnection);
2255 mVisibleBound = false;
2256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 mInputShown = false;
2258 mShowRequested = false;
2259 mShowExplicitlyRequested = false;
2260 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002261 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002263
satok42c5a162011-05-26 16:46:14 +09002264 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002265 public InputBindResult startInputOrWindowGainedFocus(
2266 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2267 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002268 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002269 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002270 if (windowToken != null) {
2271 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002272 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002273 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002274 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2275 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002276 }
2277 }
2278
2279 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002280 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2281 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002282 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2283 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002284 // Needs to check the validity before clearing calling identity
2285 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002286 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 long ident = Binder.clearCallingIdentity();
2288 try {
2289 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002290 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2291 + InputMethodClient.getStartInputReason(startInputReason)
2292 + " client=" + client.asBinder()
2293 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002294 + " missingMethods="
2295 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002296 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002297 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002299 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002300
Dianne Hackborn7663d802012-02-24 13:08:49 -08002301 ClientState cs = mClients.get(client.asBinder());
2302 if (cs == null) {
2303 throw new IllegalArgumentException("unknown client "
2304 + client.asBinder());
2305 }
2306
2307 try {
2308 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2309 // Check with the window manager to make sure this client actually
2310 // has a window with focus. If not, reject. This is thread safe
2311 // because if the focus changes some time before or after, the
2312 // next client receiving focus that has any interest in input will
2313 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002314 if (DEBUG) {
2315 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2316 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2317 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002318 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002320 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002322
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002323 if (!calledFromValidUser) {
2324 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2325 Slog.w(TAG, "If you want to interect with IME, you need "
2326 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2327 hideCurrentInputLocked(0, null);
2328 return null;
2329 }
2330
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002331 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002332 if (DEBUG) {
2333 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2334 + " attribute=" + attribute + ", token = " + windowToken);
2335 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002336 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002337 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002338 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002339 }
2340 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002341 }
2342 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002343 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002344
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002345 // Should we auto-show the IME even if the caller has not
2346 // specified what should be done with it?
2347 // We only do this automatically if the window can resize
2348 // to accommodate the IME (so what the user sees will give
2349 // them good context without input information being obscured
2350 // by the IME) or if running on a large screen where there
2351 // is more room for the target window + IME.
2352 final boolean doAutoShow =
2353 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2354 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2355 || mRes.getConfiguration().isLayoutSizeAtLeast(
2356 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002357 final boolean isTextEditor =
2358 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2359
2360 // We want to start input before showing the IME, but after closing
2361 // it. We want to do this after closing it to help the IME disappear
2362 // more quickly (not get stuck behind it initializing itself for the
2363 // new focused input, even if its window wants to hide the IME).
2364 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2367 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002368 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2370 // There is no focus view, and this window will
2371 // be behind any soft input window, so hide the
2372 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002373 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002374 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002376 } else if (isTextEditor && doAutoShow && (softInputMode &
2377 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 // There is a focus view, and we are navigating forward
2379 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002380 // We only do this automatically if the window can resize
2381 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002382 // them good context without input information being obscured
2383 // by the IME) or if running on a large screen where there
2384 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002385 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002386 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002387 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002388 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002389 didStart = true;
2390 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002391 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
2393 break;
2394 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2395 // Do nothing.
2396 break;
2397 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2398 if ((softInputMode &
2399 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002400 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002401 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
2403 break;
2404 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002405 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002406 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 break;
2408 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2409 if ((softInputMode &
2410 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002411 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002412 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002413 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002414 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002415 didStart = true;
2416 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002417 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
2419 break;
2420 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002421 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002422 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002423 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002424 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002425 didStart = true;
2426 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002427 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 break;
2429 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002430
2431 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002432 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002433 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
2436 } finally {
2437 Binder.restoreCallingIdentity(ident);
2438 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002439
2440 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002442
satok42c5a162011-05-26 16:46:14 +09002443 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002444 public void showInputMethodPickerFromClient(
2445 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002446 if (!calledFromValidUser()) {
2447 return;
2448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 synchronized (mMethodMap) {
2450 if (mCurClient == null || client == null
2451 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002452 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002453 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 }
2455
satok440aab52010-11-25 09:43:11 +09002456 // Always call subtype picker, because subtype picker is a superset of input method
2457 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002458 mHandler.sendMessage(mCaller.obtainMessageI(
2459 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002460 }
2461 }
2462
satok42c5a162011-05-26 16:46:14 +09002463 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002465 if (!calledFromValidUser()) {
2466 return;
2467 }
satok28203512010-11-24 11:06:49 +09002468 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2469 }
2470
satok42c5a162011-05-26 16:46:14 +09002471 @Override
satok28203512010-11-24 11:06:49 +09002472 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002473 if (!calledFromValidUser()) {
2474 return;
2475 }
satok28203512010-11-24 11:06:49 +09002476 synchronized (mMethodMap) {
2477 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002478 setInputMethodWithSubtypeIdLocked(token, id,
2479 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2480 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002481 } else {
2482 setInputMethod(token, id);
2483 }
2484 }
satokab751aa2010-09-14 19:17:36 +09002485 }
2486
satok42c5a162011-05-26 16:46:14 +09002487 @Override
satokb416a712010-11-25 20:42:14 +09002488 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002489 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002490 if (!calledFromValidUser()) {
2491 return;
2492 }
satokb416a712010-11-25 20:42:14 +09002493 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002494 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2495 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002496 }
2497 }
2498
satok4fc87d62011-05-20 16:13:43 +09002499 @Override
satok735cf382010-11-11 20:40:09 +09002500 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002501 if (!calledFromValidUser()) {
2502 return false;
2503 }
satok735cf382010-11-11 20:40:09 +09002504 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002505 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002506 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002507 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002508 lastImi = mMethodMap.get(lastIme.first);
2509 } else {
2510 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002511 }
satok4fc87d62011-05-20 16:13:43 +09002512 String targetLastImiId = null;
2513 int subtypeId = NOT_A_SUBTYPE_ID;
2514 if (lastIme != null && lastImi != null) {
2515 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002516 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002517 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2518 : mCurrentSubtype.hashCode();
2519 // If the last IME is the same as the current IME and the last subtype is not
2520 // defined, there is no need to switch to the last IME.
2521 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2522 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002523 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002524 }
2525 }
2526
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002527 if (TextUtils.isEmpty(targetLastImiId)
2528 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002529 // This is a safety net. If the currentSubtype can't be added to the history
2530 // and the framework couldn't find the last ime, we will make the last ime be
2531 // the most applicable enabled keyboard subtype of the system imes.
2532 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2533 if (enabled != null) {
2534 final int N = enabled.size();
2535 final String locale = mCurrentSubtype == null
2536 ? mRes.getConfiguration().locale.toString()
2537 : mCurrentSubtype.getLocale();
2538 for (int i = 0; i < N; ++i) {
2539 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002540 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002541 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002542 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2543 InputMethodUtils.getSubtypes(imi),
2544 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002545 if (keyboardSubtype != null) {
2546 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002547 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002548 imi, keyboardSubtype.hashCode());
2549 if(keyboardSubtype.getLocale().equals(locale)) {
2550 break;
2551 }
2552 }
2553 }
2554 }
2555 }
2556 }
2557
2558 if (!TextUtils.isEmpty(targetLastImiId)) {
2559 if (DEBUG) {
2560 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2561 + ", from: " + mCurMethodId + ", " + subtypeId);
2562 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002563 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002564 return true;
2565 } else {
2566 return false;
2567 }
satok735cf382010-11-11 20:40:09 +09002568 }
2569 }
2570
satoke7c6998e2011-06-03 17:57:59 +09002571 @Override
satok688bd472012-02-09 20:09:17 +09002572 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002573 if (!calledFromValidUser()) {
2574 return false;
2575 }
satok688bd472012-02-09 20:09:17 +09002576 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002577 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002578 return false;
2579 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002580 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002581 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2582 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002583 if (nextSubtype == null) {
2584 return false;
2585 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002586 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2587 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002588 return true;
2589 }
2590 }
2591
2592 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002593 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2594 if (!calledFromValidUser()) {
2595 return false;
2596 }
2597 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002598 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002599 return false;
2600 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002601 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002602 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2603 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002604 if (nextSubtype == null) {
2605 return false;
2606 }
2607 return true;
2608 }
2609 }
2610
2611 @Override
satok68f1b782011-04-11 14:26:04 +09002612 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002613 if (!calledFromValidUser()) {
2614 return null;
2615 }
satok68f1b782011-04-11 14:26:04 +09002616 synchronized (mMethodMap) {
2617 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2618 // TODO: Handle the case of the last IME with no subtypes
2619 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2620 || TextUtils.isEmpty(lastIme.second)) return null;
2621 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2622 if (lastImi == null) return null;
2623 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002624 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002625 final int lastSubtypeId =
2626 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002627 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2628 return null;
2629 }
2630 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002631 } catch (NumberFormatException e) {
2632 return null;
2633 }
2634 }
2635 }
2636
satoke7c6998e2011-06-03 17:57:59 +09002637 @Override
satokee5e77c2011-09-02 18:50:15 +09002638 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002639 if (!calledFromValidUser()) {
2640 return;
2641 }
satok91e88122011-07-18 11:11:42 +09002642 // By this IPC call, only a process which shares the same uid with the IME can add
2643 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002644 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002645 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002646 if (!mSystemReady) {
2647 return;
2648 }
satok91e88122011-07-18 11:11:42 +09002649 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002650 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002651 final String[] packageInfos;
2652 try {
2653 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2654 } catch (RemoteException e) {
2655 Slog.e(TAG, "Failed to get package infos");
2656 return;
2657 }
satok91e88122011-07-18 11:11:42 +09002658 if (packageInfos != null) {
2659 final int packageNum = packageInfos.length;
2660 for (int i = 0; i < packageNum; ++i) {
2661 if (packageInfos[i].equals(imi.getPackageName())) {
2662 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002663 final long ident = Binder.clearCallingIdentity();
2664 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002665 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002666 } finally {
2667 Binder.restoreCallingIdentity(ident);
2668 }
satokee5e77c2011-09-02 18:50:15 +09002669 return;
satok91e88122011-07-18 11:11:42 +09002670 }
2671 }
2672 }
satoke7c6998e2011-06-03 17:57:59 +09002673 }
satokee5e77c2011-09-02 18:50:15 +09002674 return;
satoke7c6998e2011-06-03 17:57:59 +09002675 }
2676
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002677 @Override
2678 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002679 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002680 }
2681
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002682 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002683 public void clearLastInputMethodWindowForTransition(IBinder token) {
2684 if (!calledFromValidUser()) {
2685 return;
2686 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002687 synchronized (mMethodMap) {
2688 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002689 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002690 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002691 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002692 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002693 }
2694
2695 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002696 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002697 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002698 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002699 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002700 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002701 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2702 if (DEBUG) {
2703 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2704 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2705 + " actual: " + sequenceNumber);
2706 }
2707 return;
2708 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002709 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2710 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002711 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002712 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002713 }
2714 }
2715
satok28203512010-11-24 11:06:49 +09002716 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002718 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2719 }
2720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002722 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2723 if (token == null) {
2724 if (mContext.checkCallingOrSelfPermission(
2725 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2726 != PackageManager.PERMISSION_GRANTED) {
2727 throw new SecurityException(
2728 "Using null token requires permission "
2729 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002731 } else if (mCurToken != token) {
2732 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2733 + " token: " + token);
2734 return;
2735 }
2736
2737 final long ident = Binder.clearCallingIdentity();
2738 try {
2739 setInputMethodLocked(id, subtypeId);
2740 } finally {
2741 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 }
2743 }
2744
satok42c5a162011-05-26 16:46:14 +09002745 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002747 if (!calledFromValidUser()) {
2748 return;
2749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002751 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 return;
2753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 long ident = Binder.clearCallingIdentity();
2755 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002756 hideCurrentInputLocked(flags, null);
2757 } finally {
2758 Binder.restoreCallingIdentity(ident);
2759 }
2760 }
2761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002762
satok42c5a162011-05-26 16:46:14 +09002763 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002764 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002765 if (!calledFromValidUser()) {
2766 return;
2767 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002768 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002769 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002770 return;
2771 }
2772 long ident = Binder.clearCallingIdentity();
2773 try {
2774 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 } finally {
2776 Binder.restoreCallingIdentity(ident);
2777 }
2778 }
2779 }
2780
2781 void setEnabledSessionInMainThread(SessionState session) {
2782 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002783 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002785 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002786 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 } catch (RemoteException e) {
2788 }
2789 }
2790 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002791 if (mEnabledSession != null && mEnabledSession.session != null) {
2792 try {
2793 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2794 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2795 } catch (RemoteException e) {
2796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798 }
2799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002800
satok42c5a162011-05-26 16:46:14 +09002801 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002803 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002805 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002806 final boolean showAuxSubtypes;
2807 switch (msg.arg1) {
2808 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2809 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2810 // implemented so that auxiliary subtypes will be excluded when the soft
2811 // keyboard is invisible.
2812 showAuxSubtypes = mInputShown;
2813 break;
2814 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2815 showAuxSubtypes = true;
2816 break;
2817 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2818 showAuxSubtypes = false;
2819 break;
2820 default:
2821 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2822 return false;
2823 }
2824 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002825 return true;
2826
satok47a44912010-10-06 16:03:58 +09002827 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002828 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002829 return true;
2830
2831 case MSG_SHOW_IM_CONFIG:
2832 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002833 return true;
2834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 case MSG_UNBIND_INPUT:
2838 try {
2839 ((IInputMethod)msg.obj).unbindInput();
2840 } catch (RemoteException e) {
2841 // There is nothing interesting about the method dying.
2842 }
2843 return true;
2844 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002845 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 try {
2847 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2848 } catch (RemoteException e) {
2849 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002850 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 return true;
2852 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002853 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002855 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002856 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002857 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 } catch (RemoteException e) {
2859 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002860 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 return true;
2862 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002863 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002865 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002866 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002867 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 } catch (RemoteException e) {
2869 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002870 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002872 case MSG_HIDE_CURRENT_INPUT_METHOD:
2873 synchronized (mMethodMap) {
2874 hideCurrentInputLocked(0, null);
2875 }
2876 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002878 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002880 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2882 } catch (RemoteException e) {
2883 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002884 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002886 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002887 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002888 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002889 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002891 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002893 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002894 // Dispose the channel if the input method is not local to this process
2895 // because the remote proxy will get its own copy when unparceled.
2896 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002897 channel.dispose();
2898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002900 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002904
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002905 case MSG_START_INPUT: {
2906 int missingMethods = msg.arg1;
2907 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002909 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002911 session.method.startInput((IInputContext) args.arg2, missingMethods,
2912 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 } catch (RemoteException e) {
2914 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002915 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002917 }
2918 case MSG_RESTART_INPUT: {
2919 int missingMethods = msg.arg1;
2920 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002922 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002924 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2925 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 } catch (RemoteException e) {
2927 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002928 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002930 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002933
Yohei Yukawa33e81792015-11-17 21:14:42 -08002934 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002936 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 } catch (RemoteException e) {
2938 // There is nothing interesting about the last client dying.
2939 }
2940 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002941 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002942 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002943 IInputMethodClient client = (IInputMethodClient)args.arg1;
2944 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002946 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002948 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002949 } finally {
2950 // Dispose the channel if the input method is not local to this process
2951 // because the remote proxy will get its own copy when unparceled.
2952 if (res.channel != null && Binder.isProxy(client)) {
2953 res.channel.dispose();
2954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002956 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002958 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002959 case MSG_SET_ACTIVE:
2960 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002961 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002962 } catch (RemoteException e) {
2963 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2964 + ((ClientState)msg.obj).pid + " uid "
2965 + ((ClientState)msg.obj).uid);
2966 }
2967 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002968 case MSG_SET_INTERACTIVE:
2969 handleSetInteractive(msg.arg1 != 0);
2970 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002971 case MSG_SWITCH_IME:
2972 handleSwitchInputMethod(msg.arg1 != 0);
2973 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002974 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2975 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002976 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002977 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002978 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002979 } catch (RemoteException e) {
2980 Slog.w(TAG, "Got RemoteException sending "
2981 + "setUserActionNotificationSequenceNumber("
2982 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002983 + clientState.pid + " uid "
2984 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002985 }
2986 return true;
2987 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002988 case MSG_REPORT_FULLSCREEN_MODE: {
2989 final boolean fullscreen = msg.arg1 != 0;
2990 final ClientState clientState = (ClientState)msg.obj;
2991 try {
2992 clientState.client.reportFullscreenMode(fullscreen);
2993 } catch (RemoteException e) {
2994 Slog.w(TAG, "Got RemoteException sending "
2995 + "reportFullscreen(" + fullscreen + ") notification to pid="
2996 + clientState.pid + " uid=" + clientState.uid);
2997 }
2998 return true;
2999 }
satok01038492012-04-09 21:08:27 +09003000
3001 // --------------------------------------------------------------
3002 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003003 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003004 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003005 case MSG_SYSTEM_UNLOCK_USER:
3006 final int userId = msg.arg1;
3007 onUnlockUser(userId);
3008 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 }
3010 return false;
3011 }
3012
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003013 private void handleSetInteractive(final boolean interactive) {
3014 synchronized (mMethodMap) {
3015 mIsInteractive = interactive;
3016 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3017
3018 // Inform the current client of the change in active status
3019 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003020 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3021 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3022 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003023 }
3024 }
3025 }
3026
Yohei Yukawaae61f712015-12-09 13:00:10 -08003027 private void handleSwitchInputMethod(final boolean forwardDirection) {
3028 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003029 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003030 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003031 if (nextSubtype == null) {
3032 return;
3033 }
3034 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003035 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3036 if (newInputMethodInfo == null) {
3037 return;
3038 }
3039 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3040 newInputMethodInfo, mCurrentSubtype);
3041 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003042 if (mSubtypeSwitchedByShortCutToast == null) {
3043 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3044 Toast.LENGTH_SHORT);
3045 } else {
3046 mSubtypeSwitchedByShortCutToast.setText(toastText);
3047 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003048 mSubtypeSwitchedByShortCutToast.show();
3049 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003050 }
3051 }
3052
satokdc9ddae2011-10-06 12:22:36 +09003053 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003054 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3055 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003056 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003057 if (DEBUG) {
3058 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3059 }
satok723a27e2010-11-11 14:58:11 +09003060 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003061 return true;
3062 }
3063
3064 return false;
3065 }
3066
Yohei Yukawa94e33302016-02-12 19:37:03 -08003067 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003068 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003069 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003070 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003071 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003072 if (!mSystemReady) {
3073 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3074 return;
3075 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003076 mMethodList.clear();
3077 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003078 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003079 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003080
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003081 // Use for queryIntentServicesAsUser
3082 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003084 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3085 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3086 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003087 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003089 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3090 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003091
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003092 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003093 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 for (int i = 0; i < services.size(); ++i) {
3095 ResolveInfo ri = services.get(i);
3096 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003097 final String imeId = InputMethodInfo.computeId(ri);
3098 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3099 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 + ": it does not require the permission "
3101 + android.Manifest.permission.BIND_INPUT_METHOD);
3102 continue;
3103 }
3104
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003105 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003107 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 try {
satoke7c6998e2011-06-03 17:57:59 +09003109 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003110 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003111 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003112 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113
3114 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003115 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003117 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003118 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 }
3120 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003121
Yohei Yukawac4e44912017-02-09 19:30:22 -08003122 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3123 // negatives when the package state remains to be the same but only the component state is
3124 // changed.
3125 {
3126 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3127 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3128 // conservative, but it seems we cannot use it for now (Issue 35176630).
3129 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3130 new Intent(InputMethod.SERVICE_INTERFACE),
3131 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3132 final int N = allInputMethodServices.size();
3133 for (int i = 0; i < N; ++i) {
3134 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3135 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3136 continue;
3137 }
3138 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3139 }
3140 }
3141
Yohei Yukawa859df052016-02-17 07:56:46 -08003142 // TODO: The following code should find better place to live.
3143 if (!resetDefaultEnabledIme) {
3144 boolean enabledImeFound = false;
3145 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3146 final int N = enabledImes.size();
3147 for (int i = 0; i < N; ++i) {
3148 final InputMethodInfo imi = enabledImes.get(i);
3149 if (mMethodList.contains(imi)) {
3150 enabledImeFound = true;
3151 break;
3152 }
3153 }
3154 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003155 if (DEBUG) {
3156 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3157 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003158 resetDefaultEnabledIme = true;
3159 resetSelectedInputMethodAndSubtypeLocked("");
3160 }
3161 }
3162
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003163 if (resetDefaultEnabledIme) {
3164 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003165 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003166 final int N = defaultEnabledIme.size();
3167 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003168 final InputMethodInfo imi = defaultEnabledIme.get(i);
3169 if (DEBUG) {
3170 Slog.d(TAG, "--- enable ime = " + imi);
3171 }
3172 setInputMethodEnabledLocked(imi.getId(), true);
3173 }
3174 }
3175
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003176 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003177 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003178 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003179 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3180 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003181 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003182 }
3183 } else {
3184 // Double check that the default IME is certainly enabled.
3185 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003186 }
3187 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003188 // Here is not the perfect place to reset the switching controller. Ideally
3189 // mSwitchingController and mSettings should be able to share the same state.
3190 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3191 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003192 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003196
satok217f5482010-12-15 05:19:19 +09003197 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003198 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003199 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003200 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3201 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003202 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003203 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003204 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003205 final int userId;
3206 synchronized (mMethodMap) {
3207 userId = mSettings.getCurrentUserId();
3208 }
3209 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003210 }
3211
3212 private void showConfigureInputMethods() {
3213 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3214 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3215 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3216 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003217 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003218 }
3219
satok2c93efc2012-04-02 19:33:47 +09003220 private boolean isScreenLocked() {
3221 return mKeyguardManager != null
3222 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3223 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003224
Seigo Nonaka14e13912015-05-06 21:04:13 -07003225 private void showInputMethodMenu(boolean showAuxSubtypes) {
3226 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003229 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003230
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003231 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003232 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003233 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003234
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003235 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003236 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003237 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3238 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003239 if (immis == null || immis.size() == 0) {
3240 return;
3241 }
3242
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003243 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244
satok688bd472012-02-09 20:09:17 +09003245 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003246 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003247 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003248
satokc3690562012-01-10 20:14:43 +09003249 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003250 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003251 if (currentSubtype != null) {
3252 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003253 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3254 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003255 }
3256 }
3257
Ken Wakasa761eb372011-03-04 19:06:18 +09003258 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003259 mIms = new InputMethodInfo[N];
3260 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003261 int checkedItem = 0;
3262 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003263 final ImeSubtypeListItem item = imList.get(i);
3264 mIms[i] = item.mImi;
3265 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003266 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003267 int subtypeId = mSubtypeIds[i];
3268 if ((subtypeId == NOT_A_SUBTYPE_ID)
3269 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3270 || (subtypeId == lastInputMethodSubtypeId)) {
3271 checkedItem = i;
3272 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003275
3276 final Context settingsContext = new ContextThemeWrapper(context,
3277 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3278
3279 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003280 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3281 @Override
3282 public void onCancel(DialogInterface dialog) {
3283 hideInputMethodMenu();
3284 }
3285 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003286
3287 final Context dialogContext = mDialogBuilder.getContext();
3288 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3289 com.android.internal.R.styleable.DialogPreference,
3290 com.android.internal.R.attr.alertDialogStyle, 0);
3291 final Drawable dialogIcon = a.getDrawable(
3292 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3293 a.recycle();
3294
3295 mDialogBuilder.setIcon(dialogIcon);
3296
Yohei Yukawad34e1482016-02-11 08:03:52 -08003297 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003298 final View tv = inflater.inflate(
3299 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3300 mDialogBuilder.setCustomTitle(tv);
3301
3302 // Setup layout for a toggle switch of the hardware keyboard
3303 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003304 mSwitchingDialogTitleView
3305 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003306 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003307 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003308 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003309 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003310 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003311 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3312 @Override
3313 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003314 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003315 // Ensure that the input method dialog is dismissed when changing
3316 // the hardware keyboard state.
3317 hideInputMethodMenu();
3318 }
3319 });
3320
Alan Viverette505e3ab2014-11-24 15:22:11 -08003321 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003322 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3323 final OnClickListener choiceListener = new OnClickListener() {
3324 @Override
3325 public void onClick(final DialogInterface dialog, final int which) {
3326 synchronized (mMethodMap) {
3327 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3328 || mSubtypeIds.length <= which) {
3329 return;
satok01038492012-04-09 21:08:27 +09003330 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003331 final InputMethodInfo im = mIms[which];
3332 int subtypeId = mSubtypeIds[which];
3333 adapter.mCheckedItem = which;
3334 adapter.notifyDataSetChanged();
3335 hideInputMethodMenu();
3336 if (im != null) {
3337 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3338 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003339 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003340 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003342 }
3343 }
3344 };
3345 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003348 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003349 final Window w = mSwitchingDialog.getWindow();
3350 final WindowManager.LayoutParams attrs = w.getAttributes();
3351 w.setType(TYPE_INPUT_METHOD_DIALOG);
3352 // Use an alternate token for the dialog for that window manager can group the token
3353 // with other IME windows based on type vs. grouping based on whichever token happens
3354 // to get selected by the system later on.
3355 attrs.token = mSwitchingDialogToken;
3356 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3357 attrs.setTitle("Select input method");
3358 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003359 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 mSwitchingDialog.show();
3361 }
3362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003363
Ken Wakasa05dbb652011-08-22 15:22:43 +09003364 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3365 private final LayoutInflater mInflater;
3366 private final int mTextViewResourceId;
3367 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003368 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003369 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3370 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3371 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003372
Ken Wakasa05dbb652011-08-22 15:22:43 +09003373 mTextViewResourceId = textViewResourceId;
3374 mItemsList = itemsList;
3375 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003376 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003377 }
3378
3379 @Override
3380 public View getView(int position, View convertView, ViewGroup parent) {
3381 final View view = convertView != null ? convertView
3382 : mInflater.inflate(mTextViewResourceId, null);
3383 if (position < 0 || position >= mItemsList.size()) return view;
3384 final ImeSubtypeListItem item = mItemsList.get(position);
3385 final CharSequence imeName = item.mImeName;
3386 final CharSequence subtypeName = item.mSubtypeName;
3387 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3388 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3389 if (TextUtils.isEmpty(subtypeName)) {
3390 firstTextView.setText(imeName);
3391 secondTextView.setVisibility(View.GONE);
3392 } else {
3393 firstTextView.setText(subtypeName);
3394 secondTextView.setText(imeName);
3395 secondTextView.setVisibility(View.VISIBLE);
3396 }
3397 final RadioButton radioButton =
3398 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3399 radioButton.setChecked(position == mCheckedItem);
3400 return view;
3401 }
3402 }
3403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003405 synchronized (mMethodMap) {
3406 hideInputMethodMenuLocked();
3407 }
3408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003409
The Android Open Source Project10592532009-03-18 17:39:46 -07003410 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003411 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412
The Android Open Source Project10592532009-03-18 17:39:46 -07003413 if (mSwitchingDialog != null) {
3414 mSwitchingDialog.dismiss();
3415 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003417
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003418 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003419 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003420 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003424
satok42c5a162011-05-26 16:46:14 +09003425 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003427 // TODO: Make this work even for non-current users?
3428 if (!calledFromValidUser()) {
3429 return false;
3430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 synchronized (mMethodMap) {
3432 if (mContext.checkCallingOrSelfPermission(
3433 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3434 != PackageManager.PERMISSION_GRANTED) {
3435 throw new SecurityException(
3436 "Requires permission "
3437 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3438 }
Anna Galusza9b278112016-01-04 11:37:37 -08003439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 long ident = Binder.clearCallingIdentity();
3441 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003442 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 } finally {
3444 Binder.restoreCallingIdentity(ident);
3445 }
3446 }
3447 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003448
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003449 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3450 // Make sure this is a valid input method.
3451 InputMethodInfo imm = mMethodMap.get(id);
3452 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003453 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003454 }
3455
satokd87c2592010-09-29 11:52:06 +09003456 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3457 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003458
satokd87c2592010-09-29 11:52:06 +09003459 if (enabled) {
3460 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3461 if (pair.first.equals(id)) {
3462 // We are enabling this input method, but it is already enabled.
3463 // Nothing to do. The previous state was enabled.
3464 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003465 }
3466 }
satokd87c2592010-09-29 11:52:06 +09003467 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3468 // Previous state was disabled.
3469 return false;
3470 } else {
3471 StringBuilder builder = new StringBuilder();
3472 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3473 builder, enabledInputMethodsList, id)) {
3474 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003475 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003476 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3477 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3478 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003479 }
3480 // Previous state was enabled.
3481 return true;
3482 } else {
3483 // We are disabling the input method but it is already disabled.
3484 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003485 return false;
3486 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003487 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003488 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003489
satok723a27e2010-11-11 14:58:11 +09003490 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3491 boolean setSubtypeOnly) {
3492 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003493 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003494
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003495 mCurUserActionNotificationSequenceNumber =
3496 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3497 if (DEBUG) {
3498 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3499 + mCurUserActionNotificationSequenceNumber);
3500 }
3501
3502 if (mCurClient != null && mCurClient.client != null) {
3503 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3504 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003505 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003506 }
3507
satok723a27e2010-11-11 14:58:11 +09003508 // Set Subtype here
3509 if (imi == null || subtypeId < 0) {
3510 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003511 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003512 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003513 if (subtypeId < imi.getSubtypeCount()) {
3514 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3515 mSettings.putSelectedSubtype(subtype.hashCode());
3516 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003517 } else {
3518 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003519 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003520 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003521 }
satokab751aa2010-09-14 19:17:36 +09003522 }
satok723a27e2010-11-11 14:58:11 +09003523
Yohei Yukawa68645a62016-02-17 07:54:20 -08003524 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003525 // Set InputMethod here
3526 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3527 }
3528 }
3529
3530 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3531 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3532 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3533 // newDefaultIme is empty when there is no candidate for the selected IME.
3534 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3535 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3536 if (subtypeHashCode != null) {
3537 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003538 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003539 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003540 } catch (NumberFormatException e) {
3541 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3542 }
3543 }
3544 }
3545 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003546 }
3547
satok4e4569d2010-11-19 18:45:53 +09003548 // If there are no selected shortcuts, tries finding the most applicable ones.
3549 private Pair<InputMethodInfo, InputMethodSubtype>
3550 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3551 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3552 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003553 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003554 boolean foundInSystemIME = false;
3555
3556 // Search applicable subtype for each InputMethodInfo
3557 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003558 final String imiId = imi.getId();
3559 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3560 continue;
3561 }
satokcd7cd292010-11-20 15:46:23 +09003562 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003563 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003564 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003565 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003566 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003567 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003568 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003569 }
satokdf31ae62011-01-15 06:19:44 +09003570 // 2. Search by the system locale from enabledSubtypes.
3571 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003572 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003573 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003574 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003575 }
satoka86f5e42011-09-02 17:12:42 +09003576 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003577 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003578 final ArrayList<InputMethodSubtype> subtypesForSearch =
3579 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003580 ? InputMethodUtils.getSubtypes(imi)
3581 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003582 // 4. Search by the current subtype's locale from all subtypes.
3583 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003584 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003585 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003586 }
3587 // 5. Search by the system locale from all subtypes.
3588 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003589 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003590 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003591 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003592 }
satokcd7cd292010-11-20 15:46:23 +09003593 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003594 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003595 // The current input method is the most applicable IME.
3596 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003597 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003598 break;
satok7599a7f2010-12-22 13:45:23 +09003599 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003600 // The system input method is 2nd applicable IME.
3601 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003602 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003603 if ((imi.getServiceInfo().applicationInfo.flags
3604 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3605 foundInSystemIME = true;
3606 }
satok4e4569d2010-11-19 18:45:53 +09003607 }
3608 }
3609 }
3610 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003611 if (mostApplicableIMI != null) {
3612 Slog.w(TAG, "Most applicable shortcut input method was:"
3613 + mostApplicableIMI.getId());
3614 if (mostApplicableSubtype != null) {
3615 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3616 + "," + mostApplicableSubtype.getMode() + ","
3617 + mostApplicableSubtype.getLocale());
3618 }
3619 }
satok4e4569d2010-11-19 18:45:53 +09003620 }
satokcd7cd292010-11-20 15:46:23 +09003621 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003622 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003623 } else {
3624 return null;
3625 }
3626 }
3627
satokab751aa2010-09-14 19:17:36 +09003628 /**
3629 * @return Return the current subtype of this input method.
3630 */
satok42c5a162011-05-26 16:46:14 +09003631 @Override
satokab751aa2010-09-14 19:17:36 +09003632 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003633 // TODO: Make this work even for non-current users?
3634 if (!calledFromValidUser()) {
3635 return null;
3636 }
3637 synchronized (mMethodMap) {
3638 return getCurrentInputMethodSubtypeLocked();
3639 }
3640 }
3641
3642 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003643 if (mCurMethodId == null) {
3644 return null;
3645 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003646 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003647 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3648 if (imi == null || imi.getSubtypeCount() == 0) {
3649 return null;
satok4e4569d2010-11-19 18:45:53 +09003650 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003651 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003652 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3653 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003654 if (subtypeId == NOT_A_SUBTYPE_ID) {
3655 // If there are no selected subtypes, the framework will try to find
3656 // the most applicable subtype from explicitly or implicitly enabled
3657 // subtypes.
3658 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003659 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003660 // If there is only one explicitly or implicitly enabled subtype,
3661 // just returns it.
3662 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3663 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3664 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003665 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003666 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003667 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003668 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003669 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003670 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3671 true);
satok4e4569d2010-11-19 18:45:53 +09003672 }
satok3ef8b292010-11-23 06:06:29 +09003673 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003674 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003675 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003676 }
3677 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003678 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003679 }
3680
satok4e4569d2010-11-19 18:45:53 +09003681 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003682 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003683 @Override
satok4e4569d2010-11-19 18:45:53 +09003684 public List getShortcutInputMethodsAndSubtypes() {
3685 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003686 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003687 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003688 // If there are no selected shortcut subtypes, the framework will try to find
3689 // the most applicable subtype from all subtypes whose mode is
3690 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003691 Pair<InputMethodInfo, InputMethodSubtype> info =
3692 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003693 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003694 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003695 ret.add(info.first);
3696 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003697 }
satok3da92232011-01-11 22:46:30 +09003698 return ret;
satokf3db1af2010-11-23 13:34:33 +09003699 }
satokf3db1af2010-11-23 13:34:33 +09003700 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3701 ret.add(imi);
3702 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3703 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003704 }
3705 }
satokf3db1af2010-11-23 13:34:33 +09003706 return ret;
satok4e4569d2010-11-19 18:45:53 +09003707 }
3708 }
3709
satok42c5a162011-05-26 16:46:14 +09003710 @Override
satokb66d2872010-11-10 01:04:04 +09003711 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003712 // TODO: Make this work even for non-current users?
3713 if (!calledFromValidUser()) {
3714 return false;
3715 }
satokb66d2872010-11-10 01:04:04 +09003716 synchronized (mMethodMap) {
3717 if (subtype != null && mCurMethodId != null) {
3718 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003719 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003720 if (subtypeId != NOT_A_SUBTYPE_ID) {
3721 setInputMethodLocked(mCurMethodId, subtypeId);
3722 return true;
3723 }
3724 }
3725 return false;
3726 }
3727 }
3728
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003729 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003730 private static class InputMethodFileManager {
3731 private static final String SYSTEM_PATH = "system";
3732 private static final String INPUT_METHOD_PATH = "inputmethod";
3733 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3734 private static final String NODE_SUBTYPES = "subtypes";
3735 private static final String NODE_SUBTYPE = "subtype";
3736 private static final String NODE_IMI = "imi";
3737 private static final String ATTR_ID = "id";
3738 private static final String ATTR_LABEL = "label";
3739 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003740 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003741 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003742 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003743 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3744 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3745 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003746 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003747 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3748 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003749 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003750 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003751 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003752 if (methodMap == null) {
3753 throw new NullPointerException("methodMap is null");
3754 }
3755 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003756 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003757 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3758 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003759 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003760 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003761 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3762 }
3763 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3764 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3765 if (!subtypeFile.exists()) {
3766 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003767 writeAdditionalInputMethodSubtypes(
3768 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003769 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003770 readAdditionalInputMethodSubtypes(
3771 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003772 }
3773 }
3774
3775 private void deleteAllInputMethodSubtypes(String imiId) {
3776 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003777 mAdditionalSubtypesMap.remove(imiId);
3778 writeAdditionalInputMethodSubtypes(
3779 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003780 }
3781 }
3782
3783 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003784 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003785 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003786 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003787 final int N = additionalSubtypes.length;
3788 for (int i = 0; i < N; ++i) {
3789 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003790 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003791 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003792 } else {
3793 Slog.w(TAG, "Duplicated subtype definition found: "
3794 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003795 }
3796 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003797 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3798 writeAdditionalInputMethodSubtypes(
3799 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003800 }
3801 }
3802
3803 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3804 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003805 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003806 }
3807 }
3808
3809 private static void writeAdditionalInputMethodSubtypes(
3810 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3811 HashMap<String, InputMethodInfo> methodMap) {
3812 // Safety net for the case that this function is called before methodMap is set.
3813 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3814 FileOutputStream fos = null;
3815 try {
3816 fos = subtypesFile.startWrite();
3817 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003818 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003819 out.startDocument(null, true);
3820 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3821 out.startTag(null, NODE_SUBTYPES);
3822 for (String imiId : allSubtypes.keySet()) {
3823 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3824 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3825 continue;
3826 }
3827 out.startTag(null, NODE_IMI);
3828 out.attribute(null, ATTR_ID, imiId);
3829 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3830 final int N = subtypesList.size();
3831 for (int i = 0; i < N; ++i) {
3832 final InputMethodSubtype subtype = subtypesList.get(i);
3833 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003834 if (subtype.hasSubtypeId()) {
3835 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3836 String.valueOf(subtype.getSubtypeId()));
3837 }
satoke7c6998e2011-06-03 17:57:59 +09003838 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3839 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3840 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003841 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3842 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003843 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3844 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3845 out.attribute(null, ATTR_IS_AUXILIARY,
3846 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003847 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3848 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003849 out.endTag(null, NODE_SUBTYPE);
3850 }
3851 out.endTag(null, NODE_IMI);
3852 }
3853 out.endTag(null, NODE_SUBTYPES);
3854 out.endDocument();
3855 subtypesFile.finishWrite(fos);
3856 } catch (java.io.IOException e) {
3857 Slog.w(TAG, "Error writing subtypes", e);
3858 if (fos != null) {
3859 subtypesFile.failWrite(fos);
3860 }
3861 }
3862 }
3863
3864 private static void readAdditionalInputMethodSubtypes(
3865 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3866 if (allSubtypes == null || subtypesFile == null) return;
3867 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003868 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003869 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003870 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003871 int type = parser.getEventType();
3872 // Skip parsing until START_TAG
3873 while ((type = parser.next()) != XmlPullParser.START_TAG
3874 && type != XmlPullParser.END_DOCUMENT) {}
3875 String firstNodeName = parser.getName();
3876 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3877 throw new XmlPullParserException("Xml doesn't start with subtypes");
3878 }
3879 final int depth =parser.getDepth();
3880 String currentImiId = null;
3881 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3882 while (((type = parser.next()) != XmlPullParser.END_TAG
3883 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3884 if (type != XmlPullParser.START_TAG)
3885 continue;
3886 final String nodeName = parser.getName();
3887 if (NODE_IMI.equals(nodeName)) {
3888 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3889 if (TextUtils.isEmpty(currentImiId)) {
3890 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3891 continue;
3892 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003893 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003894 allSubtypes.put(currentImiId, tempSubtypesArray);
3895 } else if (NODE_SUBTYPE.equals(nodeName)) {
3896 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3897 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3898 continue;
3899 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003900 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003901 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003902 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003903 parser.getAttributeValue(null, ATTR_LABEL));
3904 final String imeSubtypeLocale =
3905 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003906 final String languageTag =
3907 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003908 final String imeSubtypeMode =
3909 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3910 final String imeSubtypeExtraValue =
3911 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003912 final boolean isAuxiliary = "1".equals(String.valueOf(
3913 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003914 final boolean isAsciiCapable = "1".equals(String.valueOf(
3915 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003916 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003917 .setSubtypeNameResId(label)
3918 .setSubtypeIconResId(icon)
3919 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003920 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003921 .setSubtypeMode(imeSubtypeMode)
3922 .setSubtypeExtraValue(imeSubtypeExtraValue)
3923 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003924 .setIsAsciiCapable(isAsciiCapable);
3925 final String subtypeIdString =
3926 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3927 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003928 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003929 }
3930 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003931 }
3932 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003933 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3934 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003935 return;
satoke7c6998e2011-06-03 17:57:59 +09003936 }
3937 }
3938 }
3939
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003940 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3941 @NonNull
3942 private final Handler mHandler;
3943
3944 LocalServiceImpl(@NonNull final Handler handler) {
3945 mHandler = handler;
3946 }
3947
3948 @Override
3949 public void setInteractive(boolean interactive) {
3950 // Do everything in handler so as not to block the caller.
3951 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3952 interactive ? 1 : 0, 0));
3953 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003954
3955 @Override
3956 public void switchInputMethod(boolean forwardDirection) {
3957 // Do everything in handler so as not to block the caller.
3958 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3959 forwardDirection ? 1 : 0, 0));
3960 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003961
3962 @Override
3963 public void hideCurrentInputMethod() {
3964 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3965 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3966 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003967 }
3968
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003969 private static String imeWindowStatusToString(final int imeWindowVis) {
3970 final StringBuilder sb = new StringBuilder();
3971 boolean first = true;
3972 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3973 sb.append("Active");
3974 first = false;
3975 }
3976 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3977 if (!first) {
3978 sb.append("|");
3979 }
3980 sb.append("Visible");
3981 }
3982 return sb.toString();
3983 }
3984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003986 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3987 @Nullable Uri contentUri, @Nullable String packageName) {
3988 if (!calledFromValidUser()) {
3989 return null;
3990 }
3991
3992 if (token == null) {
3993 throw new NullPointerException("token");
3994 }
3995 if (packageName == null) {
3996 throw new NullPointerException("packageName");
3997 }
3998 if (contentUri == null) {
3999 throw new NullPointerException("contentUri");
4000 }
4001 final String contentUriScheme = contentUri.getScheme();
4002 if (!"content".equals(contentUriScheme)) {
4003 throw new InvalidParameterException("contentUri must have content scheme");
4004 }
4005
4006 synchronized (mMethodMap) {
4007 final int uid = Binder.getCallingUid();
4008 if (mCurMethodId == null) {
4009 return null;
4010 }
4011 if (mCurToken != token) {
4012 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4013 + " token=" + token);
4014 return null;
4015 }
4016 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4017 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4018 // nature of our system. Let's compare it with our internal record.
4019 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4020 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4021 + mCurAttribute.packageName + " packageName=" + packageName);
4022 return null;
4023 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004024 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004025 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004026 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004027 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004028 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4029 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4030 imeUserId);
4031 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4032 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4033 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4034 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4035 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4036 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4037 // actually allowed to "uid", which is guaranteed to be the IME's one.
4038 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4039 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004040 }
4041 }
4042
4043 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004044 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4045 if (!calledFromValidUser()) {
4046 return;
4047 }
4048 synchronized (mMethodMap) {
4049 if (!calledWithValidToken(token)) {
4050 return;
4051 }
4052 if (mCurClient != null && mCurClient.client != null) {
4053 mInFullscreenMode = fullscreen;
4054 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4055 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4056 }
4057 }
4058 }
4059
4060 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4062 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4063 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4066 + Binder.getCallingPid()
4067 + ", uid=" + Binder.getCallingUid());
4068 return;
4069 }
4070
4071 IInputMethod method;
4072 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004073 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 synchronized (mMethodMap) {
4078 p.println("Current Input Method Manager state:");
4079 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004080 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 for (int i=0; i<N; i++) {
4082 InputMethodInfo info = mMethodList.get(i);
4083 p.println(" InputMethod #" + i + ":");
4084 info.dump(p, " ");
4085 }
4086 p.println(" Clients:");
4087 for (ClientState ci : mClients.values()) {
4088 p.println(" Client " + ci + ":");
4089 p.println(" client=" + ci.client);
4090 p.println(" inputContext=" + ci.inputContext);
4091 p.println(" sessionRequested=" + ci.sessionRequested);
4092 p.println(" curSession=" + ci.curSession);
4093 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004094 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004096 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4097 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004098 focusedWindowClient = mCurFocusedWindowClient;
4099 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4101 + " mBoundToMethod=" + mBoundToMethod);
4102 p.println(" mCurToken=" + mCurToken);
4103 p.println(" mCurIntent=" + mCurIntent);
4104 method = mCurMethod;
4105 p.println(" mCurMethod=" + mCurMethod);
4106 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004107 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 p.println(" mShowRequested=" + mShowRequested
4109 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4110 + " mShowForced=" + mShowForced
4111 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004112 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004113 p.println(" mCurUserActionNotificationSequenceNumber="
4114 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004115 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004116 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004117 p.println(" mSwitchingController:");
4118 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004119 p.println(" mSettings:");
4120 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004122
Jeff Brownb88102f2010-09-08 11:49:43 -07004123 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 pw.flush();
4126 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004127 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4128 } catch (IOException | RemoteException e) {
4129 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004131 } else {
4132 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004134
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004135 if (focusedWindowClient != null && client != focusedWindowClient) {
4136 p.println(" ");
4137 p.println("Warning: Current input method client doesn't match the last focused. "
4138 + "window.");
4139 p.println("Dumping input method client in the last focused window just in case.");
4140 p.println(" ");
4141 pw.flush();
4142 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004143 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4144 } catch (IOException | RemoteException e) {
4145 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004146 }
4147 }
4148
Jeff Brownb88102f2010-09-08 11:49:43 -07004149 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 pw.flush();
4152 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004153 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4154 } catch (IOException | RemoteException e) {
4155 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004157 } else {
4158 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
4160 }
4161}