blob: b6b3c43a597bbc266ecac750a15082242bda4bc9 [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 Yukawa6db3bfe2017-02-13 12:04:41 -0800127import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700128import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import android.view.inputmethod.InputMethod;
130import android.view.inputmethod.InputMethodInfo;
131import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700132import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900133import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900134import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900135import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900136import android.widget.CompoundButton;
137import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900138import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900139import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900140import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700141import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
satoke7c6998e2011-06-03 17:57:59 +0900143import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900145import java.io.FileInputStream;
146import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147import java.io.IOException;
148import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700149import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100150import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700151import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900153import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154import java.util.HashMap;
155import java.util.List;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800156import java.util.WeakHashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158/**
159 * This class provides a system service that manages input methods.
160 */
161public class InputMethodManagerService extends IInputMethodManager.Stub
162 implements ServiceConnection, Handler.Callback {
163 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700164 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700165 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700167 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
168 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
169 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 static final int MSG_UNBIND_INPUT = 1000;
172 static final int MSG_BIND_INPUT = 1010;
173 static final int MSG_SHOW_SOFT_INPUT = 1020;
174 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700175 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_ATTACH_TOKEN = 1040;
177 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
Yohei Yukawa33e81792015-11-17 21:14:42 -0800181 static final int MSG_UNBIND_CLIENT = 3000;
182 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700183 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700184 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900185 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800186 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800187 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800188
satok01038492012-04-09 21:08:27 +0900189 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
190
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700191 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
192
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700193 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
satokf9f01002011-05-19 21:31:50 +0900195 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
196
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900197 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900198 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900199
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700200 @Retention(SOURCE)
201 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
202 private @interface HardKeyboardBehavior {
203 int WIRELESS_AFFORDANCE = 0;
204 int WIRED_AFFORDANCE = 1;
205 }
satok4e4569d2010-11-19 18:45:53 +0900206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800208 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900210 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700213 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700215 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900216 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900217 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900218 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800219 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900221 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 // All known input methods. mMethodMap also serves as the global
224 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700225 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
226 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900227 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700228 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900229 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
Yohei Yukawae0733062017-02-09 22:49:35 -0800231 /**
232 * Tracks how many times {@link #mMethodMap} was updated.
233 */
234 @GuardedBy("mMethodMap")
235 private int mMethodMapUpdateCount = 0;
236
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700237 // Used to bring IME service up to visible adjustment while it is being shown.
238 final ServiceConnection mVisibleConnection = new ServiceConnection() {
239 @Override public void onServiceConnected(ComponentName name, IBinder service) {
240 }
241
242 @Override public void onServiceDisconnected(ComponentName name) {
243 }
244 };
245 boolean mVisibleBound = false;
246
satok7cfc0ed2011-06-20 21:29:36 +0900247 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700248 private NotificationManager mNotificationManager;
249 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700250 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400251 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700252 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900253 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900254 private boolean mNotificationShown;
255
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900256 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 final ClientState client;
258 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700259
260 IInputMethodSession session;
261 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 @Override
264 public String toString() {
265 return "SessionState{uid " + client.uid + " pid " + client.pid
266 + " method " + Integer.toHexString(
267 System.identityHashCode(method))
268 + " session " + Integer.toHexString(
269 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700270 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 + "}";
272 }
273
274 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700275 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 client = _client;
277 method = _method;
278 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700279 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Jeff Brownc28867a2013-03-26 15:42:39 -0700283 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 final IInputMethodClient client;
285 final IInputContext inputContext;
286 final int uid;
287 final int pid;
288 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 boolean sessionRequested;
291 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 @Override
294 public String toString() {
295 return "ClientState{" + Integer.toHexString(
296 System.identityHashCode(this)) + " uid " + uid
297 + " pid " + pid + "}";
298 }
299
300 ClientState(IInputMethodClient _client, IInputContext _inputContext,
301 int _uid, int _pid) {
302 client = _client;
303 inputContext = _inputContext;
304 uid = _uid;
305 pid = _pid;
306 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
307 }
308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700310 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700313 * Set once the system is ready to run third party code.
314 */
315 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700317 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700318 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
319 * method. This is to be synchronized with the secure settings keyed with
320 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
321 *
322 * <p>This can be transiently {@code null} when the system is re-initializing input method
323 * settings, e.g., the system locale is just changed.</p>
324 *
325 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
326 * {@link InputMethodManagerService}.</p>
327 *
328 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700330 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * The current binding sequence number, incremented every time there is
335 * a new bind performed.
336 */
337 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * The client that is currently bound to an input method.
341 */
342 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800345 * The last window token that we confirmed to be focused. This is always updated upon reports
346 * from the input method client. If the window state is already changed before the report is
347 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700348 */
349 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700351 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800352 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
353 *
354 * @see #mCurFocusedWindow
355 */
356 int mCurFocusedWindowSoftInputMode;
357
358 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800359 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
360 */
361 ClientState mCurFocusedWindowClient;
362
363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * The input context last provided by the current client.
365 */
366 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700369 * The missing method flags for the input context last provided by the current client.
370 *
371 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
372 */
373 int mCurInputContextMissingMethods;
374
375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * The attributes last provided by the current client.
377 */
378 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700381 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700383 *
384 * <p>This can be {@code null} when no input method is connected.</p>
385 *
386 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700388 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
satokab751aa2010-09-14 19:17:36 +0900392 * The current subtype of the current input method.
393 */
394 private InputMethodSubtype mCurrentSubtype;
395
satok4e4569d2010-11-19 18:45:53 +0900396 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900397 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700398 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900399
John Spurlocke0980502013-10-25 11:59:29 -0400400 // Was the keyguard locked when this client became current?
401 private boolean mCurClientInKeyguard;
402
satokab751aa2010-09-14 19:17:36 +0900403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * Set to true if our ServiceConnection is currently actively bound to
405 * a service (whether or not we have gotten its IBinder back yet).
406 */
407 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 /**
410 * Set if the client has asked for the input method to be shown.
411 */
412 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Set if we were explicitly told to show the input method.
416 */
417 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * Set if we were forced to be shown.
421 */
422 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * Set if we last told the input method to show itself.
426 */
427 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800430 * {@code true} if the current input method is in fullscreen mode.
431 */
432 boolean mInFullscreenMode;
433
434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 * The Intent used to connect to the current input method.
436 */
437 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
440 * The token we have made for the currently active input method, to
441 * identify it in the future.
442 */
443 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
446 * If non-null, this is the input method service we are currently connected
447 * to.
448 */
449 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
452 * Time that we last initiated a bind to the input method, to determine
453 * if we should try to disconnect and reconnect to it.
454 */
455 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 /**
458 * Have we called mCurMethod.bindInput()?
459 */
460 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 /**
463 * Currently enabled session. Only touched by service thread, not
464 * protected by a lock.
465 */
466 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700469 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700471 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900473 int mCurUserActionNotificationSequenceNumber = 0;
474
Joe Onorato857fd9b2011-01-27 15:08:35 -0800475 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900476
477 /**
478 * A set of status bits regarding the active IME.
479 *
480 * <p>This value is a combination of following two bits:</p>
481 * <dl>
482 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
483 * <dd>
484 * If this bit is ON, connected IME is ready to accept touch/key events.
485 * </dd>
486 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
487 * <dd>
488 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
489 * </dd>
490 * </dl>
491 * <em>Do not update this value outside of setImeWindowStatus.</em>
492 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800493 int mImeWindowVis;
494
Ken Wakasa05dbb652011-08-22 15:22:43 +0900495 private AlertDialog.Builder mDialogBuilder;
496 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700497 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900498 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700499 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900500 private InputMethodInfo[] mIms;
501 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800502 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700503 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800504 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900505 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
506 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500507 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700508 @HardKeyboardBehavior
509 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800510
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800511 /**
512 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
513 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
514 * will not affect those tasks that are already posted.
515 *
516 * <p>Posting {@link #MSG_START_INPUT} message basically means that
517 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
518 * back in the current IME process shortly, which will also affect what the current IME starts
519 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
520 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
521 * logical input session between the client application and the current IME.</p>
522 *
523 * <p>Be careful to not keep strong references to this object forever, which can prevent
524 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
525 * </p>
526 */
527 private static class StartInputInfo {
528 @NonNull
529 final IBinder mImeToken;
530 @Nullable
531 final IBinder mTargetWindow;
532
533 StartInputInfo(@NonNull IBinder imeToken, @Nullable IBinder targetWindow) {
534 mImeToken = imeToken;
535 mTargetWindow = targetWindow;
536 }
537 }
538
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800539 @GuardedBy("mMethodMap")
540 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700543 int mUserId;
544 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700545 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800546 String mLastEnabled = "";
547
Yohei Yukawa81482972015-06-04 00:58:59 -0700548 /**
549 * <em>This constructor must be called within the lock.</em>
550 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 SettingsObserver(Handler handler) {
552 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700553 }
554
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800555 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700556 if (mRegistered && mUserId == userId) {
557 return;
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700560 if (mRegistered) {
561 mContext.getContentResolver().unregisterContentObserver(this);
562 mRegistered = false;
563 }
564 if (mUserId != userId) {
565 mLastEnabled = "";
566 mUserId = userId;
567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700569 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900570 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700571 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900572 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700573 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700574 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700575 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800576 resolver.registerContentObserver(Settings.Secure.getUriFor(
577 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700578 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
Michael Wright7b5a96b2014-08-09 19:28:42 -0700581 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800582 final Uri showImeUri = Settings.Secure.getUriFor(
583 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
584 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
585 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700587 if (showImeUri.equals(uri)) {
588 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800589 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
590 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
591 mContext.getContentResolver(),
592 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
593 0, mUserId) == 1;
594 if (mAccessibilityRequestingNoSoftKeyboard) {
595 final boolean showRequested = mShowRequested;
596 hideCurrentInputLocked(0, null);
597 mShowRequested = showRequested;
598 } else if (mShowRequested) {
599 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
600 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700601 } else {
602 boolean enabledChanged = false;
603 String newEnabled = mSettings.getEnabledInputMethodsStr();
604 if (!mLastEnabled.equals(newEnabled)) {
605 mLastEnabled = newEnabled;
606 enabledChanged = true;
607 }
608 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
611 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700612
613 @Override
614 public String toString() {
615 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
616 + " mLastEnabled=" + mLastEnabled + "}";
617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800619
Yohei Yukawa79247822017-01-23 15:26:15 -0800620 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 @Override
622 public void onReceive(Context context, Intent intent) {
623 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700624 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900625 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700626 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900627 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800628 } else if (Intent.ACTION_USER_ADDED.equals(action)
629 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100630 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800631 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700632 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
633 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
634 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
635 final String prevValue = intent.getStringExtra(
636 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
637 final String newValue = intent.getStringExtra(
638 Intent.EXTRA_SETTING_NEW_VALUE);
639 restoreEnabledInputMethods(mContext, prevValue, newValue);
640 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800641 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800642 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900643 } else {
644 Slog.w(TAG, "Unexpected intent " + intent);
645 }
646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800648
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800649 /**
650 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
651 *
652 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
653 * the users. We should ignore this event if this is about any background user's locale.</p>
654 *
655 * <p>Caution: This method must not be called when system is not ready.</p>
656 */
657 void onActionLocaleChanged() {
658 synchronized (mMethodMap) {
659 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
660 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
661 return;
662 }
663 buildInputMethodListLocked(true);
664 // If the locale is changed, needs to reset the default ime
665 resetDefaultImeLocked(mContext);
666 updateFromSettingsLocked(true);
667 mLastSystemLocales = possibleNewLocale;
668 }
669 }
670
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700671 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
672 // does not attempt to validate on the fly with any installed device policy, so must only
673 // be run in the context of initial device setup.
674 //
675 // TODO: Move this method to InputMethodUtils with adding unit tests.
676 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
677 if (DEBUG_RESTORE) {
678 Slog.i(TAG, "Restoring enabled input methods:");
679 Slog.i(TAG, "prev=" + prevValue);
680 Slog.i(TAG, " new=" + newValue);
681 }
682 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900683 ArrayMap<String, ArraySet<String>> prevMap =
684 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
685 ArrayMap<String, ArraySet<String>> newMap =
686 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700687
688 // Merge the restored ime+subtype enabled states into the live state
689 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
690 final String imeId = entry.getKey();
691 ArraySet<String> prevSubtypes = prevMap.get(imeId);
692 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700693 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700694 prevMap.put(imeId, prevSubtypes);
695 }
696 prevSubtypes.addAll(entry.getValue());
697 }
698
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700699 final String mergedImesAndSubtypesString =
700 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700701 if (DEBUG_RESTORE) {
702 Slog.i(TAG, "Merged IME string:");
703 Slog.i(TAG, " " + mergedImesAndSubtypesString);
704 }
705 Settings.Secure.putString(context.getContentResolver(),
706 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
707 }
708
Yohei Yukawac4e44912017-02-09 19:30:22 -0800709 final class MyPackageMonitor extends PackageMonitor {
710 /**
711 * Set of packages to be monitored.
712 *
713 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
714 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
715 * dynamically unless the entire package is updated, which also always triggers package
716 * rescanning.</p>
717 */
718 @GuardedBy("mMethodMap")
719 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
720
721 @GuardedBy("mMethodMap")
722 void clearPackagesToMonitorComponentChangeLocked() {
723 mPackagesToMonitorComponentChange.clear();
724 }
725
726 @GuardedBy("mMethodMap")
727 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
728 mPackagesToMonitorComponentChange.add(packageName);
729 }
730
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900731 private boolean isChangingPackagesOfCurrentUser() {
732 final int userId = getChangingUserId();
733 final boolean retval = userId == mSettings.getCurrentUserId();
734 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900735 if (!retval) {
736 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
737 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900738 }
739 return retval;
740 }
741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800743 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900744 if (!isChangingPackagesOfCurrentUser()) {
745 return false;
746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900748 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 final int N = mMethodList.size();
750 if (curInputMethodId != null) {
751 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800752 InputMethodInfo imi = mMethodList.get(i);
753 if (imi.getId().equals(curInputMethodId)) {
754 for (String pkg : packages) {
755 if (imi.getPackageName().equals(pkg)) {
756 if (!doit) {
757 return true;
758 }
satok723a27e2010-11-11 14:58:11 +0900759 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800760 chooseNewDefaultIMELocked();
761 return true;
762 }
763 }
764 }
765 }
766 }
767 }
768 return false;
769 }
770
771 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800772 public boolean onPackageChanged(String packageName, int uid, String[] components) {
773 // If this package is in the watch list, we want to check it.
774 synchronized (mMethodMap) {
775 return mPackagesToMonitorComponentChange.contains(packageName);
776 }
777 }
778
779 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800780 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900781 if (!isChangingPackagesOfCurrentUser()) {
782 return;
783 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800784 synchronized (mMethodMap) {
785 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900786 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800787 final int N = mMethodList.size();
788 if (curInputMethodId != null) {
789 for (int i=0; i<N; i++) {
790 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900791 final String imiId = imi.getId();
792 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800793 curIm = imi;
794 }
satoke7c6998e2011-06-03 17:57:59 +0900795
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800796 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900797 if (isPackageModified(imi.getPackageName())) {
798 mFileManager.deleteAllInputMethodSubtypes(imiId);
799 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800800 if (change == PACKAGE_TEMPORARY_CHANGE
801 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800802 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800803 + imi.getComponent());
804 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806 }
807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808
Yohei Yukawa94e33302016-02-12 19:37:03 -0800809 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800812
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800813 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800814 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800815 if (change == PACKAGE_TEMPORARY_CHANGE
816 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800817 ServiceInfo si = null;
818 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900819 si = mIPackageManager.getServiceInfo(
820 curIm.getComponent(), 0, mSettings.getCurrentUserId());
821 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800822 }
823 if (si == null) {
824 // Uh oh, current input method is no longer around!
825 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800826 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900827 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800828 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800829 changed = true;
830 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800831 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900832 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800833 }
834 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800835 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800836 }
satokab751aa2010-09-14 19:17:36 +0900837
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800838 if (curIm == null) {
839 // We currently don't have a default input method... is
840 // one now available?
841 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700842 } else if (!changed && isPackageModified(curIm.getPackageName())) {
843 // Even if the current input method is still available, mCurrentSubtype could
844 // be obsolete when the package is modified in practice.
845 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800846 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800847
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800848 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800849 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851 }
852 }
853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800854
Jeff Brownc28867a2013-03-26 15:42:39 -0700855 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900856 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700857 private final IInputMethod mMethod;
858 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800859
Jeff Brownc28867a2013-03-26 15:42:39 -0700860 MethodCallback(InputMethodManagerService imms, IInputMethod method,
861 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900862 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700863 mMethod = method;
864 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
satoke7c6998e2011-06-03 17:57:59 +0900867 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700868 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700869 long ident = Binder.clearCallingIdentity();
870 try {
871 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
872 } finally {
873 Binder.restoreCallingIdentity(ident);
874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
876 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800877
satok01038492012-04-09 21:08:27 +0900878 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700879 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900880 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700881 public void onHardKeyboardStatusChange(boolean available) {
882 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
883 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900884 }
885
Michael Wright7b5a96b2014-08-09 19:28:42 -0700886 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900887 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700888 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900889 }
890 synchronized(mMethodMap) {
891 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
892 && mSwitchingDialog.isShowing()) {
893 mSwitchingDialogTitleView.findViewById(
894 com.android.internal.R.id.hard_keyboard_section).setVisibility(
895 available ? View.VISIBLE : View.GONE);
896 }
897 }
898 }
899 }
900
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800901 public static final class Lifecycle extends SystemService {
902 private InputMethodManagerService mService;
903
904 public Lifecycle(Context context) {
905 super(context);
906 mService = new InputMethodManagerService(context);
907 }
908
909 @Override
910 public void onStart() {
911 LocalServices.addService(InputMethodManagerInternal.class,
912 new LocalServiceImpl(mService.mHandler));
913 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
914 }
915
916 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800917 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700918 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800919 // TODO: Dispatch this to a worker thread as needed.
920 mService.onSwitchUser(userHandle);
921 }
922
923 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800924 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700925 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800926 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800927 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
928 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
929 .getService(Context.STATUS_BAR_SERVICE);
930 mService.systemRunning(statusBarService);
931 }
932 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800933
934 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700935 public void onUnlockUser(final @UserIdInt int userHandle) {
936 // Called on ActivityManager thread.
937 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700938 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800939 }
940 }
941
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800942 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800943 synchronized(mMethodMap) {
944 final int currentUserId = mSettings.getCurrentUserId();
945 if (DEBUG) {
946 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
947 }
948 if (userId != currentUserId) {
949 return;
950 }
951 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800952 if (mSystemReady) {
953 // We need to rebuild IMEs.
954 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
955 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
956 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800957 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800958 }
959
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800960 void onSwitchUser(@UserIdInt int userId) {
961 synchronized (mMethodMap) {
962 switchUserLocked(userId);
963 }
964 }
965
Seigo Nonaka7309b122015-08-17 18:34:13 -0700966 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900967 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800969 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700971 // Note: SettingsObserver doesn't register observers in its constructor.
972 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 mIWindowManager = IWindowManager.Stub.asInterface(
974 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700975 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800976 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900977 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 public void executeMessage(Message msg) {
979 handleMessage(msg);
980 }
Mita Yuned218c72012-12-06 17:18:25 -0800981 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800982 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800983 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900984 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700985 mHasFeature = context.getPackageManager().hasSystemFeature(
986 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500987 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700988 mHardKeyboardBehavior = mContext.getResources().getInteger(
989 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900990
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400991 Bundle extras = new Bundle();
992 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
993 mImeSwitcherNotification = new Notification.Builder(mContext)
994 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
995 .setWhen(0)
996 .setOngoing(true)
997 .addExtras(extras)
998 .setCategory(Notification.CATEGORY_SYSTEM)
999 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001000
satok7cfc0ed2011-06-20 21:29:36 +09001001 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001002 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001003
1004 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001005
satok7cfc0ed2011-06-20 21:29:36 +09001006 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001007 int userId = 0;
1008 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001009 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001010 } catch (RemoteException e) {
1011 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1012 }
satok913a8922010-08-26 21:53:41 +09001013
satokd87c2592010-09-29 11:52:06 +09001014 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001015 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001016 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001017
Kenny Guy2a764942014-04-02 13:29:20 +01001018 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001019 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001020 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1021 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001022 }
1023
satok5b927c432012-05-01 20:09:34 +09001024 private void resetDefaultImeLocked(Context context) {
1025 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001026 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001027 return;
1028 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001029 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001030 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001031 if (suitableImes.isEmpty()) {
1032 Slog.i(TAG, "No default found");
1033 return;
satok5b927c432012-05-01 20:09:34 +09001034 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001035 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001036 if (DEBUG) {
1037 Slog.i(TAG, "Default found, using " + defIm.getId());
1038 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001039 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001040 }
1041
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001042 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001043 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1044 + " currentUserId=" + mSettings.getCurrentUserId());
1045
Yohei Yukawa81482972015-06-04 00:58:59 -07001046 // ContentObserver should be registered again when the user is changed
1047 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001048
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001049 // If the system is not ready or the device is not yed unlocked by the user, then we use
1050 // copy-on-write settings.
1051 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001052 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001053 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001054 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001055 // InputMethodFileManager should be reset when the user is changed
1056 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001057 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001058
1059 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1060 + " defaultImiId=" + defaultImiId);
1061
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001062 // For secondary users, the list of enabled IMEs may not have been updated since the
1063 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1064 // not be empty even if the IME has been uninstalled by the primary user.
1065 // Even in such cases, IMMS works fine because it will find the most applicable
1066 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001067 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001068 mLastSystemLocales = mRes.getConfiguration().getLocales();
1069
1070 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1071 if (mSystemReady) {
1072 hideCurrentInputLocked(0, null);
1073 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1074 buildInputMethodListLocked(initialUserSwitch);
1075 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1076 // This is the first time of the user switch and
1077 // set the current ime to the proper one.
1078 resetDefaultImeLocked(mContext);
1079 }
1080 updateFromSettingsLocked(true);
1081 try {
1082 startInputInnerLocked();
1083 } catch (RuntimeException e) {
1084 Slog.w(TAG, "Unexpected exception", e);
1085 }
1086 }
1087
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001088 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001089 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1090 mSettings.getEnabledInputMethodListLocked(), newUserId,
1091 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001092 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001093
1094 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1095 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001096 }
1097
Kenny Guy2a764942014-04-02 13:29:20 +01001098 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001099 mSettings.setCurrentProfileIds(
1100 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001101 }
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 @Override
1104 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1105 throws RemoteException {
1106 try {
1107 return super.onTransact(code, data, reply, flags);
1108 } catch (RuntimeException e) {
1109 // The input method manager only throws security exceptions, so let's
1110 // log all others.
1111 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001112 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
1114 throw e;
1115 }
1116 }
1117
Svetoslav Ganova0027152013-06-25 14:59:53 -07001118 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001119 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001120 if (DEBUG) {
1121 Slog.d(TAG, "--- systemReady");
1122 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001123 if (!mSystemReady) {
1124 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001125 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001126 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001127 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001128 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001129 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1130 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001131 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001132 if (mStatusBar != null) {
1133 mStatusBar.setIconVisibility(mSlotIme, false);
1134 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001135 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001136 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1137 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001138 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001139 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001140 mHardKeyboardListener);
1141 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001142
1143 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1144 mSettingsObserver.registerContentObserverLocked(currentUserId);
1145
1146 final IntentFilter broadcastFilter = new IntentFilter();
1147 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1148 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1149 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1150 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1151 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1152 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1153
1154 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1155 resetDefaultImeLocked(mContext);
1156 updateFromSettingsLocked(true);
1157 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1158 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1159 mContext.getBasePackageName());
1160
Dianne Hackborncc278702009-09-02 23:07:23 -07001161 try {
1162 startInputInnerLocked();
1163 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001164 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001165 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001166 }
1167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001169
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001170 // ---------------------------------------------------------------------------------------
1171 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1172 // 1) it comes from the system process
1173 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1174 private boolean calledFromValidUser() {
1175 final int uid = Binder.getCallingUid();
1176 final int userId = UserHandle.getUserId(uid);
1177 if (DEBUG) {
1178 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1179 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1180 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001181 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1182 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001183 }
Kenny Guy2a764942014-04-02 13:29:20 +01001184 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001185 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001186 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001187
1188 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1189 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1190 // must not manage background users' states in any functions.
1191 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1192 // by a token.
1193 if (mContext.checkCallingOrSelfPermission(
1194 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1195 == PackageManager.PERMISSION_GRANTED) {
1196 if (DEBUG) {
1197 Slog.d(TAG, "--- Access granted because the calling process has "
1198 + "the INTERACT_ACROSS_USERS_FULL permission");
1199 }
1200 return true;
1201 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001202 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001203 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1204 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001205 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001206 }
1207
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001208
1209 /**
1210 * Returns true iff the caller is identified to be the current input method with the token.
1211 * @param token The window token given to the input method when it was started.
1212 * @return true if and only if non-null valid token is specified.
1213 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001214 private boolean calledWithValidToken(@Nullable IBinder token) {
1215 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1216 if (DEBUG) {
1217 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1218 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1219 }
1220 return false;
1221 }
1222 if (token == null || token != mCurToken) {
1223 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1224 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1225 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001226 return false;
1227 }
1228 return true;
1229 }
1230
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001231 private boolean bindCurrentInputMethodService(
1232 Intent service, ServiceConnection conn, int flags) {
1233 if (service == null || conn == null) {
1234 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1235 return false;
1236 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001237 return mContext.bindServiceAsUser(service, conn, flags,
1238 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001239 }
1240
satoke7c6998e2011-06-03 17:57:59 +09001241 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001243 // TODO: Make this work even for non-current users?
1244 if (!calledFromValidUser()) {
1245 return Collections.emptyList();
1246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001248 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250 }
1251
satoke7c6998e2011-06-03 17:57:59 +09001252 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001254 // TODO: Make this work even for non-current users?
1255 if (!calledFromValidUser()) {
1256 return Collections.emptyList();
1257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001259 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261 }
1262
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001263 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001264 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001265 * @return enabled subtypes of the specified imi
1266 */
satoke7c6998e2011-06-03 17:57:59 +09001267 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001268 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001269 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001270 // TODO: Make this work even for non-current users?
1271 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001272 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001273 }
satok67ddf9c2010-11-17 09:45:54 +09001274 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001275 final InputMethodInfo imi;
1276 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001277 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001278 } else {
1279 imi = mMethodMap.get(imiId);
1280 }
1281 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001282 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001283 }
1284 return mSettings.getEnabledInputMethodSubtypeListLocked(
1285 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001286 }
1287 }
1288
satoke7c6998e2011-06-03 17:57:59 +09001289 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 public void addClient(IInputMethodClient client,
1291 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001292 if (!calledFromValidUser()) {
1293 return;
1294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 synchronized (mMethodMap) {
1296 mClients.put(client.asBinder(), new ClientState(client,
1297 inputContext, uid, pid));
1298 }
1299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001300
satoke7c6998e2011-06-03 17:57:59 +09001301 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001303 if (!calledFromValidUser()) {
1304 return;
1305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001307 ClientState cs = mClients.remove(client.asBinder());
1308 if (cs != null) {
1309 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001310 if (mCurClient == cs) {
1311 mCurClient = null;
1312 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001313 if (mCurFocusedWindowClient == cs) {
1314 mCurFocusedWindowClient = null;
1315 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
1318 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 void executeOrSendMessage(IInterface target, Message msg) {
1321 if (target.asBinder() instanceof Binder) {
1322 mCaller.sendMessage(msg);
1323 } else {
1324 handleMessage(msg);
1325 msg.recycle();
1326 }
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
Yohei Yukawa33e81792015-11-17 21:14:42 -08001329 void unbindCurrentClientLocked(
1330 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001332 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 + mCurClient.client.asBinder());
1334 if (mBoundToMethod) {
1335 mBoundToMethod = false;
1336 if (mCurMethod != null) {
1337 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1338 MSG_UNBIND_INPUT, mCurMethod));
1339 }
1340 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001341
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001342 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1343 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001344 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1345 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001348
The Android Open Source Project10592532009-03-18 17:39:46 -07001349 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 private int getImeShowFlags() {
1354 int flags = 0;
1355 if (mShowForced) {
1356 flags |= InputMethod.SHOW_FORCED
1357 | InputMethod.SHOW_EXPLICIT;
1358 } else if (mShowExplicitlyRequested) {
1359 flags |= InputMethod.SHOW_EXPLICIT;
1360 }
1361 return flags;
1362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 private int getAppShowFlags() {
1365 int flags = 0;
1366 if (mShowForced) {
1367 flags |= InputMethodManager.SHOW_FORCED;
1368 } else if (!mShowExplicitlyRequested) {
1369 flags |= InputMethodManager.SHOW_IMPLICIT;
1370 }
1371 return flags;
1372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001374 InputBindResult attachNewInputLocked(
1375 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 if (!mBoundToMethod) {
1377 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1378 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1379 mBoundToMethod = true;
1380 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001381
1382 final Binder startInputToken = new Binder();
1383 final StartInputInfo info = new StartInputInfo(mCurToken, mCurFocusedWindow);
1384 mStartInputMap.put(startInputToken, info);
1385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001387 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001388 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001389 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001391 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001392 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001394 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001395 (session.channel != null ? session.channel.dup() : null),
1396 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001399 InputBindResult startInputLocked(
1400 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001401 IInputMethodClient client, IInputContext inputContext,
1402 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001403 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 // If no method is currently selected, do nothing.
1405 if (mCurMethodId == null) {
1406 return mNoBinding;
1407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 ClientState cs = mClients.get(client.asBinder());
1410 if (cs == null) {
1411 throw new IllegalArgumentException("unknown client "
1412 + client.asBinder());
1413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
Yohei Yukawa74750f22016-03-22 12:54:22 -07001415 if (attribute == null) {
1416 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1417 + " uid=" + cs.uid + " pid=" + cs.pid);
1418 return null;
1419 }
1420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 try {
1422 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1423 // Check with the window manager to make sure this client actually
1424 // has a window with focus. If not, reject. This is thread safe
1425 // because if the focus changes some time before or after, the
1426 // next client receiving focus that has any interest in input will
1427 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001428 if (DEBUG) {
1429 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1430 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 return null;
1433 }
1434 } catch (RemoteException e) {
1435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001437 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001438 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001439 }
1440
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001441 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001442 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001443 @NonNull EditorInfo attribute, int controlFlags,
1444 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001445 // If no method is currently selected, do nothing.
1446 if (mCurMethodId == null) {
1447 return mNoBinding;
1448 }
1449
Yohei Yukawad57ba672015-06-08 16:39:46 -07001450 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1451 attribute.packageName)) {
1452 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1453 + " uid=" + cs.uid + " package=" + attribute.packageName);
1454 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001455 }
1456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001458 // Was the keyguard locked when switching over to the new client?
1459 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 // If the client is changing, we need to switch over to the new
1461 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001462 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001463 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001464 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465
1466 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001467 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001468 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001469 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
1471 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 // Bump up the sequence for this client and attach it.
1474 mCurSeq++;
1475 if (mCurSeq <= 0) mCurSeq = 1;
1476 mCurClient = cs;
1477 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001478 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 // Check if the input method is changing.
1482 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1483 if (cs.curSession != null) {
1484 // Fast case: if we are already connected to the input method,
1485 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001486 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001487 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
1489 if (mHaveConnection) {
1490 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 // Return to client, and we will get back with it when
1492 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001493 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001494 return new InputBindResult(null, null, mCurId, mCurSeq,
1495 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 } else if (SystemClock.uptimeMillis()
1497 < (mLastBindTime+TIME_TO_RECONNECT)) {
1498 // In this case we have connected to the service, but
1499 // don't yet have its interface. If it hasn't been too
1500 // long since we did the connection, we'll return to
1501 // the client and wait to get the service interface so
1502 // we can report back. If it has been too long, we want
1503 // to fall through so we can try a disconnect/reconnect
1504 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001505 return new InputBindResult(null, null, mCurId, mCurSeq,
1506 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1509 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
1511 }
1512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001514 return startInputInnerLocked();
1515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001517 InputBindResult startInputInnerLocked() {
1518 if (mCurMethodId == null) {
1519 return mNoBinding;
1520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001522 if (!mSystemReady) {
1523 // If the system is not yet ready, we shouldn't be running third
1524 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001525 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1526 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1530 if (info == null) {
1531 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001533
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001534 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1537 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001538 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1539 com.android.internal.R.string.input_method_binding_label);
1540 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1541 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001542 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001543 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1544 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 mLastBindTime = SystemClock.uptimeMillis();
1546 mHaveConnection = true;
1547 mCurId = info.getId();
1548 mCurToken = new Binder();
1549 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001550 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001551 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 } catch (RemoteException e) {
1553 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001554 return new InputBindResult(null, null, mCurId, mCurSeq,
1555 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 } else {
1557 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001558 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 + mCurIntent);
1560 }
1561 return null;
1562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001563
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001564 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001565 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001566 IInputMethodClient client, IInputContext inputContext,
1567 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001568 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001569 if (!calledFromValidUser()) {
1570 return null;
1571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001573 if (DEBUG) {
1574 Slog.v(TAG, "startInput: reason="
1575 + InputMethodClient.getStartInputReason(startInputReason)
1576 + " client = " + client.asBinder()
1577 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001578 + " missingMethods="
1579 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001580 + " attribute=" + attribute
1581 + " controlFlags=#" + Integer.toHexString(controlFlags));
1582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 final long ident = Binder.clearCallingIdentity();
1584 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001585 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1586 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 } finally {
1588 Binder.restoreCallingIdentity(ident);
1589 }
1590 }
1591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001592
satoke7c6998e2011-06-03 17:57:59 +09001593 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 public void finishInput(IInputMethodClient client) {
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
satoke7c6998e2011-06-03 17:57:59 +09001597 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 public void onServiceConnected(ComponentName name, IBinder service) {
1599 synchronized (mMethodMap) {
1600 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1601 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001602 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001604 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001605 return;
1606 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001607 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001608 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1609 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001611 clearClientSessionLocked(mCurClient);
1612 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 }
1614 }
1615 }
1616 }
1617
Jeff Brownc28867a2013-03-26 15:42:39 -07001618 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1619 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 synchronized (mMethodMap) {
1621 if (mCurMethod != null && method != null
1622 && mCurMethod.asBinder() == method.asBinder()) {
1623 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001624 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001626 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001627 InputBindResult res = attachNewInputLocked(
1628 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 if (res.method != null) {
1630 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001631 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001633 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 }
1635 }
1636 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001637
1638 // Session abandoned. Close its associated input channel.
1639 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001642 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001643 if (mVisibleBound) {
1644 mContext.unbindService(mVisibleConnection);
1645 mVisibleBound = false;
1646 }
1647
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001648 if (mHaveConnection) {
1649 mContext.unbindService(this);
1650 mHaveConnection = false;
1651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001653 if (mCurToken != null) {
1654 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001655 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001656 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001657 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001658 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001659 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001660 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001661 } catch (RemoteException e) {
1662 }
1663 mCurToken = null;
1664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
The Android Open Source Project10592532009-03-18 17:39:46 -07001666 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001667 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001669
Yohei Yukawa33e81792015-11-17 21:14:42 -08001670 void resetCurrentMethodAndClient(
1671 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001672 mCurMethodId = null;
1673 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001674 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001675 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001676
1677 void requestClientSessionLocked(ClientState cs) {
1678 if (!cs.sessionRequested) {
1679 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1680 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1681 cs.sessionRequested = true;
1682 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1683 MSG_CREATE_SESSION, mCurMethod, channels[1],
1684 new MethodCallback(this, mCurMethod, channels[0])));
1685 }
1686 }
1687
1688 void clearClientSessionLocked(ClientState cs) {
1689 finishSessionLocked(cs.curSession);
1690 cs.curSession = null;
1691 cs.sessionRequested = false;
1692 }
1693
1694 private void finishSessionLocked(SessionState sessionState) {
1695 if (sessionState != null) {
1696 if (sessionState.session != null) {
1697 try {
1698 sessionState.session.finishSession();
1699 } catch (RemoteException e) {
1700 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001701 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001702 }
1703 sessionState.session = null;
1704 }
1705 if (sessionState.channel != null) {
1706 sessionState.channel.dispose();
1707 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001708 }
1709 }
1710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001711
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001712 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 if (mCurMethod != null) {
1714 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001715 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001717
Jeff Brownc28867a2013-03-26 15:42:39 -07001718 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001719 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 mCurMethod = null;
1721 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001722 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001723 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001724 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001725 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001727
satoke7c6998e2011-06-03 17:57:59 +09001728 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001730 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1731 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1732 // which is irregular but can eventually happen for everyone just by continuing using the
1733 // device. Thus it is important to make sure that all the internal states are properly
1734 // refreshed when this method is called back. Running
1735 // adb install -r <APK that implements the current IME>
1736 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001738 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 + " mCurIntent=" + mCurIntent);
1740 if (mCurMethod != null && mCurIntent != null
1741 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001742 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 // We consider this to be a new bind attempt, since the system
1744 // should now try to restart the service for us.
1745 mLastBindTime = SystemClock.uptimeMillis();
1746 mShowRequested = mInputShown;
1747 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001748 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750 }
1751 }
1752
satokf9f01002011-05-19 21:31:50 +09001753 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001755 synchronized (mMethodMap) {
1756 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001757 return;
1758 }
1759 final long ident = Binder.clearCallingIdentity();
1760 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001762 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001763 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001764 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001767 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001768 CharSequence contentDescription = null;
1769 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001770 // Use PackageManager to load label
1771 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001772 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001773 mIPackageManager.getApplicationInfo(packageName, 0,
1774 mSettings.getCurrentUserId()));
1775 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001776 /* ignore */
1777 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001778 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001779 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001780 contentDescription != null
1781 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001782 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001785 } finally {
1786 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 }
1790
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001791 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001792 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001793 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001794 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001795 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001796 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001797 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1798 // When physical keyboard is attached, we show the ime switcher (or notification if
1799 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1800 // exists in the IME switcher dialog. Might be OK to remove this condition once
1801 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1802 return true;
1803 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001804 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1805 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001806 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001807
1808 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1809 final int N = imis.size();
1810 if (N > 2) return true;
1811 if (N < 1) return false;
1812 int nonAuxCount = 0;
1813 int auxCount = 0;
1814 InputMethodSubtype nonAuxSubtype = null;
1815 InputMethodSubtype auxSubtype = null;
1816 for(int i = 0; i < N; ++i) {
1817 final InputMethodInfo imi = imis.get(i);
1818 final List<InputMethodSubtype> subtypes =
1819 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1820 final int subtypeCount = subtypes.size();
1821 if (subtypeCount == 0) {
1822 ++nonAuxCount;
1823 } else {
1824 for (int j = 0; j < subtypeCount; ++j) {
1825 final InputMethodSubtype subtype = subtypes.get(j);
1826 if (!subtype.isAuxiliary()) {
1827 ++nonAuxCount;
1828 nonAuxSubtype = subtype;
1829 } else {
1830 ++auxCount;
1831 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001832 }
1833 }
satok7cfc0ed2011-06-20 21:29:36 +09001834 }
1835 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001836 if (nonAuxCount > 1 || auxCount > 1) {
1837 return true;
1838 } else if (nonAuxCount == 1 && auxCount == 1) {
1839 if (nonAuxSubtype != null && auxSubtype != null
1840 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1841 || auxSubtype.overridesImplicitlyEnabledSubtype()
1842 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1843 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1844 return false;
1845 }
1846 return true;
1847 }
1848 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001849 }
1850
John Spurlocke0980502013-10-25 11:59:29 -04001851 private boolean isKeyguardLocked() {
1852 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1853 }
1854
satokdbf29502011-08-25 15:28:23 +09001855 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001856 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001857 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
1858 int backDisposition) {
1859 if (startInputToken == null) {
1860 throw new InvalidParameterException("startInputToken cannot be null");
1861 }
1862
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001863 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001864 return;
1865 }
1866
Yohei Yukawa69e68022017-02-13 12:04:50 -08001867 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001868 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08001869 info = mStartInputMap.get(startInputToken);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001870 if (info == null) {
1871 throw new InvalidParameterException("Unknown startInputToken=" + startInputToken);
1872 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001873 mImeWindowVis = vis;
1874 mBackDisposition = backDisposition;
1875 updateSystemUiLocked(token, vis, backDisposition);
1876 }
Yohei Yukawa69e68022017-02-13 12:04:50 -08001877 mWindowManagerInternal.updateInputMethodWindowStatus(info.mImeToken,
1878 (vis & InputMethodService.IME_VISIBLE) != 0, info.mTargetWindow);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001879 }
1880
1881 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1882 synchronized (mMethodMap) {
1883 updateSystemUiLocked(token, vis, backDisposition);
1884 }
1885 }
1886
1887 // Caution! This method is called in this class. Handle multi-user carefully
1888 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1889 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001890 return;
1891 }
1892
1893 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1894 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001895 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001896 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001897 // apply policy for binder calls
1898 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1899 vis = 0;
satok06487a52010-10-29 11:37:18 +09001900 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001901 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1902 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1903 if (mStatusBar != null) {
1904 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1905 needsToShowImeSwitcher);
1906 }
1907 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1908 if (imi != null && needsToShowImeSwitcher) {
1909 // Used to load label
1910 final CharSequence title = mRes.getText(
1911 com.android.internal.R.string.select_input_method);
1912 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1913 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001914 mImeSwitcherNotification.setContentTitle(title)
1915 .setContentText(summary)
1916 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001917 try {
1918 if ((mNotificationManager != null)
1919 && !mIWindowManager.hasNavigationBar()) {
1920 if (DEBUG) {
1921 Slog.d(TAG, "--- show notification: label = " + summary);
1922 }
1923 mNotificationManager.notifyAsUser(null,
1924 com.android.internal.R.string.select_input_method,
1925 mImeSwitcherNotification.build(), UserHandle.ALL);
1926 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001927 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001928 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001929 }
1930 } else {
1931 if (mNotificationShown && mNotificationManager != null) {
1932 if (DEBUG) {
1933 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001934 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001935 mNotificationManager.cancelAsUser(null,
1936 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1937 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001938 }
satok06487a52010-10-29 11:37:18 +09001939 }
1940 } finally {
1941 Binder.restoreCallingIdentity(ident);
1942 }
1943 }
1944
satoke7c6998e2011-06-03 17:57:59 +09001945 @Override
satokf9f01002011-05-19 21:31:50 +09001946 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001947 if (!calledFromValidUser()) {
1948 return;
1949 }
satokf9f01002011-05-19 21:31:50 +09001950 synchronized (mMethodMap) {
1951 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1952 for (int i = 0; i < spans.length; ++i) {
1953 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001954 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001955 mSecureSuggestionSpans.put(ss, currentImi);
1956 }
1957 }
1958 }
1959 }
1960
satoke7c6998e2011-06-03 17:57:59 +09001961 @Override
satokf9f01002011-05-19 21:31:50 +09001962 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001963 if (!calledFromValidUser()) {
1964 return false;
1965 }
satokf9f01002011-05-19 21:31:50 +09001966 synchronized (mMethodMap) {
1967 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1968 // TODO: Do not send the intent if the process of the targetImi is already dead.
1969 if (targetImi != null) {
1970 final String[] suggestions = span.getSuggestions();
1971 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001972 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001973 final Intent intent = new Intent();
1974 // Ensures that only a class in the original IME package will receive the
1975 // notification.
satok42c5a162011-05-26 16:46:14 +09001976 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001977 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1978 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1979 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1980 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001981 final long ident = Binder.clearCallingIdentity();
1982 try {
1983 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1984 } finally {
1985 Binder.restoreCallingIdentity(ident);
1986 }
satokf9f01002011-05-19 21:31:50 +09001987 return true;
1988 }
1989 }
1990 return false;
1991 }
1992
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001993 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001994 updateInputMethodsFromSettingsLocked(enabledMayChange);
1995 updateKeyboardFromSettingsLocked();
1996 }
1997
1998 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001999 if (enabledMayChange) {
2000 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2001 for (int i=0; i<enabled.size(); i++) {
2002 // We allow the user to select "disabled until used" apps, so if they
2003 // are enabling one of those here we now need to make it enabled.
2004 InputMethodInfo imm = enabled.get(i);
2005 try {
2006 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2007 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2008 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002009 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002010 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002011 if (DEBUG) {
2012 Slog.d(TAG, "Update state(" + imm.getId()
2013 + "): DISABLED_UNTIL_USED -> DEFAULT");
2014 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002015 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2016 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002017 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2018 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002019 }
2020 } catch (RemoteException e) {
2021 }
2022 }
2023 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002024 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2025 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2026 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2027 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002028 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002029 // There is no input method selected, try to choose new applicable input method.
2030 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002031 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002032 }
2033 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002035 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002037 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002038 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
satokf3db1af2010-11-23 13:34:33 +09002040 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002041 } else {
2042 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002043 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002045 // Here is not the perfect place to reset the switching controller. Ideally
2046 // mSwitchingController and mSettings should be able to share the same state.
2047 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2048 // the same enabled IMEs list.
2049 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002050
2051 }
2052
2053 public void updateKeyboardFromSettingsLocked() {
2054 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2055 if (mSwitchingDialog != null
2056 && mSwitchingDialogTitleView != null
2057 && mSwitchingDialog.isShowing()) {
2058 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2059 com.android.internal.R.id.hard_keyboard_switch);
2060 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002063
Yohei Yukawab097b822015-12-01 10:43:08 -08002064 private void notifyInputMethodSubtypeChanged(final int userId,
2065 @Nullable final InputMethodInfo inputMethodInfo,
2066 @Nullable final InputMethodSubtype subtype) {
2067 final InputManagerInternal inputManagerInternal =
2068 LocalServices.getService(InputManagerInternal.class);
2069 if (inputManagerInternal != null) {
2070 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2071 }
2072 }
2073
satokab751aa2010-09-14 19:17:36 +09002074 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 InputMethodInfo info = mMethodMap.get(id);
2076 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002077 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002079
satokd81e9502012-05-21 12:58:45 +09002080 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002082 final int subtypeCount = info.getSubtypeCount();
2083 if (subtypeCount <= 0) {
2084 return;
satokcd7cd292010-11-20 15:46:23 +09002085 }
satokd81e9502012-05-21 12:58:45 +09002086 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2087 final InputMethodSubtype newSubtype;
2088 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2089 newSubtype = info.getSubtypeAt(subtypeId);
2090 } else {
2091 // If subtype is null, try to find the most applicable one from
2092 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002093 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002094 }
2095 if (newSubtype == null || oldSubtype == null) {
2096 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2097 + ", new subtype = " + newSubtype);
2098 return;
2099 }
2100 if (newSubtype != oldSubtype) {
2101 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2102 if (mCurMethod != null) {
2103 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002104 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002105 mCurMethod.changeInputMethodSubtype(newSubtype);
2106 } catch (RemoteException e) {
2107 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002108 return;
satokab751aa2010-09-14 19:17:36 +09002109 }
2110 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002111 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 return;
2114 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002115
satokd81e9502012-05-21 12:58:45 +09002116 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 final long ident = Binder.clearCallingIdentity();
2118 try {
satokab751aa2010-09-14 19:17:36 +09002119 // Set a subtype to this input method.
2120 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002121 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2122 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2123 // because mCurMethodId is stored as a history in
2124 // setSelectedInputMethodAndSubtypeLocked().
2125 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002127 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002129 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002131 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002133 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 } finally {
2135 Binder.restoreCallingIdentity(ident);
2136 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002137
2138 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2139 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002141
satok42c5a162011-05-26 16:46:14 +09002142 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002143 public boolean showSoftInput(IInputMethodClient client, int flags,
2144 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002145 if (!calledFromValidUser()) {
2146 return false;
2147 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002148 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 long ident = Binder.clearCallingIdentity();
2150 try {
2151 synchronized (mMethodMap) {
2152 if (mCurClient == null || client == null
2153 || mCurClient.client.asBinder() != client.asBinder()) {
2154 try {
2155 // We need to check if this is the current client with
2156 // focus in the window manager, to allow this call to
2157 // be made before input is started in it.
2158 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002159 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002160 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002163 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 }
2165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002166
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002167 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002168 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170 } finally {
2171 Binder.restoreCallingIdentity(ident);
2172 }
2173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002174
The Android Open Source Project4df24232009-03-05 14:34:35 -08002175 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002177 if (mAccessibilityRequestingNoSoftKeyboard) {
2178 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
Anna Galusza9b278112016-01-04 11:37:37 -08002180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2182 mShowExplicitlyRequested = true;
2183 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002184 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2185 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002187
Dianne Hackborncc278702009-09-02 23:07:23 -07002188 if (!mSystemReady) {
2189 return false;
2190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002191
The Android Open Source Project4df24232009-03-05 14:34:35 -08002192 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002194 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002195 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2196 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2197 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002199 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002200 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002201 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002202 | Context.BIND_TREAT_LIKE_ACTIVITY
2203 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002204 mVisibleBound = true;
2205 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002206 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002208 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 // The client has asked to have the input method shown, but
2210 // we have been sitting here too long with a connection to the
2211 // service and no interface received, so let's disconnect/connect
2212 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002213 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002215 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002217 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002218 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002219 } else {
2220 if (DEBUG) {
2221 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2222 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002225
The Android Open Source Project4df24232009-03-05 14:34:35 -08002226 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002228
satok42c5a162011-05-26 16:46:14 +09002229 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002230 public boolean hideSoftInput(IInputMethodClient client, int flags,
2231 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002232 if (!calledFromValidUser()) {
2233 return false;
2234 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002235 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 long ident = Binder.clearCallingIdentity();
2237 try {
2238 synchronized (mMethodMap) {
2239 if (mCurClient == null || client == null
2240 || mCurClient.client.asBinder() != client.asBinder()) {
2241 try {
2242 // We need to check if this is the current client with
2243 // focus in the window manager, to allow this call to
2244 // be made before input is started in it.
2245 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002246 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2247 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002248 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002251 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 }
2253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002254
Joe Onorato8a9b2202010-02-26 18:56:32 -08002255 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002256 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
2258 } finally {
2259 Binder.restoreCallingIdentity(ident);
2260 }
2261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002262
The Android Open Source Project4df24232009-03-05 14:34:35 -08002263 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2265 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002266 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 -08002267 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
2269 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002270 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 -08002271 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002273
2274 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2275 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2276 // to be updated with the new value sent from IME process. Even in such a transient state
2277 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2278 // application process as a valid request, and have even promised such a behavior with CTS
2279 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2280 // IMMS#InputShown indicates that the software keyboard is shown.
2281 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2282 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2283 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002284 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002285 if (shouldHideSoftInput) {
2286 // The IME will report its visible state again after the following message finally
2287 // delivered to the IME process as an IPC. Hence the inconsistency between
2288 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2289 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002290 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2291 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2292 res = true;
2293 } else {
2294 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002296 if (mHaveConnection && mVisibleBound) {
2297 mContext.unbindService(mVisibleConnection);
2298 mVisibleBound = false;
2299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 mInputShown = false;
2301 mShowRequested = false;
2302 mShowExplicitlyRequested = false;
2303 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002304 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002306
satok42c5a162011-05-26 16:46:14 +09002307 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002308 public InputBindResult startInputOrWindowGainedFocus(
2309 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2310 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002311 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002312 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002313 if (windowToken != null) {
2314 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002315 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002316 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002317 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2318 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002319 }
2320 }
2321
2322 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002323 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002324 IInputMethodClient client, IBinder windowToken, int controlFlags,
2325 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002326 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2327 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002328 // Needs to check the validity before clearing calling identity
2329 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002330 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 long ident = Binder.clearCallingIdentity();
2332 try {
2333 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002334 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2335 + InputMethodClient.getStartInputReason(startInputReason)
2336 + " client=" + client.asBinder()
2337 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002338 + " missingMethods="
2339 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002340 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002341 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002342 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002343 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002344
Dianne Hackborn7663d802012-02-24 13:08:49 -08002345 ClientState cs = mClients.get(client.asBinder());
2346 if (cs == null) {
2347 throw new IllegalArgumentException("unknown client "
2348 + client.asBinder());
2349 }
2350
2351 try {
2352 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2353 // Check with the window manager to make sure this client actually
2354 // has a window with focus. If not, reject. This is thread safe
2355 // because if the focus changes some time before or after, the
2356 // next client receiving focus that has any interest in input will
2357 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002358 if (DEBUG) {
2359 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2360 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2361 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002362 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002364 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002366
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002367 if (!calledFromValidUser) {
2368 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2369 Slog.w(TAG, "If you want to interect with IME, you need "
2370 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2371 hideCurrentInputLocked(0, null);
2372 return null;
2373 }
2374
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002375 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002376 if (DEBUG) {
2377 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2378 + " attribute=" + attribute + ", token = " + windowToken);
2379 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002380 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002381 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002382 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002383 }
2384 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002385 }
2386 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002387 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002388 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002389
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002390 // Should we auto-show the IME even if the caller has not
2391 // specified what should be done with it?
2392 // We only do this automatically if the window can resize
2393 // to accommodate the IME (so what the user sees will give
2394 // them good context without input information being obscured
2395 // by the IME) or if running on a large screen where there
2396 // is more room for the target window + IME.
2397 final boolean doAutoShow =
2398 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2399 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2400 || mRes.getConfiguration().isLayoutSizeAtLeast(
2401 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002402 final boolean isTextEditor =
2403 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2404
2405 // We want to start input before showing the IME, but after closing
2406 // it. We want to do this after closing it to help the IME disappear
2407 // more quickly (not get stuck behind it initializing itself for the
2408 // new focused input, even if its window wants to hide the IME).
2409 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2412 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002413 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2415 // There is no focus view, and this window will
2416 // be behind any soft input window, so hide the
2417 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002418 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002419 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002421 } else if (isTextEditor && doAutoShow && (softInputMode &
2422 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 // There is a focus view, and we are navigating forward
2424 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002425 // We only do this automatically if the window can resize
2426 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002427 // them good context without input information being obscured
2428 // by the IME) or if running on a large screen where there
2429 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002430 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002431 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002432 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002433 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002434 didStart = true;
2435 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002436 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438 break;
2439 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2440 // Do nothing.
2441 break;
2442 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2443 if ((softInputMode &
2444 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002445 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002446 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448 break;
2449 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002450 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002451 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 break;
2453 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2454 if ((softInputMode &
2455 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002456 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002457 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002458 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002459 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002460 didStart = true;
2461 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002462 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 }
2464 break;
2465 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002466 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002467 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002468 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002469 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002470 didStart = true;
2471 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002472 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 break;
2474 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002475
2476 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002477 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002478 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 }
2481 } finally {
2482 Binder.restoreCallingIdentity(ident);
2483 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002484
2485 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002487
satok42c5a162011-05-26 16:46:14 +09002488 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002489 public void showInputMethodPickerFromClient(
2490 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002491 if (!calledFromValidUser()) {
2492 return;
2493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 synchronized (mMethodMap) {
2495 if (mCurClient == null || client == null
2496 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002497 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002498 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 }
2500
satok440aab52010-11-25 09:43:11 +09002501 // Always call subtype picker, because subtype picker is a superset of input method
2502 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002503 mHandler.sendMessage(mCaller.obtainMessageI(
2504 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002505 }
2506 }
2507
satok42c5a162011-05-26 16:46:14 +09002508 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002510 if (!calledFromValidUser()) {
2511 return;
2512 }
satok28203512010-11-24 11:06:49 +09002513 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2514 }
2515
satok42c5a162011-05-26 16:46:14 +09002516 @Override
satok28203512010-11-24 11:06:49 +09002517 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002518 if (!calledFromValidUser()) {
2519 return;
2520 }
satok28203512010-11-24 11:06:49 +09002521 synchronized (mMethodMap) {
2522 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002523 setInputMethodWithSubtypeIdLocked(token, id,
2524 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2525 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002526 } else {
2527 setInputMethod(token, id);
2528 }
2529 }
satokab751aa2010-09-14 19:17:36 +09002530 }
2531
satok42c5a162011-05-26 16:46:14 +09002532 @Override
satokb416a712010-11-25 20:42:14 +09002533 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002534 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002535 if (!calledFromValidUser()) {
2536 return;
2537 }
satokb416a712010-11-25 20:42:14 +09002538 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002539 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2540 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002541 }
2542 }
2543
satok4fc87d62011-05-20 16:13:43 +09002544 @Override
satok735cf382010-11-11 20:40:09 +09002545 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002546 if (!calledFromValidUser()) {
2547 return false;
2548 }
satok735cf382010-11-11 20:40:09 +09002549 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002550 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002551 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002552 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002553 lastImi = mMethodMap.get(lastIme.first);
2554 } else {
2555 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002556 }
satok4fc87d62011-05-20 16:13:43 +09002557 String targetLastImiId = null;
2558 int subtypeId = NOT_A_SUBTYPE_ID;
2559 if (lastIme != null && lastImi != null) {
2560 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002561 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002562 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2563 : mCurrentSubtype.hashCode();
2564 // If the last IME is the same as the current IME and the last subtype is not
2565 // defined, there is no need to switch to the last IME.
2566 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2567 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002568 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002569 }
2570 }
2571
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002572 if (TextUtils.isEmpty(targetLastImiId)
2573 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002574 // This is a safety net. If the currentSubtype can't be added to the history
2575 // and the framework couldn't find the last ime, we will make the last ime be
2576 // the most applicable enabled keyboard subtype of the system imes.
2577 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2578 if (enabled != null) {
2579 final int N = enabled.size();
2580 final String locale = mCurrentSubtype == null
2581 ? mRes.getConfiguration().locale.toString()
2582 : mCurrentSubtype.getLocale();
2583 for (int i = 0; i < N; ++i) {
2584 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002585 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002586 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002587 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2588 InputMethodUtils.getSubtypes(imi),
2589 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002590 if (keyboardSubtype != null) {
2591 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002592 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002593 imi, keyboardSubtype.hashCode());
2594 if(keyboardSubtype.getLocale().equals(locale)) {
2595 break;
2596 }
2597 }
2598 }
2599 }
2600 }
2601 }
2602
2603 if (!TextUtils.isEmpty(targetLastImiId)) {
2604 if (DEBUG) {
2605 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2606 + ", from: " + mCurMethodId + ", " + subtypeId);
2607 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002608 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002609 return true;
2610 } else {
2611 return false;
2612 }
satok735cf382010-11-11 20:40:09 +09002613 }
2614 }
2615
satoke7c6998e2011-06-03 17:57:59 +09002616 @Override
satok688bd472012-02-09 20:09:17 +09002617 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002618 if (!calledFromValidUser()) {
2619 return false;
2620 }
satok688bd472012-02-09 20:09:17 +09002621 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002622 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002623 return false;
2624 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002625 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002626 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2627 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002628 if (nextSubtype == null) {
2629 return false;
2630 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002631 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2632 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002633 return true;
2634 }
2635 }
2636
2637 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002638 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2639 if (!calledFromValidUser()) {
2640 return false;
2641 }
2642 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002643 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002644 return false;
2645 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002646 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002647 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2648 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002649 if (nextSubtype == null) {
2650 return false;
2651 }
2652 return true;
2653 }
2654 }
2655
2656 @Override
satok68f1b782011-04-11 14:26:04 +09002657 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002658 if (!calledFromValidUser()) {
2659 return null;
2660 }
satok68f1b782011-04-11 14:26:04 +09002661 synchronized (mMethodMap) {
2662 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2663 // TODO: Handle the case of the last IME with no subtypes
2664 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2665 || TextUtils.isEmpty(lastIme.second)) return null;
2666 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2667 if (lastImi == null) return null;
2668 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002669 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002670 final int lastSubtypeId =
2671 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002672 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2673 return null;
2674 }
2675 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002676 } catch (NumberFormatException e) {
2677 return null;
2678 }
2679 }
2680 }
2681
satoke7c6998e2011-06-03 17:57:59 +09002682 @Override
satokee5e77c2011-09-02 18:50:15 +09002683 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002684 if (!calledFromValidUser()) {
2685 return;
2686 }
satok91e88122011-07-18 11:11:42 +09002687 // By this IPC call, only a process which shares the same uid with the IME can add
2688 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002689 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002690 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002691 if (!mSystemReady) {
2692 return;
2693 }
satok91e88122011-07-18 11:11:42 +09002694 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002695 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002696 final String[] packageInfos;
2697 try {
2698 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2699 } catch (RemoteException e) {
2700 Slog.e(TAG, "Failed to get package infos");
2701 return;
2702 }
satok91e88122011-07-18 11:11:42 +09002703 if (packageInfos != null) {
2704 final int packageNum = packageInfos.length;
2705 for (int i = 0; i < packageNum; ++i) {
2706 if (packageInfos[i].equals(imi.getPackageName())) {
2707 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002708 final long ident = Binder.clearCallingIdentity();
2709 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002710 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002711 } finally {
2712 Binder.restoreCallingIdentity(ident);
2713 }
satokee5e77c2011-09-02 18:50:15 +09002714 return;
satok91e88122011-07-18 11:11:42 +09002715 }
2716 }
2717 }
satoke7c6998e2011-06-03 17:57:59 +09002718 }
satokee5e77c2011-09-02 18:50:15 +09002719 return;
satoke7c6998e2011-06-03 17:57:59 +09002720 }
2721
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002722 @Override
2723 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002724 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002725 }
2726
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002727 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002728 public void clearLastInputMethodWindowForTransition(IBinder token) {
2729 if (!calledFromValidUser()) {
2730 return;
2731 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002732 synchronized (mMethodMap) {
2733 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002734 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002735 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002736 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002737 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002738 }
2739
2740 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002741 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002742 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002743 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002744 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002745 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002746 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2747 if (DEBUG) {
2748 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2749 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2750 + " actual: " + sequenceNumber);
2751 }
2752 return;
2753 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002754 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2755 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002756 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002757 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002758 }
2759 }
2760
satok28203512010-11-24 11:06:49 +09002761 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002763 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2764 }
2765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002767 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2768 if (token == null) {
2769 if (mContext.checkCallingOrSelfPermission(
2770 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2771 != PackageManager.PERMISSION_GRANTED) {
2772 throw new SecurityException(
2773 "Using null token requires permission "
2774 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002776 } else if (mCurToken != token) {
2777 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2778 + " token: " + token);
2779 return;
2780 }
2781
2782 final long ident = Binder.clearCallingIdentity();
2783 try {
2784 setInputMethodLocked(id, subtypeId);
2785 } finally {
2786 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 }
2788 }
2789
satok42c5a162011-05-26 16:46:14 +09002790 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002792 if (!calledFromValidUser()) {
2793 return;
2794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002796 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 return;
2798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 long ident = Binder.clearCallingIdentity();
2800 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002801 hideCurrentInputLocked(flags, null);
2802 } finally {
2803 Binder.restoreCallingIdentity(ident);
2804 }
2805 }
2806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002807
satok42c5a162011-05-26 16:46:14 +09002808 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002809 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002810 if (!calledFromValidUser()) {
2811 return;
2812 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002813 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002814 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002815 return;
2816 }
2817 long ident = Binder.clearCallingIdentity();
2818 try {
2819 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 } finally {
2821 Binder.restoreCallingIdentity(ident);
2822 }
2823 }
2824 }
2825
2826 void setEnabledSessionInMainThread(SessionState session) {
2827 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002828 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002830 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002831 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 } catch (RemoteException e) {
2833 }
2834 }
2835 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002836 if (mEnabledSession != null && mEnabledSession.session != null) {
2837 try {
2838 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2839 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2840 } catch (RemoteException e) {
2841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
2843 }
2844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002845
satok42c5a162011-05-26 16:46:14 +09002846 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002848 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002850 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002851 final boolean showAuxSubtypes;
2852 switch (msg.arg1) {
2853 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2854 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2855 // implemented so that auxiliary subtypes will be excluded when the soft
2856 // keyboard is invisible.
2857 showAuxSubtypes = mInputShown;
2858 break;
2859 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2860 showAuxSubtypes = true;
2861 break;
2862 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2863 showAuxSubtypes = false;
2864 break;
2865 default:
2866 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2867 return false;
2868 }
2869 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002870 return true;
2871
satok47a44912010-10-06 16:03:58 +09002872 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002873 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002874 return true;
2875
2876 case MSG_SHOW_IM_CONFIG:
2877 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002878 return true;
2879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 case MSG_UNBIND_INPUT:
2883 try {
2884 ((IInputMethod)msg.obj).unbindInput();
2885 } catch (RemoteException e) {
2886 // There is nothing interesting about the method dying.
2887 }
2888 return true;
2889 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002890 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 try {
2892 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2893 } catch (RemoteException e) {
2894 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002895 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 return true;
2897 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002898 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002900 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002901 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002902 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 } catch (RemoteException e) {
2904 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002905 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 return true;
2907 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002908 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002910 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002911 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002912 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 } catch (RemoteException e) {
2914 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002915 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002917 case MSG_HIDE_CURRENT_INPUT_METHOD:
2918 synchronized (mMethodMap) {
2919 hideCurrentInputLocked(0, null);
2920 }
2921 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002923 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002925 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2927 } catch (RemoteException e) {
2928 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002929 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002931 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002932 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002933 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002934 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002936 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002938 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002939 // Dispose the channel if the input method is not local to this process
2940 // because the remote proxy will get its own copy when unparceled.
2941 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002942 channel.dispose();
2943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002945 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002949
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002950 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08002951 final int missingMethods = msg.arg1;
2952 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002953 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002954 final IBinder startInputToken = (IBinder) args.arg1;
2955 final SessionState session = (SessionState) args.arg2;
2956 final IInputContext inputContext = (IInputContext) args.arg3;
2957 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002960 session.method.startInput(startInputToken, inputContext, missingMethods,
2961 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 } catch (RemoteException e) {
2963 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002964 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002966 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002969
Yohei Yukawa33e81792015-11-17 21:14:42 -08002970 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002972 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 } catch (RemoteException e) {
2974 // There is nothing interesting about the last client dying.
2975 }
2976 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002977 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002978 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002979 IInputMethodClient client = (IInputMethodClient)args.arg1;
2980 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002982 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002984 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002985 } finally {
2986 // Dispose the channel if the input method is not local to this process
2987 // because the remote proxy will get its own copy when unparceled.
2988 if (res.channel != null && Binder.isProxy(client)) {
2989 res.channel.dispose();
2990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002992 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002994 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002995 case MSG_SET_ACTIVE:
2996 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002997 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002998 } catch (RemoteException e) {
2999 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3000 + ((ClientState)msg.obj).pid + " uid "
3001 + ((ClientState)msg.obj).uid);
3002 }
3003 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003004 case MSG_SET_INTERACTIVE:
3005 handleSetInteractive(msg.arg1 != 0);
3006 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003007 case MSG_SWITCH_IME:
3008 handleSwitchInputMethod(msg.arg1 != 0);
3009 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003010 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3011 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003012 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003013 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003014 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003015 } catch (RemoteException e) {
3016 Slog.w(TAG, "Got RemoteException sending "
3017 + "setUserActionNotificationSequenceNumber("
3018 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003019 + clientState.pid + " uid "
3020 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003021 }
3022 return true;
3023 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003024 case MSG_REPORT_FULLSCREEN_MODE: {
3025 final boolean fullscreen = msg.arg1 != 0;
3026 final ClientState clientState = (ClientState)msg.obj;
3027 try {
3028 clientState.client.reportFullscreenMode(fullscreen);
3029 } catch (RemoteException e) {
3030 Slog.w(TAG, "Got RemoteException sending "
3031 + "reportFullscreen(" + fullscreen + ") notification to pid="
3032 + clientState.pid + " uid=" + clientState.uid);
3033 }
3034 return true;
3035 }
satok01038492012-04-09 21:08:27 +09003036
3037 // --------------------------------------------------------------
3038 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003039 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003040 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003041 case MSG_SYSTEM_UNLOCK_USER:
3042 final int userId = msg.arg1;
3043 onUnlockUser(userId);
3044 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 }
3046 return false;
3047 }
3048
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003049 private void handleSetInteractive(final boolean interactive) {
3050 synchronized (mMethodMap) {
3051 mIsInteractive = interactive;
3052 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3053
3054 // Inform the current client of the change in active status
3055 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003056 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3057 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3058 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003059 }
3060 }
3061 }
3062
Yohei Yukawaae61f712015-12-09 13:00:10 -08003063 private void handleSwitchInputMethod(final boolean forwardDirection) {
3064 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003065 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003066 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003067 if (nextSubtype == null) {
3068 return;
3069 }
3070 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003071 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3072 if (newInputMethodInfo == null) {
3073 return;
3074 }
3075 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3076 newInputMethodInfo, mCurrentSubtype);
3077 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003078 if (mSubtypeSwitchedByShortCutToast == null) {
3079 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3080 Toast.LENGTH_SHORT);
3081 } else {
3082 mSubtypeSwitchedByShortCutToast.setText(toastText);
3083 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003084 mSubtypeSwitchedByShortCutToast.show();
3085 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003086 }
3087 }
3088
satokdc9ddae2011-10-06 12:22:36 +09003089 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003090 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3091 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003092 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003093 if (DEBUG) {
3094 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3095 }
satok723a27e2010-11-11 14:58:11 +09003096 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003097 return true;
3098 }
3099
3100 return false;
3101 }
3102
Yohei Yukawa94e33302016-02-12 19:37:03 -08003103 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003104 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003105 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003106 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003107 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003108 if (!mSystemReady) {
3109 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3110 return;
3111 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003112 mMethodList.clear();
3113 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003114 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003115 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003116
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003117 // Use for queryIntentServicesAsUser
3118 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003120 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3121 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3122 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003123 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003125 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3126 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003127
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003128 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003129 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 for (int i = 0; i < services.size(); ++i) {
3131 ResolveInfo ri = services.get(i);
3132 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003133 final String imeId = InputMethodInfo.computeId(ri);
3134 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3135 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 + ": it does not require the permission "
3137 + android.Manifest.permission.BIND_INPUT_METHOD);
3138 continue;
3139 }
3140
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003141 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003143 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 try {
satoke7c6998e2011-06-03 17:57:59 +09003145 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003146 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003147 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003148 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149
3150 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003151 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003153 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003154 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 }
3156 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003157
Yohei Yukawac4e44912017-02-09 19:30:22 -08003158 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3159 // negatives when the package state remains to be the same but only the component state is
3160 // changed.
3161 {
3162 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3163 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3164 // conservative, but it seems we cannot use it for now (Issue 35176630).
3165 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3166 new Intent(InputMethod.SERVICE_INTERFACE),
3167 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3168 final int N = allInputMethodServices.size();
3169 for (int i = 0; i < N; ++i) {
3170 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3171 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3172 continue;
3173 }
3174 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3175 }
3176 }
3177
Yohei Yukawa859df052016-02-17 07:56:46 -08003178 // TODO: The following code should find better place to live.
3179 if (!resetDefaultEnabledIme) {
3180 boolean enabledImeFound = false;
3181 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3182 final int N = enabledImes.size();
3183 for (int i = 0; i < N; ++i) {
3184 final InputMethodInfo imi = enabledImes.get(i);
3185 if (mMethodList.contains(imi)) {
3186 enabledImeFound = true;
3187 break;
3188 }
3189 }
3190 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003191 if (DEBUG) {
3192 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3193 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003194 resetDefaultEnabledIme = true;
3195 resetSelectedInputMethodAndSubtypeLocked("");
3196 }
3197 }
3198
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003199 if (resetDefaultEnabledIme) {
3200 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003201 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003202 final int N = defaultEnabledIme.size();
3203 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003204 final InputMethodInfo imi = defaultEnabledIme.get(i);
3205 if (DEBUG) {
3206 Slog.d(TAG, "--- enable ime = " + imi);
3207 }
3208 setInputMethodEnabledLocked(imi.getId(), true);
3209 }
3210 }
3211
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003212 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003213 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003214 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003215 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3216 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003217 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003218 }
3219 } else {
3220 // Double check that the default IME is certainly enabled.
3221 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003222 }
3223 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003224 // Here is not the perfect place to reset the switching controller. Ideally
3225 // mSwitchingController and mSettings should be able to share the same state.
3226 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3227 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003228 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003232
satok217f5482010-12-15 05:19:19 +09003233 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003234 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003235 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003236 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3237 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003238 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003239 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003240 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003241 final int userId;
3242 synchronized (mMethodMap) {
3243 userId = mSettings.getCurrentUserId();
3244 }
3245 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003246 }
3247
3248 private void showConfigureInputMethods() {
3249 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3250 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3251 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3252 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003253 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003254 }
3255
satok2c93efc2012-04-02 19:33:47 +09003256 private boolean isScreenLocked() {
3257 return mKeyguardManager != null
3258 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3259 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003260
Seigo Nonaka14e13912015-05-06 21:04:13 -07003261 private void showInputMethodMenu(boolean showAuxSubtypes) {
3262 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003265 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003266
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003267 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003268 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003269 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003270
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003271 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003272 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003273 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3274 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003275 if (immis == null || immis.size() == 0) {
3276 return;
3277 }
3278
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003279 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280
satok688bd472012-02-09 20:09:17 +09003281 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003282 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003283 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003284
satokc3690562012-01-10 20:14:43 +09003285 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003286 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003287 if (currentSubtype != null) {
3288 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003289 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3290 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003291 }
3292 }
3293
Ken Wakasa761eb372011-03-04 19:06:18 +09003294 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003295 mIms = new InputMethodInfo[N];
3296 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003297 int checkedItem = 0;
3298 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003299 final ImeSubtypeListItem item = imList.get(i);
3300 mIms[i] = item.mImi;
3301 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003302 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003303 int subtypeId = mSubtypeIds[i];
3304 if ((subtypeId == NOT_A_SUBTYPE_ID)
3305 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3306 || (subtypeId == lastInputMethodSubtypeId)) {
3307 checkedItem = i;
3308 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003311
3312 final Context settingsContext = new ContextThemeWrapper(context,
3313 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3314
3315 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003316 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3317 @Override
3318 public void onCancel(DialogInterface dialog) {
3319 hideInputMethodMenu();
3320 }
3321 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003322
3323 final Context dialogContext = mDialogBuilder.getContext();
3324 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3325 com.android.internal.R.styleable.DialogPreference,
3326 com.android.internal.R.attr.alertDialogStyle, 0);
3327 final Drawable dialogIcon = a.getDrawable(
3328 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3329 a.recycle();
3330
3331 mDialogBuilder.setIcon(dialogIcon);
3332
Yohei Yukawad34e1482016-02-11 08:03:52 -08003333 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003334 final View tv = inflater.inflate(
3335 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3336 mDialogBuilder.setCustomTitle(tv);
3337
3338 // Setup layout for a toggle switch of the hardware keyboard
3339 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003340 mSwitchingDialogTitleView
3341 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003342 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003343 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003344 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003345 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003346 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003347 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3348 @Override
3349 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003350 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003351 // Ensure that the input method dialog is dismissed when changing
3352 // the hardware keyboard state.
3353 hideInputMethodMenu();
3354 }
3355 });
3356
Alan Viverette505e3ab2014-11-24 15:22:11 -08003357 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003358 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3359 final OnClickListener choiceListener = new OnClickListener() {
3360 @Override
3361 public void onClick(final DialogInterface dialog, final int which) {
3362 synchronized (mMethodMap) {
3363 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3364 || mSubtypeIds.length <= which) {
3365 return;
satok01038492012-04-09 21:08:27 +09003366 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003367 final InputMethodInfo im = mIms[which];
3368 int subtypeId = mSubtypeIds[which];
3369 adapter.mCheckedItem = which;
3370 adapter.notifyDataSetChanged();
3371 hideInputMethodMenu();
3372 if (im != null) {
3373 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3374 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003375 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003376 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003378 }
3379 }
3380 };
3381 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003384 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003385 final Window w = mSwitchingDialog.getWindow();
3386 final WindowManager.LayoutParams attrs = w.getAttributes();
3387 w.setType(TYPE_INPUT_METHOD_DIALOG);
3388 // Use an alternate token for the dialog for that window manager can group the token
3389 // with other IME windows based on type vs. grouping based on whichever token happens
3390 // to get selected by the system later on.
3391 attrs.token = mSwitchingDialogToken;
3392 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3393 attrs.setTitle("Select input method");
3394 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003395 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 mSwitchingDialog.show();
3397 }
3398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003399
Ken Wakasa05dbb652011-08-22 15:22:43 +09003400 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3401 private final LayoutInflater mInflater;
3402 private final int mTextViewResourceId;
3403 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003404 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003405 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3406 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3407 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003408
Ken Wakasa05dbb652011-08-22 15:22:43 +09003409 mTextViewResourceId = textViewResourceId;
3410 mItemsList = itemsList;
3411 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003412 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003413 }
3414
3415 @Override
3416 public View getView(int position, View convertView, ViewGroup parent) {
3417 final View view = convertView != null ? convertView
3418 : mInflater.inflate(mTextViewResourceId, null);
3419 if (position < 0 || position >= mItemsList.size()) return view;
3420 final ImeSubtypeListItem item = mItemsList.get(position);
3421 final CharSequence imeName = item.mImeName;
3422 final CharSequence subtypeName = item.mSubtypeName;
3423 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3424 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3425 if (TextUtils.isEmpty(subtypeName)) {
3426 firstTextView.setText(imeName);
3427 secondTextView.setVisibility(View.GONE);
3428 } else {
3429 firstTextView.setText(subtypeName);
3430 secondTextView.setText(imeName);
3431 secondTextView.setVisibility(View.VISIBLE);
3432 }
3433 final RadioButton radioButton =
3434 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3435 radioButton.setChecked(position == mCheckedItem);
3436 return view;
3437 }
3438 }
3439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003441 synchronized (mMethodMap) {
3442 hideInputMethodMenuLocked();
3443 }
3444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003445
The Android Open Source Project10592532009-03-18 17:39:46 -07003446 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003447 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448
The Android Open Source Project10592532009-03-18 17:39:46 -07003449 if (mSwitchingDialog != null) {
3450 mSwitchingDialog.dismiss();
3451 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003453
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003454 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003455 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003456 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003460
satok42c5a162011-05-26 16:46:14 +09003461 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003463 // TODO: Make this work even for non-current users?
3464 if (!calledFromValidUser()) {
3465 return false;
3466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 synchronized (mMethodMap) {
3468 if (mContext.checkCallingOrSelfPermission(
3469 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3470 != PackageManager.PERMISSION_GRANTED) {
3471 throw new SecurityException(
3472 "Requires permission "
3473 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3474 }
Anna Galusza9b278112016-01-04 11:37:37 -08003475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 long ident = Binder.clearCallingIdentity();
3477 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003478 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 } finally {
3480 Binder.restoreCallingIdentity(ident);
3481 }
3482 }
3483 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003484
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003485 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3486 // Make sure this is a valid input method.
3487 InputMethodInfo imm = mMethodMap.get(id);
3488 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003489 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003490 }
3491
satokd87c2592010-09-29 11:52:06 +09003492 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3493 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003494
satokd87c2592010-09-29 11:52:06 +09003495 if (enabled) {
3496 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3497 if (pair.first.equals(id)) {
3498 // We are enabling this input method, but it is already enabled.
3499 // Nothing to do. The previous state was enabled.
3500 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003501 }
3502 }
satokd87c2592010-09-29 11:52:06 +09003503 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3504 // Previous state was disabled.
3505 return false;
3506 } else {
3507 StringBuilder builder = new StringBuilder();
3508 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3509 builder, enabledInputMethodsList, id)) {
3510 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003511 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003512 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3513 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3514 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003515 }
3516 // Previous state was enabled.
3517 return true;
3518 } else {
3519 // We are disabling the input method but it is already disabled.
3520 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003521 return false;
3522 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003523 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003524 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003525
satok723a27e2010-11-11 14:58:11 +09003526 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3527 boolean setSubtypeOnly) {
3528 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003529 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003530
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003531 mCurUserActionNotificationSequenceNumber =
3532 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3533 if (DEBUG) {
3534 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3535 + mCurUserActionNotificationSequenceNumber);
3536 }
3537
3538 if (mCurClient != null && mCurClient.client != null) {
3539 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3540 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003541 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003542 }
3543
satok723a27e2010-11-11 14:58:11 +09003544 // Set Subtype here
3545 if (imi == null || subtypeId < 0) {
3546 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003547 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003548 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003549 if (subtypeId < imi.getSubtypeCount()) {
3550 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3551 mSettings.putSelectedSubtype(subtype.hashCode());
3552 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003553 } else {
3554 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003555 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003556 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003557 }
satokab751aa2010-09-14 19:17:36 +09003558 }
satok723a27e2010-11-11 14:58:11 +09003559
Yohei Yukawa68645a62016-02-17 07:54:20 -08003560 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003561 // Set InputMethod here
3562 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3563 }
3564 }
3565
3566 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3567 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3568 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3569 // newDefaultIme is empty when there is no candidate for the selected IME.
3570 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3571 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3572 if (subtypeHashCode != null) {
3573 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003574 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003575 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003576 } catch (NumberFormatException e) {
3577 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3578 }
3579 }
3580 }
3581 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003582 }
3583
satok4e4569d2010-11-19 18:45:53 +09003584 // If there are no selected shortcuts, tries finding the most applicable ones.
3585 private Pair<InputMethodInfo, InputMethodSubtype>
3586 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3587 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3588 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003589 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003590 boolean foundInSystemIME = false;
3591
3592 // Search applicable subtype for each InputMethodInfo
3593 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003594 final String imiId = imi.getId();
3595 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3596 continue;
3597 }
satokcd7cd292010-11-20 15:46:23 +09003598 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003599 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003600 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003601 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003602 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003603 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003604 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003605 }
satokdf31ae62011-01-15 06:19:44 +09003606 // 2. Search by the system locale from enabledSubtypes.
3607 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003608 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003609 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003610 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003611 }
satoka86f5e42011-09-02 17:12:42 +09003612 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003613 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003614 final ArrayList<InputMethodSubtype> subtypesForSearch =
3615 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003616 ? InputMethodUtils.getSubtypes(imi)
3617 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003618 // 4. Search by the current subtype's locale from all subtypes.
3619 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003620 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003621 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003622 }
3623 // 5. Search by the system locale from all subtypes.
3624 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003625 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003626 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003627 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003628 }
satokcd7cd292010-11-20 15:46:23 +09003629 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003630 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003631 // The current input method is the most applicable IME.
3632 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003633 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003634 break;
satok7599a7f2010-12-22 13:45:23 +09003635 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003636 // The system input method is 2nd applicable IME.
3637 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003638 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003639 if ((imi.getServiceInfo().applicationInfo.flags
3640 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3641 foundInSystemIME = true;
3642 }
satok4e4569d2010-11-19 18:45:53 +09003643 }
3644 }
3645 }
3646 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003647 if (mostApplicableIMI != null) {
3648 Slog.w(TAG, "Most applicable shortcut input method was:"
3649 + mostApplicableIMI.getId());
3650 if (mostApplicableSubtype != null) {
3651 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3652 + "," + mostApplicableSubtype.getMode() + ","
3653 + mostApplicableSubtype.getLocale());
3654 }
3655 }
satok4e4569d2010-11-19 18:45:53 +09003656 }
satokcd7cd292010-11-20 15:46:23 +09003657 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003658 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003659 } else {
3660 return null;
3661 }
3662 }
3663
satokab751aa2010-09-14 19:17:36 +09003664 /**
3665 * @return Return the current subtype of this input method.
3666 */
satok42c5a162011-05-26 16:46:14 +09003667 @Override
satokab751aa2010-09-14 19:17:36 +09003668 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003669 // TODO: Make this work even for non-current users?
3670 if (!calledFromValidUser()) {
3671 return null;
3672 }
3673 synchronized (mMethodMap) {
3674 return getCurrentInputMethodSubtypeLocked();
3675 }
3676 }
3677
3678 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003679 if (mCurMethodId == null) {
3680 return null;
3681 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003682 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003683 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3684 if (imi == null || imi.getSubtypeCount() == 0) {
3685 return null;
satok4e4569d2010-11-19 18:45:53 +09003686 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003687 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003688 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3689 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003690 if (subtypeId == NOT_A_SUBTYPE_ID) {
3691 // If there are no selected subtypes, the framework will try to find
3692 // the most applicable subtype from explicitly or implicitly enabled
3693 // subtypes.
3694 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003695 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003696 // If there is only one explicitly or implicitly enabled subtype,
3697 // just returns it.
3698 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3699 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3700 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003701 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003702 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003703 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003704 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003705 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003706 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3707 true);
satok4e4569d2010-11-19 18:45:53 +09003708 }
satok3ef8b292010-11-23 06:06:29 +09003709 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003710 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003711 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003712 }
3713 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003714 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003715 }
3716
satok4e4569d2010-11-19 18:45:53 +09003717 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003718 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003719 @Override
satok4e4569d2010-11-19 18:45:53 +09003720 public List getShortcutInputMethodsAndSubtypes() {
3721 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003722 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003723 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003724 // If there are no selected shortcut subtypes, the framework will try to find
3725 // the most applicable subtype from all subtypes whose mode is
3726 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003727 Pair<InputMethodInfo, InputMethodSubtype> info =
3728 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003729 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003730 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003731 ret.add(info.first);
3732 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003733 }
satok3da92232011-01-11 22:46:30 +09003734 return ret;
satokf3db1af2010-11-23 13:34:33 +09003735 }
satokf3db1af2010-11-23 13:34:33 +09003736 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3737 ret.add(imi);
3738 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3739 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003740 }
3741 }
satokf3db1af2010-11-23 13:34:33 +09003742 return ret;
satok4e4569d2010-11-19 18:45:53 +09003743 }
3744 }
3745
satok42c5a162011-05-26 16:46:14 +09003746 @Override
satokb66d2872010-11-10 01:04:04 +09003747 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003748 // TODO: Make this work even for non-current users?
3749 if (!calledFromValidUser()) {
3750 return false;
3751 }
satokb66d2872010-11-10 01:04:04 +09003752 synchronized (mMethodMap) {
3753 if (subtype != null && mCurMethodId != null) {
3754 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003755 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003756 if (subtypeId != NOT_A_SUBTYPE_ID) {
3757 setInputMethodLocked(mCurMethodId, subtypeId);
3758 return true;
3759 }
3760 }
3761 return false;
3762 }
3763 }
3764
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003765 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003766 private static class InputMethodFileManager {
3767 private static final String SYSTEM_PATH = "system";
3768 private static final String INPUT_METHOD_PATH = "inputmethod";
3769 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3770 private static final String NODE_SUBTYPES = "subtypes";
3771 private static final String NODE_SUBTYPE = "subtype";
3772 private static final String NODE_IMI = "imi";
3773 private static final String ATTR_ID = "id";
3774 private static final String ATTR_LABEL = "label";
3775 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003776 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003777 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003778 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003779 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3780 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3781 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003782 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003783 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3784 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003785 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003786 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003787 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003788 if (methodMap == null) {
3789 throw new NullPointerException("methodMap is null");
3790 }
3791 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003792 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003793 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3794 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003795 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003796 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003797 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3798 }
3799 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3800 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3801 if (!subtypeFile.exists()) {
3802 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003803 writeAdditionalInputMethodSubtypes(
3804 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003805 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003806 readAdditionalInputMethodSubtypes(
3807 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003808 }
3809 }
3810
3811 private void deleteAllInputMethodSubtypes(String imiId) {
3812 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003813 mAdditionalSubtypesMap.remove(imiId);
3814 writeAdditionalInputMethodSubtypes(
3815 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003816 }
3817 }
3818
3819 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003820 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003821 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003822 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003823 final int N = additionalSubtypes.length;
3824 for (int i = 0; i < N; ++i) {
3825 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003826 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003827 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003828 } else {
3829 Slog.w(TAG, "Duplicated subtype definition found: "
3830 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003831 }
3832 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003833 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3834 writeAdditionalInputMethodSubtypes(
3835 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003836 }
3837 }
3838
3839 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3840 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003841 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003842 }
3843 }
3844
3845 private static void writeAdditionalInputMethodSubtypes(
3846 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3847 HashMap<String, InputMethodInfo> methodMap) {
3848 // Safety net for the case that this function is called before methodMap is set.
3849 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3850 FileOutputStream fos = null;
3851 try {
3852 fos = subtypesFile.startWrite();
3853 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003854 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003855 out.startDocument(null, true);
3856 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3857 out.startTag(null, NODE_SUBTYPES);
3858 for (String imiId : allSubtypes.keySet()) {
3859 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3860 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3861 continue;
3862 }
3863 out.startTag(null, NODE_IMI);
3864 out.attribute(null, ATTR_ID, imiId);
3865 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3866 final int N = subtypesList.size();
3867 for (int i = 0; i < N; ++i) {
3868 final InputMethodSubtype subtype = subtypesList.get(i);
3869 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003870 if (subtype.hasSubtypeId()) {
3871 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3872 String.valueOf(subtype.getSubtypeId()));
3873 }
satoke7c6998e2011-06-03 17:57:59 +09003874 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3875 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3876 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003877 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3878 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003879 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3880 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3881 out.attribute(null, ATTR_IS_AUXILIARY,
3882 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003883 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3884 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003885 out.endTag(null, NODE_SUBTYPE);
3886 }
3887 out.endTag(null, NODE_IMI);
3888 }
3889 out.endTag(null, NODE_SUBTYPES);
3890 out.endDocument();
3891 subtypesFile.finishWrite(fos);
3892 } catch (java.io.IOException e) {
3893 Slog.w(TAG, "Error writing subtypes", e);
3894 if (fos != null) {
3895 subtypesFile.failWrite(fos);
3896 }
3897 }
3898 }
3899
3900 private static void readAdditionalInputMethodSubtypes(
3901 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3902 if (allSubtypes == null || subtypesFile == null) return;
3903 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003904 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003905 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003906 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003907 int type = parser.getEventType();
3908 // Skip parsing until START_TAG
3909 while ((type = parser.next()) != XmlPullParser.START_TAG
3910 && type != XmlPullParser.END_DOCUMENT) {}
3911 String firstNodeName = parser.getName();
3912 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3913 throw new XmlPullParserException("Xml doesn't start with subtypes");
3914 }
3915 final int depth =parser.getDepth();
3916 String currentImiId = null;
3917 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3918 while (((type = parser.next()) != XmlPullParser.END_TAG
3919 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3920 if (type != XmlPullParser.START_TAG)
3921 continue;
3922 final String nodeName = parser.getName();
3923 if (NODE_IMI.equals(nodeName)) {
3924 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3925 if (TextUtils.isEmpty(currentImiId)) {
3926 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3927 continue;
3928 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003929 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003930 allSubtypes.put(currentImiId, tempSubtypesArray);
3931 } else if (NODE_SUBTYPE.equals(nodeName)) {
3932 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3933 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3934 continue;
3935 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003936 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003937 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003938 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003939 parser.getAttributeValue(null, ATTR_LABEL));
3940 final String imeSubtypeLocale =
3941 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003942 final String languageTag =
3943 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003944 final String imeSubtypeMode =
3945 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3946 final String imeSubtypeExtraValue =
3947 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003948 final boolean isAuxiliary = "1".equals(String.valueOf(
3949 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003950 final boolean isAsciiCapable = "1".equals(String.valueOf(
3951 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003952 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003953 .setSubtypeNameResId(label)
3954 .setSubtypeIconResId(icon)
3955 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003956 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003957 .setSubtypeMode(imeSubtypeMode)
3958 .setSubtypeExtraValue(imeSubtypeExtraValue)
3959 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003960 .setIsAsciiCapable(isAsciiCapable);
3961 final String subtypeIdString =
3962 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3963 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003964 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003965 }
3966 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003967 }
3968 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003969 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3970 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003971 return;
satoke7c6998e2011-06-03 17:57:59 +09003972 }
3973 }
3974 }
3975
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003976 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3977 @NonNull
3978 private final Handler mHandler;
3979
3980 LocalServiceImpl(@NonNull final Handler handler) {
3981 mHandler = handler;
3982 }
3983
3984 @Override
3985 public void setInteractive(boolean interactive) {
3986 // Do everything in handler so as not to block the caller.
3987 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3988 interactive ? 1 : 0, 0));
3989 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003990
3991 @Override
3992 public void switchInputMethod(boolean forwardDirection) {
3993 // Do everything in handler so as not to block the caller.
3994 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3995 forwardDirection ? 1 : 0, 0));
3996 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003997
3998 @Override
3999 public void hideCurrentInputMethod() {
4000 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4001 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4002 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004003 }
4004
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004005 private static String imeWindowStatusToString(final int imeWindowVis) {
4006 final StringBuilder sb = new StringBuilder();
4007 boolean first = true;
4008 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4009 sb.append("Active");
4010 first = false;
4011 }
4012 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4013 if (!first) {
4014 sb.append("|");
4015 }
4016 sb.append("Visible");
4017 }
4018 return sb.toString();
4019 }
4020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004022 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4023 @Nullable Uri contentUri, @Nullable String packageName) {
4024 if (!calledFromValidUser()) {
4025 return null;
4026 }
4027
4028 if (token == null) {
4029 throw new NullPointerException("token");
4030 }
4031 if (packageName == null) {
4032 throw new NullPointerException("packageName");
4033 }
4034 if (contentUri == null) {
4035 throw new NullPointerException("contentUri");
4036 }
4037 final String contentUriScheme = contentUri.getScheme();
4038 if (!"content".equals(contentUriScheme)) {
4039 throw new InvalidParameterException("contentUri must have content scheme");
4040 }
4041
4042 synchronized (mMethodMap) {
4043 final int uid = Binder.getCallingUid();
4044 if (mCurMethodId == null) {
4045 return null;
4046 }
4047 if (mCurToken != token) {
4048 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4049 + " token=" + token);
4050 return null;
4051 }
4052 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4053 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4054 // nature of our system. Let's compare it with our internal record.
4055 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4056 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4057 + mCurAttribute.packageName + " packageName=" + packageName);
4058 return null;
4059 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004060 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004061 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004062 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004063 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004064 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4065 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4066 imeUserId);
4067 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4068 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4069 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4070 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4071 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4072 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4073 // actually allowed to "uid", which is guaranteed to be the IME's one.
4074 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4075 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004076 }
4077 }
4078
4079 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004080 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4081 if (!calledFromValidUser()) {
4082 return;
4083 }
4084 synchronized (mMethodMap) {
4085 if (!calledWithValidToken(token)) {
4086 return;
4087 }
4088 if (mCurClient != null && mCurClient.client != null) {
4089 mInFullscreenMode = fullscreen;
4090 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4091 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4092 }
4093 }
4094 }
4095
4096 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4098 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4099 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4102 + Binder.getCallingPid()
4103 + ", uid=" + Binder.getCallingUid());
4104 return;
4105 }
4106
4107 IInputMethod method;
4108 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004109 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 synchronized (mMethodMap) {
4114 p.println("Current Input Method Manager state:");
4115 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004116 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 for (int i=0; i<N; i++) {
4118 InputMethodInfo info = mMethodList.get(i);
4119 p.println(" InputMethod #" + i + ":");
4120 info.dump(p, " ");
4121 }
4122 p.println(" Clients:");
4123 for (ClientState ci : mClients.values()) {
4124 p.println(" Client " + ci + ":");
4125 p.println(" client=" + ci.client);
4126 p.println(" inputContext=" + ci.inputContext);
4127 p.println(" sessionRequested=" + ci.sessionRequested);
4128 p.println(" curSession=" + ci.curSession);
4129 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004130 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004132 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004133 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4134 + " softInputMode=" +
4135 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4136 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004137 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4139 + " mBoundToMethod=" + mBoundToMethod);
4140 p.println(" mCurToken=" + mCurToken);
4141 p.println(" mCurIntent=" + mCurIntent);
4142 method = mCurMethod;
4143 p.println(" mCurMethod=" + mCurMethod);
4144 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004145 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 p.println(" mShowRequested=" + mShowRequested
4147 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4148 + " mShowForced=" + mShowForced
4149 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004150 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004151 p.println(" mCurUserActionNotificationSequenceNumber="
4152 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004153 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004154 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004155 p.println(" mSwitchingController:");
4156 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004157 p.println(" mSettings:");
4158 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004160
Jeff Brownb88102f2010-09-08 11:49:43 -07004161 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 pw.flush();
4164 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004165 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4166 } catch (IOException | RemoteException e) {
4167 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004169 } else {
4170 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004172
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004173 if (focusedWindowClient != null && client != focusedWindowClient) {
4174 p.println(" ");
4175 p.println("Warning: Current input method client doesn't match the last focused. "
4176 + "window.");
4177 p.println("Dumping input method client in the last focused window just in case.");
4178 p.println(" ");
4179 pw.flush();
4180 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004181 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4182 } catch (IOException | RemoteException e) {
4183 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004184 }
4185 }
4186
Jeff Brownb88102f2010-09-08 11:49:43 -07004187 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 pw.flush();
4190 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004191 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4192 } catch (IOException | RemoteException e) {
4193 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004195 } else {
4196 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198 }
4199}