blob: 1dadcf5908cd18206044d9733c9ec567f033c4b8 [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
Yohei Yukawa69e68022017-02-13 12:04:50 -08001859 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001860 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08001861 info = mStartInputMap.get(startInputToken);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001862 if (info == null) {
1863 throw new InvalidParameterException("Unknown startInputToken=" + startInputToken);
1864 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001865 mImeWindowVis = vis;
1866 mBackDisposition = backDisposition;
1867 updateSystemUiLocked(token, vis, backDisposition);
1868 }
Yohei Yukawa69e68022017-02-13 12:04:50 -08001869 mWindowManagerInternal.updateInputMethodWindowStatus(info.mImeToken,
1870 (vis & InputMethodService.IME_VISIBLE) != 0, info.mTargetWindow);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001871 }
1872
1873 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1874 synchronized (mMethodMap) {
1875 updateSystemUiLocked(token, vis, backDisposition);
1876 }
1877 }
1878
1879 // Caution! This method is called in this class. Handle multi-user carefully
1880 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1881 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001882 return;
1883 }
1884
1885 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1886 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001887 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001888 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001889 // apply policy for binder calls
1890 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1891 vis = 0;
satok06487a52010-10-29 11:37:18 +09001892 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001893 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1894 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1895 if (mStatusBar != null) {
1896 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1897 needsToShowImeSwitcher);
1898 }
1899 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1900 if (imi != null && needsToShowImeSwitcher) {
1901 // Used to load label
1902 final CharSequence title = mRes.getText(
1903 com.android.internal.R.string.select_input_method);
1904 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1905 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001906 mImeSwitcherNotification.setContentTitle(title)
1907 .setContentText(summary)
1908 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001909 try {
1910 if ((mNotificationManager != null)
1911 && !mIWindowManager.hasNavigationBar()) {
1912 if (DEBUG) {
1913 Slog.d(TAG, "--- show notification: label = " + summary);
1914 }
1915 mNotificationManager.notifyAsUser(null,
1916 com.android.internal.R.string.select_input_method,
1917 mImeSwitcherNotification.build(), UserHandle.ALL);
1918 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001919 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001920 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001921 }
1922 } else {
1923 if (mNotificationShown && mNotificationManager != null) {
1924 if (DEBUG) {
1925 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001926 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001927 mNotificationManager.cancelAsUser(null,
1928 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1929 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001930 }
satok06487a52010-10-29 11:37:18 +09001931 }
1932 } finally {
1933 Binder.restoreCallingIdentity(ident);
1934 }
1935 }
1936
satoke7c6998e2011-06-03 17:57:59 +09001937 @Override
satokf9f01002011-05-19 21:31:50 +09001938 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001939 if (!calledFromValidUser()) {
1940 return;
1941 }
satokf9f01002011-05-19 21:31:50 +09001942 synchronized (mMethodMap) {
1943 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1944 for (int i = 0; i < spans.length; ++i) {
1945 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001946 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001947 mSecureSuggestionSpans.put(ss, currentImi);
1948 }
1949 }
1950 }
1951 }
1952
satoke7c6998e2011-06-03 17:57:59 +09001953 @Override
satokf9f01002011-05-19 21:31:50 +09001954 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001955 if (!calledFromValidUser()) {
1956 return false;
1957 }
satokf9f01002011-05-19 21:31:50 +09001958 synchronized (mMethodMap) {
1959 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1960 // TODO: Do not send the intent if the process of the targetImi is already dead.
1961 if (targetImi != null) {
1962 final String[] suggestions = span.getSuggestions();
1963 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001964 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001965 final Intent intent = new Intent();
1966 // Ensures that only a class in the original IME package will receive the
1967 // notification.
satok42c5a162011-05-26 16:46:14 +09001968 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001969 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1970 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1971 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1972 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001973 final long ident = Binder.clearCallingIdentity();
1974 try {
1975 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1976 } finally {
1977 Binder.restoreCallingIdentity(ident);
1978 }
satokf9f01002011-05-19 21:31:50 +09001979 return true;
1980 }
1981 }
1982 return false;
1983 }
1984
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001985 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001986 updateInputMethodsFromSettingsLocked(enabledMayChange);
1987 updateKeyboardFromSettingsLocked();
1988 }
1989
1990 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001991 if (enabledMayChange) {
1992 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1993 for (int i=0; i<enabled.size(); i++) {
1994 // We allow the user to select "disabled until used" apps, so if they
1995 // are enabling one of those here we now need to make it enabled.
1996 InputMethodInfo imm = enabled.get(i);
1997 try {
1998 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1999 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2000 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002001 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002002 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002003 if (DEBUG) {
2004 Slog.d(TAG, "Update state(" + imm.getId()
2005 + "): DISABLED_UNTIL_USED -> DEFAULT");
2006 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002007 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2008 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002009 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2010 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002011 }
2012 } catch (RemoteException e) {
2013 }
2014 }
2015 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002016 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2017 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2018 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2019 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002021 // There is no input method selected, try to choose new applicable input method.
2022 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002023 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002024 }
2025 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002027 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002029 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002030 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 }
satokf3db1af2010-11-23 13:34:33 +09002032 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002033 } else {
2034 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002035 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002037 // Here is not the perfect place to reset the switching controller. Ideally
2038 // mSwitchingController and mSettings should be able to share the same state.
2039 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2040 // the same enabled IMEs list.
2041 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002042
2043 }
2044
2045 public void updateKeyboardFromSettingsLocked() {
2046 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2047 if (mSwitchingDialog != null
2048 && mSwitchingDialogTitleView != null
2049 && mSwitchingDialog.isShowing()) {
2050 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2051 com.android.internal.R.id.hard_keyboard_switch);
2052 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002055
Yohei Yukawab097b822015-12-01 10:43:08 -08002056 private void notifyInputMethodSubtypeChanged(final int userId,
2057 @Nullable final InputMethodInfo inputMethodInfo,
2058 @Nullable final InputMethodSubtype subtype) {
2059 final InputManagerInternal inputManagerInternal =
2060 LocalServices.getService(InputManagerInternal.class);
2061 if (inputManagerInternal != null) {
2062 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2063 }
2064 }
2065
satokab751aa2010-09-14 19:17:36 +09002066 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 InputMethodInfo info = mMethodMap.get(id);
2068 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002069 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002071
satokd81e9502012-05-21 12:58:45 +09002072 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002074 final int subtypeCount = info.getSubtypeCount();
2075 if (subtypeCount <= 0) {
2076 return;
satokcd7cd292010-11-20 15:46:23 +09002077 }
satokd81e9502012-05-21 12:58:45 +09002078 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2079 final InputMethodSubtype newSubtype;
2080 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2081 newSubtype = info.getSubtypeAt(subtypeId);
2082 } else {
2083 // If subtype is null, try to find the most applicable one from
2084 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002085 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002086 }
2087 if (newSubtype == null || oldSubtype == null) {
2088 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2089 + ", new subtype = " + newSubtype);
2090 return;
2091 }
2092 if (newSubtype != oldSubtype) {
2093 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2094 if (mCurMethod != null) {
2095 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002096 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002097 mCurMethod.changeInputMethodSubtype(newSubtype);
2098 } catch (RemoteException e) {
2099 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002100 return;
satokab751aa2010-09-14 19:17:36 +09002101 }
2102 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002103 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 return;
2106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002107
satokd81e9502012-05-21 12:58:45 +09002108 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 final long ident = Binder.clearCallingIdentity();
2110 try {
satokab751aa2010-09-14 19:17:36 +09002111 // Set a subtype to this input method.
2112 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002113 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2114 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2115 // because mCurMethodId is stored as a history in
2116 // setSelectedInputMethodAndSubtypeLocked().
2117 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002119 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002121 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002123 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002125 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 } finally {
2127 Binder.restoreCallingIdentity(ident);
2128 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002129
2130 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2131 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002133
satok42c5a162011-05-26 16:46:14 +09002134 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002135 public boolean showSoftInput(IInputMethodClient client, int flags,
2136 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002137 if (!calledFromValidUser()) {
2138 return false;
2139 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002140 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 long ident = Binder.clearCallingIdentity();
2142 try {
2143 synchronized (mMethodMap) {
2144 if (mCurClient == null || client == null
2145 || mCurClient.client.asBinder() != client.asBinder()) {
2146 try {
2147 // We need to check if this is the current client with
2148 // focus in the window manager, to allow this call to
2149 // be made before input is started in it.
2150 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002151 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002155 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 }
2157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002158
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002159 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002160 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
2162 } finally {
2163 Binder.restoreCallingIdentity(ident);
2164 }
2165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002166
The Android Open Source Project4df24232009-03-05 14:34:35 -08002167 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002169 if (mAccessibilityRequestingNoSoftKeyboard) {
2170 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
Anna Galusza9b278112016-01-04 11:37:37 -08002172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2174 mShowExplicitlyRequested = true;
2175 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002176 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2177 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002179
Dianne Hackborncc278702009-09-02 23:07:23 -07002180 if (!mSystemReady) {
2181 return false;
2182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002183
The Android Open Source Project4df24232009-03-05 14:34:35 -08002184 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002186 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2188 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2189 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002191 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002192 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002193 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002194 | Context.BIND_TREAT_LIKE_ACTIVITY
2195 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002196 mVisibleBound = true;
2197 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002198 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002200 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 // The client has asked to have the input method shown, but
2202 // we have been sitting here too long with a connection to the
2203 // service and no interface received, so let's disconnect/connect
2204 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002205 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002207 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002209 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002210 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002211 } else {
2212 if (DEBUG) {
2213 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2214 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
The Android Open Source Project4df24232009-03-05 14:34:35 -08002218 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002220
satok42c5a162011-05-26 16:46:14 +09002221 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222 public boolean hideSoftInput(IInputMethodClient client, int flags,
2223 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002224 if (!calledFromValidUser()) {
2225 return false;
2226 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002227 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 long ident = Binder.clearCallingIdentity();
2229 try {
2230 synchronized (mMethodMap) {
2231 if (mCurClient == null || client == null
2232 || mCurClient.client.asBinder() != client.asBinder()) {
2233 try {
2234 // We need to check if this is the current client with
2235 // focus in the window manager, to allow this call to
2236 // be made before input is started in it.
2237 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002238 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2239 + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002243 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 }
2245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002246
Joe Onorato8a9b2202010-02-26 18:56:32 -08002247 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002248 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 } finally {
2251 Binder.restoreCallingIdentity(ident);
2252 }
2253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002254
The Android Open Source Project4df24232009-03-05 14:34:35 -08002255 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2257 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002258 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 -08002259 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 }
2261 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002262 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 -08002263 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002265
2266 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2267 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2268 // to be updated with the new value sent from IME process. Even in such a transient state
2269 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2270 // application process as a valid request, and have even promised such a behavior with CTS
2271 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2272 // IMMS#InputShown indicates that the software keyboard is shown.
2273 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2274 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2275 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002276 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002277 if (shouldHideSoftInput) {
2278 // The IME will report its visible state again after the following message finally
2279 // delivered to the IME process as an IPC. Hence the inconsistency between
2280 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2281 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002282 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2283 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2284 res = true;
2285 } else {
2286 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002288 if (mHaveConnection && mVisibleBound) {
2289 mContext.unbindService(mVisibleConnection);
2290 mVisibleBound = false;
2291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 mInputShown = false;
2293 mShowRequested = false;
2294 mShowExplicitlyRequested = false;
2295 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002296 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002298
satok42c5a162011-05-26 16:46:14 +09002299 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002300 public InputBindResult startInputOrWindowGainedFocus(
2301 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2302 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002303 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002304 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002305 if (windowToken != null) {
2306 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002307 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002308 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002309 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2310 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002311 }
2312 }
2313
2314 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002315 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2316 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002317 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2318 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002319 // Needs to check the validity before clearing calling identity
2320 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002321 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 long ident = Binder.clearCallingIdentity();
2323 try {
2324 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002325 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2326 + InputMethodClient.getStartInputReason(startInputReason)
2327 + " client=" + client.asBinder()
2328 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002329 + " missingMethods="
2330 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002331 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002332 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002334 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002335
Dianne Hackborn7663d802012-02-24 13:08:49 -08002336 ClientState cs = mClients.get(client.asBinder());
2337 if (cs == null) {
2338 throw new IllegalArgumentException("unknown client "
2339 + client.asBinder());
2340 }
2341
2342 try {
2343 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2344 // Check with the window manager to make sure this client actually
2345 // has a window with focus. If not, reject. This is thread safe
2346 // because if the focus changes some time before or after, the
2347 // next client receiving focus that has any interest in input will
2348 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002349 if (DEBUG) {
2350 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2351 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2352 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002353 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002355 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002357
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002358 if (!calledFromValidUser) {
2359 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2360 Slog.w(TAG, "If you want to interect with IME, you need "
2361 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2362 hideCurrentInputLocked(0, null);
2363 return null;
2364 }
2365
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002366 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002367 if (DEBUG) {
2368 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2369 + " attribute=" + attribute + ", token = " + windowToken);
2370 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002371 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002372 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002373 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002374 }
2375 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002376 }
2377 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002378 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002379
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002380 // Should we auto-show the IME even if the caller has not
2381 // specified what should be done with it?
2382 // We only do this automatically if the window can resize
2383 // to accommodate the IME (so what the user sees will give
2384 // them good context without input information being obscured
2385 // by the IME) or if running on a large screen where there
2386 // is more room for the target window + IME.
2387 final boolean doAutoShow =
2388 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2389 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2390 || mRes.getConfiguration().isLayoutSizeAtLeast(
2391 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002392 final boolean isTextEditor =
2393 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2394
2395 // We want to start input before showing the IME, but after closing
2396 // it. We want to do this after closing it to help the IME disappear
2397 // more quickly (not get stuck behind it initializing itself for the
2398 // new focused input, even if its window wants to hide the IME).
2399 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2402 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002403 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2405 // There is no focus view, and this window will
2406 // be behind any soft input window, so hide the
2407 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002408 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002409 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002411 } else if (isTextEditor && doAutoShow && (softInputMode &
2412 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 // There is a focus view, and we are navigating forward
2414 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002415 // We only do this automatically if the window can resize
2416 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002417 // them good context without input information being obscured
2418 // by the IME) or if running on a large screen where there
2419 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002420 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002421 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002422 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002423 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002424 didStart = true;
2425 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002426 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428 break;
2429 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2430 // Do nothing.
2431 break;
2432 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2433 if ((softInputMode &
2434 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002435 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002436 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
2438 break;
2439 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002440 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002441 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 break;
2443 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2444 if ((softInputMode &
2445 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002446 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002447 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002448 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002449 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002450 didStart = true;
2451 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002452 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 }
2454 break;
2455 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002456 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002457 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002458 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002459 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002460 didStart = true;
2461 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002462 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 break;
2464 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002465
2466 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002467 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002468 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 }
2471 } finally {
2472 Binder.restoreCallingIdentity(ident);
2473 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002474
2475 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002477
satok42c5a162011-05-26 16:46:14 +09002478 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002479 public void showInputMethodPickerFromClient(
2480 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002481 if (!calledFromValidUser()) {
2482 return;
2483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 synchronized (mMethodMap) {
2485 if (mCurClient == null || client == null
2486 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002487 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002488 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 }
2490
satok440aab52010-11-25 09:43:11 +09002491 // Always call subtype picker, because subtype picker is a superset of input method
2492 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002493 mHandler.sendMessage(mCaller.obtainMessageI(
2494 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002495 }
2496 }
2497
satok42c5a162011-05-26 16:46:14 +09002498 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002500 if (!calledFromValidUser()) {
2501 return;
2502 }
satok28203512010-11-24 11:06:49 +09002503 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2504 }
2505
satok42c5a162011-05-26 16:46:14 +09002506 @Override
satok28203512010-11-24 11:06:49 +09002507 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002508 if (!calledFromValidUser()) {
2509 return;
2510 }
satok28203512010-11-24 11:06:49 +09002511 synchronized (mMethodMap) {
2512 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002513 setInputMethodWithSubtypeIdLocked(token, id,
2514 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2515 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002516 } else {
2517 setInputMethod(token, id);
2518 }
2519 }
satokab751aa2010-09-14 19:17:36 +09002520 }
2521
satok42c5a162011-05-26 16:46:14 +09002522 @Override
satokb416a712010-11-25 20:42:14 +09002523 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002524 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002525 if (!calledFromValidUser()) {
2526 return;
2527 }
satokb416a712010-11-25 20:42:14 +09002528 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002529 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2530 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002531 }
2532 }
2533
satok4fc87d62011-05-20 16:13:43 +09002534 @Override
satok735cf382010-11-11 20:40:09 +09002535 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002536 if (!calledFromValidUser()) {
2537 return false;
2538 }
satok735cf382010-11-11 20:40:09 +09002539 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002540 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002541 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002542 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002543 lastImi = mMethodMap.get(lastIme.first);
2544 } else {
2545 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002546 }
satok4fc87d62011-05-20 16:13:43 +09002547 String targetLastImiId = null;
2548 int subtypeId = NOT_A_SUBTYPE_ID;
2549 if (lastIme != null && lastImi != null) {
2550 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002551 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002552 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2553 : mCurrentSubtype.hashCode();
2554 // If the last IME is the same as the current IME and the last subtype is not
2555 // defined, there is no need to switch to the last IME.
2556 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2557 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002558 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002559 }
2560 }
2561
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002562 if (TextUtils.isEmpty(targetLastImiId)
2563 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002564 // This is a safety net. If the currentSubtype can't be added to the history
2565 // and the framework couldn't find the last ime, we will make the last ime be
2566 // the most applicable enabled keyboard subtype of the system imes.
2567 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2568 if (enabled != null) {
2569 final int N = enabled.size();
2570 final String locale = mCurrentSubtype == null
2571 ? mRes.getConfiguration().locale.toString()
2572 : mCurrentSubtype.getLocale();
2573 for (int i = 0; i < N; ++i) {
2574 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002575 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002576 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002577 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2578 InputMethodUtils.getSubtypes(imi),
2579 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002580 if (keyboardSubtype != null) {
2581 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002582 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002583 imi, keyboardSubtype.hashCode());
2584 if(keyboardSubtype.getLocale().equals(locale)) {
2585 break;
2586 }
2587 }
2588 }
2589 }
2590 }
2591 }
2592
2593 if (!TextUtils.isEmpty(targetLastImiId)) {
2594 if (DEBUG) {
2595 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2596 + ", from: " + mCurMethodId + ", " + subtypeId);
2597 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002598 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002599 return true;
2600 } else {
2601 return false;
2602 }
satok735cf382010-11-11 20:40:09 +09002603 }
2604 }
2605
satoke7c6998e2011-06-03 17:57:59 +09002606 @Override
satok688bd472012-02-09 20:09:17 +09002607 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002608 if (!calledFromValidUser()) {
2609 return false;
2610 }
satok688bd472012-02-09 20:09:17 +09002611 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002612 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002613 return false;
2614 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002615 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002616 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2617 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002618 if (nextSubtype == null) {
2619 return false;
2620 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002621 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2622 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002623 return true;
2624 }
2625 }
2626
2627 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002628 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2629 if (!calledFromValidUser()) {
2630 return false;
2631 }
2632 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002633 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002634 return false;
2635 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002636 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002637 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2638 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002639 if (nextSubtype == null) {
2640 return false;
2641 }
2642 return true;
2643 }
2644 }
2645
2646 @Override
satok68f1b782011-04-11 14:26:04 +09002647 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002648 if (!calledFromValidUser()) {
2649 return null;
2650 }
satok68f1b782011-04-11 14:26:04 +09002651 synchronized (mMethodMap) {
2652 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2653 // TODO: Handle the case of the last IME with no subtypes
2654 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2655 || TextUtils.isEmpty(lastIme.second)) return null;
2656 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2657 if (lastImi == null) return null;
2658 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002659 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002660 final int lastSubtypeId =
2661 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002662 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2663 return null;
2664 }
2665 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002666 } catch (NumberFormatException e) {
2667 return null;
2668 }
2669 }
2670 }
2671
satoke7c6998e2011-06-03 17:57:59 +09002672 @Override
satokee5e77c2011-09-02 18:50:15 +09002673 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002674 if (!calledFromValidUser()) {
2675 return;
2676 }
satok91e88122011-07-18 11:11:42 +09002677 // By this IPC call, only a process which shares the same uid with the IME can add
2678 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002679 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002680 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002681 if (!mSystemReady) {
2682 return;
2683 }
satok91e88122011-07-18 11:11:42 +09002684 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002685 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002686 final String[] packageInfos;
2687 try {
2688 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2689 } catch (RemoteException e) {
2690 Slog.e(TAG, "Failed to get package infos");
2691 return;
2692 }
satok91e88122011-07-18 11:11:42 +09002693 if (packageInfos != null) {
2694 final int packageNum = packageInfos.length;
2695 for (int i = 0; i < packageNum; ++i) {
2696 if (packageInfos[i].equals(imi.getPackageName())) {
2697 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002698 final long ident = Binder.clearCallingIdentity();
2699 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002700 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002701 } finally {
2702 Binder.restoreCallingIdentity(ident);
2703 }
satokee5e77c2011-09-02 18:50:15 +09002704 return;
satok91e88122011-07-18 11:11:42 +09002705 }
2706 }
2707 }
satoke7c6998e2011-06-03 17:57:59 +09002708 }
satokee5e77c2011-09-02 18:50:15 +09002709 return;
satoke7c6998e2011-06-03 17:57:59 +09002710 }
2711
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002712 @Override
2713 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002714 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002715 }
2716
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002717 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002718 public void clearLastInputMethodWindowForTransition(IBinder token) {
2719 if (!calledFromValidUser()) {
2720 return;
2721 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002722 synchronized (mMethodMap) {
2723 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002724 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002725 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002726 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002727 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002728 }
2729
2730 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002731 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002732 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002733 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002734 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002735 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002736 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2737 if (DEBUG) {
2738 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2739 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2740 + " actual: " + sequenceNumber);
2741 }
2742 return;
2743 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002744 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2745 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002746 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002747 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002748 }
2749 }
2750
satok28203512010-11-24 11:06:49 +09002751 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002753 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2754 }
2755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002757 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2758 if (token == null) {
2759 if (mContext.checkCallingOrSelfPermission(
2760 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2761 != PackageManager.PERMISSION_GRANTED) {
2762 throw new SecurityException(
2763 "Using null token requires permission "
2764 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002766 } else if (mCurToken != token) {
2767 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2768 + " token: " + token);
2769 return;
2770 }
2771
2772 final long ident = Binder.clearCallingIdentity();
2773 try {
2774 setInputMethodLocked(id, subtypeId);
2775 } finally {
2776 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 }
2778 }
2779
satok42c5a162011-05-26 16:46:14 +09002780 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002782 if (!calledFromValidUser()) {
2783 return;
2784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002786 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 return;
2788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 long ident = Binder.clearCallingIdentity();
2790 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002791 hideCurrentInputLocked(flags, null);
2792 } finally {
2793 Binder.restoreCallingIdentity(ident);
2794 }
2795 }
2796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002797
satok42c5a162011-05-26 16:46:14 +09002798 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002799 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002800 if (!calledFromValidUser()) {
2801 return;
2802 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002803 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002804 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002805 return;
2806 }
2807 long ident = Binder.clearCallingIdentity();
2808 try {
2809 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 } finally {
2811 Binder.restoreCallingIdentity(ident);
2812 }
2813 }
2814 }
2815
2816 void setEnabledSessionInMainThread(SessionState session) {
2817 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002818 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002820 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002821 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 } catch (RemoteException e) {
2823 }
2824 }
2825 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002826 if (mEnabledSession != null && mEnabledSession.session != null) {
2827 try {
2828 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2829 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2830 } catch (RemoteException e) {
2831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 }
2833 }
2834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002835
satok42c5a162011-05-26 16:46:14 +09002836 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002838 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002840 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002841 final boolean showAuxSubtypes;
2842 switch (msg.arg1) {
2843 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2844 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2845 // implemented so that auxiliary subtypes will be excluded when the soft
2846 // keyboard is invisible.
2847 showAuxSubtypes = mInputShown;
2848 break;
2849 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2850 showAuxSubtypes = true;
2851 break;
2852 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2853 showAuxSubtypes = false;
2854 break;
2855 default:
2856 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2857 return false;
2858 }
2859 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002860 return true;
2861
satok47a44912010-10-06 16:03:58 +09002862 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002863 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002864 return true;
2865
2866 case MSG_SHOW_IM_CONFIG:
2867 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002868 return true;
2869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 case MSG_UNBIND_INPUT:
2873 try {
2874 ((IInputMethod)msg.obj).unbindInput();
2875 } catch (RemoteException e) {
2876 // There is nothing interesting about the method dying.
2877 }
2878 return true;
2879 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002880 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 try {
2882 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2883 } catch (RemoteException e) {
2884 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002885 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 return true;
2887 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002888 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002890 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002891 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002892 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 } catch (RemoteException e) {
2894 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002895 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 return true;
2897 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002898 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002900 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002901 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002902 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 } catch (RemoteException e) {
2904 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002905 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002907 case MSG_HIDE_CURRENT_INPUT_METHOD:
2908 synchronized (mMethodMap) {
2909 hideCurrentInputLocked(0, null);
2910 }
2911 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002913 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002915 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2917 } catch (RemoteException e) {
2918 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002919 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002921 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002922 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002923 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002924 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002926 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002928 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002929 // Dispose the channel if the input method is not local to this process
2930 // because the remote proxy will get its own copy when unparceled.
2931 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002932 channel.dispose();
2933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002935 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002939
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002940 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08002941 final int missingMethods = msg.arg1;
2942 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002943 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002944 final IBinder startInputToken = (IBinder) args.arg1;
2945 final SessionState session = (SessionState) args.arg2;
2946 final IInputContext inputContext = (IInputContext) args.arg3;
2947 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002950 session.method.startInput(startInputToken, inputContext, missingMethods,
2951 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 } catch (RemoteException e) {
2953 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002954 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002959
Yohei Yukawa33e81792015-11-17 21:14:42 -08002960 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002962 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 } catch (RemoteException e) {
2964 // There is nothing interesting about the last client dying.
2965 }
2966 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002967 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002968 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002969 IInputMethodClient client = (IInputMethodClient)args.arg1;
2970 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002972 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002974 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002975 } finally {
2976 // Dispose the channel if the input method is not local to this process
2977 // because the remote proxy will get its own copy when unparceled.
2978 if (res.channel != null && Binder.isProxy(client)) {
2979 res.channel.dispose();
2980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002982 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002984 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002985 case MSG_SET_ACTIVE:
2986 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002987 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002988 } catch (RemoteException e) {
2989 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2990 + ((ClientState)msg.obj).pid + " uid "
2991 + ((ClientState)msg.obj).uid);
2992 }
2993 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002994 case MSG_SET_INTERACTIVE:
2995 handleSetInteractive(msg.arg1 != 0);
2996 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002997 case MSG_SWITCH_IME:
2998 handleSwitchInputMethod(msg.arg1 != 0);
2999 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003000 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3001 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003002 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003003 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003004 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003005 } catch (RemoteException e) {
3006 Slog.w(TAG, "Got RemoteException sending "
3007 + "setUserActionNotificationSequenceNumber("
3008 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003009 + clientState.pid + " uid "
3010 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003011 }
3012 return true;
3013 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003014 case MSG_REPORT_FULLSCREEN_MODE: {
3015 final boolean fullscreen = msg.arg1 != 0;
3016 final ClientState clientState = (ClientState)msg.obj;
3017 try {
3018 clientState.client.reportFullscreenMode(fullscreen);
3019 } catch (RemoteException e) {
3020 Slog.w(TAG, "Got RemoteException sending "
3021 + "reportFullscreen(" + fullscreen + ") notification to pid="
3022 + clientState.pid + " uid=" + clientState.uid);
3023 }
3024 return true;
3025 }
satok01038492012-04-09 21:08:27 +09003026
3027 // --------------------------------------------------------------
3028 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003029 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003030 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003031 case MSG_SYSTEM_UNLOCK_USER:
3032 final int userId = msg.arg1;
3033 onUnlockUser(userId);
3034 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 }
3036 return false;
3037 }
3038
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003039 private void handleSetInteractive(final boolean interactive) {
3040 synchronized (mMethodMap) {
3041 mIsInteractive = interactive;
3042 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3043
3044 // Inform the current client of the change in active status
3045 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003046 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3047 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3048 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003049 }
3050 }
3051 }
3052
Yohei Yukawaae61f712015-12-09 13:00:10 -08003053 private void handleSwitchInputMethod(final boolean forwardDirection) {
3054 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003055 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003056 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003057 if (nextSubtype == null) {
3058 return;
3059 }
3060 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003061 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3062 if (newInputMethodInfo == null) {
3063 return;
3064 }
3065 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3066 newInputMethodInfo, mCurrentSubtype);
3067 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003068 if (mSubtypeSwitchedByShortCutToast == null) {
3069 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3070 Toast.LENGTH_SHORT);
3071 } else {
3072 mSubtypeSwitchedByShortCutToast.setText(toastText);
3073 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003074 mSubtypeSwitchedByShortCutToast.show();
3075 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003076 }
3077 }
3078
satokdc9ddae2011-10-06 12:22:36 +09003079 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003080 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3081 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003082 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003083 if (DEBUG) {
3084 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3085 }
satok723a27e2010-11-11 14:58:11 +09003086 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003087 return true;
3088 }
3089
3090 return false;
3091 }
3092
Yohei Yukawa94e33302016-02-12 19:37:03 -08003093 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003094 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003095 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003096 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003097 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003098 if (!mSystemReady) {
3099 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3100 return;
3101 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003102 mMethodList.clear();
3103 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003104 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003105 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003106
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003107 // Use for queryIntentServicesAsUser
3108 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003110 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3111 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3112 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003113 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003115 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3116 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003117
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003118 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003119 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 for (int i = 0; i < services.size(); ++i) {
3121 ResolveInfo ri = services.get(i);
3122 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003123 final String imeId = InputMethodInfo.computeId(ri);
3124 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3125 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 + ": it does not require the permission "
3127 + android.Manifest.permission.BIND_INPUT_METHOD);
3128 continue;
3129 }
3130
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003131 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003133 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 try {
satoke7c6998e2011-06-03 17:57:59 +09003135 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003136 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003137 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003138 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139
3140 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003141 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003143 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003144 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 }
3146 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003147
Yohei Yukawac4e44912017-02-09 19:30:22 -08003148 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3149 // negatives when the package state remains to be the same but only the component state is
3150 // changed.
3151 {
3152 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3153 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3154 // conservative, but it seems we cannot use it for now (Issue 35176630).
3155 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3156 new Intent(InputMethod.SERVICE_INTERFACE),
3157 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3158 final int N = allInputMethodServices.size();
3159 for (int i = 0; i < N; ++i) {
3160 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3161 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3162 continue;
3163 }
3164 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3165 }
3166 }
3167
Yohei Yukawa859df052016-02-17 07:56:46 -08003168 // TODO: The following code should find better place to live.
3169 if (!resetDefaultEnabledIme) {
3170 boolean enabledImeFound = false;
3171 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3172 final int N = enabledImes.size();
3173 for (int i = 0; i < N; ++i) {
3174 final InputMethodInfo imi = enabledImes.get(i);
3175 if (mMethodList.contains(imi)) {
3176 enabledImeFound = true;
3177 break;
3178 }
3179 }
3180 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003181 if (DEBUG) {
3182 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3183 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003184 resetDefaultEnabledIme = true;
3185 resetSelectedInputMethodAndSubtypeLocked("");
3186 }
3187 }
3188
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003189 if (resetDefaultEnabledIme) {
3190 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003191 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003192 final int N = defaultEnabledIme.size();
3193 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003194 final InputMethodInfo imi = defaultEnabledIme.get(i);
3195 if (DEBUG) {
3196 Slog.d(TAG, "--- enable ime = " + imi);
3197 }
3198 setInputMethodEnabledLocked(imi.getId(), true);
3199 }
3200 }
3201
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003202 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003203 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003204 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003205 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3206 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003207 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003208 }
3209 } else {
3210 // Double check that the default IME is certainly enabled.
3211 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003212 }
3213 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003214 // Here is not the perfect place to reset the switching controller. Ideally
3215 // mSwitchingController and mSettings should be able to share the same state.
3216 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3217 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003218 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003222
satok217f5482010-12-15 05:19:19 +09003223 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003224 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003225 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003226 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3227 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003228 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003229 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003230 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003231 final int userId;
3232 synchronized (mMethodMap) {
3233 userId = mSettings.getCurrentUserId();
3234 }
3235 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003236 }
3237
3238 private void showConfigureInputMethods() {
3239 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3240 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3241 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3242 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003243 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003244 }
3245
satok2c93efc2012-04-02 19:33:47 +09003246 private boolean isScreenLocked() {
3247 return mKeyguardManager != null
3248 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3249 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003250
Seigo Nonaka14e13912015-05-06 21:04:13 -07003251 private void showInputMethodMenu(boolean showAuxSubtypes) {
3252 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003255 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003256
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003257 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003258 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003259 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003260
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003261 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003262 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003263 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3264 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003265 if (immis == null || immis.size() == 0) {
3266 return;
3267 }
3268
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003269 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270
satok688bd472012-02-09 20:09:17 +09003271 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003272 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003273 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003274
satokc3690562012-01-10 20:14:43 +09003275 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003276 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003277 if (currentSubtype != null) {
3278 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003279 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3280 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003281 }
3282 }
3283
Ken Wakasa761eb372011-03-04 19:06:18 +09003284 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003285 mIms = new InputMethodInfo[N];
3286 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003287 int checkedItem = 0;
3288 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003289 final ImeSubtypeListItem item = imList.get(i);
3290 mIms[i] = item.mImi;
3291 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003292 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003293 int subtypeId = mSubtypeIds[i];
3294 if ((subtypeId == NOT_A_SUBTYPE_ID)
3295 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3296 || (subtypeId == lastInputMethodSubtypeId)) {
3297 checkedItem = i;
3298 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003301
3302 final Context settingsContext = new ContextThemeWrapper(context,
3303 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3304
3305 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003306 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3307 @Override
3308 public void onCancel(DialogInterface dialog) {
3309 hideInputMethodMenu();
3310 }
3311 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003312
3313 final Context dialogContext = mDialogBuilder.getContext();
3314 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3315 com.android.internal.R.styleable.DialogPreference,
3316 com.android.internal.R.attr.alertDialogStyle, 0);
3317 final Drawable dialogIcon = a.getDrawable(
3318 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3319 a.recycle();
3320
3321 mDialogBuilder.setIcon(dialogIcon);
3322
Yohei Yukawad34e1482016-02-11 08:03:52 -08003323 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003324 final View tv = inflater.inflate(
3325 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3326 mDialogBuilder.setCustomTitle(tv);
3327
3328 // Setup layout for a toggle switch of the hardware keyboard
3329 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003330 mSwitchingDialogTitleView
3331 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003332 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003333 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003334 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003335 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003336 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003337 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3338 @Override
3339 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003340 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003341 // Ensure that the input method dialog is dismissed when changing
3342 // the hardware keyboard state.
3343 hideInputMethodMenu();
3344 }
3345 });
3346
Alan Viverette505e3ab2014-11-24 15:22:11 -08003347 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003348 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3349 final OnClickListener choiceListener = new OnClickListener() {
3350 @Override
3351 public void onClick(final DialogInterface dialog, final int which) {
3352 synchronized (mMethodMap) {
3353 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3354 || mSubtypeIds.length <= which) {
3355 return;
satok01038492012-04-09 21:08:27 +09003356 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003357 final InputMethodInfo im = mIms[which];
3358 int subtypeId = mSubtypeIds[which];
3359 adapter.mCheckedItem = which;
3360 adapter.notifyDataSetChanged();
3361 hideInputMethodMenu();
3362 if (im != null) {
3363 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3364 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003365 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003366 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003368 }
3369 }
3370 };
3371 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003374 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003375 final Window w = mSwitchingDialog.getWindow();
3376 final WindowManager.LayoutParams attrs = w.getAttributes();
3377 w.setType(TYPE_INPUT_METHOD_DIALOG);
3378 // Use an alternate token for the dialog for that window manager can group the token
3379 // with other IME windows based on type vs. grouping based on whichever token happens
3380 // to get selected by the system later on.
3381 attrs.token = mSwitchingDialogToken;
3382 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3383 attrs.setTitle("Select input method");
3384 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003385 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 mSwitchingDialog.show();
3387 }
3388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003389
Ken Wakasa05dbb652011-08-22 15:22:43 +09003390 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3391 private final LayoutInflater mInflater;
3392 private final int mTextViewResourceId;
3393 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003394 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003395 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3396 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3397 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003398
Ken Wakasa05dbb652011-08-22 15:22:43 +09003399 mTextViewResourceId = textViewResourceId;
3400 mItemsList = itemsList;
3401 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003402 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003403 }
3404
3405 @Override
3406 public View getView(int position, View convertView, ViewGroup parent) {
3407 final View view = convertView != null ? convertView
3408 : mInflater.inflate(mTextViewResourceId, null);
3409 if (position < 0 || position >= mItemsList.size()) return view;
3410 final ImeSubtypeListItem item = mItemsList.get(position);
3411 final CharSequence imeName = item.mImeName;
3412 final CharSequence subtypeName = item.mSubtypeName;
3413 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3414 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3415 if (TextUtils.isEmpty(subtypeName)) {
3416 firstTextView.setText(imeName);
3417 secondTextView.setVisibility(View.GONE);
3418 } else {
3419 firstTextView.setText(subtypeName);
3420 secondTextView.setText(imeName);
3421 secondTextView.setVisibility(View.VISIBLE);
3422 }
3423 final RadioButton radioButton =
3424 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3425 radioButton.setChecked(position == mCheckedItem);
3426 return view;
3427 }
3428 }
3429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003431 synchronized (mMethodMap) {
3432 hideInputMethodMenuLocked();
3433 }
3434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003435
The Android Open Source Project10592532009-03-18 17:39:46 -07003436 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003437 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438
The Android Open Source Project10592532009-03-18 17:39:46 -07003439 if (mSwitchingDialog != null) {
3440 mSwitchingDialog.dismiss();
3441 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003443
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003444 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003445 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003446 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003450
satok42c5a162011-05-26 16:46:14 +09003451 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003453 // TODO: Make this work even for non-current users?
3454 if (!calledFromValidUser()) {
3455 return false;
3456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 synchronized (mMethodMap) {
3458 if (mContext.checkCallingOrSelfPermission(
3459 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3460 != PackageManager.PERMISSION_GRANTED) {
3461 throw new SecurityException(
3462 "Requires permission "
3463 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3464 }
Anna Galusza9b278112016-01-04 11:37:37 -08003465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 long ident = Binder.clearCallingIdentity();
3467 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003468 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 } finally {
3470 Binder.restoreCallingIdentity(ident);
3471 }
3472 }
3473 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003474
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003475 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3476 // Make sure this is a valid input method.
3477 InputMethodInfo imm = mMethodMap.get(id);
3478 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003479 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003480 }
3481
satokd87c2592010-09-29 11:52:06 +09003482 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3483 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003484
satokd87c2592010-09-29 11:52:06 +09003485 if (enabled) {
3486 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3487 if (pair.first.equals(id)) {
3488 // We are enabling this input method, but it is already enabled.
3489 // Nothing to do. The previous state was enabled.
3490 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003491 }
3492 }
satokd87c2592010-09-29 11:52:06 +09003493 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3494 // Previous state was disabled.
3495 return false;
3496 } else {
3497 StringBuilder builder = new StringBuilder();
3498 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3499 builder, enabledInputMethodsList, id)) {
3500 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003501 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003502 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3503 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3504 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003505 }
3506 // Previous state was enabled.
3507 return true;
3508 } else {
3509 // We are disabling the input method but it is already disabled.
3510 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003511 return false;
3512 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003513 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003514 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003515
satok723a27e2010-11-11 14:58:11 +09003516 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3517 boolean setSubtypeOnly) {
3518 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003519 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003520
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003521 mCurUserActionNotificationSequenceNumber =
3522 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3523 if (DEBUG) {
3524 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3525 + mCurUserActionNotificationSequenceNumber);
3526 }
3527
3528 if (mCurClient != null && mCurClient.client != null) {
3529 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3530 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003531 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003532 }
3533
satok723a27e2010-11-11 14:58:11 +09003534 // Set Subtype here
3535 if (imi == null || subtypeId < 0) {
3536 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003537 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003538 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003539 if (subtypeId < imi.getSubtypeCount()) {
3540 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3541 mSettings.putSelectedSubtype(subtype.hashCode());
3542 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003543 } else {
3544 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003545 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003546 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003547 }
satokab751aa2010-09-14 19:17:36 +09003548 }
satok723a27e2010-11-11 14:58:11 +09003549
Yohei Yukawa68645a62016-02-17 07:54:20 -08003550 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003551 // Set InputMethod here
3552 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3553 }
3554 }
3555
3556 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3557 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3558 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3559 // newDefaultIme is empty when there is no candidate for the selected IME.
3560 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3561 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3562 if (subtypeHashCode != null) {
3563 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003564 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003565 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003566 } catch (NumberFormatException e) {
3567 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3568 }
3569 }
3570 }
3571 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003572 }
3573
satok4e4569d2010-11-19 18:45:53 +09003574 // If there are no selected shortcuts, tries finding the most applicable ones.
3575 private Pair<InputMethodInfo, InputMethodSubtype>
3576 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3577 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3578 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003579 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003580 boolean foundInSystemIME = false;
3581
3582 // Search applicable subtype for each InputMethodInfo
3583 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003584 final String imiId = imi.getId();
3585 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3586 continue;
3587 }
satokcd7cd292010-11-20 15:46:23 +09003588 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003589 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003590 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003591 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003592 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003593 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003594 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003595 }
satokdf31ae62011-01-15 06:19:44 +09003596 // 2. Search by the system locale from enabledSubtypes.
3597 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003598 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003599 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003600 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003601 }
satoka86f5e42011-09-02 17:12:42 +09003602 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003603 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003604 final ArrayList<InputMethodSubtype> subtypesForSearch =
3605 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003606 ? InputMethodUtils.getSubtypes(imi)
3607 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003608 // 4. Search by the current subtype's locale from all subtypes.
3609 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003610 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003611 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003612 }
3613 // 5. Search by the system locale from all subtypes.
3614 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003615 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003616 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003617 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003618 }
satokcd7cd292010-11-20 15:46:23 +09003619 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003620 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003621 // The current input method is the most applicable IME.
3622 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003623 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003624 break;
satok7599a7f2010-12-22 13:45:23 +09003625 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003626 // The system input method is 2nd applicable IME.
3627 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003628 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003629 if ((imi.getServiceInfo().applicationInfo.flags
3630 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3631 foundInSystemIME = true;
3632 }
satok4e4569d2010-11-19 18:45:53 +09003633 }
3634 }
3635 }
3636 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003637 if (mostApplicableIMI != null) {
3638 Slog.w(TAG, "Most applicable shortcut input method was:"
3639 + mostApplicableIMI.getId());
3640 if (mostApplicableSubtype != null) {
3641 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3642 + "," + mostApplicableSubtype.getMode() + ","
3643 + mostApplicableSubtype.getLocale());
3644 }
3645 }
satok4e4569d2010-11-19 18:45:53 +09003646 }
satokcd7cd292010-11-20 15:46:23 +09003647 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003648 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003649 } else {
3650 return null;
3651 }
3652 }
3653
satokab751aa2010-09-14 19:17:36 +09003654 /**
3655 * @return Return the current subtype of this input method.
3656 */
satok42c5a162011-05-26 16:46:14 +09003657 @Override
satokab751aa2010-09-14 19:17:36 +09003658 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003659 // TODO: Make this work even for non-current users?
3660 if (!calledFromValidUser()) {
3661 return null;
3662 }
3663 synchronized (mMethodMap) {
3664 return getCurrentInputMethodSubtypeLocked();
3665 }
3666 }
3667
3668 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003669 if (mCurMethodId == null) {
3670 return null;
3671 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003672 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003673 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3674 if (imi == null || imi.getSubtypeCount() == 0) {
3675 return null;
satok4e4569d2010-11-19 18:45:53 +09003676 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003677 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003678 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3679 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003680 if (subtypeId == NOT_A_SUBTYPE_ID) {
3681 // If there are no selected subtypes, the framework will try to find
3682 // the most applicable subtype from explicitly or implicitly enabled
3683 // subtypes.
3684 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003685 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003686 // If there is only one explicitly or implicitly enabled subtype,
3687 // just returns it.
3688 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3689 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3690 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003691 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003692 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003693 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003694 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003695 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003696 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3697 true);
satok4e4569d2010-11-19 18:45:53 +09003698 }
satok3ef8b292010-11-23 06:06:29 +09003699 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003700 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003701 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003702 }
3703 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003704 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003705 }
3706
satok4e4569d2010-11-19 18:45:53 +09003707 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003708 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003709 @Override
satok4e4569d2010-11-19 18:45:53 +09003710 public List getShortcutInputMethodsAndSubtypes() {
3711 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003712 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003713 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003714 // If there are no selected shortcut subtypes, the framework will try to find
3715 // the most applicable subtype from all subtypes whose mode is
3716 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003717 Pair<InputMethodInfo, InputMethodSubtype> info =
3718 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003719 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003720 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003721 ret.add(info.first);
3722 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003723 }
satok3da92232011-01-11 22:46:30 +09003724 return ret;
satokf3db1af2010-11-23 13:34:33 +09003725 }
satokf3db1af2010-11-23 13:34:33 +09003726 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3727 ret.add(imi);
3728 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3729 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003730 }
3731 }
satokf3db1af2010-11-23 13:34:33 +09003732 return ret;
satok4e4569d2010-11-19 18:45:53 +09003733 }
3734 }
3735
satok42c5a162011-05-26 16:46:14 +09003736 @Override
satokb66d2872010-11-10 01:04:04 +09003737 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003738 // TODO: Make this work even for non-current users?
3739 if (!calledFromValidUser()) {
3740 return false;
3741 }
satokb66d2872010-11-10 01:04:04 +09003742 synchronized (mMethodMap) {
3743 if (subtype != null && mCurMethodId != null) {
3744 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003745 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003746 if (subtypeId != NOT_A_SUBTYPE_ID) {
3747 setInputMethodLocked(mCurMethodId, subtypeId);
3748 return true;
3749 }
3750 }
3751 return false;
3752 }
3753 }
3754
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003755 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003756 private static class InputMethodFileManager {
3757 private static final String SYSTEM_PATH = "system";
3758 private static final String INPUT_METHOD_PATH = "inputmethod";
3759 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3760 private static final String NODE_SUBTYPES = "subtypes";
3761 private static final String NODE_SUBTYPE = "subtype";
3762 private static final String NODE_IMI = "imi";
3763 private static final String ATTR_ID = "id";
3764 private static final String ATTR_LABEL = "label";
3765 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003766 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003767 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003768 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003769 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3770 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3771 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003772 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003773 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3774 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003775 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003776 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003777 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003778 if (methodMap == null) {
3779 throw new NullPointerException("methodMap is null");
3780 }
3781 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003782 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003783 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3784 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003785 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003786 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003787 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3788 }
3789 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3790 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3791 if (!subtypeFile.exists()) {
3792 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003793 writeAdditionalInputMethodSubtypes(
3794 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003795 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003796 readAdditionalInputMethodSubtypes(
3797 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003798 }
3799 }
3800
3801 private void deleteAllInputMethodSubtypes(String imiId) {
3802 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003803 mAdditionalSubtypesMap.remove(imiId);
3804 writeAdditionalInputMethodSubtypes(
3805 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003806 }
3807 }
3808
3809 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003810 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003811 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003812 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003813 final int N = additionalSubtypes.length;
3814 for (int i = 0; i < N; ++i) {
3815 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003816 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003817 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003818 } else {
3819 Slog.w(TAG, "Duplicated subtype definition found: "
3820 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003821 }
3822 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003823 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3824 writeAdditionalInputMethodSubtypes(
3825 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003826 }
3827 }
3828
3829 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3830 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003831 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003832 }
3833 }
3834
3835 private static void writeAdditionalInputMethodSubtypes(
3836 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3837 HashMap<String, InputMethodInfo> methodMap) {
3838 // Safety net for the case that this function is called before methodMap is set.
3839 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3840 FileOutputStream fos = null;
3841 try {
3842 fos = subtypesFile.startWrite();
3843 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003844 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003845 out.startDocument(null, true);
3846 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3847 out.startTag(null, NODE_SUBTYPES);
3848 for (String imiId : allSubtypes.keySet()) {
3849 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3850 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3851 continue;
3852 }
3853 out.startTag(null, NODE_IMI);
3854 out.attribute(null, ATTR_ID, imiId);
3855 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3856 final int N = subtypesList.size();
3857 for (int i = 0; i < N; ++i) {
3858 final InputMethodSubtype subtype = subtypesList.get(i);
3859 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003860 if (subtype.hasSubtypeId()) {
3861 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3862 String.valueOf(subtype.getSubtypeId()));
3863 }
satoke7c6998e2011-06-03 17:57:59 +09003864 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3865 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3866 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003867 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3868 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003869 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3870 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3871 out.attribute(null, ATTR_IS_AUXILIARY,
3872 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003873 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3874 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003875 out.endTag(null, NODE_SUBTYPE);
3876 }
3877 out.endTag(null, NODE_IMI);
3878 }
3879 out.endTag(null, NODE_SUBTYPES);
3880 out.endDocument();
3881 subtypesFile.finishWrite(fos);
3882 } catch (java.io.IOException e) {
3883 Slog.w(TAG, "Error writing subtypes", e);
3884 if (fos != null) {
3885 subtypesFile.failWrite(fos);
3886 }
3887 }
3888 }
3889
3890 private static void readAdditionalInputMethodSubtypes(
3891 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3892 if (allSubtypes == null || subtypesFile == null) return;
3893 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003894 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003895 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003896 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003897 int type = parser.getEventType();
3898 // Skip parsing until START_TAG
3899 while ((type = parser.next()) != XmlPullParser.START_TAG
3900 && type != XmlPullParser.END_DOCUMENT) {}
3901 String firstNodeName = parser.getName();
3902 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3903 throw new XmlPullParserException("Xml doesn't start with subtypes");
3904 }
3905 final int depth =parser.getDepth();
3906 String currentImiId = null;
3907 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3908 while (((type = parser.next()) != XmlPullParser.END_TAG
3909 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3910 if (type != XmlPullParser.START_TAG)
3911 continue;
3912 final String nodeName = parser.getName();
3913 if (NODE_IMI.equals(nodeName)) {
3914 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3915 if (TextUtils.isEmpty(currentImiId)) {
3916 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3917 continue;
3918 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003919 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003920 allSubtypes.put(currentImiId, tempSubtypesArray);
3921 } else if (NODE_SUBTYPE.equals(nodeName)) {
3922 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3923 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3924 continue;
3925 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003926 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003927 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003928 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003929 parser.getAttributeValue(null, ATTR_LABEL));
3930 final String imeSubtypeLocale =
3931 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003932 final String languageTag =
3933 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003934 final String imeSubtypeMode =
3935 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3936 final String imeSubtypeExtraValue =
3937 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003938 final boolean isAuxiliary = "1".equals(String.valueOf(
3939 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003940 final boolean isAsciiCapable = "1".equals(String.valueOf(
3941 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003942 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003943 .setSubtypeNameResId(label)
3944 .setSubtypeIconResId(icon)
3945 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003946 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003947 .setSubtypeMode(imeSubtypeMode)
3948 .setSubtypeExtraValue(imeSubtypeExtraValue)
3949 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003950 .setIsAsciiCapable(isAsciiCapable);
3951 final String subtypeIdString =
3952 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3953 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003954 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003955 }
3956 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003957 }
3958 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003959 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3960 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003961 return;
satoke7c6998e2011-06-03 17:57:59 +09003962 }
3963 }
3964 }
3965
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003966 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3967 @NonNull
3968 private final Handler mHandler;
3969
3970 LocalServiceImpl(@NonNull final Handler handler) {
3971 mHandler = handler;
3972 }
3973
3974 @Override
3975 public void setInteractive(boolean interactive) {
3976 // Do everything in handler so as not to block the caller.
3977 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3978 interactive ? 1 : 0, 0));
3979 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003980
3981 @Override
3982 public void switchInputMethod(boolean forwardDirection) {
3983 // Do everything in handler so as not to block the caller.
3984 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3985 forwardDirection ? 1 : 0, 0));
3986 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003987
3988 @Override
3989 public void hideCurrentInputMethod() {
3990 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3991 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3992 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003993 }
3994
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003995 private static String imeWindowStatusToString(final int imeWindowVis) {
3996 final StringBuilder sb = new StringBuilder();
3997 boolean first = true;
3998 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3999 sb.append("Active");
4000 first = false;
4001 }
4002 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4003 if (!first) {
4004 sb.append("|");
4005 }
4006 sb.append("Visible");
4007 }
4008 return sb.toString();
4009 }
4010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004012 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4013 @Nullable Uri contentUri, @Nullable String packageName) {
4014 if (!calledFromValidUser()) {
4015 return null;
4016 }
4017
4018 if (token == null) {
4019 throw new NullPointerException("token");
4020 }
4021 if (packageName == null) {
4022 throw new NullPointerException("packageName");
4023 }
4024 if (contentUri == null) {
4025 throw new NullPointerException("contentUri");
4026 }
4027 final String contentUriScheme = contentUri.getScheme();
4028 if (!"content".equals(contentUriScheme)) {
4029 throw new InvalidParameterException("contentUri must have content scheme");
4030 }
4031
4032 synchronized (mMethodMap) {
4033 final int uid = Binder.getCallingUid();
4034 if (mCurMethodId == null) {
4035 return null;
4036 }
4037 if (mCurToken != token) {
4038 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4039 + " token=" + token);
4040 return null;
4041 }
4042 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4043 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4044 // nature of our system. Let's compare it with our internal record.
4045 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4046 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4047 + mCurAttribute.packageName + " packageName=" + packageName);
4048 return null;
4049 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004050 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004051 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004052 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004053 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004054 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4055 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4056 imeUserId);
4057 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4058 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4059 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4060 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4061 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4062 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4063 // actually allowed to "uid", which is guaranteed to be the IME's one.
4064 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4065 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004066 }
4067 }
4068
4069 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004070 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4071 if (!calledFromValidUser()) {
4072 return;
4073 }
4074 synchronized (mMethodMap) {
4075 if (!calledWithValidToken(token)) {
4076 return;
4077 }
4078 if (mCurClient != null && mCurClient.client != null) {
4079 mInFullscreenMode = fullscreen;
4080 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4081 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4082 }
4083 }
4084 }
4085
4086 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4088 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4089 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4092 + Binder.getCallingPid()
4093 + ", uid=" + Binder.getCallingUid());
4094 return;
4095 }
4096
4097 IInputMethod method;
4098 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004099 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 synchronized (mMethodMap) {
4104 p.println("Current Input Method Manager state:");
4105 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004106 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 for (int i=0; i<N; i++) {
4108 InputMethodInfo info = mMethodList.get(i);
4109 p.println(" InputMethod #" + i + ":");
4110 info.dump(p, " ");
4111 }
4112 p.println(" Clients:");
4113 for (ClientState ci : mClients.values()) {
4114 p.println(" Client " + ci + ":");
4115 p.println(" client=" + ci.client);
4116 p.println(" inputContext=" + ci.inputContext);
4117 p.println(" sessionRequested=" + ci.sessionRequested);
4118 p.println(" curSession=" + ci.curSession);
4119 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004120 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004122 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4123 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004124 focusedWindowClient = mCurFocusedWindowClient;
4125 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4127 + " mBoundToMethod=" + mBoundToMethod);
4128 p.println(" mCurToken=" + mCurToken);
4129 p.println(" mCurIntent=" + mCurIntent);
4130 method = mCurMethod;
4131 p.println(" mCurMethod=" + mCurMethod);
4132 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004133 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 p.println(" mShowRequested=" + mShowRequested
4135 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4136 + " mShowForced=" + mShowForced
4137 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004138 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004139 p.println(" mCurUserActionNotificationSequenceNumber="
4140 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004141 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004142 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004143 p.println(" mSwitchingController:");
4144 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004145 p.println(" mSettings:");
4146 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004148
Jeff Brownb88102f2010-09-08 11:49:43 -07004149 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 pw.flush();
4152 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004153 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4154 } catch (IOException | RemoteException e) {
4155 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004157 } else {
4158 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004160
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004161 if (focusedWindowClient != null && client != focusedWindowClient) {
4162 p.println(" ");
4163 p.println("Warning: Current input method client doesn't match the last focused. "
4164 + "window.");
4165 p.println("Dumping input method client in the last focused window just in case.");
4166 p.println(" ");
4167 pw.flush();
4168 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004169 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4170 } catch (IOException | RemoteException e) {
4171 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004172 }
4173 }
4174
Jeff Brownb88102f2010-09-08 11:49:43 -07004175 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 pw.flush();
4178 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004179 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4180 } catch (IOException | RemoteException e) {
4181 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004183 } else {
4184 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 }
4186 }
4187}