blob: b9c47e759bda117215dd387ac8314367ef019b2f [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;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
Yohei Yukawa33e81792015-11-17 21:14:42 -0800179 static final int MSG_UNBIND_CLIENT = 3000;
180 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700181 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700182 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900183 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800184 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800185 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
satok01038492012-04-09 21:08:27 +0900187 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
188
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700189 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
190
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700191 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
satokf9f01002011-05-19 21:31:50 +0900193 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
194
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900195 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900196 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900197
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700198 @Retention(SOURCE)
199 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
200 private @interface HardKeyboardBehavior {
201 int WIRELESS_AFFORDANCE = 0;
202 int WIRED_AFFORDANCE = 1;
203 }
satok4e4569d2010-11-19 18:45:53 +0900204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800206 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900208 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700211 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700213 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900214 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900215 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900216 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800217 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900219 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 // All known input methods. mMethodMap also serves as the global
222 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700223 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
224 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900225 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700226 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900227 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
Yohei Yukawae0733062017-02-09 22:49:35 -0800229 /**
230 * Tracks how many times {@link #mMethodMap} was updated.
231 */
232 @GuardedBy("mMethodMap")
233 private int mMethodMapUpdateCount = 0;
234
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700235 // Used to bring IME service up to visible adjustment while it is being shown.
236 final ServiceConnection mVisibleConnection = new ServiceConnection() {
237 @Override public void onServiceConnected(ComponentName name, IBinder service) {
238 }
239
240 @Override public void onServiceDisconnected(ComponentName name) {
241 }
242 };
243 boolean mVisibleBound = false;
244
satok7cfc0ed2011-06-20 21:29:36 +0900245 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700246 private NotificationManager mNotificationManager;
247 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700248 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400249 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700250 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900251 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900252 private boolean mNotificationShown;
253
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900254 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 final ClientState client;
256 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700257
258 IInputMethodSession session;
259 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 @Override
262 public String toString() {
263 return "SessionState{uid " + client.uid + " pid " + client.pid
264 + " method " + Integer.toHexString(
265 System.identityHashCode(method))
266 + " session " + Integer.toHexString(
267 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700268 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 + "}";
270 }
271
272 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700273 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 client = _client;
275 method = _method;
276 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700277 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
279 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
Jeff Brownc28867a2013-03-26 15:42:39 -0700281 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 final IInputMethodClient client;
283 final IInputContext inputContext;
284 final int uid;
285 final int pid;
286 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 boolean sessionRequested;
289 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 @Override
292 public String toString() {
293 return "ClientState{" + Integer.toHexString(
294 System.identityHashCode(this)) + " uid " + uid
295 + " pid " + pid + "}";
296 }
297
298 ClientState(IInputMethodClient _client, IInputContext _inputContext,
299 int _uid, int _pid) {
300 client = _client;
301 inputContext = _inputContext;
302 uid = _uid;
303 pid = _pid;
304 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
305 }
306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700308 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700311 * Set once the system is ready to run third party code.
312 */
313 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700315 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700316 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
317 * method. This is to be synchronized with the secure settings keyed with
318 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
319 *
320 * <p>This can be transiently {@code null} when the system is re-initializing input method
321 * settings, e.g., the system locale is just changed.</p>
322 *
323 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
324 * {@link InputMethodManagerService}.</p>
325 *
326 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700328 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * The current binding sequence number, incremented every time there is
333 * a new bind performed.
334 */
335 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * The client that is currently bound to an input method.
339 */
340 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800343 * The last window token that we confirmed to be focused. This is always updated upon reports
344 * from the input method client. If the window state is already changed before the report is
345 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700346 */
347 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700349 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800350 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
351 */
352 ClientState mCurFocusedWindowClient;
353
354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * The input context last provided by the current client.
356 */
357 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700360 * The missing method flags for the input context last provided by the current client.
361 *
362 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
363 */
364 int mCurInputContextMissingMethods;
365
366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * The attributes last provided by the current client.
368 */
369 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700372 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700374 *
375 * <p>This can be {@code null} when no input method is connected.</p>
376 *
377 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700379 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /**
satokab751aa2010-09-14 19:17:36 +0900383 * The current subtype of the current input method.
384 */
385 private InputMethodSubtype mCurrentSubtype;
386
satok4e4569d2010-11-19 18:45:53 +0900387 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900388 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700389 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900390
John Spurlocke0980502013-10-25 11:59:29 -0400391 // Was the keyguard locked when this client became current?
392 private boolean mCurClientInKeyguard;
393
satokab751aa2010-09-14 19:17:36 +0900394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 * Set to true if our ServiceConnection is currently actively bound to
396 * a service (whether or not we have gotten its IBinder back yet).
397 */
398 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 /**
401 * Set if the client has asked for the input method to be shown.
402 */
403 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 /**
406 * Set if we were explicitly told to show the input method.
407 */
408 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 /**
411 * Set if we were forced to be shown.
412 */
413 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
416 * Set if we last told the input method to show itself.
417 */
418 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800421 * {@code true} if the current input method is in fullscreen mode.
422 */
423 boolean mInFullscreenMode;
424
425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * The Intent used to connect to the current input method.
427 */
428 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * The token we have made for the currently active input method, to
432 * identify it in the future.
433 */
434 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * If non-null, this is the input method service we are currently connected
438 * to.
439 */
440 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /**
443 * Time that we last initiated a bind to the input method, to determine
444 * if we should try to disconnect and reconnect to it.
445 */
446 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 /**
449 * Have we called mCurMethod.bindInput()?
450 */
451 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 /**
454 * Currently enabled session. Only touched by service thread, not
455 * protected by a lock.
456 */
457 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700460 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700462 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900464 int mCurUserActionNotificationSequenceNumber = 0;
465
Joe Onorato857fd9b2011-01-27 15:08:35 -0800466 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900467
468 /**
469 * A set of status bits regarding the active IME.
470 *
471 * <p>This value is a combination of following two bits:</p>
472 * <dl>
473 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
474 * <dd>
475 * If this bit is ON, connected IME is ready to accept touch/key events.
476 * </dd>
477 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
478 * <dd>
479 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
480 * </dd>
481 * </dl>
482 * <em>Do not update this value outside of setImeWindowStatus.</em>
483 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800484 int mImeWindowVis;
485
Ken Wakasa05dbb652011-08-22 15:22:43 +0900486 private AlertDialog.Builder mDialogBuilder;
487 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700488 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900489 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700490 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900491 private InputMethodInfo[] mIms;
492 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800493 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700494 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800495 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900496 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
497 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500498 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700499 @HardKeyboardBehavior
500 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700503 int mUserId;
504 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700505 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800506 String mLastEnabled = "";
507
Yohei Yukawa81482972015-06-04 00:58:59 -0700508 /**
509 * <em>This constructor must be called within the lock.</em>
510 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 SettingsObserver(Handler handler) {
512 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700513 }
514
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800515 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700516 if (mRegistered && mUserId == userId) {
517 return;
518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700520 if (mRegistered) {
521 mContext.getContentResolver().unregisterContentObserver(this);
522 mRegistered = false;
523 }
524 if (mUserId != userId) {
525 mLastEnabled = "";
526 mUserId = userId;
527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700529 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900530 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700531 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900532 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700533 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700534 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700535 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800536 resolver.registerContentObserver(Settings.Secure.getUriFor(
537 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700538 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800540
Michael Wright7b5a96b2014-08-09 19:28:42 -0700541 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800542 final Uri showImeUri = Settings.Secure.getUriFor(
543 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
544 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
545 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700547 if (showImeUri.equals(uri)) {
548 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800549 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
550 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
551 mContext.getContentResolver(),
552 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
553 0, mUserId) == 1;
554 if (mAccessibilityRequestingNoSoftKeyboard) {
555 final boolean showRequested = mShowRequested;
556 hideCurrentInputLocked(0, null);
557 mShowRequested = showRequested;
558 } else if (mShowRequested) {
559 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
560 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700561 } else {
562 boolean enabledChanged = false;
563 String newEnabled = mSettings.getEnabledInputMethodsStr();
564 if (!mLastEnabled.equals(newEnabled)) {
565 mLastEnabled = newEnabled;
566 enabledChanged = true;
567 }
568 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700572
573 @Override
574 public String toString() {
575 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
576 + " mLastEnabled=" + mLastEnabled + "}";
577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
Yohei Yukawa79247822017-01-23 15:26:15 -0800580 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900581 @Override
582 public void onReceive(Context context, Intent intent) {
583 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700584 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900585 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700586 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900587 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800588 } else if (Intent.ACTION_USER_ADDED.equals(action)
589 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100590 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800591 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700592 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
593 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
594 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
595 final String prevValue = intent.getStringExtra(
596 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
597 final String newValue = intent.getStringExtra(
598 Intent.EXTRA_SETTING_NEW_VALUE);
599 restoreEnabledInputMethods(mContext, prevValue, newValue);
600 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800601 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800602 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900603 } else {
604 Slog.w(TAG, "Unexpected intent " + intent);
605 }
606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800608
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800609 /**
610 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
611 *
612 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
613 * the users. We should ignore this event if this is about any background user's locale.</p>
614 *
615 * <p>Caution: This method must not be called when system is not ready.</p>
616 */
617 void onActionLocaleChanged() {
618 synchronized (mMethodMap) {
619 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
620 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
621 return;
622 }
623 buildInputMethodListLocked(true);
624 // If the locale is changed, needs to reset the default ime
625 resetDefaultImeLocked(mContext);
626 updateFromSettingsLocked(true);
627 mLastSystemLocales = possibleNewLocale;
628 }
629 }
630
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700631 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
632 // does not attempt to validate on the fly with any installed device policy, so must only
633 // be run in the context of initial device setup.
634 //
635 // TODO: Move this method to InputMethodUtils with adding unit tests.
636 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
637 if (DEBUG_RESTORE) {
638 Slog.i(TAG, "Restoring enabled input methods:");
639 Slog.i(TAG, "prev=" + prevValue);
640 Slog.i(TAG, " new=" + newValue);
641 }
642 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900643 ArrayMap<String, ArraySet<String>> prevMap =
644 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
645 ArrayMap<String, ArraySet<String>> newMap =
646 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700647
648 // Merge the restored ime+subtype enabled states into the live state
649 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
650 final String imeId = entry.getKey();
651 ArraySet<String> prevSubtypes = prevMap.get(imeId);
652 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700653 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700654 prevMap.put(imeId, prevSubtypes);
655 }
656 prevSubtypes.addAll(entry.getValue());
657 }
658
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700659 final String mergedImesAndSubtypesString =
660 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700661 if (DEBUG_RESTORE) {
662 Slog.i(TAG, "Merged IME string:");
663 Slog.i(TAG, " " + mergedImesAndSubtypesString);
664 }
665 Settings.Secure.putString(context.getContentResolver(),
666 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
667 }
668
Yohei Yukawac4e44912017-02-09 19:30:22 -0800669 final class MyPackageMonitor extends PackageMonitor {
670 /**
671 * Set of packages to be monitored.
672 *
673 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
674 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
675 * dynamically unless the entire package is updated, which also always triggers package
676 * rescanning.</p>
677 */
678 @GuardedBy("mMethodMap")
679 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
680
681 @GuardedBy("mMethodMap")
682 void clearPackagesToMonitorComponentChangeLocked() {
683 mPackagesToMonitorComponentChange.clear();
684 }
685
686 @GuardedBy("mMethodMap")
687 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
688 mPackagesToMonitorComponentChange.add(packageName);
689 }
690
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900691 private boolean isChangingPackagesOfCurrentUser() {
692 final int userId = getChangingUserId();
693 final boolean retval = userId == mSettings.getCurrentUserId();
694 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900695 if (!retval) {
696 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
697 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900698 }
699 return retval;
700 }
701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800703 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900704 if (!isChangingPackagesOfCurrentUser()) {
705 return false;
706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900708 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 final int N = mMethodList.size();
710 if (curInputMethodId != null) {
711 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800712 InputMethodInfo imi = mMethodList.get(i);
713 if (imi.getId().equals(curInputMethodId)) {
714 for (String pkg : packages) {
715 if (imi.getPackageName().equals(pkg)) {
716 if (!doit) {
717 return true;
718 }
satok723a27e2010-11-11 14:58:11 +0900719 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800720 chooseNewDefaultIMELocked();
721 return true;
722 }
723 }
724 }
725 }
726 }
727 }
728 return false;
729 }
730
731 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800732 public boolean onPackageChanged(String packageName, int uid, String[] components) {
733 // If this package is in the watch list, we want to check it.
734 synchronized (mMethodMap) {
735 return mPackagesToMonitorComponentChange.contains(packageName);
736 }
737 }
738
739 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800740 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900741 if (!isChangingPackagesOfCurrentUser()) {
742 return;
743 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800744 synchronized (mMethodMap) {
745 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900746 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800747 final int N = mMethodList.size();
748 if (curInputMethodId != null) {
749 for (int i=0; i<N; i++) {
750 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900751 final String imiId = imi.getId();
752 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800753 curIm = imi;
754 }
satoke7c6998e2011-06-03 17:57:59 +0900755
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800756 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900757 if (isPackageModified(imi.getPackageName())) {
758 mFileManager.deleteAllInputMethodSubtypes(imiId);
759 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800760 if (change == PACKAGE_TEMPORARY_CHANGE
761 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800762 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800763 + imi.getComponent());
764 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
766 }
767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800768
Yohei Yukawa94e33302016-02-12 19:37:03 -0800769 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800773 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800774 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800775 if (change == PACKAGE_TEMPORARY_CHANGE
776 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800777 ServiceInfo si = null;
778 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900779 si = mIPackageManager.getServiceInfo(
780 curIm.getComponent(), 0, mSettings.getCurrentUserId());
781 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800782 }
783 if (si == null) {
784 // Uh oh, current input method is no longer around!
785 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800786 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900787 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800788 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800789 changed = true;
790 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800791 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900792 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800793 }
794 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800795 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800796 }
satokab751aa2010-09-14 19:17:36 +0900797
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800798 if (curIm == null) {
799 // We currently don't have a default input method... is
800 // one now available?
801 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700802 } else if (!changed && isPackageModified(curIm.getPackageName())) {
803 // Even if the current input method is still available, mCurrentSubtype could
804 // be obsolete when the package is modified in practice.
805 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800808 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800809 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811 }
812 }
813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
Jeff Brownc28867a2013-03-26 15:42:39 -0700815 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900816 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700817 private final IInputMethod mMethod;
818 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819
Jeff Brownc28867a2013-03-26 15:42:39 -0700820 MethodCallback(InputMethodManagerService imms, IInputMethod method,
821 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900822 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700823 mMethod = method;
824 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800826
satoke7c6998e2011-06-03 17:57:59 +0900827 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700828 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700829 long ident = Binder.clearCallingIdentity();
830 try {
831 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
832 } finally {
833 Binder.restoreCallingIdentity(ident);
834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
836 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800837
satok01038492012-04-09 21:08:27 +0900838 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700839 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900840 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700841 public void onHardKeyboardStatusChange(boolean available) {
842 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
843 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900844 }
845
Michael Wright7b5a96b2014-08-09 19:28:42 -0700846 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900847 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700848 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900849 }
850 synchronized(mMethodMap) {
851 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
852 && mSwitchingDialog.isShowing()) {
853 mSwitchingDialogTitleView.findViewById(
854 com.android.internal.R.id.hard_keyboard_section).setVisibility(
855 available ? View.VISIBLE : View.GONE);
856 }
857 }
858 }
859 }
860
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800861 public static final class Lifecycle extends SystemService {
862 private InputMethodManagerService mService;
863
864 public Lifecycle(Context context) {
865 super(context);
866 mService = new InputMethodManagerService(context);
867 }
868
869 @Override
870 public void onStart() {
871 LocalServices.addService(InputMethodManagerInternal.class,
872 new LocalServiceImpl(mService.mHandler));
873 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
874 }
875
876 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800877 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700878 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800879 // TODO: Dispatch this to a worker thread as needed.
880 mService.onSwitchUser(userHandle);
881 }
882
883 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800884 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700885 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800886 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800887 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
888 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
889 .getService(Context.STATUS_BAR_SERVICE);
890 mService.systemRunning(statusBarService);
891 }
892 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800893
894 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700895 public void onUnlockUser(final @UserIdInt int userHandle) {
896 // Called on ActivityManager thread.
897 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700898 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800899 }
900 }
901
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800902 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800903 synchronized(mMethodMap) {
904 final int currentUserId = mSettings.getCurrentUserId();
905 if (DEBUG) {
906 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
907 }
908 if (userId != currentUserId) {
909 return;
910 }
911 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800912 if (mSystemReady) {
913 // We need to rebuild IMEs.
914 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
915 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
916 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800917 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800918 }
919
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800920 void onSwitchUser(@UserIdInt int userId) {
921 synchronized (mMethodMap) {
922 switchUserLocked(userId);
923 }
924 }
925
Seigo Nonaka7309b122015-08-17 18:34:13 -0700926 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900927 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800929 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700931 // Note: SettingsObserver doesn't register observers in its constructor.
932 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 mIWindowManager = IWindowManager.Stub.asInterface(
934 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700935 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800936 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900937 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public void executeMessage(Message msg) {
939 handleMessage(msg);
940 }
Mita Yuned218c72012-12-06 17:18:25 -0800941 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800942 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800943 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900944 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700945 mHasFeature = context.getPackageManager().hasSystemFeature(
946 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500947 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700948 mHardKeyboardBehavior = mContext.getResources().getInteger(
949 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900950
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400951 Bundle extras = new Bundle();
952 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
953 mImeSwitcherNotification = new Notification.Builder(mContext)
954 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
955 .setWhen(0)
956 .setOngoing(true)
957 .addExtras(extras)
958 .setCategory(Notification.CATEGORY_SYSTEM)
959 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400960
satok7cfc0ed2011-06-20 21:29:36 +0900961 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900962 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900963
964 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900965
satok7cfc0ed2011-06-20 21:29:36 +0900966 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900967 int userId = 0;
968 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800969 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 } catch (RemoteException e) {
971 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
972 }
satok913a8922010-08-26 21:53:41 +0900973
satokd87c2592010-09-29 11:52:06 +0900974 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900975 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800976 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700977
Kenny Guy2a764942014-04-02 13:29:20 +0100978 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900979 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800980 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
981 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900982 }
983
satok5b927c432012-05-01 20:09:34 +0900984 private void resetDefaultImeLocked(Context context) {
985 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800986 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900987 return;
988 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800989 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800990 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800991 if (suitableImes.isEmpty()) {
992 Slog.i(TAG, "No default found");
993 return;
satok5b927c432012-05-01 20:09:34 +0900994 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800995 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -0800996 if (DEBUG) {
997 Slog.i(TAG, "Default found, using " + defIm.getId());
998 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800999 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001000 }
1001
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001002 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001003 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1004 + " currentUserId=" + mSettings.getCurrentUserId());
1005
Yohei Yukawa81482972015-06-04 00:58:59 -07001006 // ContentObserver should be registered again when the user is changed
1007 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001008
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001009 // If the system is not ready or the device is not yed unlocked by the user, then we use
1010 // copy-on-write settings.
1011 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001012 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001013 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001014 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001015 // InputMethodFileManager should be reset when the user is changed
1016 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001017 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001018
1019 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1020 + " defaultImiId=" + defaultImiId);
1021
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001022 // For secondary users, the list of enabled IMEs may not have been updated since the
1023 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1024 // not be empty even if the IME has been uninstalled by the primary user.
1025 // Even in such cases, IMMS works fine because it will find the most applicable
1026 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001027 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001028 mLastSystemLocales = mRes.getConfiguration().getLocales();
1029
1030 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1031 if (mSystemReady) {
1032 hideCurrentInputLocked(0, null);
1033 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1034 buildInputMethodListLocked(initialUserSwitch);
1035 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1036 // This is the first time of the user switch and
1037 // set the current ime to the proper one.
1038 resetDefaultImeLocked(mContext);
1039 }
1040 updateFromSettingsLocked(true);
1041 try {
1042 startInputInnerLocked();
1043 } catch (RuntimeException e) {
1044 Slog.w(TAG, "Unexpected exception", e);
1045 }
1046 }
1047
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001048 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001049 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1050 mSettings.getEnabledInputMethodListLocked(), newUserId,
1051 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001052 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001053
1054 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1055 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001056 }
1057
Kenny Guy2a764942014-04-02 13:29:20 +01001058 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001059 mSettings.setCurrentProfileIds(
1060 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001061 }
1062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 @Override
1064 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1065 throws RemoteException {
1066 try {
1067 return super.onTransact(code, data, reply, flags);
1068 } catch (RuntimeException e) {
1069 // The input method manager only throws security exceptions, so let's
1070 // log all others.
1071 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001072 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074 throw e;
1075 }
1076 }
1077
Svetoslav Ganova0027152013-06-25 14:59:53 -07001078 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001079 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001080 if (DEBUG) {
1081 Slog.d(TAG, "--- systemReady");
1082 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001083 if (!mSystemReady) {
1084 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001085 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001086 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001087 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001088 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001089 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1090 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001091 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001092 if (mStatusBar != null) {
1093 mStatusBar.setIconVisibility(mSlotIme, false);
1094 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001095 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001096 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1097 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001098 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001099 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001100 mHardKeyboardListener);
1101 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001102
1103 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1104 mSettingsObserver.registerContentObserverLocked(currentUserId);
1105
1106 final IntentFilter broadcastFilter = new IntentFilter();
1107 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1108 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1109 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1110 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1111 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1112 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1113
1114 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1115 resetDefaultImeLocked(mContext);
1116 updateFromSettingsLocked(true);
1117 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1118 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1119 mContext.getBasePackageName());
1120
Dianne Hackborncc278702009-09-02 23:07:23 -07001121 try {
1122 startInputInnerLocked();
1123 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001124 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001125 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001126 }
1127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001129
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001130 // ---------------------------------------------------------------------------------------
1131 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1132 // 1) it comes from the system process
1133 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1134 private boolean calledFromValidUser() {
1135 final int uid = Binder.getCallingUid();
1136 final int userId = UserHandle.getUserId(uid);
1137 if (DEBUG) {
1138 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1139 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1140 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001141 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1142 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001143 }
Kenny Guy2a764942014-04-02 13:29:20 +01001144 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001145 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001146 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001147
1148 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1149 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1150 // must not manage background users' states in any functions.
1151 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1152 // by a token.
1153 if (mContext.checkCallingOrSelfPermission(
1154 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1155 == PackageManager.PERMISSION_GRANTED) {
1156 if (DEBUG) {
1157 Slog.d(TAG, "--- Access granted because the calling process has "
1158 + "the INTERACT_ACROSS_USERS_FULL permission");
1159 }
1160 return true;
1161 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001162 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001163 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1164 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001165 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001166 }
1167
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001168
1169 /**
1170 * Returns true iff the caller is identified to be the current input method with the token.
1171 * @param token The window token given to the input method when it was started.
1172 * @return true if and only if non-null valid token is specified.
1173 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001174 private boolean calledWithValidToken(@Nullable IBinder token) {
1175 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1176 if (DEBUG) {
1177 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1178 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1179 }
1180 return false;
1181 }
1182 if (token == null || token != mCurToken) {
1183 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1184 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1185 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001186 return false;
1187 }
1188 return true;
1189 }
1190
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 private boolean bindCurrentInputMethodService(
1192 Intent service, ServiceConnection conn, int flags) {
1193 if (service == null || conn == null) {
1194 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1195 return false;
1196 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001197 return mContext.bindServiceAsUser(service, conn, flags,
1198 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001199 }
1200
satoke7c6998e2011-06-03 17:57:59 +09001201 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001203 // TODO: Make this work even for non-current users?
1204 if (!calledFromValidUser()) {
1205 return Collections.emptyList();
1206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001208 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210 }
1211
satoke7c6998e2011-06-03 17:57:59 +09001212 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001214 // TODO: Make this work even for non-current users?
1215 if (!calledFromValidUser()) {
1216 return Collections.emptyList();
1217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001219 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 }
1221 }
1222
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001223 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001224 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001225 * @return enabled subtypes of the specified imi
1226 */
satoke7c6998e2011-06-03 17:57:59 +09001227 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001228 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001229 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001230 // TODO: Make this work even for non-current users?
1231 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001232 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001233 }
satok67ddf9c2010-11-17 09:45:54 +09001234 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001235 final InputMethodInfo imi;
1236 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001237 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001238 } else {
1239 imi = mMethodMap.get(imiId);
1240 }
1241 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001242 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001243 }
1244 return mSettings.getEnabledInputMethodSubtypeListLocked(
1245 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001246 }
1247 }
1248
satoke7c6998e2011-06-03 17:57:59 +09001249 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 public void addClient(IInputMethodClient client,
1251 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001252 if (!calledFromValidUser()) {
1253 return;
1254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 synchronized (mMethodMap) {
1256 mClients.put(client.asBinder(), new ClientState(client,
1257 inputContext, uid, pid));
1258 }
1259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
satoke7c6998e2011-06-03 17:57:59 +09001261 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001263 if (!calledFromValidUser()) {
1264 return;
1265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001267 ClientState cs = mClients.remove(client.asBinder());
1268 if (cs != null) {
1269 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001270 if (mCurClient == cs) {
1271 mCurClient = null;
1272 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001273 if (mCurFocusedWindowClient == cs) {
1274 mCurFocusedWindowClient = null;
1275 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 void executeOrSendMessage(IInterface target, Message msg) {
1281 if (target.asBinder() instanceof Binder) {
1282 mCaller.sendMessage(msg);
1283 } else {
1284 handleMessage(msg);
1285 msg.recycle();
1286 }
1287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001288
Yohei Yukawa33e81792015-11-17 21:14:42 -08001289 void unbindCurrentClientLocked(
1290 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001292 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 + mCurClient.client.asBinder());
1294 if (mBoundToMethod) {
1295 mBoundToMethod = false;
1296 if (mCurMethod != null) {
1297 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1298 MSG_UNBIND_INPUT, mCurMethod));
1299 }
1300 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001301
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001302 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1303 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001304 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1305 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
The Android Open Source Project10592532009-03-18 17:39:46 -07001309 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 private int getImeShowFlags() {
1314 int flags = 0;
1315 if (mShowForced) {
1316 flags |= InputMethod.SHOW_FORCED
1317 | InputMethod.SHOW_EXPLICIT;
1318 } else if (mShowExplicitlyRequested) {
1319 flags |= InputMethod.SHOW_EXPLICIT;
1320 }
1321 return flags;
1322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 private int getAppShowFlags() {
1325 int flags = 0;
1326 if (mShowForced) {
1327 flags |= InputMethodManager.SHOW_FORCED;
1328 } else if (!mShowExplicitlyRequested) {
1329 flags |= InputMethodManager.SHOW_IMPLICIT;
1330 }
1331 return flags;
1332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001334 InputBindResult attachNewInputLocked(
1335 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 if (!mBoundToMethod) {
1337 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1338 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1339 mBoundToMethod = true;
1340 }
1341 final SessionState session = mCurClient.curSession;
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001342 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOO(
1343 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
1344 session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001346 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001347 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001349 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001350 (session.channel != null ? session.channel.dup() : null),
1351 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001353
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001354 InputBindResult startInputLocked(
1355 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001356 IInputMethodClient client, IInputContext inputContext,
1357 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001358 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 // If no method is currently selected, do nothing.
1360 if (mCurMethodId == null) {
1361 return mNoBinding;
1362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 ClientState cs = mClients.get(client.asBinder());
1365 if (cs == null) {
1366 throw new IllegalArgumentException("unknown client "
1367 + client.asBinder());
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
Yohei Yukawa74750f22016-03-22 12:54:22 -07001370 if (attribute == null) {
1371 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1372 + " uid=" + cs.uid + " pid=" + cs.pid);
1373 return null;
1374 }
1375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 try {
1377 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1378 // Check with the window manager to make sure this client actually
1379 // has a window with focus. If not, reject. This is thread safe
1380 // because if the focus changes some time before or after, the
1381 // next client receiving focus that has any interest in input will
1382 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001383 if (DEBUG) {
1384 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1385 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 return null;
1388 }
1389 } catch (RemoteException e) {
1390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001391
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001392 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001393 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001394 }
1395
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001396 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001397 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001398 @NonNull EditorInfo attribute, int controlFlags,
1399 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001400 // If no method is currently selected, do nothing.
1401 if (mCurMethodId == null) {
1402 return mNoBinding;
1403 }
1404
Yohei Yukawad57ba672015-06-08 16:39:46 -07001405 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1406 attribute.packageName)) {
1407 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1408 + " uid=" + cs.uid + " package=" + attribute.packageName);
1409 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001410 }
1411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001413 // Was the keyguard locked when switching over to the new client?
1414 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 // If the client is changing, we need to switch over to the new
1416 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001417 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001418 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001419 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420
1421 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001422 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001423 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001424 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 // Bump up the sequence for this client and attach it.
1429 mCurSeq++;
1430 if (mCurSeq <= 0) mCurSeq = 1;
1431 mCurClient = cs;
1432 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001433 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 // Check if the input method is changing.
1437 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1438 if (cs.curSession != null) {
1439 // Fast case: if we are already connected to the input method,
1440 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001441 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001442 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
1444 if (mHaveConnection) {
1445 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 // Return to client, and we will get back with it when
1447 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001448 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001449 return new InputBindResult(null, null, mCurId, mCurSeq,
1450 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 } else if (SystemClock.uptimeMillis()
1452 < (mLastBindTime+TIME_TO_RECONNECT)) {
1453 // In this case we have connected to the service, but
1454 // don't yet have its interface. If it hasn't been too
1455 // long since we did the connection, we'll return to
1456 // the client and wait to get the service interface so
1457 // we can report back. If it has been too long, we want
1458 // to fall through so we can try a disconnect/reconnect
1459 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001460 return new InputBindResult(null, null, mCurId, mCurSeq,
1461 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1464 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466 }
1467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001469 return startInputInnerLocked();
1470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001472 InputBindResult startInputInnerLocked() {
1473 if (mCurMethodId == null) {
1474 return mNoBinding;
1475 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001477 if (!mSystemReady) {
1478 // If the system is not yet ready, we shouldn't be running third
1479 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001480 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1481 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1485 if (info == null) {
1486 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001488
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001489 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1492 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001493 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1494 com.android.internal.R.string.input_method_binding_label);
1495 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1496 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001497 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001498 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1499 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 mLastBindTime = SystemClock.uptimeMillis();
1501 mHaveConnection = true;
1502 mCurId = info.getId();
1503 mCurToken = new Binder();
1504 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001505 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001506 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 } catch (RemoteException e) {
1508 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001509 return new InputBindResult(null, null, mCurId, mCurSeq,
1510 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 } else {
1512 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001513 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 + mCurIntent);
1515 }
1516 return null;
1517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001518
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001519 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001520 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001521 IInputMethodClient client, IInputContext inputContext,
1522 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001523 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001524 if (!calledFromValidUser()) {
1525 return null;
1526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001528 if (DEBUG) {
1529 Slog.v(TAG, "startInput: reason="
1530 + InputMethodClient.getStartInputReason(startInputReason)
1531 + " client = " + client.asBinder()
1532 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001533 + " missingMethods="
1534 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001535 + " attribute=" + attribute
1536 + " controlFlags=#" + Integer.toHexString(controlFlags));
1537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 final long ident = Binder.clearCallingIdentity();
1539 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001540 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1541 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 } finally {
1543 Binder.restoreCallingIdentity(ident);
1544 }
1545 }
1546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001547
satoke7c6998e2011-06-03 17:57:59 +09001548 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 public void finishInput(IInputMethodClient client) {
1550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001551
satoke7c6998e2011-06-03 17:57:59 +09001552 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 public void onServiceConnected(ComponentName name, IBinder service) {
1554 synchronized (mMethodMap) {
1555 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1556 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001557 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001558 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001559 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001560 return;
1561 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001562 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001563 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1564 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001566 clearClientSessionLocked(mCurClient);
1567 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569 }
1570 }
1571 }
1572
Jeff Brownc28867a2013-03-26 15:42:39 -07001573 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1574 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 synchronized (mMethodMap) {
1576 if (mCurMethod != null && method != null
1577 && mCurMethod.asBinder() == method.asBinder()) {
1578 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001579 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001581 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001582 InputBindResult res = attachNewInputLocked(
1583 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 if (res.method != null) {
1585 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001586 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001588 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 }
1590 }
1591 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001592
1593 // Session abandoned. Close its associated input channel.
1594 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001597 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001598 if (mVisibleBound) {
1599 mContext.unbindService(mVisibleConnection);
1600 mVisibleBound = false;
1601 }
1602
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001603 if (mHaveConnection) {
1604 mContext.unbindService(this);
1605 mHaveConnection = false;
1606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001608 if (mCurToken != null) {
1609 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001610 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001611 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001612 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001613 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001614 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001615 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001616 } catch (RemoteException e) {
1617 }
1618 mCurToken = null;
1619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001620
The Android Open Source Project10592532009-03-18 17:39:46 -07001621 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001622 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001623 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001624
Yohei Yukawa33e81792015-11-17 21:14:42 -08001625 void resetCurrentMethodAndClient(
1626 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001627 mCurMethodId = null;
1628 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001629 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001630 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001631
1632 void requestClientSessionLocked(ClientState cs) {
1633 if (!cs.sessionRequested) {
1634 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1635 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1636 cs.sessionRequested = true;
1637 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1638 MSG_CREATE_SESSION, mCurMethod, channels[1],
1639 new MethodCallback(this, mCurMethod, channels[0])));
1640 }
1641 }
1642
1643 void clearClientSessionLocked(ClientState cs) {
1644 finishSessionLocked(cs.curSession);
1645 cs.curSession = null;
1646 cs.sessionRequested = false;
1647 }
1648
1649 private void finishSessionLocked(SessionState sessionState) {
1650 if (sessionState != null) {
1651 if (sessionState.session != null) {
1652 try {
1653 sessionState.session.finishSession();
1654 } catch (RemoteException e) {
1655 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001656 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001657 }
1658 sessionState.session = null;
1659 }
1660 if (sessionState.channel != null) {
1661 sessionState.channel.dispose();
1662 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001663 }
1664 }
1665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001667 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 if (mCurMethod != null) {
1669 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001670 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001672
Jeff Brownc28867a2013-03-26 15:42:39 -07001673 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001674 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 mCurMethod = null;
1676 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001677 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001678 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001679 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001680 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001682
satoke7c6998e2011-06-03 17:57:59 +09001683 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001685 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1686 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1687 // which is irregular but can eventually happen for everyone just by continuing using the
1688 // device. Thus it is important to make sure that all the internal states are properly
1689 // refreshed when this method is called back. Running
1690 // adb install -r <APK that implements the current IME>
1691 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001693 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 + " mCurIntent=" + mCurIntent);
1695 if (mCurMethod != null && mCurIntent != null
1696 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001697 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 // We consider this to be a new bind attempt, since the system
1699 // should now try to restart the service for us.
1700 mLastBindTime = SystemClock.uptimeMillis();
1701 mShowRequested = mInputShown;
1702 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001703 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
1705 }
1706 }
1707
satokf9f01002011-05-19 21:31:50 +09001708 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001710 synchronized (mMethodMap) {
1711 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001712 return;
1713 }
1714 final long ident = Binder.clearCallingIdentity();
1715 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001717 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001718 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001719 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001722 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001723 CharSequence contentDescription = null;
1724 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001725 // Use PackageManager to load label
1726 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001727 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001728 mIPackageManager.getApplicationInfo(packageName, 0,
1729 mSettings.getCurrentUserId()));
1730 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001731 /* ignore */
1732 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001733 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001734 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001735 contentDescription != null
1736 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001737 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001740 } finally {
1741 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 }
1745
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001746 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001747 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001748 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001749 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001750 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001751 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001752 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1753 // When physical keyboard is attached, we show the ime switcher (or notification if
1754 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1755 // exists in the IME switcher dialog. Might be OK to remove this condition once
1756 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1757 return true;
1758 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001759 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1760 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001761 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001762
1763 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1764 final int N = imis.size();
1765 if (N > 2) return true;
1766 if (N < 1) return false;
1767 int nonAuxCount = 0;
1768 int auxCount = 0;
1769 InputMethodSubtype nonAuxSubtype = null;
1770 InputMethodSubtype auxSubtype = null;
1771 for(int i = 0; i < N; ++i) {
1772 final InputMethodInfo imi = imis.get(i);
1773 final List<InputMethodSubtype> subtypes =
1774 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1775 final int subtypeCount = subtypes.size();
1776 if (subtypeCount == 0) {
1777 ++nonAuxCount;
1778 } else {
1779 for (int j = 0; j < subtypeCount; ++j) {
1780 final InputMethodSubtype subtype = subtypes.get(j);
1781 if (!subtype.isAuxiliary()) {
1782 ++nonAuxCount;
1783 nonAuxSubtype = subtype;
1784 } else {
1785 ++auxCount;
1786 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001787 }
1788 }
satok7cfc0ed2011-06-20 21:29:36 +09001789 }
1790 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001791 if (nonAuxCount > 1 || auxCount > 1) {
1792 return true;
1793 } else if (nonAuxCount == 1 && auxCount == 1) {
1794 if (nonAuxSubtype != null && auxSubtype != null
1795 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1796 || auxSubtype.overridesImplicitlyEnabledSubtype()
1797 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1798 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1799 return false;
1800 }
1801 return true;
1802 }
1803 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001804 }
1805
John Spurlocke0980502013-10-25 11:59:29 -04001806 private boolean isKeyguardLocked() {
1807 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1808 }
1809
satokdbf29502011-08-25 15:28:23 +09001810 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001811 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001812 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001813 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001814 return;
1815 }
1816
1817 synchronized (mMethodMap) {
1818 mImeWindowVis = vis;
1819 mBackDisposition = backDisposition;
1820 updateSystemUiLocked(token, vis, backDisposition);
1821 }
1822 }
1823
1824 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1825 synchronized (mMethodMap) {
1826 updateSystemUiLocked(token, vis, backDisposition);
1827 }
1828 }
1829
1830 // Caution! This method is called in this class. Handle multi-user carefully
1831 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1832 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001833 return;
1834 }
1835
1836 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1837 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001838 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001839 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001840 // apply policy for binder calls
1841 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1842 vis = 0;
satok06487a52010-10-29 11:37:18 +09001843 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001844 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1845 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1846 if (mStatusBar != null) {
1847 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1848 needsToShowImeSwitcher);
1849 }
1850 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1851 if (imi != null && needsToShowImeSwitcher) {
1852 // Used to load label
1853 final CharSequence title = mRes.getText(
1854 com.android.internal.R.string.select_input_method);
1855 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1856 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001857 mImeSwitcherNotification.setContentTitle(title)
1858 .setContentText(summary)
1859 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001860 try {
1861 if ((mNotificationManager != null)
1862 && !mIWindowManager.hasNavigationBar()) {
1863 if (DEBUG) {
1864 Slog.d(TAG, "--- show notification: label = " + summary);
1865 }
1866 mNotificationManager.notifyAsUser(null,
1867 com.android.internal.R.string.select_input_method,
1868 mImeSwitcherNotification.build(), UserHandle.ALL);
1869 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001870 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001871 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001872 }
1873 } else {
1874 if (mNotificationShown && mNotificationManager != null) {
1875 if (DEBUG) {
1876 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001877 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001878 mNotificationManager.cancelAsUser(null,
1879 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1880 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001881 }
satok06487a52010-10-29 11:37:18 +09001882 }
1883 } finally {
1884 Binder.restoreCallingIdentity(ident);
1885 }
1886 }
1887
satoke7c6998e2011-06-03 17:57:59 +09001888 @Override
satokf9f01002011-05-19 21:31:50 +09001889 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001890 if (!calledFromValidUser()) {
1891 return;
1892 }
satokf9f01002011-05-19 21:31:50 +09001893 synchronized (mMethodMap) {
1894 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1895 for (int i = 0; i < spans.length; ++i) {
1896 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001897 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001898 mSecureSuggestionSpans.put(ss, currentImi);
1899 }
1900 }
1901 }
1902 }
1903
satoke7c6998e2011-06-03 17:57:59 +09001904 @Override
satokf9f01002011-05-19 21:31:50 +09001905 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001906 if (!calledFromValidUser()) {
1907 return false;
1908 }
satokf9f01002011-05-19 21:31:50 +09001909 synchronized (mMethodMap) {
1910 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1911 // TODO: Do not send the intent if the process of the targetImi is already dead.
1912 if (targetImi != null) {
1913 final String[] suggestions = span.getSuggestions();
1914 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001915 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001916 final Intent intent = new Intent();
1917 // Ensures that only a class in the original IME package will receive the
1918 // notification.
satok42c5a162011-05-26 16:46:14 +09001919 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001920 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1921 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1922 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1923 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001924 final long ident = Binder.clearCallingIdentity();
1925 try {
1926 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1927 } finally {
1928 Binder.restoreCallingIdentity(ident);
1929 }
satokf9f01002011-05-19 21:31:50 +09001930 return true;
1931 }
1932 }
1933 return false;
1934 }
1935
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001936 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001937 updateInputMethodsFromSettingsLocked(enabledMayChange);
1938 updateKeyboardFromSettingsLocked();
1939 }
1940
1941 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001942 if (enabledMayChange) {
1943 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1944 for (int i=0; i<enabled.size(); i++) {
1945 // We allow the user to select "disabled until used" apps, so if they
1946 // are enabling one of those here we now need to make it enabled.
1947 InputMethodInfo imm = enabled.get(i);
1948 try {
1949 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1950 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1951 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001952 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001953 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001954 if (DEBUG) {
1955 Slog.d(TAG, "Update state(" + imm.getId()
1956 + "): DISABLED_UNTIL_USED -> DEFAULT");
1957 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001958 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1959 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001960 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1961 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001962 }
1963 } catch (RemoteException e) {
1964 }
1965 }
1966 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001967 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1968 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1969 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1970 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001971 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001972 // There is no input method selected, try to choose new applicable input method.
1973 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001974 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001975 }
1976 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001978 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001980 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001981 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 }
satokf3db1af2010-11-23 13:34:33 +09001983 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001984 } else {
1985 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001986 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001988 // Here is not the perfect place to reset the switching controller. Ideally
1989 // mSwitchingController and mSettings should be able to share the same state.
1990 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1991 // the same enabled IMEs list.
1992 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001993
1994 }
1995
1996 public void updateKeyboardFromSettingsLocked() {
1997 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1998 if (mSwitchingDialog != null
1999 && mSwitchingDialogTitleView != null
2000 && mSwitchingDialog.isShowing()) {
2001 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2002 com.android.internal.R.id.hard_keyboard_switch);
2003 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
Yohei Yukawab097b822015-12-01 10:43:08 -08002007 private void notifyInputMethodSubtypeChanged(final int userId,
2008 @Nullable final InputMethodInfo inputMethodInfo,
2009 @Nullable final InputMethodSubtype subtype) {
2010 final InputManagerInternal inputManagerInternal =
2011 LocalServices.getService(InputManagerInternal.class);
2012 if (inputManagerInternal != null) {
2013 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2014 }
2015 }
2016
satokab751aa2010-09-14 19:17:36 +09002017 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 InputMethodInfo info = mMethodMap.get(id);
2019 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002020 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002022
satokd81e9502012-05-21 12:58:45 +09002023 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002025 final int subtypeCount = info.getSubtypeCount();
2026 if (subtypeCount <= 0) {
2027 return;
satokcd7cd292010-11-20 15:46:23 +09002028 }
satokd81e9502012-05-21 12:58:45 +09002029 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2030 final InputMethodSubtype newSubtype;
2031 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2032 newSubtype = info.getSubtypeAt(subtypeId);
2033 } else {
2034 // If subtype is null, try to find the most applicable one from
2035 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002036 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002037 }
2038 if (newSubtype == null || oldSubtype == null) {
2039 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2040 + ", new subtype = " + newSubtype);
2041 return;
2042 }
2043 if (newSubtype != oldSubtype) {
2044 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2045 if (mCurMethod != null) {
2046 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002047 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002048 mCurMethod.changeInputMethodSubtype(newSubtype);
2049 } catch (RemoteException e) {
2050 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002051 return;
satokab751aa2010-09-14 19:17:36 +09002052 }
2053 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002054 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 return;
2057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002058
satokd81e9502012-05-21 12:58:45 +09002059 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 final long ident = Binder.clearCallingIdentity();
2061 try {
satokab751aa2010-09-14 19:17:36 +09002062 // Set a subtype to this input method.
2063 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002064 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2065 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2066 // because mCurMethodId is stored as a history in
2067 // setSelectedInputMethodAndSubtypeLocked().
2068 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002070 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002072 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002074 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002076 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 } finally {
2078 Binder.restoreCallingIdentity(ident);
2079 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002080
2081 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2082 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002084
satok42c5a162011-05-26 16:46:14 +09002085 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 public boolean showSoftInput(IInputMethodClient client, int flags,
2087 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002088 if (!calledFromValidUser()) {
2089 return false;
2090 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002091 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092 long ident = Binder.clearCallingIdentity();
2093 try {
2094 synchronized (mMethodMap) {
2095 if (mCurClient == null || client == null
2096 || mCurClient.client.asBinder() != client.asBinder()) {
2097 try {
2098 // We need to check if this is the current client with
2099 // focus in the window manager, to allow this call to
2100 // be made before input is started in it.
2101 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002102 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002103 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
2105 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002106 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
2108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002109
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002110 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
2113 } finally {
2114 Binder.restoreCallingIdentity(ident);
2115 }
2116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002117
The Android Open Source Project4df24232009-03-05 14:34:35 -08002118 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002120 if (mAccessibilityRequestingNoSoftKeyboard) {
2121 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
Anna Galusza9b278112016-01-04 11:37:37 -08002123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2125 mShowExplicitlyRequested = true;
2126 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002127 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2128 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
Dianne Hackborncc278702009-09-02 23:07:23 -07002131 if (!mSystemReady) {
2132 return false;
2133 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002134
The Android Open Source Project4df24232009-03-05 14:34:35 -08002135 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002137 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002138 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2139 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2140 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002142 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002143 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002144 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002145 | Context.BIND_TREAT_LIKE_ACTIVITY
2146 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002147 mVisibleBound = true;
2148 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002149 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002151 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 // The client has asked to have the input method shown, but
2153 // we have been sitting here too long with a connection to the
2154 // service and no interface received, so let's disconnect/connect
2155 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002156 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002158 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002160 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002161 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002162 } else {
2163 if (DEBUG) {
2164 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2165 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002168
The Android Open Source Project4df24232009-03-05 14:34:35 -08002169 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002171
satok42c5a162011-05-26 16:46:14 +09002172 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002173 public boolean hideSoftInput(IInputMethodClient client, int flags,
2174 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002175 if (!calledFromValidUser()) {
2176 return false;
2177 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002178 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 long ident = Binder.clearCallingIdentity();
2180 try {
2181 synchronized (mMethodMap) {
2182 if (mCurClient == null || client == null
2183 || mCurClient.client.asBinder() != client.asBinder()) {
2184 try {
2185 // We need to check if this is the current client with
2186 // focus in the window manager, to allow this call to
2187 // be made before input is started in it.
2188 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002189 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2190 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002191 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
2193 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002194 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
2196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002197
Joe Onorato8a9b2202010-02-26 18:56:32 -08002198 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002199 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
2201 } finally {
2202 Binder.restoreCallingIdentity(ident);
2203 }
2204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002205
The Android Open Source Project4df24232009-03-05 14:34:35 -08002206 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2208 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002209 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 -08002210 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 }
2212 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002213 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 -08002214 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002216
2217 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2218 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2219 // to be updated with the new value sent from IME process. Even in such a transient state
2220 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2221 // application process as a valid request, and have even promised such a behavior with CTS
2222 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2223 // IMMS#InputShown indicates that the software keyboard is shown.
2224 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2225 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2226 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002227 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002228 if (shouldHideSoftInput) {
2229 // The IME will report its visible state again after the following message finally
2230 // delivered to the IME process as an IPC. Hence the inconsistency between
2231 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2232 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002233 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2234 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2235 res = true;
2236 } else {
2237 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002239 if (mHaveConnection && mVisibleBound) {
2240 mContext.unbindService(mVisibleConnection);
2241 mVisibleBound = false;
2242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 mInputShown = false;
2244 mShowRequested = false;
2245 mShowExplicitlyRequested = false;
2246 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002247 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002249
satok42c5a162011-05-26 16:46:14 +09002250 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002251 public InputBindResult startInputOrWindowGainedFocus(
2252 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2253 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002254 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002255 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002256 if (windowToken != null) {
2257 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002258 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002259 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002260 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2261 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002262 }
2263 }
2264
2265 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002266 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2267 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002268 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2269 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002270 // Needs to check the validity before clearing calling identity
2271 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002272 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 long ident = Binder.clearCallingIdentity();
2274 try {
2275 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002276 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2277 + InputMethodClient.getStartInputReason(startInputReason)
2278 + " client=" + client.asBinder()
2279 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002280 + " missingMethods="
2281 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002282 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002285 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002286
Dianne Hackborn7663d802012-02-24 13:08:49 -08002287 ClientState cs = mClients.get(client.asBinder());
2288 if (cs == null) {
2289 throw new IllegalArgumentException("unknown client "
2290 + client.asBinder());
2291 }
2292
2293 try {
2294 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2295 // Check with the window manager to make sure this client actually
2296 // has a window with focus. If not, reject. This is thread safe
2297 // because if the focus changes some time before or after, the
2298 // next client receiving focus that has any interest in input will
2299 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002300 if (DEBUG) {
2301 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2302 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2303 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002304 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002306 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002308
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002309 if (!calledFromValidUser) {
2310 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2311 Slog.w(TAG, "If you want to interect with IME, you need "
2312 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2313 hideCurrentInputLocked(0, null);
2314 return null;
2315 }
2316
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002317 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002318 if (DEBUG) {
2319 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2320 + " attribute=" + attribute + ", token = " + windowToken);
2321 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002322 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002323 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002324 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002325 }
2326 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002327 }
2328 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002329 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002330
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002331 // Should we auto-show the IME even if the caller has not
2332 // specified what should be done with it?
2333 // We only do this automatically if the window can resize
2334 // to accommodate the IME (so what the user sees will give
2335 // them good context without input information being obscured
2336 // by the IME) or if running on a large screen where there
2337 // is more room for the target window + IME.
2338 final boolean doAutoShow =
2339 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2340 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2341 || mRes.getConfiguration().isLayoutSizeAtLeast(
2342 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002343 final boolean isTextEditor =
2344 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2345
2346 // We want to start input before showing the IME, but after closing
2347 // it. We want to do this after closing it to help the IME disappear
2348 // more quickly (not get stuck behind it initializing itself for the
2349 // new focused input, even if its window wants to hide the IME).
2350 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2353 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002354 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2356 // There is no focus view, and this window will
2357 // be behind any soft input window, so hide the
2358 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002359 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002360 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002362 } else if (isTextEditor && doAutoShow && (softInputMode &
2363 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 // There is a focus view, and we are navigating forward
2365 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002366 // We only do this automatically if the window can resize
2367 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002368 // them good context without input information being obscured
2369 // by the IME) or if running on a large screen where there
2370 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002371 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002372 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002373 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002374 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002375 didStart = true;
2376 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002377 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 }
2379 break;
2380 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2381 // Do nothing.
2382 break;
2383 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2384 if ((softInputMode &
2385 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002386 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002387 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 }
2389 break;
2390 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002391 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002392 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 break;
2394 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2395 if ((softInputMode &
2396 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002397 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002398 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002399 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002400 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002401 didStart = true;
2402 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002403 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405 break;
2406 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002407 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002408 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002409 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002410 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002411 didStart = true;
2412 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002413 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 break;
2415 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002416
2417 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002418 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002419 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 }
2422 } finally {
2423 Binder.restoreCallingIdentity(ident);
2424 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002425
2426 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002428
satok42c5a162011-05-26 16:46:14 +09002429 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002430 public void showInputMethodPickerFromClient(
2431 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002432 if (!calledFromValidUser()) {
2433 return;
2434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 synchronized (mMethodMap) {
2436 if (mCurClient == null || client == null
2437 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002438 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002439 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 }
2441
satok440aab52010-11-25 09:43:11 +09002442 // Always call subtype picker, because subtype picker is a superset of input method
2443 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002444 mHandler.sendMessage(mCaller.obtainMessageI(
2445 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002446 }
2447 }
2448
satok42c5a162011-05-26 16:46:14 +09002449 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002451 if (!calledFromValidUser()) {
2452 return;
2453 }
satok28203512010-11-24 11:06:49 +09002454 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2455 }
2456
satok42c5a162011-05-26 16:46:14 +09002457 @Override
satok28203512010-11-24 11:06:49 +09002458 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002459 if (!calledFromValidUser()) {
2460 return;
2461 }
satok28203512010-11-24 11:06:49 +09002462 synchronized (mMethodMap) {
2463 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002464 setInputMethodWithSubtypeIdLocked(token, id,
2465 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2466 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002467 } else {
2468 setInputMethod(token, id);
2469 }
2470 }
satokab751aa2010-09-14 19:17:36 +09002471 }
2472
satok42c5a162011-05-26 16:46:14 +09002473 @Override
satokb416a712010-11-25 20:42:14 +09002474 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002475 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002476 if (!calledFromValidUser()) {
2477 return;
2478 }
satokb416a712010-11-25 20:42:14 +09002479 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002480 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2481 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002482 }
2483 }
2484
satok4fc87d62011-05-20 16:13:43 +09002485 @Override
satok735cf382010-11-11 20:40:09 +09002486 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002487 if (!calledFromValidUser()) {
2488 return false;
2489 }
satok735cf382010-11-11 20:40:09 +09002490 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002491 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002492 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002493 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002494 lastImi = mMethodMap.get(lastIme.first);
2495 } else {
2496 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002497 }
satok4fc87d62011-05-20 16:13:43 +09002498 String targetLastImiId = null;
2499 int subtypeId = NOT_A_SUBTYPE_ID;
2500 if (lastIme != null && lastImi != null) {
2501 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002502 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002503 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2504 : mCurrentSubtype.hashCode();
2505 // If the last IME is the same as the current IME and the last subtype is not
2506 // defined, there is no need to switch to the last IME.
2507 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2508 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002509 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002510 }
2511 }
2512
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002513 if (TextUtils.isEmpty(targetLastImiId)
2514 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002515 // This is a safety net. If the currentSubtype can't be added to the history
2516 // and the framework couldn't find the last ime, we will make the last ime be
2517 // the most applicable enabled keyboard subtype of the system imes.
2518 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2519 if (enabled != null) {
2520 final int N = enabled.size();
2521 final String locale = mCurrentSubtype == null
2522 ? mRes.getConfiguration().locale.toString()
2523 : mCurrentSubtype.getLocale();
2524 for (int i = 0; i < N; ++i) {
2525 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002526 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002527 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002528 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2529 InputMethodUtils.getSubtypes(imi),
2530 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002531 if (keyboardSubtype != null) {
2532 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002533 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002534 imi, keyboardSubtype.hashCode());
2535 if(keyboardSubtype.getLocale().equals(locale)) {
2536 break;
2537 }
2538 }
2539 }
2540 }
2541 }
2542 }
2543
2544 if (!TextUtils.isEmpty(targetLastImiId)) {
2545 if (DEBUG) {
2546 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2547 + ", from: " + mCurMethodId + ", " + subtypeId);
2548 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002549 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002550 return true;
2551 } else {
2552 return false;
2553 }
satok735cf382010-11-11 20:40:09 +09002554 }
2555 }
2556
satoke7c6998e2011-06-03 17:57:59 +09002557 @Override
satok688bd472012-02-09 20:09:17 +09002558 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002559 if (!calledFromValidUser()) {
2560 return false;
2561 }
satok688bd472012-02-09 20:09:17 +09002562 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002563 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002564 return false;
2565 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002566 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002567 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2568 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002569 if (nextSubtype == null) {
2570 return false;
2571 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002572 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2573 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002574 return true;
2575 }
2576 }
2577
2578 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002579 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2580 if (!calledFromValidUser()) {
2581 return false;
2582 }
2583 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002584 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002585 return false;
2586 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002587 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002588 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2589 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002590 if (nextSubtype == null) {
2591 return false;
2592 }
2593 return true;
2594 }
2595 }
2596
2597 @Override
satok68f1b782011-04-11 14:26:04 +09002598 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002599 if (!calledFromValidUser()) {
2600 return null;
2601 }
satok68f1b782011-04-11 14:26:04 +09002602 synchronized (mMethodMap) {
2603 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2604 // TODO: Handle the case of the last IME with no subtypes
2605 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2606 || TextUtils.isEmpty(lastIme.second)) return null;
2607 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2608 if (lastImi == null) return null;
2609 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002610 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002611 final int lastSubtypeId =
2612 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002613 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2614 return null;
2615 }
2616 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002617 } catch (NumberFormatException e) {
2618 return null;
2619 }
2620 }
2621 }
2622
satoke7c6998e2011-06-03 17:57:59 +09002623 @Override
satokee5e77c2011-09-02 18:50:15 +09002624 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002625 if (!calledFromValidUser()) {
2626 return;
2627 }
satok91e88122011-07-18 11:11:42 +09002628 // By this IPC call, only a process which shares the same uid with the IME can add
2629 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002630 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002631 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002632 if (!mSystemReady) {
2633 return;
2634 }
satok91e88122011-07-18 11:11:42 +09002635 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002636 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002637 final String[] packageInfos;
2638 try {
2639 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2640 } catch (RemoteException e) {
2641 Slog.e(TAG, "Failed to get package infos");
2642 return;
2643 }
satok91e88122011-07-18 11:11:42 +09002644 if (packageInfos != null) {
2645 final int packageNum = packageInfos.length;
2646 for (int i = 0; i < packageNum; ++i) {
2647 if (packageInfos[i].equals(imi.getPackageName())) {
2648 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002649 final long ident = Binder.clearCallingIdentity();
2650 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002651 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002652 } finally {
2653 Binder.restoreCallingIdentity(ident);
2654 }
satokee5e77c2011-09-02 18:50:15 +09002655 return;
satok91e88122011-07-18 11:11:42 +09002656 }
2657 }
2658 }
satoke7c6998e2011-06-03 17:57:59 +09002659 }
satokee5e77c2011-09-02 18:50:15 +09002660 return;
satoke7c6998e2011-06-03 17:57:59 +09002661 }
2662
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002663 @Override
2664 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002665 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002666 }
2667
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002668 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002669 public void clearLastInputMethodWindowForTransition(IBinder token) {
2670 if (!calledFromValidUser()) {
2671 return;
2672 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002673 synchronized (mMethodMap) {
2674 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002675 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002676 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002677 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002678 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002679 }
2680
2681 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002682 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002683 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002684 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002685 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002686 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002687 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2688 if (DEBUG) {
2689 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2690 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2691 + " actual: " + sequenceNumber);
2692 }
2693 return;
2694 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002695 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2696 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002697 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002698 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002699 }
2700 }
2701
satok28203512010-11-24 11:06:49 +09002702 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002704 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2705 }
2706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002708 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2709 if (token == null) {
2710 if (mContext.checkCallingOrSelfPermission(
2711 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2712 != PackageManager.PERMISSION_GRANTED) {
2713 throw new SecurityException(
2714 "Using null token requires permission "
2715 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002717 } else if (mCurToken != token) {
2718 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2719 + " token: " + token);
2720 return;
2721 }
2722
2723 final long ident = Binder.clearCallingIdentity();
2724 try {
2725 setInputMethodLocked(id, subtypeId);
2726 } finally {
2727 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 }
2729 }
2730
satok42c5a162011-05-26 16:46:14 +09002731 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002733 if (!calledFromValidUser()) {
2734 return;
2735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002737 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 return;
2739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 long ident = Binder.clearCallingIdentity();
2741 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002742 hideCurrentInputLocked(flags, null);
2743 } finally {
2744 Binder.restoreCallingIdentity(ident);
2745 }
2746 }
2747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002748
satok42c5a162011-05-26 16:46:14 +09002749 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002750 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002751 if (!calledFromValidUser()) {
2752 return;
2753 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002754 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002755 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002756 return;
2757 }
2758 long ident = Binder.clearCallingIdentity();
2759 try {
2760 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 } finally {
2762 Binder.restoreCallingIdentity(ident);
2763 }
2764 }
2765 }
2766
2767 void setEnabledSessionInMainThread(SessionState session) {
2768 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002769 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002771 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002772 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 } catch (RemoteException e) {
2774 }
2775 }
2776 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002777 if (mEnabledSession != null && mEnabledSession.session != null) {
2778 try {
2779 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2780 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2781 } catch (RemoteException e) {
2782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 }
2784 }
2785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002786
satok42c5a162011-05-26 16:46:14 +09002787 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002789 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002791 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002792 final boolean showAuxSubtypes;
2793 switch (msg.arg1) {
2794 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2795 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2796 // implemented so that auxiliary subtypes will be excluded when the soft
2797 // keyboard is invisible.
2798 showAuxSubtypes = mInputShown;
2799 break;
2800 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2801 showAuxSubtypes = true;
2802 break;
2803 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2804 showAuxSubtypes = false;
2805 break;
2806 default:
2807 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2808 return false;
2809 }
2810 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002811 return true;
2812
satok47a44912010-10-06 16:03:58 +09002813 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002814 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002815 return true;
2816
2817 case MSG_SHOW_IM_CONFIG:
2818 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002819 return true;
2820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 case MSG_UNBIND_INPUT:
2824 try {
2825 ((IInputMethod)msg.obj).unbindInput();
2826 } catch (RemoteException e) {
2827 // There is nothing interesting about the method dying.
2828 }
2829 return true;
2830 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002831 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 try {
2833 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2834 } catch (RemoteException e) {
2835 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002836 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 return true;
2838 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002839 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002841 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002842 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002843 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 } catch (RemoteException e) {
2845 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002846 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 return true;
2848 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002849 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002851 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002852 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002853 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 } catch (RemoteException e) {
2855 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002856 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002858 case MSG_HIDE_CURRENT_INPUT_METHOD:
2859 synchronized (mMethodMap) {
2860 hideCurrentInputLocked(0, null);
2861 }
2862 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002864 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002866 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2868 } 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;
Jeff Brownc28867a2013-03-26 15:42:39 -07002872 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002873 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002874 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002875 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002877 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002879 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002880 // Dispose the channel if the input method is not local to this process
2881 // because the remote proxy will get its own copy when unparceled.
2882 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002883 channel.dispose();
2884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002886 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002890
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002891 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08002892 final int missingMethods = msg.arg1;
2893 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002894 args = (SomeArgs) msg.obj;
Yohei Yukawaf7526b52017-02-11 20:57:10 -08002895 final SessionState session = (SessionState) args.arg1;
2896 final IInputContext inputContext = (IInputContext) args.arg2;
2897 final EditorInfo editorInfo = (EditorInfo) args.arg3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 setEnabledSessionInMainThread(session);
Yohei Yukawaf7526b52017-02-11 20:57:10 -08002900 session.method.startInput(inputContext, missingMethods, editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 } catch (RemoteException e) {
2902 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002903 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002905 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002908
Yohei Yukawa33e81792015-11-17 21:14:42 -08002909 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002911 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 } catch (RemoteException e) {
2913 // There is nothing interesting about the last client dying.
2914 }
2915 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002916 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002917 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002918 IInputMethodClient client = (IInputMethodClient)args.arg1;
2919 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002921 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002923 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002924 } finally {
2925 // Dispose the channel if the input method is not local to this process
2926 // because the remote proxy will get its own copy when unparceled.
2927 if (res.channel != null && Binder.isProxy(client)) {
2928 res.channel.dispose();
2929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002931 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002933 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002934 case MSG_SET_ACTIVE:
2935 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002936 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002937 } catch (RemoteException e) {
2938 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2939 + ((ClientState)msg.obj).pid + " uid "
2940 + ((ClientState)msg.obj).uid);
2941 }
2942 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002943 case MSG_SET_INTERACTIVE:
2944 handleSetInteractive(msg.arg1 != 0);
2945 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002946 case MSG_SWITCH_IME:
2947 handleSwitchInputMethod(msg.arg1 != 0);
2948 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002949 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2950 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002951 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002952 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002953 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002954 } catch (RemoteException e) {
2955 Slog.w(TAG, "Got RemoteException sending "
2956 + "setUserActionNotificationSequenceNumber("
2957 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002958 + clientState.pid + " uid "
2959 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002960 }
2961 return true;
2962 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002963 case MSG_REPORT_FULLSCREEN_MODE: {
2964 final boolean fullscreen = msg.arg1 != 0;
2965 final ClientState clientState = (ClientState)msg.obj;
2966 try {
2967 clientState.client.reportFullscreenMode(fullscreen);
2968 } catch (RemoteException e) {
2969 Slog.w(TAG, "Got RemoteException sending "
2970 + "reportFullscreen(" + fullscreen + ") notification to pid="
2971 + clientState.pid + " uid=" + clientState.uid);
2972 }
2973 return true;
2974 }
satok01038492012-04-09 21:08:27 +09002975
2976 // --------------------------------------------------------------
2977 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002978 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002979 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002980 case MSG_SYSTEM_UNLOCK_USER:
2981 final int userId = msg.arg1;
2982 onUnlockUser(userId);
2983 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 }
2985 return false;
2986 }
2987
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002988 private void handleSetInteractive(final boolean interactive) {
2989 synchronized (mMethodMap) {
2990 mIsInteractive = interactive;
2991 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2992
2993 // Inform the current client of the change in active status
2994 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002995 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
2996 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
2997 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002998 }
2999 }
3000 }
3001
Yohei Yukawaae61f712015-12-09 13:00:10 -08003002 private void handleSwitchInputMethod(final boolean forwardDirection) {
3003 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003004 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003005 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003006 if (nextSubtype == null) {
3007 return;
3008 }
3009 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003010 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3011 if (newInputMethodInfo == null) {
3012 return;
3013 }
3014 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3015 newInputMethodInfo, mCurrentSubtype);
3016 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003017 if (mSubtypeSwitchedByShortCutToast == null) {
3018 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3019 Toast.LENGTH_SHORT);
3020 } else {
3021 mSubtypeSwitchedByShortCutToast.setText(toastText);
3022 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003023 mSubtypeSwitchedByShortCutToast.show();
3024 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003025 }
3026 }
3027
satokdc9ddae2011-10-06 12:22:36 +09003028 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003029 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3030 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003031 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003032 if (DEBUG) {
3033 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3034 }
satok723a27e2010-11-11 14:58:11 +09003035 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003036 return true;
3037 }
3038
3039 return false;
3040 }
3041
Yohei Yukawa94e33302016-02-12 19:37:03 -08003042 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003043 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003044 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003045 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003046 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003047 if (!mSystemReady) {
3048 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3049 return;
3050 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003051 mMethodList.clear();
3052 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003053 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003054 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003055
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003056 // Use for queryIntentServicesAsUser
3057 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003059 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3060 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3061 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003062 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003064 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3065 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003066
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003067 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003068 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 for (int i = 0; i < services.size(); ++i) {
3070 ResolveInfo ri = services.get(i);
3071 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003072 final String imeId = InputMethodInfo.computeId(ri);
3073 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3074 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 + ": it does not require the permission "
3076 + android.Manifest.permission.BIND_INPUT_METHOD);
3077 continue;
3078 }
3079
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003080 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003082 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 try {
satoke7c6998e2011-06-03 17:57:59 +09003084 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003085 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003086 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003087 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088
3089 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003090 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003092 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003093 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 }
3095 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003096
Yohei Yukawac4e44912017-02-09 19:30:22 -08003097 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3098 // negatives when the package state remains to be the same but only the component state is
3099 // changed.
3100 {
3101 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3102 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3103 // conservative, but it seems we cannot use it for now (Issue 35176630).
3104 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3105 new Intent(InputMethod.SERVICE_INTERFACE),
3106 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3107 final int N = allInputMethodServices.size();
3108 for (int i = 0; i < N; ++i) {
3109 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3110 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3111 continue;
3112 }
3113 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3114 }
3115 }
3116
Yohei Yukawa859df052016-02-17 07:56:46 -08003117 // TODO: The following code should find better place to live.
3118 if (!resetDefaultEnabledIme) {
3119 boolean enabledImeFound = false;
3120 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3121 final int N = enabledImes.size();
3122 for (int i = 0; i < N; ++i) {
3123 final InputMethodInfo imi = enabledImes.get(i);
3124 if (mMethodList.contains(imi)) {
3125 enabledImeFound = true;
3126 break;
3127 }
3128 }
3129 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003130 if (DEBUG) {
3131 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3132 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003133 resetDefaultEnabledIme = true;
3134 resetSelectedInputMethodAndSubtypeLocked("");
3135 }
3136 }
3137
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003138 if (resetDefaultEnabledIme) {
3139 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003140 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003141 final int N = defaultEnabledIme.size();
3142 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003143 final InputMethodInfo imi = defaultEnabledIme.get(i);
3144 if (DEBUG) {
3145 Slog.d(TAG, "--- enable ime = " + imi);
3146 }
3147 setInputMethodEnabledLocked(imi.getId(), true);
3148 }
3149 }
3150
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003151 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003152 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003153 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003154 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3155 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003156 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003157 }
3158 } else {
3159 // Double check that the default IME is certainly enabled.
3160 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003161 }
3162 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003163 // Here is not the perfect place to reset the switching controller. Ideally
3164 // mSwitchingController and mSettings should be able to share the same state.
3165 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3166 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003167 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003171
satok217f5482010-12-15 05:19:19 +09003172 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003173 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003174 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003175 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3176 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003177 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003178 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003179 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003180 final int userId;
3181 synchronized (mMethodMap) {
3182 userId = mSettings.getCurrentUserId();
3183 }
3184 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003185 }
3186
3187 private void showConfigureInputMethods() {
3188 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3189 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3190 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3191 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003192 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003193 }
3194
satok2c93efc2012-04-02 19:33:47 +09003195 private boolean isScreenLocked() {
3196 return mKeyguardManager != null
3197 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3198 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003199
Seigo Nonaka14e13912015-05-06 21:04:13 -07003200 private void showInputMethodMenu(boolean showAuxSubtypes) {
3201 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003204 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003205
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003206 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003207 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003208 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003209
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003210 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003211 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003212 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3213 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003214 if (immis == null || immis.size() == 0) {
3215 return;
3216 }
3217
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003218 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219
satok688bd472012-02-09 20:09:17 +09003220 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003221 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003222 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003223
satokc3690562012-01-10 20:14:43 +09003224 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003225 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003226 if (currentSubtype != null) {
3227 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003228 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3229 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003230 }
3231 }
3232
Ken Wakasa761eb372011-03-04 19:06:18 +09003233 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003234 mIms = new InputMethodInfo[N];
3235 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003236 int checkedItem = 0;
3237 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003238 final ImeSubtypeListItem item = imList.get(i);
3239 mIms[i] = item.mImi;
3240 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003241 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003242 int subtypeId = mSubtypeIds[i];
3243 if ((subtypeId == NOT_A_SUBTYPE_ID)
3244 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3245 || (subtypeId == lastInputMethodSubtypeId)) {
3246 checkedItem = i;
3247 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003250
3251 final Context settingsContext = new ContextThemeWrapper(context,
3252 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3253
3254 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003255 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3256 @Override
3257 public void onCancel(DialogInterface dialog) {
3258 hideInputMethodMenu();
3259 }
3260 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003261
3262 final Context dialogContext = mDialogBuilder.getContext();
3263 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3264 com.android.internal.R.styleable.DialogPreference,
3265 com.android.internal.R.attr.alertDialogStyle, 0);
3266 final Drawable dialogIcon = a.getDrawable(
3267 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3268 a.recycle();
3269
3270 mDialogBuilder.setIcon(dialogIcon);
3271
Yohei Yukawad34e1482016-02-11 08:03:52 -08003272 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003273 final View tv = inflater.inflate(
3274 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3275 mDialogBuilder.setCustomTitle(tv);
3276
3277 // Setup layout for a toggle switch of the hardware keyboard
3278 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003279 mSwitchingDialogTitleView
3280 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003281 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003282 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003283 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003284 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003285 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003286 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3287 @Override
3288 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003289 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003290 // Ensure that the input method dialog is dismissed when changing
3291 // the hardware keyboard state.
3292 hideInputMethodMenu();
3293 }
3294 });
3295
Alan Viverette505e3ab2014-11-24 15:22:11 -08003296 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003297 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3298 final OnClickListener choiceListener = new OnClickListener() {
3299 @Override
3300 public void onClick(final DialogInterface dialog, final int which) {
3301 synchronized (mMethodMap) {
3302 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3303 || mSubtypeIds.length <= which) {
3304 return;
satok01038492012-04-09 21:08:27 +09003305 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003306 final InputMethodInfo im = mIms[which];
3307 int subtypeId = mSubtypeIds[which];
3308 adapter.mCheckedItem = which;
3309 adapter.notifyDataSetChanged();
3310 hideInputMethodMenu();
3311 if (im != null) {
3312 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3313 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003314 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003315 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003317 }
3318 }
3319 };
3320 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003323 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003324 final Window w = mSwitchingDialog.getWindow();
3325 final WindowManager.LayoutParams attrs = w.getAttributes();
3326 w.setType(TYPE_INPUT_METHOD_DIALOG);
3327 // Use an alternate token for the dialog for that window manager can group the token
3328 // with other IME windows based on type vs. grouping based on whichever token happens
3329 // to get selected by the system later on.
3330 attrs.token = mSwitchingDialogToken;
3331 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3332 attrs.setTitle("Select input method");
3333 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003334 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 mSwitchingDialog.show();
3336 }
3337 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003338
Ken Wakasa05dbb652011-08-22 15:22:43 +09003339 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3340 private final LayoutInflater mInflater;
3341 private final int mTextViewResourceId;
3342 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003343 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003344 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3345 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3346 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003347
Ken Wakasa05dbb652011-08-22 15:22:43 +09003348 mTextViewResourceId = textViewResourceId;
3349 mItemsList = itemsList;
3350 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003351 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003352 }
3353
3354 @Override
3355 public View getView(int position, View convertView, ViewGroup parent) {
3356 final View view = convertView != null ? convertView
3357 : mInflater.inflate(mTextViewResourceId, null);
3358 if (position < 0 || position >= mItemsList.size()) return view;
3359 final ImeSubtypeListItem item = mItemsList.get(position);
3360 final CharSequence imeName = item.mImeName;
3361 final CharSequence subtypeName = item.mSubtypeName;
3362 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3363 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3364 if (TextUtils.isEmpty(subtypeName)) {
3365 firstTextView.setText(imeName);
3366 secondTextView.setVisibility(View.GONE);
3367 } else {
3368 firstTextView.setText(subtypeName);
3369 secondTextView.setText(imeName);
3370 secondTextView.setVisibility(View.VISIBLE);
3371 }
3372 final RadioButton radioButton =
3373 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3374 radioButton.setChecked(position == mCheckedItem);
3375 return view;
3376 }
3377 }
3378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003380 synchronized (mMethodMap) {
3381 hideInputMethodMenuLocked();
3382 }
3383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003384
The Android Open Source Project10592532009-03-18 17:39:46 -07003385 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003386 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387
The Android Open Source Project10592532009-03-18 17:39:46 -07003388 if (mSwitchingDialog != null) {
3389 mSwitchingDialog.dismiss();
3390 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003392
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003393 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003394 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003395 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003399
satok42c5a162011-05-26 16:46:14 +09003400 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003402 // TODO: Make this work even for non-current users?
3403 if (!calledFromValidUser()) {
3404 return false;
3405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 synchronized (mMethodMap) {
3407 if (mContext.checkCallingOrSelfPermission(
3408 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3409 != PackageManager.PERMISSION_GRANTED) {
3410 throw new SecurityException(
3411 "Requires permission "
3412 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3413 }
Anna Galusza9b278112016-01-04 11:37:37 -08003414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 long ident = Binder.clearCallingIdentity();
3416 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003417 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 } finally {
3419 Binder.restoreCallingIdentity(ident);
3420 }
3421 }
3422 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003423
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003424 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3425 // Make sure this is a valid input method.
3426 InputMethodInfo imm = mMethodMap.get(id);
3427 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003428 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003429 }
3430
satokd87c2592010-09-29 11:52:06 +09003431 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3432 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003433
satokd87c2592010-09-29 11:52:06 +09003434 if (enabled) {
3435 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3436 if (pair.first.equals(id)) {
3437 // We are enabling this input method, but it is already enabled.
3438 // Nothing to do. The previous state was enabled.
3439 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003440 }
3441 }
satokd87c2592010-09-29 11:52:06 +09003442 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3443 // Previous state was disabled.
3444 return false;
3445 } else {
3446 StringBuilder builder = new StringBuilder();
3447 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3448 builder, enabledInputMethodsList, id)) {
3449 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003450 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003451 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3452 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3453 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003454 }
3455 // Previous state was enabled.
3456 return true;
3457 } else {
3458 // We are disabling the input method but it is already disabled.
3459 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003460 return false;
3461 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003462 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003463 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003464
satok723a27e2010-11-11 14:58:11 +09003465 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3466 boolean setSubtypeOnly) {
3467 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003468 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003469
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003470 mCurUserActionNotificationSequenceNumber =
3471 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3472 if (DEBUG) {
3473 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3474 + mCurUserActionNotificationSequenceNumber);
3475 }
3476
3477 if (mCurClient != null && mCurClient.client != null) {
3478 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3479 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003480 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003481 }
3482
satok723a27e2010-11-11 14:58:11 +09003483 // Set Subtype here
3484 if (imi == null || subtypeId < 0) {
3485 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003486 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003487 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003488 if (subtypeId < imi.getSubtypeCount()) {
3489 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3490 mSettings.putSelectedSubtype(subtype.hashCode());
3491 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003492 } else {
3493 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003494 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003495 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003496 }
satokab751aa2010-09-14 19:17:36 +09003497 }
satok723a27e2010-11-11 14:58:11 +09003498
Yohei Yukawa68645a62016-02-17 07:54:20 -08003499 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003500 // Set InputMethod here
3501 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3502 }
3503 }
3504
3505 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3506 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3507 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3508 // newDefaultIme is empty when there is no candidate for the selected IME.
3509 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3510 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3511 if (subtypeHashCode != null) {
3512 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003513 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003514 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003515 } catch (NumberFormatException e) {
3516 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3517 }
3518 }
3519 }
3520 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003521 }
3522
satok4e4569d2010-11-19 18:45:53 +09003523 // If there are no selected shortcuts, tries finding the most applicable ones.
3524 private Pair<InputMethodInfo, InputMethodSubtype>
3525 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3526 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3527 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003528 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003529 boolean foundInSystemIME = false;
3530
3531 // Search applicable subtype for each InputMethodInfo
3532 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003533 final String imiId = imi.getId();
3534 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3535 continue;
3536 }
satokcd7cd292010-11-20 15:46:23 +09003537 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003538 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003539 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003540 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003541 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003542 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003543 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003544 }
satokdf31ae62011-01-15 06:19:44 +09003545 // 2. Search by the system locale from enabledSubtypes.
3546 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003547 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003548 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003549 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003550 }
satoka86f5e42011-09-02 17:12:42 +09003551 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003552 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003553 final ArrayList<InputMethodSubtype> subtypesForSearch =
3554 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003555 ? InputMethodUtils.getSubtypes(imi)
3556 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003557 // 4. Search by the current subtype's locale from all subtypes.
3558 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003559 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003560 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003561 }
3562 // 5. Search by the system locale from all subtypes.
3563 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003564 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003565 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003566 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003567 }
satokcd7cd292010-11-20 15:46:23 +09003568 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003569 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003570 // The current input method is the most applicable IME.
3571 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003572 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003573 break;
satok7599a7f2010-12-22 13:45:23 +09003574 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003575 // The system input method is 2nd applicable IME.
3576 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003577 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003578 if ((imi.getServiceInfo().applicationInfo.flags
3579 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3580 foundInSystemIME = true;
3581 }
satok4e4569d2010-11-19 18:45:53 +09003582 }
3583 }
3584 }
3585 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003586 if (mostApplicableIMI != null) {
3587 Slog.w(TAG, "Most applicable shortcut input method was:"
3588 + mostApplicableIMI.getId());
3589 if (mostApplicableSubtype != null) {
3590 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3591 + "," + mostApplicableSubtype.getMode() + ","
3592 + mostApplicableSubtype.getLocale());
3593 }
3594 }
satok4e4569d2010-11-19 18:45:53 +09003595 }
satokcd7cd292010-11-20 15:46:23 +09003596 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003597 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003598 } else {
3599 return null;
3600 }
3601 }
3602
satokab751aa2010-09-14 19:17:36 +09003603 /**
3604 * @return Return the current subtype of this input method.
3605 */
satok42c5a162011-05-26 16:46:14 +09003606 @Override
satokab751aa2010-09-14 19:17:36 +09003607 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003608 // TODO: Make this work even for non-current users?
3609 if (!calledFromValidUser()) {
3610 return null;
3611 }
3612 synchronized (mMethodMap) {
3613 return getCurrentInputMethodSubtypeLocked();
3614 }
3615 }
3616
3617 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003618 if (mCurMethodId == null) {
3619 return null;
3620 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003621 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003622 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3623 if (imi == null || imi.getSubtypeCount() == 0) {
3624 return null;
satok4e4569d2010-11-19 18:45:53 +09003625 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003626 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003627 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3628 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003629 if (subtypeId == NOT_A_SUBTYPE_ID) {
3630 // If there are no selected subtypes, the framework will try to find
3631 // the most applicable subtype from explicitly or implicitly enabled
3632 // subtypes.
3633 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003634 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003635 // If there is only one explicitly or implicitly enabled subtype,
3636 // just returns it.
3637 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3638 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3639 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003640 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003641 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003642 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003643 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003644 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003645 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3646 true);
satok4e4569d2010-11-19 18:45:53 +09003647 }
satok3ef8b292010-11-23 06:06:29 +09003648 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003649 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003650 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003651 }
3652 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003653 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003654 }
3655
satok4e4569d2010-11-19 18:45:53 +09003656 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003657 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003658 @Override
satok4e4569d2010-11-19 18:45:53 +09003659 public List getShortcutInputMethodsAndSubtypes() {
3660 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003661 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003662 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003663 // If there are no selected shortcut subtypes, the framework will try to find
3664 // the most applicable subtype from all subtypes whose mode is
3665 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003666 Pair<InputMethodInfo, InputMethodSubtype> info =
3667 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003668 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003669 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003670 ret.add(info.first);
3671 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003672 }
satok3da92232011-01-11 22:46:30 +09003673 return ret;
satokf3db1af2010-11-23 13:34:33 +09003674 }
satokf3db1af2010-11-23 13:34:33 +09003675 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3676 ret.add(imi);
3677 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3678 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003679 }
3680 }
satokf3db1af2010-11-23 13:34:33 +09003681 return ret;
satok4e4569d2010-11-19 18:45:53 +09003682 }
3683 }
3684
satok42c5a162011-05-26 16:46:14 +09003685 @Override
satokb66d2872010-11-10 01:04:04 +09003686 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003687 // TODO: Make this work even for non-current users?
3688 if (!calledFromValidUser()) {
3689 return false;
3690 }
satokb66d2872010-11-10 01:04:04 +09003691 synchronized (mMethodMap) {
3692 if (subtype != null && mCurMethodId != null) {
3693 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003694 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003695 if (subtypeId != NOT_A_SUBTYPE_ID) {
3696 setInputMethodLocked(mCurMethodId, subtypeId);
3697 return true;
3698 }
3699 }
3700 return false;
3701 }
3702 }
3703
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003704 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003705 private static class InputMethodFileManager {
3706 private static final String SYSTEM_PATH = "system";
3707 private static final String INPUT_METHOD_PATH = "inputmethod";
3708 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3709 private static final String NODE_SUBTYPES = "subtypes";
3710 private static final String NODE_SUBTYPE = "subtype";
3711 private static final String NODE_IMI = "imi";
3712 private static final String ATTR_ID = "id";
3713 private static final String ATTR_LABEL = "label";
3714 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003715 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003716 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003717 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003718 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3719 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3720 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003721 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003722 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3723 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003724 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003725 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003726 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003727 if (methodMap == null) {
3728 throw new NullPointerException("methodMap is null");
3729 }
3730 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003731 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003732 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3733 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003734 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003735 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003736 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3737 }
3738 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3739 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3740 if (!subtypeFile.exists()) {
3741 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003742 writeAdditionalInputMethodSubtypes(
3743 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003744 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003745 readAdditionalInputMethodSubtypes(
3746 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003747 }
3748 }
3749
3750 private void deleteAllInputMethodSubtypes(String imiId) {
3751 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003752 mAdditionalSubtypesMap.remove(imiId);
3753 writeAdditionalInputMethodSubtypes(
3754 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003755 }
3756 }
3757
3758 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003759 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003760 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003761 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003762 final int N = additionalSubtypes.length;
3763 for (int i = 0; i < N; ++i) {
3764 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003765 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003766 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003767 } else {
3768 Slog.w(TAG, "Duplicated subtype definition found: "
3769 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003770 }
3771 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003772 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3773 writeAdditionalInputMethodSubtypes(
3774 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003775 }
3776 }
3777
3778 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3779 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003780 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003781 }
3782 }
3783
3784 private static void writeAdditionalInputMethodSubtypes(
3785 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3786 HashMap<String, InputMethodInfo> methodMap) {
3787 // Safety net for the case that this function is called before methodMap is set.
3788 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3789 FileOutputStream fos = null;
3790 try {
3791 fos = subtypesFile.startWrite();
3792 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003793 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003794 out.startDocument(null, true);
3795 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3796 out.startTag(null, NODE_SUBTYPES);
3797 for (String imiId : allSubtypes.keySet()) {
3798 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3799 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3800 continue;
3801 }
3802 out.startTag(null, NODE_IMI);
3803 out.attribute(null, ATTR_ID, imiId);
3804 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3805 final int N = subtypesList.size();
3806 for (int i = 0; i < N; ++i) {
3807 final InputMethodSubtype subtype = subtypesList.get(i);
3808 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003809 if (subtype.hasSubtypeId()) {
3810 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3811 String.valueOf(subtype.getSubtypeId()));
3812 }
satoke7c6998e2011-06-03 17:57:59 +09003813 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3814 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3815 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003816 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3817 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003818 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3819 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3820 out.attribute(null, ATTR_IS_AUXILIARY,
3821 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003822 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3823 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003824 out.endTag(null, NODE_SUBTYPE);
3825 }
3826 out.endTag(null, NODE_IMI);
3827 }
3828 out.endTag(null, NODE_SUBTYPES);
3829 out.endDocument();
3830 subtypesFile.finishWrite(fos);
3831 } catch (java.io.IOException e) {
3832 Slog.w(TAG, "Error writing subtypes", e);
3833 if (fos != null) {
3834 subtypesFile.failWrite(fos);
3835 }
3836 }
3837 }
3838
3839 private static void readAdditionalInputMethodSubtypes(
3840 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3841 if (allSubtypes == null || subtypesFile == null) return;
3842 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003843 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003844 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003845 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003846 int type = parser.getEventType();
3847 // Skip parsing until START_TAG
3848 while ((type = parser.next()) != XmlPullParser.START_TAG
3849 && type != XmlPullParser.END_DOCUMENT) {}
3850 String firstNodeName = parser.getName();
3851 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3852 throw new XmlPullParserException("Xml doesn't start with subtypes");
3853 }
3854 final int depth =parser.getDepth();
3855 String currentImiId = null;
3856 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3857 while (((type = parser.next()) != XmlPullParser.END_TAG
3858 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3859 if (type != XmlPullParser.START_TAG)
3860 continue;
3861 final String nodeName = parser.getName();
3862 if (NODE_IMI.equals(nodeName)) {
3863 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3864 if (TextUtils.isEmpty(currentImiId)) {
3865 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3866 continue;
3867 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003868 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003869 allSubtypes.put(currentImiId, tempSubtypesArray);
3870 } else if (NODE_SUBTYPE.equals(nodeName)) {
3871 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3872 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3873 continue;
3874 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003875 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003876 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003877 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003878 parser.getAttributeValue(null, ATTR_LABEL));
3879 final String imeSubtypeLocale =
3880 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003881 final String languageTag =
3882 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003883 final String imeSubtypeMode =
3884 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3885 final String imeSubtypeExtraValue =
3886 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003887 final boolean isAuxiliary = "1".equals(String.valueOf(
3888 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003889 final boolean isAsciiCapable = "1".equals(String.valueOf(
3890 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003891 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003892 .setSubtypeNameResId(label)
3893 .setSubtypeIconResId(icon)
3894 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003895 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003896 .setSubtypeMode(imeSubtypeMode)
3897 .setSubtypeExtraValue(imeSubtypeExtraValue)
3898 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003899 .setIsAsciiCapable(isAsciiCapable);
3900 final String subtypeIdString =
3901 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3902 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003903 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003904 }
3905 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003906 }
3907 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003908 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3909 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003910 return;
satoke7c6998e2011-06-03 17:57:59 +09003911 }
3912 }
3913 }
3914
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003915 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3916 @NonNull
3917 private final Handler mHandler;
3918
3919 LocalServiceImpl(@NonNull final Handler handler) {
3920 mHandler = handler;
3921 }
3922
3923 @Override
3924 public void setInteractive(boolean interactive) {
3925 // Do everything in handler so as not to block the caller.
3926 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3927 interactive ? 1 : 0, 0));
3928 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003929
3930 @Override
3931 public void switchInputMethod(boolean forwardDirection) {
3932 // Do everything in handler so as not to block the caller.
3933 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3934 forwardDirection ? 1 : 0, 0));
3935 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003936
3937 @Override
3938 public void hideCurrentInputMethod() {
3939 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3940 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3941 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003942 }
3943
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003944 private static String imeWindowStatusToString(final int imeWindowVis) {
3945 final StringBuilder sb = new StringBuilder();
3946 boolean first = true;
3947 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3948 sb.append("Active");
3949 first = false;
3950 }
3951 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3952 if (!first) {
3953 sb.append("|");
3954 }
3955 sb.append("Visible");
3956 }
3957 return sb.toString();
3958 }
3959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003961 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3962 @Nullable Uri contentUri, @Nullable String packageName) {
3963 if (!calledFromValidUser()) {
3964 return null;
3965 }
3966
3967 if (token == null) {
3968 throw new NullPointerException("token");
3969 }
3970 if (packageName == null) {
3971 throw new NullPointerException("packageName");
3972 }
3973 if (contentUri == null) {
3974 throw new NullPointerException("contentUri");
3975 }
3976 final String contentUriScheme = contentUri.getScheme();
3977 if (!"content".equals(contentUriScheme)) {
3978 throw new InvalidParameterException("contentUri must have content scheme");
3979 }
3980
3981 synchronized (mMethodMap) {
3982 final int uid = Binder.getCallingUid();
3983 if (mCurMethodId == null) {
3984 return null;
3985 }
3986 if (mCurToken != token) {
3987 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3988 + " token=" + token);
3989 return null;
3990 }
3991 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3992 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3993 // nature of our system. Let's compare it with our internal record.
3994 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3995 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3996 + mCurAttribute.packageName + " packageName=" + packageName);
3997 return null;
3998 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003999 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004000 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004001 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004002 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004003 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4004 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4005 imeUserId);
4006 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4007 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4008 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4009 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4010 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4011 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4012 // actually allowed to "uid", which is guaranteed to be the IME's one.
4013 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4014 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004015 }
4016 }
4017
4018 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004019 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4020 if (!calledFromValidUser()) {
4021 return;
4022 }
4023 synchronized (mMethodMap) {
4024 if (!calledWithValidToken(token)) {
4025 return;
4026 }
4027 if (mCurClient != null && mCurClient.client != null) {
4028 mInFullscreenMode = fullscreen;
4029 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4030 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4031 }
4032 }
4033 }
4034
4035 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4037 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4038 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4041 + Binder.getCallingPid()
4042 + ", uid=" + Binder.getCallingUid());
4043 return;
4044 }
4045
4046 IInputMethod method;
4047 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004048 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 synchronized (mMethodMap) {
4053 p.println("Current Input Method Manager state:");
4054 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004055 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 for (int i=0; i<N; i++) {
4057 InputMethodInfo info = mMethodList.get(i);
4058 p.println(" InputMethod #" + i + ":");
4059 info.dump(p, " ");
4060 }
4061 p.println(" Clients:");
4062 for (ClientState ci : mClients.values()) {
4063 p.println(" Client " + ci + ":");
4064 p.println(" client=" + ci.client);
4065 p.println(" inputContext=" + ci.inputContext);
4066 p.println(" sessionRequested=" + ci.sessionRequested);
4067 p.println(" curSession=" + ci.curSession);
4068 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004069 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004071 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4072 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004073 focusedWindowClient = mCurFocusedWindowClient;
4074 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4076 + " mBoundToMethod=" + mBoundToMethod);
4077 p.println(" mCurToken=" + mCurToken);
4078 p.println(" mCurIntent=" + mCurIntent);
4079 method = mCurMethod;
4080 p.println(" mCurMethod=" + mCurMethod);
4081 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004082 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 p.println(" mShowRequested=" + mShowRequested
4084 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4085 + " mShowForced=" + mShowForced
4086 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004087 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004088 p.println(" mCurUserActionNotificationSequenceNumber="
4089 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004090 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004091 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004092 p.println(" mSwitchingController:");
4093 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004094 p.println(" mSettings:");
4095 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004097
Jeff Brownb88102f2010-09-08 11:49:43 -07004098 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 pw.flush();
4101 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004102 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4103 } catch (IOException | RemoteException e) {
4104 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004106 } else {
4107 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004109
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004110 if (focusedWindowClient != null && client != focusedWindowClient) {
4111 p.println(" ");
4112 p.println("Warning: Current input method client doesn't match the last focused. "
4113 + "window.");
4114 p.println("Dumping input method client in the last focused window just in case.");
4115 p.println(" ");
4116 pw.flush();
4117 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004118 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4119 } catch (IOException | RemoteException e) {
4120 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004121 }
4122 }
4123
Jeff Brownb88102f2010-09-08 11:49:43 -07004124 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 pw.flush();
4127 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004128 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4129 } catch (IOException | RemoteException e) {
4130 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004132 } else {
4133 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 }
4135 }
4136}