blob: b6a0e5cc9957fb81d55afa874ff7f42059c087dc [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 Yukawae39d4ed2015-11-19 03:38:49 -0800352 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
353 */
354 ClientState mCurFocusedWindowClient;
355
356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 * The input context last provided by the current client.
358 */
359 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700362 * The missing method flags for the input context last provided by the current client.
363 *
364 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
365 */
366 int mCurInputContextMissingMethods;
367
368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 * The attributes last provided by the current client.
370 */
371 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700374 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700376 *
377 * <p>This can be {@code null} when no input method is connected.</p>
378 *
379 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700381 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 /**
satokab751aa2010-09-14 19:17:36 +0900385 * The current subtype of the current input method.
386 */
387 private InputMethodSubtype mCurrentSubtype;
388
satok4e4569d2010-11-19 18:45:53 +0900389 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900390 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700391 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900392
John Spurlocke0980502013-10-25 11:59:29 -0400393 // Was the keyguard locked when this client became current?
394 private boolean mCurClientInKeyguard;
395
satokab751aa2010-09-14 19:17:36 +0900396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 * Set to true if our ServiceConnection is currently actively bound to
398 * a service (whether or not we have gotten its IBinder back yet).
399 */
400 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * Set if the client has asked for the input method to be shown.
404 */
405 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 /**
408 * Set if we were explicitly told to show the input method.
409 */
410 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
413 * Set if we were forced to be shown.
414 */
415 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
418 * Set if we last told the input method to show itself.
419 */
420 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800423 * {@code true} if the current input method is in fullscreen mode.
424 */
425 boolean mInFullscreenMode;
426
427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * The Intent used to connect to the current input method.
429 */
430 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 /**
433 * The token we have made for the currently active input method, to
434 * identify it in the future.
435 */
436 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 /**
439 * If non-null, this is the input method service we are currently connected
440 * to.
441 */
442 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
445 * Time that we last initiated a bind to the input method, to determine
446 * if we should try to disconnect and reconnect to it.
447 */
448 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
451 * Have we called mCurMethod.bindInput()?
452 */
453 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
456 * Currently enabled session. Only touched by service thread, not
457 * protected by a lock.
458 */
459 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700462 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700464 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900466 int mCurUserActionNotificationSequenceNumber = 0;
467
Joe Onorato857fd9b2011-01-27 15:08:35 -0800468 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900469
470 /**
471 * A set of status bits regarding the active IME.
472 *
473 * <p>This value is a combination of following two bits:</p>
474 * <dl>
475 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
476 * <dd>
477 * If this bit is ON, connected IME is ready to accept touch/key events.
478 * </dd>
479 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
480 * <dd>
481 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
482 * </dd>
483 * </dl>
484 * <em>Do not update this value outside of setImeWindowStatus.</em>
485 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800486 int mImeWindowVis;
487
Ken Wakasa05dbb652011-08-22 15:22:43 +0900488 private AlertDialog.Builder mDialogBuilder;
489 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700490 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900491 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700492 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900493 private InputMethodInfo[] mIms;
494 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800495 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700496 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800497 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900498 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
499 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500500 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700501 @HardKeyboardBehavior
502 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800503
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800504 /**
505 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
506 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
507 * will not affect those tasks that are already posted.
508 *
509 * <p>Posting {@link #MSG_START_INPUT} message basically means that
510 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
511 * back in the current IME process shortly, which will also affect what the current IME starts
512 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
513 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
514 * logical input session between the client application and the current IME.</p>
515 *
516 * <p>Be careful to not keep strong references to this object forever, which can prevent
517 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
518 * </p>
519 */
520 private static class StartInputInfo {
521 @NonNull
522 final IBinder mImeToken;
523 @Nullable
524 final IBinder mTargetWindow;
525
526 StartInputInfo(@NonNull IBinder imeToken, @Nullable IBinder targetWindow) {
527 mImeToken = imeToken;
528 mTargetWindow = targetWindow;
529 }
530 }
531
532 private WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700535 int mUserId;
536 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700537 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800538 String mLastEnabled = "";
539
Yohei Yukawa81482972015-06-04 00:58:59 -0700540 /**
541 * <em>This constructor must be called within the lock.</em>
542 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 SettingsObserver(Handler handler) {
544 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700545 }
546
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800547 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700548 if (mRegistered && mUserId == userId) {
549 return;
550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700552 if (mRegistered) {
553 mContext.getContentResolver().unregisterContentObserver(this);
554 mRegistered = false;
555 }
556 if (mUserId != userId) {
557 mLastEnabled = "";
558 mUserId = userId;
559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700561 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900562 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700563 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900564 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700565 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700566 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700567 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800568 resolver.registerContentObserver(Settings.Secure.getUriFor(
569 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700570 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
Michael Wright7b5a96b2014-08-09 19:28:42 -0700573 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800574 final Uri showImeUri = Settings.Secure.getUriFor(
575 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
576 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
577 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700579 if (showImeUri.equals(uri)) {
580 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800581 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
582 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
583 mContext.getContentResolver(),
584 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
585 0, mUserId) == 1;
586 if (mAccessibilityRequestingNoSoftKeyboard) {
587 final boolean showRequested = mShowRequested;
588 hideCurrentInputLocked(0, null);
589 mShowRequested = showRequested;
590 } else if (mShowRequested) {
591 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
592 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700593 } else {
594 boolean enabledChanged = false;
595 String newEnabled = mSettings.getEnabledInputMethodsStr();
596 if (!mLastEnabled.equals(newEnabled)) {
597 mLastEnabled = newEnabled;
598 enabledChanged = true;
599 }
600 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700604
605 @Override
606 public String toString() {
607 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
608 + " mLastEnabled=" + mLastEnabled + "}";
609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800611
Yohei Yukawa79247822017-01-23 15:26:15 -0800612 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900613 @Override
614 public void onReceive(Context context, Intent intent) {
615 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700616 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900617 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700618 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900619 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800620 } else if (Intent.ACTION_USER_ADDED.equals(action)
621 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100622 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800623 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700624 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
625 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
626 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
627 final String prevValue = intent.getStringExtra(
628 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
629 final String newValue = intent.getStringExtra(
630 Intent.EXTRA_SETTING_NEW_VALUE);
631 restoreEnabledInputMethods(mContext, prevValue, newValue);
632 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800633 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800634 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 } else {
636 Slog.w(TAG, "Unexpected intent " + intent);
637 }
638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800640
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800641 /**
642 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
643 *
644 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
645 * the users. We should ignore this event if this is about any background user's locale.</p>
646 *
647 * <p>Caution: This method must not be called when system is not ready.</p>
648 */
649 void onActionLocaleChanged() {
650 synchronized (mMethodMap) {
651 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
652 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
653 return;
654 }
655 buildInputMethodListLocked(true);
656 // If the locale is changed, needs to reset the default ime
657 resetDefaultImeLocked(mContext);
658 updateFromSettingsLocked(true);
659 mLastSystemLocales = possibleNewLocale;
660 }
661 }
662
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700663 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
664 // does not attempt to validate on the fly with any installed device policy, so must only
665 // be run in the context of initial device setup.
666 //
667 // TODO: Move this method to InputMethodUtils with adding unit tests.
668 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
669 if (DEBUG_RESTORE) {
670 Slog.i(TAG, "Restoring enabled input methods:");
671 Slog.i(TAG, "prev=" + prevValue);
672 Slog.i(TAG, " new=" + newValue);
673 }
674 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900675 ArrayMap<String, ArraySet<String>> prevMap =
676 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
677 ArrayMap<String, ArraySet<String>> newMap =
678 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700679
680 // Merge the restored ime+subtype enabled states into the live state
681 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
682 final String imeId = entry.getKey();
683 ArraySet<String> prevSubtypes = prevMap.get(imeId);
684 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700685 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700686 prevMap.put(imeId, prevSubtypes);
687 }
688 prevSubtypes.addAll(entry.getValue());
689 }
690
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700691 final String mergedImesAndSubtypesString =
692 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700693 if (DEBUG_RESTORE) {
694 Slog.i(TAG, "Merged IME string:");
695 Slog.i(TAG, " " + mergedImesAndSubtypesString);
696 }
697 Settings.Secure.putString(context.getContentResolver(),
698 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
699 }
700
Yohei Yukawac4e44912017-02-09 19:30:22 -0800701 final class MyPackageMonitor extends PackageMonitor {
702 /**
703 * Set of packages to be monitored.
704 *
705 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
706 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
707 * dynamically unless the entire package is updated, which also always triggers package
708 * rescanning.</p>
709 */
710 @GuardedBy("mMethodMap")
711 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
712
713 @GuardedBy("mMethodMap")
714 void clearPackagesToMonitorComponentChangeLocked() {
715 mPackagesToMonitorComponentChange.clear();
716 }
717
718 @GuardedBy("mMethodMap")
719 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
720 mPackagesToMonitorComponentChange.add(packageName);
721 }
722
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900723 private boolean isChangingPackagesOfCurrentUser() {
724 final int userId = getChangingUserId();
725 final boolean retval = userId == mSettings.getCurrentUserId();
726 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900727 if (!retval) {
728 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
729 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900730 }
731 return retval;
732 }
733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800735 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900736 if (!isChangingPackagesOfCurrentUser()) {
737 return false;
738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900740 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 final int N = mMethodList.size();
742 if (curInputMethodId != null) {
743 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800744 InputMethodInfo imi = mMethodList.get(i);
745 if (imi.getId().equals(curInputMethodId)) {
746 for (String pkg : packages) {
747 if (imi.getPackageName().equals(pkg)) {
748 if (!doit) {
749 return true;
750 }
satok723a27e2010-11-11 14:58:11 +0900751 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800752 chooseNewDefaultIMELocked();
753 return true;
754 }
755 }
756 }
757 }
758 }
759 }
760 return false;
761 }
762
763 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800764 public boolean onPackageChanged(String packageName, int uid, String[] components) {
765 // If this package is in the watch list, we want to check it.
766 synchronized (mMethodMap) {
767 return mPackagesToMonitorComponentChange.contains(packageName);
768 }
769 }
770
771 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800772 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 if (!isChangingPackagesOfCurrentUser()) {
774 return;
775 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800776 synchronized (mMethodMap) {
777 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900778 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800779 final int N = mMethodList.size();
780 if (curInputMethodId != null) {
781 for (int i=0; i<N; i++) {
782 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900783 final String imiId = imi.getId();
784 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800785 curIm = imi;
786 }
satoke7c6998e2011-06-03 17:57:59 +0900787
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800788 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900789 if (isPackageModified(imi.getPackageName())) {
790 mFileManager.deleteAllInputMethodSubtypes(imiId);
791 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800792 if (change == PACKAGE_TEMPORARY_CHANGE
793 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800794 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800795 + imi.getComponent());
796 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798 }
799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800800
Yohei Yukawa94e33302016-02-12 19:37:03 -0800801 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800804
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800805 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800806 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800807 if (change == PACKAGE_TEMPORARY_CHANGE
808 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800809 ServiceInfo si = null;
810 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900811 si = mIPackageManager.getServiceInfo(
812 curIm.getComponent(), 0, mSettings.getCurrentUserId());
813 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800814 }
815 if (si == null) {
816 // Uh oh, current input method is no longer around!
817 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800818 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900819 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800820 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800821 changed = true;
822 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800823 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900824 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800825 }
826 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800827 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800828 }
satokab751aa2010-09-14 19:17:36 +0900829
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800830 if (curIm == null) {
831 // We currently don't have a default input method... is
832 // one now available?
833 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700834 } else if (!changed && isPackageModified(curIm.getPackageName())) {
835 // Even if the current input method is still available, mCurrentSubtype could
836 // be obsolete when the package is modified in practice.
837 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800839
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800840 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800841 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843 }
844 }
845 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800846
Jeff Brownc28867a2013-03-26 15:42:39 -0700847 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900848 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700849 private final IInputMethod mMethod;
850 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800851
Jeff Brownc28867a2013-03-26 15:42:39 -0700852 MethodCallback(InputMethodManagerService imms, IInputMethod method,
853 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900854 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700855 mMethod = method;
856 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
satoke7c6998e2011-06-03 17:57:59 +0900859 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700860 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700861 long ident = Binder.clearCallingIdentity();
862 try {
863 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
864 } finally {
865 Binder.restoreCallingIdentity(ident);
866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
satok01038492012-04-09 21:08:27 +0900870 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700871 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900872 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700873 public void onHardKeyboardStatusChange(boolean available) {
874 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
875 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900876 }
877
Michael Wright7b5a96b2014-08-09 19:28:42 -0700878 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900879 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700880 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900881 }
882 synchronized(mMethodMap) {
883 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
884 && mSwitchingDialog.isShowing()) {
885 mSwitchingDialogTitleView.findViewById(
886 com.android.internal.R.id.hard_keyboard_section).setVisibility(
887 available ? View.VISIBLE : View.GONE);
888 }
889 }
890 }
891 }
892
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800893 public static final class Lifecycle extends SystemService {
894 private InputMethodManagerService mService;
895
896 public Lifecycle(Context context) {
897 super(context);
898 mService = new InputMethodManagerService(context);
899 }
900
901 @Override
902 public void onStart() {
903 LocalServices.addService(InputMethodManagerInternal.class,
904 new LocalServiceImpl(mService.mHandler));
905 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
906 }
907
908 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800909 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700910 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800911 // TODO: Dispatch this to a worker thread as needed.
912 mService.onSwitchUser(userHandle);
913 }
914
915 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800916 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700917 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800918 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800919 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
920 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
921 .getService(Context.STATUS_BAR_SERVICE);
922 mService.systemRunning(statusBarService);
923 }
924 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800925
926 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700927 public void onUnlockUser(final @UserIdInt int userHandle) {
928 // Called on ActivityManager thread.
929 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700930 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800931 }
932 }
933
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800934 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800935 synchronized(mMethodMap) {
936 final int currentUserId = mSettings.getCurrentUserId();
937 if (DEBUG) {
938 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
939 }
940 if (userId != currentUserId) {
941 return;
942 }
943 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800944 if (mSystemReady) {
945 // We need to rebuild IMEs.
946 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
947 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
948 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800949 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800950 }
951
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800952 void onSwitchUser(@UserIdInt int userId) {
953 synchronized (mMethodMap) {
954 switchUserLocked(userId);
955 }
956 }
957
Seigo Nonaka7309b122015-08-17 18:34:13 -0700958 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900959 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800961 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700963 // Note: SettingsObserver doesn't register observers in its constructor.
964 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 mIWindowManager = IWindowManager.Stub.asInterface(
966 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700967 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800968 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900969 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 public void executeMessage(Message msg) {
971 handleMessage(msg);
972 }
Mita Yuned218c72012-12-06 17:18:25 -0800973 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800974 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800975 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900976 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700977 mHasFeature = context.getPackageManager().hasSystemFeature(
978 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500979 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700980 mHardKeyboardBehavior = mContext.getResources().getInteger(
981 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900982
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400983 Bundle extras = new Bundle();
984 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
985 mImeSwitcherNotification = new Notification.Builder(mContext)
986 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
987 .setWhen(0)
988 .setOngoing(true)
989 .addExtras(extras)
990 .setCategory(Notification.CATEGORY_SYSTEM)
991 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400992
satok7cfc0ed2011-06-20 21:29:36 +0900993 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900994 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900995
996 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900997
satok7cfc0ed2011-06-20 21:29:36 +0900998 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 int userId = 0;
1000 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001001 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001002 } catch (RemoteException e) {
1003 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1004 }
satok913a8922010-08-26 21:53:41 +09001005
satokd87c2592010-09-29 11:52:06 +09001006 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001007 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001008 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001009
Kenny Guy2a764942014-04-02 13:29:20 +01001010 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001011 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001012 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1013 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001014 }
1015
satok5b927c432012-05-01 20:09:34 +09001016 private void resetDefaultImeLocked(Context context) {
1017 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001018 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001019 return;
1020 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001021 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001022 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001023 if (suitableImes.isEmpty()) {
1024 Slog.i(TAG, "No default found");
1025 return;
satok5b927c432012-05-01 20:09:34 +09001026 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001027 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001028 if (DEBUG) {
1029 Slog.i(TAG, "Default found, using " + defIm.getId());
1030 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001031 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001032 }
1033
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001034 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001035 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1036 + " currentUserId=" + mSettings.getCurrentUserId());
1037
Yohei Yukawa81482972015-06-04 00:58:59 -07001038 // ContentObserver should be registered again when the user is changed
1039 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001040
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001041 // If the system is not ready or the device is not yed unlocked by the user, then we use
1042 // copy-on-write settings.
1043 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001044 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001045 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001046 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001047 // InputMethodFileManager should be reset when the user is changed
1048 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001049 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001050
1051 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1052 + " defaultImiId=" + defaultImiId);
1053
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001054 // For secondary users, the list of enabled IMEs may not have been updated since the
1055 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1056 // not be empty even if the IME has been uninstalled by the primary user.
1057 // Even in such cases, IMMS works fine because it will find the most applicable
1058 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001059 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001060 mLastSystemLocales = mRes.getConfiguration().getLocales();
1061
1062 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1063 if (mSystemReady) {
1064 hideCurrentInputLocked(0, null);
1065 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1066 buildInputMethodListLocked(initialUserSwitch);
1067 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1068 // This is the first time of the user switch and
1069 // set the current ime to the proper one.
1070 resetDefaultImeLocked(mContext);
1071 }
1072 updateFromSettingsLocked(true);
1073 try {
1074 startInputInnerLocked();
1075 } catch (RuntimeException e) {
1076 Slog.w(TAG, "Unexpected exception", e);
1077 }
1078 }
1079
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001080 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001081 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1082 mSettings.getEnabledInputMethodListLocked(), newUserId,
1083 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001084 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001085
1086 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1087 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001088 }
1089
Kenny Guy2a764942014-04-02 13:29:20 +01001090 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001091 mSettings.setCurrentProfileIds(
1092 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001093 }
1094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 @Override
1096 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1097 throws RemoteException {
1098 try {
1099 return super.onTransact(code, data, reply, flags);
1100 } catch (RuntimeException e) {
1101 // The input method manager only throws security exceptions, so let's
1102 // log all others.
1103 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001104 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106 throw e;
1107 }
1108 }
1109
Svetoslav Ganova0027152013-06-25 14:59:53 -07001110 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001111 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 if (DEBUG) {
1113 Slog.d(TAG, "--- systemReady");
1114 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001115 if (!mSystemReady) {
1116 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001117 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001118 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001119 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001120 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001121 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1122 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001123 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001124 if (mStatusBar != null) {
1125 mStatusBar.setIconVisibility(mSlotIme, false);
1126 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001127 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001128 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1129 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001130 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001131 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001132 mHardKeyboardListener);
1133 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001134
1135 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1136 mSettingsObserver.registerContentObserverLocked(currentUserId);
1137
1138 final IntentFilter broadcastFilter = new IntentFilter();
1139 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1140 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1141 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1142 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1143 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1144 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1145
1146 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1147 resetDefaultImeLocked(mContext);
1148 updateFromSettingsLocked(true);
1149 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1150 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1151 mContext.getBasePackageName());
1152
Dianne Hackborncc278702009-09-02 23:07:23 -07001153 try {
1154 startInputInnerLocked();
1155 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001156 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001157 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001158 }
1159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001162 // ---------------------------------------------------------------------------------------
1163 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1164 // 1) it comes from the system process
1165 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1166 private boolean calledFromValidUser() {
1167 final int uid = Binder.getCallingUid();
1168 final int userId = UserHandle.getUserId(uid);
1169 if (DEBUG) {
1170 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1171 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1172 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001173 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1174 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 }
Kenny Guy2a764942014-04-02 13:29:20 +01001176 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001177 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001178 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001179
1180 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1181 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1182 // must not manage background users' states in any functions.
1183 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1184 // by a token.
1185 if (mContext.checkCallingOrSelfPermission(
1186 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1187 == PackageManager.PERMISSION_GRANTED) {
1188 if (DEBUG) {
1189 Slog.d(TAG, "--- Access granted because the calling process has "
1190 + "the INTERACT_ACROSS_USERS_FULL permission");
1191 }
1192 return true;
1193 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001194 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001195 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1196 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001197 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001198 }
1199
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001200
1201 /**
1202 * Returns true iff the caller is identified to be the current input method with the token.
1203 * @param token The window token given to the input method when it was started.
1204 * @return true if and only if non-null valid token is specified.
1205 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001206 private boolean calledWithValidToken(@Nullable IBinder token) {
1207 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1208 if (DEBUG) {
1209 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1210 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1211 }
1212 return false;
1213 }
1214 if (token == null || token != mCurToken) {
1215 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1216 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1217 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001218 return false;
1219 }
1220 return true;
1221 }
1222
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001223 private boolean bindCurrentInputMethodService(
1224 Intent service, ServiceConnection conn, int flags) {
1225 if (service == null || conn == null) {
1226 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1227 return false;
1228 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001229 return mContext.bindServiceAsUser(service, conn, flags,
1230 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001231 }
1232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001235 // TODO: Make this work even for non-current users?
1236 if (!calledFromValidUser()) {
1237 return Collections.emptyList();
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001240 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 }
1243
satoke7c6998e2011-06-03 17:57:59 +09001244 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001246 // TODO: Make this work even for non-current users?
1247 if (!calledFromValidUser()) {
1248 return Collections.emptyList();
1249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001251 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
1253 }
1254
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001255 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001256 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001257 * @return enabled subtypes of the specified imi
1258 */
satoke7c6998e2011-06-03 17:57:59 +09001259 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001260 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001261 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001262 // TODO: Make this work even for non-current users?
1263 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001264 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001265 }
satok67ddf9c2010-11-17 09:45:54 +09001266 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001267 final InputMethodInfo imi;
1268 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001269 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001270 } else {
1271 imi = mMethodMap.get(imiId);
1272 }
1273 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001274 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001275 }
1276 return mSettings.getEnabledInputMethodSubtypeListLocked(
1277 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001278 }
1279 }
1280
satoke7c6998e2011-06-03 17:57:59 +09001281 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 public void addClient(IInputMethodClient client,
1283 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001284 if (!calledFromValidUser()) {
1285 return;
1286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 synchronized (mMethodMap) {
1288 mClients.put(client.asBinder(), new ClientState(client,
1289 inputContext, uid, pid));
1290 }
1291 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001292
satoke7c6998e2011-06-03 17:57:59 +09001293 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001295 if (!calledFromValidUser()) {
1296 return;
1297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001299 ClientState cs = mClients.remove(client.asBinder());
1300 if (cs != null) {
1301 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001302 if (mCurClient == cs) {
1303 mCurClient = null;
1304 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001305 if (mCurFocusedWindowClient == cs) {
1306 mCurFocusedWindowClient = null;
1307 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 void executeOrSendMessage(IInterface target, Message msg) {
1313 if (target.asBinder() instanceof Binder) {
1314 mCaller.sendMessage(msg);
1315 } else {
1316 handleMessage(msg);
1317 msg.recycle();
1318 }
1319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001320
Yohei Yukawa33e81792015-11-17 21:14:42 -08001321 void unbindCurrentClientLocked(
1322 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001324 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 + mCurClient.client.asBinder());
1326 if (mBoundToMethod) {
1327 mBoundToMethod = false;
1328 if (mCurMethod != null) {
1329 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1330 MSG_UNBIND_INPUT, mCurMethod));
1331 }
1332 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001333
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001334 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1335 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001336 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1337 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
The Android Open Source Project10592532009-03-18 17:39:46 -07001341 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 private int getImeShowFlags() {
1346 int flags = 0;
1347 if (mShowForced) {
1348 flags |= InputMethod.SHOW_FORCED
1349 | InputMethod.SHOW_EXPLICIT;
1350 } else if (mShowExplicitlyRequested) {
1351 flags |= InputMethod.SHOW_EXPLICIT;
1352 }
1353 return flags;
1354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 private int getAppShowFlags() {
1357 int flags = 0;
1358 if (mShowForced) {
1359 flags |= InputMethodManager.SHOW_FORCED;
1360 } else if (!mShowExplicitlyRequested) {
1361 flags |= InputMethodManager.SHOW_IMPLICIT;
1362 }
1363 return flags;
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001366 InputBindResult attachNewInputLocked(
1367 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 if (!mBoundToMethod) {
1369 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1370 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1371 mBoundToMethod = true;
1372 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001373
1374 final Binder startInputToken = new Binder();
1375 final StartInputInfo info = new StartInputInfo(mCurToken, mCurFocusedWindow);
1376 mStartInputMap.put(startInputToken, info);
1377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001379 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001380 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001381 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001383 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001384 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001386 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001387 (session.channel != null ? session.channel.dup() : null),
1388 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001390
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001391 InputBindResult startInputLocked(
1392 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001393 IInputMethodClient client, IInputContext inputContext,
1394 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001395 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 // If no method is currently selected, do nothing.
1397 if (mCurMethodId == null) {
1398 return mNoBinding;
1399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 ClientState cs = mClients.get(client.asBinder());
1402 if (cs == null) {
1403 throw new IllegalArgumentException("unknown client "
1404 + client.asBinder());
1405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406
Yohei Yukawa74750f22016-03-22 12:54:22 -07001407 if (attribute == null) {
1408 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1409 + " uid=" + cs.uid + " pid=" + cs.pid);
1410 return null;
1411 }
1412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 try {
1414 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1415 // Check with the window manager to make sure this client actually
1416 // has a window with focus. If not, reject. This is thread safe
1417 // because if the focus changes some time before or after, the
1418 // next client receiving focus that has any interest in input will
1419 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001420 if (DEBUG) {
1421 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1422 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 return null;
1425 }
1426 } catch (RemoteException e) {
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001429 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001430 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001431 }
1432
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001433 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001434 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001435 @NonNull EditorInfo attribute, int controlFlags,
1436 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001437 // If no method is currently selected, do nothing.
1438 if (mCurMethodId == null) {
1439 return mNoBinding;
1440 }
1441
Yohei Yukawad57ba672015-06-08 16:39:46 -07001442 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1443 attribute.packageName)) {
1444 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1445 + " uid=" + cs.uid + " package=" + attribute.packageName);
1446 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001447 }
1448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001450 // Was the keyguard locked when switching over to the new client?
1451 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 // If the client is changing, we need to switch over to the new
1453 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001454 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001455 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001456 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457
1458 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001459 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001460 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001461 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
1463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 // Bump up the sequence for this client and attach it.
1466 mCurSeq++;
1467 if (mCurSeq <= 0) mCurSeq = 1;
1468 mCurClient = cs;
1469 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001470 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 // Check if the input method is changing.
1474 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1475 if (cs.curSession != null) {
1476 // Fast case: if we are already connected to the input method,
1477 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001478 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001479 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481 if (mHaveConnection) {
1482 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 // Return to client, and we will get back with it when
1484 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001485 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001486 return new InputBindResult(null, null, mCurId, mCurSeq,
1487 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 } else if (SystemClock.uptimeMillis()
1489 < (mLastBindTime+TIME_TO_RECONNECT)) {
1490 // In this case we have connected to the service, but
1491 // don't yet have its interface. If it hasn't been too
1492 // long since we did the connection, we'll return to
1493 // the client and wait to get the service interface so
1494 // we can report back. If it has been too long, we want
1495 // to fall through so we can try a disconnect/reconnect
1496 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001497 return new InputBindResult(null, null, mCurId, mCurSeq,
1498 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001500 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1501 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
1503 }
1504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001505
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001506 return startInputInnerLocked();
1507 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001509 InputBindResult startInputInnerLocked() {
1510 if (mCurMethodId == null) {
1511 return mNoBinding;
1512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001514 if (!mSystemReady) {
1515 // If the system is not yet ready, we shouldn't be running third
1516 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001517 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1518 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1522 if (info == null) {
1523 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001526 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1529 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001530 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1531 com.android.internal.R.string.input_method_binding_label);
1532 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1533 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001534 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001535 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1536 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 mLastBindTime = SystemClock.uptimeMillis();
1538 mHaveConnection = true;
1539 mCurId = info.getId();
1540 mCurToken = new Binder();
1541 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001542 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001543 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 } catch (RemoteException e) {
1545 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001546 return new InputBindResult(null, null, mCurId, mCurSeq,
1547 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 } else {
1549 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001550 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 + mCurIntent);
1552 }
1553 return null;
1554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001555
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001556 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001557 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001558 IInputMethodClient client, IInputContext inputContext,
1559 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001560 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001561 if (!calledFromValidUser()) {
1562 return null;
1563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001565 if (DEBUG) {
1566 Slog.v(TAG, "startInput: reason="
1567 + InputMethodClient.getStartInputReason(startInputReason)
1568 + " client = " + client.asBinder()
1569 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001570 + " missingMethods="
1571 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001572 + " attribute=" + attribute
1573 + " controlFlags=#" + Integer.toHexString(controlFlags));
1574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 final long ident = Binder.clearCallingIdentity();
1576 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001577 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1578 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 } finally {
1580 Binder.restoreCallingIdentity(ident);
1581 }
1582 }
1583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001584
satoke7c6998e2011-06-03 17:57:59 +09001585 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 public void finishInput(IInputMethodClient client) {
1587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
satoke7c6998e2011-06-03 17:57:59 +09001589 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 public void onServiceConnected(ComponentName name, IBinder service) {
1591 synchronized (mMethodMap) {
1592 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1593 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001594 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001595 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001596 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001597 return;
1598 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001599 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001600 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1601 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001603 clearClientSessionLocked(mCurClient);
1604 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
1606 }
1607 }
1608 }
1609
Jeff Brownc28867a2013-03-26 15:42:39 -07001610 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1611 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 synchronized (mMethodMap) {
1613 if (mCurMethod != null && method != null
1614 && mCurMethod.asBinder() == method.asBinder()) {
1615 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001616 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001618 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001619 InputBindResult res = attachNewInputLocked(
1620 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 if (res.method != null) {
1622 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001623 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001625 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
1627 }
1628 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001629
1630 // Session abandoned. Close its associated input channel.
1631 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001633
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001634 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001635 if (mVisibleBound) {
1636 mContext.unbindService(mVisibleConnection);
1637 mVisibleBound = false;
1638 }
1639
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001640 if (mHaveConnection) {
1641 mContext.unbindService(this);
1642 mHaveConnection = false;
1643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001644
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001645 if (mCurToken != null) {
1646 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001647 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001648 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001649 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001650 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001651 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001652 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001653 } catch (RemoteException e) {
1654 }
1655 mCurToken = null;
1656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001657
The Android Open Source Project10592532009-03-18 17:39:46 -07001658 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001659 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001660 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001661
Yohei Yukawa33e81792015-11-17 21:14:42 -08001662 void resetCurrentMethodAndClient(
1663 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001664 mCurMethodId = null;
1665 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001666 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001667 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001668
1669 void requestClientSessionLocked(ClientState cs) {
1670 if (!cs.sessionRequested) {
1671 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1672 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1673 cs.sessionRequested = true;
1674 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1675 MSG_CREATE_SESSION, mCurMethod, channels[1],
1676 new MethodCallback(this, mCurMethod, channels[0])));
1677 }
1678 }
1679
1680 void clearClientSessionLocked(ClientState cs) {
1681 finishSessionLocked(cs.curSession);
1682 cs.curSession = null;
1683 cs.sessionRequested = false;
1684 }
1685
1686 private void finishSessionLocked(SessionState sessionState) {
1687 if (sessionState != null) {
1688 if (sessionState.session != null) {
1689 try {
1690 sessionState.session.finishSession();
1691 } catch (RemoteException e) {
1692 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001693 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001694 }
1695 sessionState.session = null;
1696 }
1697 if (sessionState.channel != null) {
1698 sessionState.channel.dispose();
1699 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001700 }
1701 }
1702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001704 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 if (mCurMethod != null) {
1706 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001707 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001709
Jeff Brownc28867a2013-03-26 15:42:39 -07001710 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001711 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 mCurMethod = null;
1713 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001714 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001715 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001716 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001717 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001719
satoke7c6998e2011-06-03 17:57:59 +09001720 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001722 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1723 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1724 // which is irregular but can eventually happen for everyone just by continuing using the
1725 // device. Thus it is important to make sure that all the internal states are properly
1726 // refreshed when this method is called back. Running
1727 // adb install -r <APK that implements the current IME>
1728 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001730 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 + " mCurIntent=" + mCurIntent);
1732 if (mCurMethod != null && mCurIntent != null
1733 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001734 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 // We consider this to be a new bind attempt, since the system
1736 // should now try to restart the service for us.
1737 mLastBindTime = SystemClock.uptimeMillis();
1738 mShowRequested = mInputShown;
1739 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001740 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
1742 }
1743 }
1744
satokf9f01002011-05-19 21:31:50 +09001745 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001747 synchronized (mMethodMap) {
1748 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001749 return;
1750 }
1751 final long ident = Binder.clearCallingIdentity();
1752 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001754 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001755 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001756 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001759 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001760 CharSequence contentDescription = null;
1761 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001762 // Use PackageManager to load label
1763 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001764 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001765 mIPackageManager.getApplicationInfo(packageName, 0,
1766 mSettings.getCurrentUserId()));
1767 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001768 /* ignore */
1769 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001770 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001771 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001772 contentDescription != null
1773 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001774 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001777 } finally {
1778 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
1781 }
1782
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001783 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001784 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001785 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001786 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001787 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001788 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001789 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1790 // When physical keyboard is attached, we show the ime switcher (or notification if
1791 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1792 // exists in the IME switcher dialog. Might be OK to remove this condition once
1793 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1794 return true;
1795 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001796 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1797 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001798 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001799
1800 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1801 final int N = imis.size();
1802 if (N > 2) return true;
1803 if (N < 1) return false;
1804 int nonAuxCount = 0;
1805 int auxCount = 0;
1806 InputMethodSubtype nonAuxSubtype = null;
1807 InputMethodSubtype auxSubtype = null;
1808 for(int i = 0; i < N; ++i) {
1809 final InputMethodInfo imi = imis.get(i);
1810 final List<InputMethodSubtype> subtypes =
1811 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1812 final int subtypeCount = subtypes.size();
1813 if (subtypeCount == 0) {
1814 ++nonAuxCount;
1815 } else {
1816 for (int j = 0; j < subtypeCount; ++j) {
1817 final InputMethodSubtype subtype = subtypes.get(j);
1818 if (!subtype.isAuxiliary()) {
1819 ++nonAuxCount;
1820 nonAuxSubtype = subtype;
1821 } else {
1822 ++auxCount;
1823 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001824 }
1825 }
satok7cfc0ed2011-06-20 21:29:36 +09001826 }
1827 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001828 if (nonAuxCount > 1 || auxCount > 1) {
1829 return true;
1830 } else if (nonAuxCount == 1 && auxCount == 1) {
1831 if (nonAuxSubtype != null && auxSubtype != null
1832 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1833 || auxSubtype.overridesImplicitlyEnabledSubtype()
1834 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1835 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1836 return false;
1837 }
1838 return true;
1839 }
1840 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001841 }
1842
John Spurlocke0980502013-10-25 11:59:29 -04001843 private boolean isKeyguardLocked() {
1844 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1845 }
1846
satokdbf29502011-08-25 15:28:23 +09001847 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001848 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001849 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
1850 int backDisposition) {
1851 if (startInputToken == null) {
1852 throw new InvalidParameterException("startInputToken cannot be null");
1853 }
1854
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001855 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001856 return;
1857 }
1858
1859 synchronized (mMethodMap) {
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001860 final StartInputInfo info = mStartInputMap.get(startInputToken);
1861 if (info == null) {
1862 throw new InvalidParameterException("Unknown startInputToken=" + startInputToken);
1863 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001864 mImeWindowVis = vis;
1865 mBackDisposition = backDisposition;
1866 updateSystemUiLocked(token, vis, backDisposition);
1867 }
1868 }
1869
1870 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1871 synchronized (mMethodMap) {
1872 updateSystemUiLocked(token, vis, backDisposition);
1873 }
1874 }
1875
1876 // Caution! This method is called in this class. Handle multi-user carefully
1877 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1878 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001879 return;
1880 }
1881
1882 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1883 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001884 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001885 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001886 // apply policy for binder calls
1887 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1888 vis = 0;
satok06487a52010-10-29 11:37:18 +09001889 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001890 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1891 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1892 if (mStatusBar != null) {
1893 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1894 needsToShowImeSwitcher);
1895 }
1896 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1897 if (imi != null && needsToShowImeSwitcher) {
1898 // Used to load label
1899 final CharSequence title = mRes.getText(
1900 com.android.internal.R.string.select_input_method);
1901 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1902 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001903 mImeSwitcherNotification.setContentTitle(title)
1904 .setContentText(summary)
1905 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001906 try {
1907 if ((mNotificationManager != null)
1908 && !mIWindowManager.hasNavigationBar()) {
1909 if (DEBUG) {
1910 Slog.d(TAG, "--- show notification: label = " + summary);
1911 }
1912 mNotificationManager.notifyAsUser(null,
1913 com.android.internal.R.string.select_input_method,
1914 mImeSwitcherNotification.build(), UserHandle.ALL);
1915 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001916 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001917 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001918 }
1919 } else {
1920 if (mNotificationShown && mNotificationManager != null) {
1921 if (DEBUG) {
1922 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001923 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001924 mNotificationManager.cancelAsUser(null,
1925 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1926 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001927 }
satok06487a52010-10-29 11:37:18 +09001928 }
1929 } finally {
1930 Binder.restoreCallingIdentity(ident);
1931 }
1932 }
1933
satoke7c6998e2011-06-03 17:57:59 +09001934 @Override
satokf9f01002011-05-19 21:31:50 +09001935 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001936 if (!calledFromValidUser()) {
1937 return;
1938 }
satokf9f01002011-05-19 21:31:50 +09001939 synchronized (mMethodMap) {
1940 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1941 for (int i = 0; i < spans.length; ++i) {
1942 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001943 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001944 mSecureSuggestionSpans.put(ss, currentImi);
1945 }
1946 }
1947 }
1948 }
1949
satoke7c6998e2011-06-03 17:57:59 +09001950 @Override
satokf9f01002011-05-19 21:31:50 +09001951 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001952 if (!calledFromValidUser()) {
1953 return false;
1954 }
satokf9f01002011-05-19 21:31:50 +09001955 synchronized (mMethodMap) {
1956 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1957 // TODO: Do not send the intent if the process of the targetImi is already dead.
1958 if (targetImi != null) {
1959 final String[] suggestions = span.getSuggestions();
1960 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001961 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001962 final Intent intent = new Intent();
1963 // Ensures that only a class in the original IME package will receive the
1964 // notification.
satok42c5a162011-05-26 16:46:14 +09001965 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001966 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1967 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1968 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1969 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001970 final long ident = Binder.clearCallingIdentity();
1971 try {
1972 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1973 } finally {
1974 Binder.restoreCallingIdentity(ident);
1975 }
satokf9f01002011-05-19 21:31:50 +09001976 return true;
1977 }
1978 }
1979 return false;
1980 }
1981
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001982 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001983 updateInputMethodsFromSettingsLocked(enabledMayChange);
1984 updateKeyboardFromSettingsLocked();
1985 }
1986
1987 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001988 if (enabledMayChange) {
1989 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1990 for (int i=0; i<enabled.size(); i++) {
1991 // We allow the user to select "disabled until used" apps, so if they
1992 // are enabling one of those here we now need to make it enabled.
1993 InputMethodInfo imm = enabled.get(i);
1994 try {
1995 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1996 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1997 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001998 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001999 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002000 if (DEBUG) {
2001 Slog.d(TAG, "Update state(" + imm.getId()
2002 + "): DISABLED_UNTIL_USED -> DEFAULT");
2003 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002004 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2005 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002006 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2007 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002008 }
2009 } catch (RemoteException e) {
2010 }
2011 }
2012 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002013 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2014 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2015 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2016 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002017 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002018 // There is no input method selected, try to choose new applicable input method.
2019 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002021 }
2022 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002024 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002026 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002027 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 }
satokf3db1af2010-11-23 13:34:33 +09002029 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002030 } else {
2031 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002032 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002034 // Here is not the perfect place to reset the switching controller. Ideally
2035 // mSwitchingController and mSettings should be able to share the same state.
2036 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2037 // the same enabled IMEs list.
2038 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002039
2040 }
2041
2042 public void updateKeyboardFromSettingsLocked() {
2043 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2044 if (mSwitchingDialog != null
2045 && mSwitchingDialogTitleView != null
2046 && mSwitchingDialog.isShowing()) {
2047 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2048 com.android.internal.R.id.hard_keyboard_switch);
2049 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002052
Yohei Yukawab097b822015-12-01 10:43:08 -08002053 private void notifyInputMethodSubtypeChanged(final int userId,
2054 @Nullable final InputMethodInfo inputMethodInfo,
2055 @Nullable final InputMethodSubtype subtype) {
2056 final InputManagerInternal inputManagerInternal =
2057 LocalServices.getService(InputManagerInternal.class);
2058 if (inputManagerInternal != null) {
2059 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2060 }
2061 }
2062
satokab751aa2010-09-14 19:17:36 +09002063 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 InputMethodInfo info = mMethodMap.get(id);
2065 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002066 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002068
satokd81e9502012-05-21 12:58:45 +09002069 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002071 final int subtypeCount = info.getSubtypeCount();
2072 if (subtypeCount <= 0) {
2073 return;
satokcd7cd292010-11-20 15:46:23 +09002074 }
satokd81e9502012-05-21 12:58:45 +09002075 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2076 final InputMethodSubtype newSubtype;
2077 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2078 newSubtype = info.getSubtypeAt(subtypeId);
2079 } else {
2080 // If subtype is null, try to find the most applicable one from
2081 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002082 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002083 }
2084 if (newSubtype == null || oldSubtype == null) {
2085 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2086 + ", new subtype = " + newSubtype);
2087 return;
2088 }
2089 if (newSubtype != oldSubtype) {
2090 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2091 if (mCurMethod != null) {
2092 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002093 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002094 mCurMethod.changeInputMethodSubtype(newSubtype);
2095 } catch (RemoteException e) {
2096 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002097 return;
satokab751aa2010-09-14 19:17:36 +09002098 }
2099 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002100 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 return;
2103 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002104
satokd81e9502012-05-21 12:58:45 +09002105 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 final long ident = Binder.clearCallingIdentity();
2107 try {
satokab751aa2010-09-14 19:17:36 +09002108 // Set a subtype to this input method.
2109 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002110 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2111 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2112 // because mCurMethodId is stored as a history in
2113 // setSelectedInputMethodAndSubtypeLocked().
2114 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002116 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002118 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002120 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002122 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 } finally {
2124 Binder.restoreCallingIdentity(ident);
2125 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002126
2127 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2128 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
satok42c5a162011-05-26 16:46:14 +09002131 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 public boolean showSoftInput(IInputMethodClient client, int flags,
2133 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002134 if (!calledFromValidUser()) {
2135 return false;
2136 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002137 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 long ident = Binder.clearCallingIdentity();
2139 try {
2140 synchronized (mMethodMap) {
2141 if (mCurClient == null || client == null
2142 || mCurClient.client.asBinder() != client.asBinder()) {
2143 try {
2144 // We need to check if this is the current client with
2145 // focus in the window manager, to allow this call to
2146 // be made before input is started in it.
2147 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002148 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002149 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002152 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
2154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002155
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002156 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002157 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 }
2159 } finally {
2160 Binder.restoreCallingIdentity(ident);
2161 }
2162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002163
The Android Open Source Project4df24232009-03-05 14:34:35 -08002164 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002166 if (mAccessibilityRequestingNoSoftKeyboard) {
2167 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
Anna Galusza9b278112016-01-04 11:37:37 -08002169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2171 mShowExplicitlyRequested = true;
2172 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002173 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2174 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002176
Dianne Hackborncc278702009-09-02 23:07:23 -07002177 if (!mSystemReady) {
2178 return false;
2179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002180
The Android Open Source Project4df24232009-03-05 14:34:35 -08002181 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002183 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002184 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2185 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2186 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002188 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002189 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002190 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002191 | Context.BIND_TREAT_LIKE_ACTIVITY
2192 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002193 mVisibleBound = true;
2194 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002195 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002197 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 // The client has asked to have the input method shown, but
2199 // we have been sitting here too long with a connection to the
2200 // service and no interface received, so let's disconnect/connect
2201 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002202 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002204 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002206 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002207 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002208 } else {
2209 if (DEBUG) {
2210 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2211 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002214
The Android Open Source Project4df24232009-03-05 14:34:35 -08002215 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
satok42c5a162011-05-26 16:46:14 +09002218 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002219 public boolean hideSoftInput(IInputMethodClient client, int flags,
2220 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002221 if (!calledFromValidUser()) {
2222 return false;
2223 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002224 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 long ident = Binder.clearCallingIdentity();
2226 try {
2227 synchronized (mMethodMap) {
2228 if (mCurClient == null || client == null
2229 || mCurClient.client.asBinder() != client.asBinder()) {
2230 try {
2231 // We need to check if this is the current client with
2232 // focus in the window manager, to allow this call to
2233 // be made before input is started in it.
2234 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002235 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2236 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002237 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
2239 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002240 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
2242 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002243
Joe Onorato8a9b2202010-02-26 18:56:32 -08002244 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002245 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
2247 } finally {
2248 Binder.restoreCallingIdentity(ident);
2249 }
2250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002251
The Android Open Source Project4df24232009-03-05 14:34:35 -08002252 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2254 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002255 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 -08002256 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
2258 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002259 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 -08002260 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002262
2263 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2264 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2265 // to be updated with the new value sent from IME process. Even in such a transient state
2266 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2267 // application process as a valid request, and have even promised such a behavior with CTS
2268 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2269 // IMMS#InputShown indicates that the software keyboard is shown.
2270 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2271 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2272 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002273 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002274 if (shouldHideSoftInput) {
2275 // The IME will report its visible state again after the following message finally
2276 // delivered to the IME process as an IPC. Hence the inconsistency between
2277 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2278 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002279 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2280 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2281 res = true;
2282 } else {
2283 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002285 if (mHaveConnection && mVisibleBound) {
2286 mContext.unbindService(mVisibleConnection);
2287 mVisibleBound = false;
2288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 mInputShown = false;
2290 mShowRequested = false;
2291 mShowExplicitlyRequested = false;
2292 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002293 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002295
satok42c5a162011-05-26 16:46:14 +09002296 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002297 public InputBindResult startInputOrWindowGainedFocus(
2298 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2299 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002300 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002301 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002302 if (windowToken != null) {
2303 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002304 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002305 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002306 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2307 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002308 }
2309 }
2310
2311 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002312 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2313 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002314 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2315 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002316 // Needs to check the validity before clearing calling identity
2317 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002318 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 long ident = Binder.clearCallingIdentity();
2320 try {
2321 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002322 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2323 + InputMethodClient.getStartInputReason(startInputReason)
2324 + " client=" + client.asBinder()
2325 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002326 + " missingMethods="
2327 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002328 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002329 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002331 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002332
Dianne Hackborn7663d802012-02-24 13:08:49 -08002333 ClientState cs = mClients.get(client.asBinder());
2334 if (cs == null) {
2335 throw new IllegalArgumentException("unknown client "
2336 + client.asBinder());
2337 }
2338
2339 try {
2340 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2341 // Check with the window manager to make sure this client actually
2342 // has a window with focus. If not, reject. This is thread safe
2343 // because if the focus changes some time before or after, the
2344 // next client receiving focus that has any interest in input will
2345 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002346 if (DEBUG) {
2347 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2348 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2349 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002350 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002352 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002354
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002355 if (!calledFromValidUser) {
2356 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2357 Slog.w(TAG, "If you want to interect with IME, you need "
2358 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2359 hideCurrentInputLocked(0, null);
2360 return null;
2361 }
2362
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002363 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002364 if (DEBUG) {
2365 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2366 + " attribute=" + attribute + ", token = " + windowToken);
2367 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002368 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002369 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002370 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002371 }
2372 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002373 }
2374 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002375 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002376
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002377 // Should we auto-show the IME even if the caller has not
2378 // specified what should be done with it?
2379 // We only do this automatically if the window can resize
2380 // to accommodate the IME (so what the user sees will give
2381 // them good context without input information being obscured
2382 // by the IME) or if running on a large screen where there
2383 // is more room for the target window + IME.
2384 final boolean doAutoShow =
2385 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2386 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2387 || mRes.getConfiguration().isLayoutSizeAtLeast(
2388 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002389 final boolean isTextEditor =
2390 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2391
2392 // We want to start input before showing the IME, but after closing
2393 // it. We want to do this after closing it to help the IME disappear
2394 // more quickly (not get stuck behind it initializing itself for the
2395 // new focused input, even if its window wants to hide the IME).
2396 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2399 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002400 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2402 // There is no focus view, and this window will
2403 // be behind any soft input window, so hide the
2404 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002405 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002406 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002408 } else if (isTextEditor && doAutoShow && (softInputMode &
2409 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 // There is a focus view, and we are navigating forward
2411 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002412 // We only do this automatically if the window can resize
2413 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002414 // them good context without input information being obscured
2415 // by the IME) or if running on a large screen where there
2416 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002417 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002418 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002419 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002420 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002421 didStart = true;
2422 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002423 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 }
2425 break;
2426 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2427 // Do nothing.
2428 break;
2429 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2430 if ((softInputMode &
2431 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002432 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002433 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 }
2435 break;
2436 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002437 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002438 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 break;
2440 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2441 if ((softInputMode &
2442 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002443 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002444 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002445 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002446 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002447 didStart = true;
2448 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002449 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
2451 break;
2452 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002453 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002454 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002455 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002456 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002457 didStart = true;
2458 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002459 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 break;
2461 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002462
2463 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002464 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002465 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
2468 } finally {
2469 Binder.restoreCallingIdentity(ident);
2470 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002471
2472 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002474
satok42c5a162011-05-26 16:46:14 +09002475 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002476 public void showInputMethodPickerFromClient(
2477 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002478 if (!calledFromValidUser()) {
2479 return;
2480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 synchronized (mMethodMap) {
2482 if (mCurClient == null || client == null
2483 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002484 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002485 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 }
2487
satok440aab52010-11-25 09:43:11 +09002488 // Always call subtype picker, because subtype picker is a superset of input method
2489 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002490 mHandler.sendMessage(mCaller.obtainMessageI(
2491 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002492 }
2493 }
2494
satok42c5a162011-05-26 16:46:14 +09002495 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002497 if (!calledFromValidUser()) {
2498 return;
2499 }
satok28203512010-11-24 11:06:49 +09002500 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2501 }
2502
satok42c5a162011-05-26 16:46:14 +09002503 @Override
satok28203512010-11-24 11:06:49 +09002504 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002505 if (!calledFromValidUser()) {
2506 return;
2507 }
satok28203512010-11-24 11:06:49 +09002508 synchronized (mMethodMap) {
2509 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002510 setInputMethodWithSubtypeIdLocked(token, id,
2511 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2512 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002513 } else {
2514 setInputMethod(token, id);
2515 }
2516 }
satokab751aa2010-09-14 19:17:36 +09002517 }
2518
satok42c5a162011-05-26 16:46:14 +09002519 @Override
satokb416a712010-11-25 20:42:14 +09002520 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002521 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002522 if (!calledFromValidUser()) {
2523 return;
2524 }
satokb416a712010-11-25 20:42:14 +09002525 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002526 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2527 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002528 }
2529 }
2530
satok4fc87d62011-05-20 16:13:43 +09002531 @Override
satok735cf382010-11-11 20:40:09 +09002532 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002533 if (!calledFromValidUser()) {
2534 return false;
2535 }
satok735cf382010-11-11 20:40:09 +09002536 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002537 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002538 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002539 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002540 lastImi = mMethodMap.get(lastIme.first);
2541 } else {
2542 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002543 }
satok4fc87d62011-05-20 16:13:43 +09002544 String targetLastImiId = null;
2545 int subtypeId = NOT_A_SUBTYPE_ID;
2546 if (lastIme != null && lastImi != null) {
2547 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002548 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002549 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2550 : mCurrentSubtype.hashCode();
2551 // If the last IME is the same as the current IME and the last subtype is not
2552 // defined, there is no need to switch to the last IME.
2553 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2554 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002555 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002556 }
2557 }
2558
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002559 if (TextUtils.isEmpty(targetLastImiId)
2560 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002561 // This is a safety net. If the currentSubtype can't be added to the history
2562 // and the framework couldn't find the last ime, we will make the last ime be
2563 // the most applicable enabled keyboard subtype of the system imes.
2564 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2565 if (enabled != null) {
2566 final int N = enabled.size();
2567 final String locale = mCurrentSubtype == null
2568 ? mRes.getConfiguration().locale.toString()
2569 : mCurrentSubtype.getLocale();
2570 for (int i = 0; i < N; ++i) {
2571 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002572 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002573 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002574 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2575 InputMethodUtils.getSubtypes(imi),
2576 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002577 if (keyboardSubtype != null) {
2578 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002579 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002580 imi, keyboardSubtype.hashCode());
2581 if(keyboardSubtype.getLocale().equals(locale)) {
2582 break;
2583 }
2584 }
2585 }
2586 }
2587 }
2588 }
2589
2590 if (!TextUtils.isEmpty(targetLastImiId)) {
2591 if (DEBUG) {
2592 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2593 + ", from: " + mCurMethodId + ", " + subtypeId);
2594 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002595 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002596 return true;
2597 } else {
2598 return false;
2599 }
satok735cf382010-11-11 20:40:09 +09002600 }
2601 }
2602
satoke7c6998e2011-06-03 17:57:59 +09002603 @Override
satok688bd472012-02-09 20:09:17 +09002604 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002605 if (!calledFromValidUser()) {
2606 return false;
2607 }
satok688bd472012-02-09 20:09:17 +09002608 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002609 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002610 return false;
2611 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002612 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002613 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2614 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002615 if (nextSubtype == null) {
2616 return false;
2617 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002618 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2619 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002620 return true;
2621 }
2622 }
2623
2624 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002625 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2626 if (!calledFromValidUser()) {
2627 return false;
2628 }
2629 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002630 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002631 return false;
2632 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002633 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002634 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2635 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002636 if (nextSubtype == null) {
2637 return false;
2638 }
2639 return true;
2640 }
2641 }
2642
2643 @Override
satok68f1b782011-04-11 14:26:04 +09002644 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002645 if (!calledFromValidUser()) {
2646 return null;
2647 }
satok68f1b782011-04-11 14:26:04 +09002648 synchronized (mMethodMap) {
2649 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2650 // TODO: Handle the case of the last IME with no subtypes
2651 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2652 || TextUtils.isEmpty(lastIme.second)) return null;
2653 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2654 if (lastImi == null) return null;
2655 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002656 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002657 final int lastSubtypeId =
2658 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002659 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2660 return null;
2661 }
2662 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002663 } catch (NumberFormatException e) {
2664 return null;
2665 }
2666 }
2667 }
2668
satoke7c6998e2011-06-03 17:57:59 +09002669 @Override
satokee5e77c2011-09-02 18:50:15 +09002670 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002671 if (!calledFromValidUser()) {
2672 return;
2673 }
satok91e88122011-07-18 11:11:42 +09002674 // By this IPC call, only a process which shares the same uid with the IME can add
2675 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002676 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002677 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002678 if (!mSystemReady) {
2679 return;
2680 }
satok91e88122011-07-18 11:11:42 +09002681 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002682 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002683 final String[] packageInfos;
2684 try {
2685 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2686 } catch (RemoteException e) {
2687 Slog.e(TAG, "Failed to get package infos");
2688 return;
2689 }
satok91e88122011-07-18 11:11:42 +09002690 if (packageInfos != null) {
2691 final int packageNum = packageInfos.length;
2692 for (int i = 0; i < packageNum; ++i) {
2693 if (packageInfos[i].equals(imi.getPackageName())) {
2694 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002695 final long ident = Binder.clearCallingIdentity();
2696 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002697 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002698 } finally {
2699 Binder.restoreCallingIdentity(ident);
2700 }
satokee5e77c2011-09-02 18:50:15 +09002701 return;
satok91e88122011-07-18 11:11:42 +09002702 }
2703 }
2704 }
satoke7c6998e2011-06-03 17:57:59 +09002705 }
satokee5e77c2011-09-02 18:50:15 +09002706 return;
satoke7c6998e2011-06-03 17:57:59 +09002707 }
2708
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002709 @Override
2710 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002711 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002712 }
2713
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002714 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002715 public void clearLastInputMethodWindowForTransition(IBinder token) {
2716 if (!calledFromValidUser()) {
2717 return;
2718 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002719 synchronized (mMethodMap) {
2720 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002721 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002722 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002723 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002724 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002725 }
2726
2727 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002728 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002729 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002730 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002731 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002732 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002733 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2734 if (DEBUG) {
2735 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2736 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2737 + " actual: " + sequenceNumber);
2738 }
2739 return;
2740 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002741 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2742 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002743 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002744 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002745 }
2746 }
2747
satok28203512010-11-24 11:06:49 +09002748 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002750 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2751 }
2752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002754 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2755 if (token == null) {
2756 if (mContext.checkCallingOrSelfPermission(
2757 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2758 != PackageManager.PERMISSION_GRANTED) {
2759 throw new SecurityException(
2760 "Using null token requires permission "
2761 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002763 } else if (mCurToken != token) {
2764 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2765 + " token: " + token);
2766 return;
2767 }
2768
2769 final long ident = Binder.clearCallingIdentity();
2770 try {
2771 setInputMethodLocked(id, subtypeId);
2772 } finally {
2773 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 }
2775 }
2776
satok42c5a162011-05-26 16:46:14 +09002777 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002779 if (!calledFromValidUser()) {
2780 return;
2781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002783 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 return;
2785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 long ident = Binder.clearCallingIdentity();
2787 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002788 hideCurrentInputLocked(flags, null);
2789 } finally {
2790 Binder.restoreCallingIdentity(ident);
2791 }
2792 }
2793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002794
satok42c5a162011-05-26 16:46:14 +09002795 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002796 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002797 if (!calledFromValidUser()) {
2798 return;
2799 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002800 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002801 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002802 return;
2803 }
2804 long ident = Binder.clearCallingIdentity();
2805 try {
2806 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 } finally {
2808 Binder.restoreCallingIdentity(ident);
2809 }
2810 }
2811 }
2812
2813 void setEnabledSessionInMainThread(SessionState session) {
2814 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002815 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002817 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002818 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 } catch (RemoteException e) {
2820 }
2821 }
2822 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002823 if (mEnabledSession != null && mEnabledSession.session != null) {
2824 try {
2825 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2826 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2827 } catch (RemoteException e) {
2828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 }
2830 }
2831 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002832
satok42c5a162011-05-26 16:46:14 +09002833 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002835 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002837 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002838 final boolean showAuxSubtypes;
2839 switch (msg.arg1) {
2840 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2841 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2842 // implemented so that auxiliary subtypes will be excluded when the soft
2843 // keyboard is invisible.
2844 showAuxSubtypes = mInputShown;
2845 break;
2846 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2847 showAuxSubtypes = true;
2848 break;
2849 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2850 showAuxSubtypes = false;
2851 break;
2852 default:
2853 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2854 return false;
2855 }
2856 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002857 return true;
2858
satok47a44912010-10-06 16:03:58 +09002859 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002860 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002861 return true;
2862
2863 case MSG_SHOW_IM_CONFIG:
2864 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002865 return true;
2866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 case MSG_UNBIND_INPUT:
2870 try {
2871 ((IInputMethod)msg.obj).unbindInput();
2872 } catch (RemoteException e) {
2873 // There is nothing interesting about the method dying.
2874 }
2875 return true;
2876 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002877 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 try {
2879 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2880 } catch (RemoteException e) {
2881 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002882 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 return true;
2884 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002885 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002887 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002888 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002889 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 } catch (RemoteException e) {
2891 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002892 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 return true;
2894 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002895 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002897 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002898 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002899 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 } catch (RemoteException e) {
2901 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002902 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002904 case MSG_HIDE_CURRENT_INPUT_METHOD:
2905 synchronized (mMethodMap) {
2906 hideCurrentInputLocked(0, null);
2907 }
2908 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002910 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002912 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2914 } catch (RemoteException e) {
2915 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002916 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002918 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002919 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002920 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002921 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002923 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002925 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002926 // Dispose the channel if the input method is not local to this process
2927 // because the remote proxy will get its own copy when unparceled.
2928 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002929 channel.dispose();
2930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002932 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002936
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002937 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08002938 final int missingMethods = msg.arg1;
2939 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002940 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002941 final IBinder startInputToken = (IBinder) args.arg1;
2942 final SessionState session = (SessionState) args.arg2;
2943 final IInputContext inputContext = (IInputContext) args.arg3;
2944 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002947 session.method.startInput(startInputToken, inputContext, missingMethods,
2948 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 } catch (RemoteException e) {
2950 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002951 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002953 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002956
Yohei Yukawa33e81792015-11-17 21:14:42 -08002957 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002959 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 } catch (RemoteException e) {
2961 // There is nothing interesting about the last client dying.
2962 }
2963 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002964 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002965 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002966 IInputMethodClient client = (IInputMethodClient)args.arg1;
2967 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002969 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002971 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002972 } finally {
2973 // Dispose the channel if the input method is not local to this process
2974 // because the remote proxy will get its own copy when unparceled.
2975 if (res.channel != null && Binder.isProxy(client)) {
2976 res.channel.dispose();
2977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002979 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002981 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002982 case MSG_SET_ACTIVE:
2983 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002984 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002985 } catch (RemoteException e) {
2986 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2987 + ((ClientState)msg.obj).pid + " uid "
2988 + ((ClientState)msg.obj).uid);
2989 }
2990 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002991 case MSG_SET_INTERACTIVE:
2992 handleSetInteractive(msg.arg1 != 0);
2993 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002994 case MSG_SWITCH_IME:
2995 handleSwitchInputMethod(msg.arg1 != 0);
2996 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002997 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2998 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002999 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003000 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003001 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003002 } catch (RemoteException e) {
3003 Slog.w(TAG, "Got RemoteException sending "
3004 + "setUserActionNotificationSequenceNumber("
3005 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003006 + clientState.pid + " uid "
3007 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003008 }
3009 return true;
3010 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003011 case MSG_REPORT_FULLSCREEN_MODE: {
3012 final boolean fullscreen = msg.arg1 != 0;
3013 final ClientState clientState = (ClientState)msg.obj;
3014 try {
3015 clientState.client.reportFullscreenMode(fullscreen);
3016 } catch (RemoteException e) {
3017 Slog.w(TAG, "Got RemoteException sending "
3018 + "reportFullscreen(" + fullscreen + ") notification to pid="
3019 + clientState.pid + " uid=" + clientState.uid);
3020 }
3021 return true;
3022 }
satok01038492012-04-09 21:08:27 +09003023
3024 // --------------------------------------------------------------
3025 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003026 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003027 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003028 case MSG_SYSTEM_UNLOCK_USER:
3029 final int userId = msg.arg1;
3030 onUnlockUser(userId);
3031 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 }
3033 return false;
3034 }
3035
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003036 private void handleSetInteractive(final boolean interactive) {
3037 synchronized (mMethodMap) {
3038 mIsInteractive = interactive;
3039 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3040
3041 // Inform the current client of the change in active status
3042 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003043 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3044 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3045 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003046 }
3047 }
3048 }
3049
Yohei Yukawaae61f712015-12-09 13:00:10 -08003050 private void handleSwitchInputMethod(final boolean forwardDirection) {
3051 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003052 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003053 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003054 if (nextSubtype == null) {
3055 return;
3056 }
3057 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003058 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3059 if (newInputMethodInfo == null) {
3060 return;
3061 }
3062 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3063 newInputMethodInfo, mCurrentSubtype);
3064 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003065 if (mSubtypeSwitchedByShortCutToast == null) {
3066 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3067 Toast.LENGTH_SHORT);
3068 } else {
3069 mSubtypeSwitchedByShortCutToast.setText(toastText);
3070 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003071 mSubtypeSwitchedByShortCutToast.show();
3072 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003073 }
3074 }
3075
satokdc9ddae2011-10-06 12:22:36 +09003076 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003077 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3078 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003079 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003080 if (DEBUG) {
3081 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3082 }
satok723a27e2010-11-11 14:58:11 +09003083 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003084 return true;
3085 }
3086
3087 return false;
3088 }
3089
Yohei Yukawa94e33302016-02-12 19:37:03 -08003090 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003091 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003092 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003093 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003094 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003095 if (!mSystemReady) {
3096 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3097 return;
3098 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003099 mMethodList.clear();
3100 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003101 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003102 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003103
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003104 // Use for queryIntentServicesAsUser
3105 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003107 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3108 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3109 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003110 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003112 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3113 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003114
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003115 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003116 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 for (int i = 0; i < services.size(); ++i) {
3118 ResolveInfo ri = services.get(i);
3119 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003120 final String imeId = InputMethodInfo.computeId(ri);
3121 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3122 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 + ": it does not require the permission "
3124 + android.Manifest.permission.BIND_INPUT_METHOD);
3125 continue;
3126 }
3127
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003128 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003130 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 try {
satoke7c6998e2011-06-03 17:57:59 +09003132 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003133 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003134 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003135 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136
3137 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003138 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003140 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003141 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 }
3143 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003144
Yohei Yukawac4e44912017-02-09 19:30:22 -08003145 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3146 // negatives when the package state remains to be the same but only the component state is
3147 // changed.
3148 {
3149 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3150 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3151 // conservative, but it seems we cannot use it for now (Issue 35176630).
3152 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3153 new Intent(InputMethod.SERVICE_INTERFACE),
3154 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3155 final int N = allInputMethodServices.size();
3156 for (int i = 0; i < N; ++i) {
3157 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3158 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3159 continue;
3160 }
3161 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3162 }
3163 }
3164
Yohei Yukawa859df052016-02-17 07:56:46 -08003165 // TODO: The following code should find better place to live.
3166 if (!resetDefaultEnabledIme) {
3167 boolean enabledImeFound = false;
3168 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3169 final int N = enabledImes.size();
3170 for (int i = 0; i < N; ++i) {
3171 final InputMethodInfo imi = enabledImes.get(i);
3172 if (mMethodList.contains(imi)) {
3173 enabledImeFound = true;
3174 break;
3175 }
3176 }
3177 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003178 if (DEBUG) {
3179 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3180 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003181 resetDefaultEnabledIme = true;
3182 resetSelectedInputMethodAndSubtypeLocked("");
3183 }
3184 }
3185
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003186 if (resetDefaultEnabledIme) {
3187 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003188 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003189 final int N = defaultEnabledIme.size();
3190 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003191 final InputMethodInfo imi = defaultEnabledIme.get(i);
3192 if (DEBUG) {
3193 Slog.d(TAG, "--- enable ime = " + imi);
3194 }
3195 setInputMethodEnabledLocked(imi.getId(), true);
3196 }
3197 }
3198
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003199 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003200 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003201 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003202 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3203 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003204 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003205 }
3206 } else {
3207 // Double check that the default IME is certainly enabled.
3208 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003209 }
3210 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003211 // Here is not the perfect place to reset the switching controller. Ideally
3212 // mSwitchingController and mSettings should be able to share the same state.
3213 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3214 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003215 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003219
satok217f5482010-12-15 05:19:19 +09003220 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003221 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003222 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003223 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3224 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003225 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003226 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003227 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003228 final int userId;
3229 synchronized (mMethodMap) {
3230 userId = mSettings.getCurrentUserId();
3231 }
3232 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003233 }
3234
3235 private void showConfigureInputMethods() {
3236 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3237 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3238 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3239 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003240 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003241 }
3242
satok2c93efc2012-04-02 19:33:47 +09003243 private boolean isScreenLocked() {
3244 return mKeyguardManager != null
3245 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3246 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003247
Seigo Nonaka14e13912015-05-06 21:04:13 -07003248 private void showInputMethodMenu(boolean showAuxSubtypes) {
3249 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003252 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003253
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003254 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003255 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003256 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003257
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003258 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003259 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003260 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3261 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003262 if (immis == null || immis.size() == 0) {
3263 return;
3264 }
3265
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003266 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267
satok688bd472012-02-09 20:09:17 +09003268 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003269 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003270 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003271
satokc3690562012-01-10 20:14:43 +09003272 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003273 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003274 if (currentSubtype != null) {
3275 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003276 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3277 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003278 }
3279 }
3280
Ken Wakasa761eb372011-03-04 19:06:18 +09003281 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003282 mIms = new InputMethodInfo[N];
3283 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003284 int checkedItem = 0;
3285 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003286 final ImeSubtypeListItem item = imList.get(i);
3287 mIms[i] = item.mImi;
3288 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003289 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003290 int subtypeId = mSubtypeIds[i];
3291 if ((subtypeId == NOT_A_SUBTYPE_ID)
3292 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3293 || (subtypeId == lastInputMethodSubtypeId)) {
3294 checkedItem = i;
3295 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003298
3299 final Context settingsContext = new ContextThemeWrapper(context,
3300 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3301
3302 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003303 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3304 @Override
3305 public void onCancel(DialogInterface dialog) {
3306 hideInputMethodMenu();
3307 }
3308 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003309
3310 final Context dialogContext = mDialogBuilder.getContext();
3311 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3312 com.android.internal.R.styleable.DialogPreference,
3313 com.android.internal.R.attr.alertDialogStyle, 0);
3314 final Drawable dialogIcon = a.getDrawable(
3315 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3316 a.recycle();
3317
3318 mDialogBuilder.setIcon(dialogIcon);
3319
Yohei Yukawad34e1482016-02-11 08:03:52 -08003320 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003321 final View tv = inflater.inflate(
3322 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3323 mDialogBuilder.setCustomTitle(tv);
3324
3325 // Setup layout for a toggle switch of the hardware keyboard
3326 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003327 mSwitchingDialogTitleView
3328 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003329 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003330 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003331 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003332 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003333 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003334 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3335 @Override
3336 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003337 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003338 // Ensure that the input method dialog is dismissed when changing
3339 // the hardware keyboard state.
3340 hideInputMethodMenu();
3341 }
3342 });
3343
Alan Viverette505e3ab2014-11-24 15:22:11 -08003344 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003345 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3346 final OnClickListener choiceListener = new OnClickListener() {
3347 @Override
3348 public void onClick(final DialogInterface dialog, final int which) {
3349 synchronized (mMethodMap) {
3350 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3351 || mSubtypeIds.length <= which) {
3352 return;
satok01038492012-04-09 21:08:27 +09003353 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003354 final InputMethodInfo im = mIms[which];
3355 int subtypeId = mSubtypeIds[which];
3356 adapter.mCheckedItem = which;
3357 adapter.notifyDataSetChanged();
3358 hideInputMethodMenu();
3359 if (im != null) {
3360 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3361 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003362 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003363 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003365 }
3366 }
3367 };
3368 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003371 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003372 final Window w = mSwitchingDialog.getWindow();
3373 final WindowManager.LayoutParams attrs = w.getAttributes();
3374 w.setType(TYPE_INPUT_METHOD_DIALOG);
3375 // Use an alternate token for the dialog for that window manager can group the token
3376 // with other IME windows based on type vs. grouping based on whichever token happens
3377 // to get selected by the system later on.
3378 attrs.token = mSwitchingDialogToken;
3379 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3380 attrs.setTitle("Select input method");
3381 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003382 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 mSwitchingDialog.show();
3384 }
3385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003386
Ken Wakasa05dbb652011-08-22 15:22:43 +09003387 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3388 private final LayoutInflater mInflater;
3389 private final int mTextViewResourceId;
3390 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003391 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003392 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3393 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3394 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003395
Ken Wakasa05dbb652011-08-22 15:22:43 +09003396 mTextViewResourceId = textViewResourceId;
3397 mItemsList = itemsList;
3398 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003399 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003400 }
3401
3402 @Override
3403 public View getView(int position, View convertView, ViewGroup parent) {
3404 final View view = convertView != null ? convertView
3405 : mInflater.inflate(mTextViewResourceId, null);
3406 if (position < 0 || position >= mItemsList.size()) return view;
3407 final ImeSubtypeListItem item = mItemsList.get(position);
3408 final CharSequence imeName = item.mImeName;
3409 final CharSequence subtypeName = item.mSubtypeName;
3410 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3411 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3412 if (TextUtils.isEmpty(subtypeName)) {
3413 firstTextView.setText(imeName);
3414 secondTextView.setVisibility(View.GONE);
3415 } else {
3416 firstTextView.setText(subtypeName);
3417 secondTextView.setText(imeName);
3418 secondTextView.setVisibility(View.VISIBLE);
3419 }
3420 final RadioButton radioButton =
3421 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3422 radioButton.setChecked(position == mCheckedItem);
3423 return view;
3424 }
3425 }
3426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003428 synchronized (mMethodMap) {
3429 hideInputMethodMenuLocked();
3430 }
3431 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003432
The Android Open Source Project10592532009-03-18 17:39:46 -07003433 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003434 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435
The Android Open Source Project10592532009-03-18 17:39:46 -07003436 if (mSwitchingDialog != null) {
3437 mSwitchingDialog.dismiss();
3438 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003440
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003441 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003442 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003443 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003447
satok42c5a162011-05-26 16:46:14 +09003448 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003450 // TODO: Make this work even for non-current users?
3451 if (!calledFromValidUser()) {
3452 return false;
3453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 synchronized (mMethodMap) {
3455 if (mContext.checkCallingOrSelfPermission(
3456 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3457 != PackageManager.PERMISSION_GRANTED) {
3458 throw new SecurityException(
3459 "Requires permission "
3460 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3461 }
Anna Galusza9b278112016-01-04 11:37:37 -08003462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 long ident = Binder.clearCallingIdentity();
3464 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003465 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 } finally {
3467 Binder.restoreCallingIdentity(ident);
3468 }
3469 }
3470 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003471
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003472 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3473 // Make sure this is a valid input method.
3474 InputMethodInfo imm = mMethodMap.get(id);
3475 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003476 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003477 }
3478
satokd87c2592010-09-29 11:52:06 +09003479 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3480 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003481
satokd87c2592010-09-29 11:52:06 +09003482 if (enabled) {
3483 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3484 if (pair.first.equals(id)) {
3485 // We are enabling this input method, but it is already enabled.
3486 // Nothing to do. The previous state was enabled.
3487 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003488 }
3489 }
satokd87c2592010-09-29 11:52:06 +09003490 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3491 // Previous state was disabled.
3492 return false;
3493 } else {
3494 StringBuilder builder = new StringBuilder();
3495 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3496 builder, enabledInputMethodsList, id)) {
3497 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003498 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003499 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3500 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3501 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003502 }
3503 // Previous state was enabled.
3504 return true;
3505 } else {
3506 // We are disabling the input method but it is already disabled.
3507 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003508 return false;
3509 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003510 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003511 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003512
satok723a27e2010-11-11 14:58:11 +09003513 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3514 boolean setSubtypeOnly) {
3515 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003516 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003517
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003518 mCurUserActionNotificationSequenceNumber =
3519 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3520 if (DEBUG) {
3521 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3522 + mCurUserActionNotificationSequenceNumber);
3523 }
3524
3525 if (mCurClient != null && mCurClient.client != null) {
3526 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3527 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003528 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003529 }
3530
satok723a27e2010-11-11 14:58:11 +09003531 // Set Subtype here
3532 if (imi == null || subtypeId < 0) {
3533 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003534 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003535 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003536 if (subtypeId < imi.getSubtypeCount()) {
3537 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3538 mSettings.putSelectedSubtype(subtype.hashCode());
3539 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003540 } else {
3541 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003542 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003543 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003544 }
satokab751aa2010-09-14 19:17:36 +09003545 }
satok723a27e2010-11-11 14:58:11 +09003546
Yohei Yukawa68645a62016-02-17 07:54:20 -08003547 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003548 // Set InputMethod here
3549 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3550 }
3551 }
3552
3553 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3554 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3555 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3556 // newDefaultIme is empty when there is no candidate for the selected IME.
3557 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3558 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3559 if (subtypeHashCode != null) {
3560 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003561 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003562 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003563 } catch (NumberFormatException e) {
3564 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3565 }
3566 }
3567 }
3568 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003569 }
3570
satok4e4569d2010-11-19 18:45:53 +09003571 // If there are no selected shortcuts, tries finding the most applicable ones.
3572 private Pair<InputMethodInfo, InputMethodSubtype>
3573 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3574 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3575 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003576 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003577 boolean foundInSystemIME = false;
3578
3579 // Search applicable subtype for each InputMethodInfo
3580 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003581 final String imiId = imi.getId();
3582 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3583 continue;
3584 }
satokcd7cd292010-11-20 15:46:23 +09003585 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003586 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003587 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003588 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003589 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003590 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003591 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003592 }
satokdf31ae62011-01-15 06:19:44 +09003593 // 2. Search by the system locale from enabledSubtypes.
3594 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003595 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003596 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003597 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003598 }
satoka86f5e42011-09-02 17:12:42 +09003599 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003600 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003601 final ArrayList<InputMethodSubtype> subtypesForSearch =
3602 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003603 ? InputMethodUtils.getSubtypes(imi)
3604 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003605 // 4. Search by the current subtype's locale from all subtypes.
3606 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003607 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003608 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003609 }
3610 // 5. Search by the system locale from all subtypes.
3611 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003612 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003613 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003614 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003615 }
satokcd7cd292010-11-20 15:46:23 +09003616 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003617 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003618 // The current input method is the most applicable IME.
3619 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003620 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003621 break;
satok7599a7f2010-12-22 13:45:23 +09003622 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003623 // The system input method is 2nd applicable IME.
3624 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003625 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003626 if ((imi.getServiceInfo().applicationInfo.flags
3627 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3628 foundInSystemIME = true;
3629 }
satok4e4569d2010-11-19 18:45:53 +09003630 }
3631 }
3632 }
3633 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003634 if (mostApplicableIMI != null) {
3635 Slog.w(TAG, "Most applicable shortcut input method was:"
3636 + mostApplicableIMI.getId());
3637 if (mostApplicableSubtype != null) {
3638 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3639 + "," + mostApplicableSubtype.getMode() + ","
3640 + mostApplicableSubtype.getLocale());
3641 }
3642 }
satok4e4569d2010-11-19 18:45:53 +09003643 }
satokcd7cd292010-11-20 15:46:23 +09003644 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003645 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003646 } else {
3647 return null;
3648 }
3649 }
3650
satokab751aa2010-09-14 19:17:36 +09003651 /**
3652 * @return Return the current subtype of this input method.
3653 */
satok42c5a162011-05-26 16:46:14 +09003654 @Override
satokab751aa2010-09-14 19:17:36 +09003655 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003656 // TODO: Make this work even for non-current users?
3657 if (!calledFromValidUser()) {
3658 return null;
3659 }
3660 synchronized (mMethodMap) {
3661 return getCurrentInputMethodSubtypeLocked();
3662 }
3663 }
3664
3665 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003666 if (mCurMethodId == null) {
3667 return null;
3668 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003669 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003670 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3671 if (imi == null || imi.getSubtypeCount() == 0) {
3672 return null;
satok4e4569d2010-11-19 18:45:53 +09003673 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003674 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003675 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3676 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003677 if (subtypeId == NOT_A_SUBTYPE_ID) {
3678 // If there are no selected subtypes, the framework will try to find
3679 // the most applicable subtype from explicitly or implicitly enabled
3680 // subtypes.
3681 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003682 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003683 // If there is only one explicitly or implicitly enabled subtype,
3684 // just returns it.
3685 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3686 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3687 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003688 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003689 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003690 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003691 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003692 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003693 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3694 true);
satok4e4569d2010-11-19 18:45:53 +09003695 }
satok3ef8b292010-11-23 06:06:29 +09003696 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003697 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003698 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003699 }
3700 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003701 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003702 }
3703
satok4e4569d2010-11-19 18:45:53 +09003704 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003705 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003706 @Override
satok4e4569d2010-11-19 18:45:53 +09003707 public List getShortcutInputMethodsAndSubtypes() {
3708 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003709 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003710 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003711 // If there are no selected shortcut subtypes, the framework will try to find
3712 // the most applicable subtype from all subtypes whose mode is
3713 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003714 Pair<InputMethodInfo, InputMethodSubtype> info =
3715 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003716 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003717 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003718 ret.add(info.first);
3719 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003720 }
satok3da92232011-01-11 22:46:30 +09003721 return ret;
satokf3db1af2010-11-23 13:34:33 +09003722 }
satokf3db1af2010-11-23 13:34:33 +09003723 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3724 ret.add(imi);
3725 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3726 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003727 }
3728 }
satokf3db1af2010-11-23 13:34:33 +09003729 return ret;
satok4e4569d2010-11-19 18:45:53 +09003730 }
3731 }
3732
satok42c5a162011-05-26 16:46:14 +09003733 @Override
satokb66d2872010-11-10 01:04:04 +09003734 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003735 // TODO: Make this work even for non-current users?
3736 if (!calledFromValidUser()) {
3737 return false;
3738 }
satokb66d2872010-11-10 01:04:04 +09003739 synchronized (mMethodMap) {
3740 if (subtype != null && mCurMethodId != null) {
3741 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003742 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003743 if (subtypeId != NOT_A_SUBTYPE_ID) {
3744 setInputMethodLocked(mCurMethodId, subtypeId);
3745 return true;
3746 }
3747 }
3748 return false;
3749 }
3750 }
3751
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003752 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003753 private static class InputMethodFileManager {
3754 private static final String SYSTEM_PATH = "system";
3755 private static final String INPUT_METHOD_PATH = "inputmethod";
3756 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3757 private static final String NODE_SUBTYPES = "subtypes";
3758 private static final String NODE_SUBTYPE = "subtype";
3759 private static final String NODE_IMI = "imi";
3760 private static final String ATTR_ID = "id";
3761 private static final String ATTR_LABEL = "label";
3762 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003763 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003764 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003765 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003766 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3767 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3768 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003769 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003770 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3771 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003772 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003773 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003774 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003775 if (methodMap == null) {
3776 throw new NullPointerException("methodMap is null");
3777 }
3778 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003779 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003780 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3781 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003782 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003783 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003784 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3785 }
3786 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3787 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3788 if (!subtypeFile.exists()) {
3789 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003790 writeAdditionalInputMethodSubtypes(
3791 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003792 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003793 readAdditionalInputMethodSubtypes(
3794 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003795 }
3796 }
3797
3798 private void deleteAllInputMethodSubtypes(String imiId) {
3799 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003800 mAdditionalSubtypesMap.remove(imiId);
3801 writeAdditionalInputMethodSubtypes(
3802 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003803 }
3804 }
3805
3806 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003807 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003808 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003809 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003810 final int N = additionalSubtypes.length;
3811 for (int i = 0; i < N; ++i) {
3812 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003813 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003814 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003815 } else {
3816 Slog.w(TAG, "Duplicated subtype definition found: "
3817 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003818 }
3819 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003820 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3821 writeAdditionalInputMethodSubtypes(
3822 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003823 }
3824 }
3825
3826 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3827 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003828 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003829 }
3830 }
3831
3832 private static void writeAdditionalInputMethodSubtypes(
3833 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3834 HashMap<String, InputMethodInfo> methodMap) {
3835 // Safety net for the case that this function is called before methodMap is set.
3836 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3837 FileOutputStream fos = null;
3838 try {
3839 fos = subtypesFile.startWrite();
3840 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003841 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003842 out.startDocument(null, true);
3843 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3844 out.startTag(null, NODE_SUBTYPES);
3845 for (String imiId : allSubtypes.keySet()) {
3846 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3847 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3848 continue;
3849 }
3850 out.startTag(null, NODE_IMI);
3851 out.attribute(null, ATTR_ID, imiId);
3852 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3853 final int N = subtypesList.size();
3854 for (int i = 0; i < N; ++i) {
3855 final InputMethodSubtype subtype = subtypesList.get(i);
3856 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003857 if (subtype.hasSubtypeId()) {
3858 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3859 String.valueOf(subtype.getSubtypeId()));
3860 }
satoke7c6998e2011-06-03 17:57:59 +09003861 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3862 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3863 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003864 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3865 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003866 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3867 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3868 out.attribute(null, ATTR_IS_AUXILIARY,
3869 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003870 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3871 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003872 out.endTag(null, NODE_SUBTYPE);
3873 }
3874 out.endTag(null, NODE_IMI);
3875 }
3876 out.endTag(null, NODE_SUBTYPES);
3877 out.endDocument();
3878 subtypesFile.finishWrite(fos);
3879 } catch (java.io.IOException e) {
3880 Slog.w(TAG, "Error writing subtypes", e);
3881 if (fos != null) {
3882 subtypesFile.failWrite(fos);
3883 }
3884 }
3885 }
3886
3887 private static void readAdditionalInputMethodSubtypes(
3888 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3889 if (allSubtypes == null || subtypesFile == null) return;
3890 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003891 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003892 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003893 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003894 int type = parser.getEventType();
3895 // Skip parsing until START_TAG
3896 while ((type = parser.next()) != XmlPullParser.START_TAG
3897 && type != XmlPullParser.END_DOCUMENT) {}
3898 String firstNodeName = parser.getName();
3899 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3900 throw new XmlPullParserException("Xml doesn't start with subtypes");
3901 }
3902 final int depth =parser.getDepth();
3903 String currentImiId = null;
3904 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3905 while (((type = parser.next()) != XmlPullParser.END_TAG
3906 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3907 if (type != XmlPullParser.START_TAG)
3908 continue;
3909 final String nodeName = parser.getName();
3910 if (NODE_IMI.equals(nodeName)) {
3911 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3912 if (TextUtils.isEmpty(currentImiId)) {
3913 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3914 continue;
3915 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003916 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003917 allSubtypes.put(currentImiId, tempSubtypesArray);
3918 } else if (NODE_SUBTYPE.equals(nodeName)) {
3919 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3920 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3921 continue;
3922 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003923 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003924 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003925 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003926 parser.getAttributeValue(null, ATTR_LABEL));
3927 final String imeSubtypeLocale =
3928 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003929 final String languageTag =
3930 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003931 final String imeSubtypeMode =
3932 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3933 final String imeSubtypeExtraValue =
3934 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003935 final boolean isAuxiliary = "1".equals(String.valueOf(
3936 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003937 final boolean isAsciiCapable = "1".equals(String.valueOf(
3938 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003939 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003940 .setSubtypeNameResId(label)
3941 .setSubtypeIconResId(icon)
3942 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003943 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003944 .setSubtypeMode(imeSubtypeMode)
3945 .setSubtypeExtraValue(imeSubtypeExtraValue)
3946 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003947 .setIsAsciiCapable(isAsciiCapable);
3948 final String subtypeIdString =
3949 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3950 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003951 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003952 }
3953 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003954 }
3955 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003956 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3957 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003958 return;
satoke7c6998e2011-06-03 17:57:59 +09003959 }
3960 }
3961 }
3962
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003963 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3964 @NonNull
3965 private final Handler mHandler;
3966
3967 LocalServiceImpl(@NonNull final Handler handler) {
3968 mHandler = handler;
3969 }
3970
3971 @Override
3972 public void setInteractive(boolean interactive) {
3973 // Do everything in handler so as not to block the caller.
3974 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3975 interactive ? 1 : 0, 0));
3976 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003977
3978 @Override
3979 public void switchInputMethod(boolean forwardDirection) {
3980 // Do everything in handler so as not to block the caller.
3981 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3982 forwardDirection ? 1 : 0, 0));
3983 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003984
3985 @Override
3986 public void hideCurrentInputMethod() {
3987 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3988 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3989 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003990 }
3991
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003992 private static String imeWindowStatusToString(final int imeWindowVis) {
3993 final StringBuilder sb = new StringBuilder();
3994 boolean first = true;
3995 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3996 sb.append("Active");
3997 first = false;
3998 }
3999 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4000 if (!first) {
4001 sb.append("|");
4002 }
4003 sb.append("Visible");
4004 }
4005 return sb.toString();
4006 }
4007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004009 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4010 @Nullable Uri contentUri, @Nullable String packageName) {
4011 if (!calledFromValidUser()) {
4012 return null;
4013 }
4014
4015 if (token == null) {
4016 throw new NullPointerException("token");
4017 }
4018 if (packageName == null) {
4019 throw new NullPointerException("packageName");
4020 }
4021 if (contentUri == null) {
4022 throw new NullPointerException("contentUri");
4023 }
4024 final String contentUriScheme = contentUri.getScheme();
4025 if (!"content".equals(contentUriScheme)) {
4026 throw new InvalidParameterException("contentUri must have content scheme");
4027 }
4028
4029 synchronized (mMethodMap) {
4030 final int uid = Binder.getCallingUid();
4031 if (mCurMethodId == null) {
4032 return null;
4033 }
4034 if (mCurToken != token) {
4035 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4036 + " token=" + token);
4037 return null;
4038 }
4039 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4040 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4041 // nature of our system. Let's compare it with our internal record.
4042 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4043 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4044 + mCurAttribute.packageName + " packageName=" + packageName);
4045 return null;
4046 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004047 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004048 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004049 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004050 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004051 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4052 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4053 imeUserId);
4054 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4055 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4056 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4057 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4058 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4059 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4060 // actually allowed to "uid", which is guaranteed to be the IME's one.
4061 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4062 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004063 }
4064 }
4065
4066 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004067 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4068 if (!calledFromValidUser()) {
4069 return;
4070 }
4071 synchronized (mMethodMap) {
4072 if (!calledWithValidToken(token)) {
4073 return;
4074 }
4075 if (mCurClient != null && mCurClient.client != null) {
4076 mInFullscreenMode = fullscreen;
4077 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4078 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4079 }
4080 }
4081 }
4082
4083 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4085 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4086 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4089 + Binder.getCallingPid()
4090 + ", uid=" + Binder.getCallingUid());
4091 return;
4092 }
4093
4094 IInputMethod method;
4095 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004096 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 synchronized (mMethodMap) {
4101 p.println("Current Input Method Manager state:");
4102 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004103 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 for (int i=0; i<N; i++) {
4105 InputMethodInfo info = mMethodList.get(i);
4106 p.println(" InputMethod #" + i + ":");
4107 info.dump(p, " ");
4108 }
4109 p.println(" Clients:");
4110 for (ClientState ci : mClients.values()) {
4111 p.println(" Client " + ci + ":");
4112 p.println(" client=" + ci.client);
4113 p.println(" inputContext=" + ci.inputContext);
4114 p.println(" sessionRequested=" + ci.sessionRequested);
4115 p.println(" curSession=" + ci.curSession);
4116 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004117 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004119 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4120 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004121 focusedWindowClient = mCurFocusedWindowClient;
4122 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4124 + " mBoundToMethod=" + mBoundToMethod);
4125 p.println(" mCurToken=" + mCurToken);
4126 p.println(" mCurIntent=" + mCurIntent);
4127 method = mCurMethod;
4128 p.println(" mCurMethod=" + mCurMethod);
4129 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004130 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 p.println(" mShowRequested=" + mShowRequested
4132 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4133 + " mShowForced=" + mShowForced
4134 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004135 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004136 p.println(" mCurUserActionNotificationSequenceNumber="
4137 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004138 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004139 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004140 p.println(" mSwitchingController:");
4141 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004142 p.println(" mSettings:");
4143 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004145
Jeff Brownb88102f2010-09-08 11:49:43 -07004146 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 pw.flush();
4149 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004150 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4151 } catch (IOException | RemoteException e) {
4152 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004154 } else {
4155 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004157
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004158 if (focusedWindowClient != null && client != focusedWindowClient) {
4159 p.println(" ");
4160 p.println("Warning: Current input method client doesn't match the last focused. "
4161 + "window.");
4162 p.println("Dumping input method client in the last focused window just in case.");
4163 p.println(" ");
4164 pw.flush();
4165 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004166 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4167 } catch (IOException | RemoteException e) {
4168 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004169 }
4170 }
4171
Jeff Brownb88102f2010-09-08 11:49:43 -07004172 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 pw.flush();
4175 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004176 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4177 } catch (IOException | RemoteException e) {
4178 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004180 } else {
4181 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 }
4183 }
4184}