blob: f718fa11598e2a62f7c6f2ae1fa893294fec5c87 [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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080024import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070025import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090026import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090028import com.android.internal.inputmethod.InputMethodUtils;
29import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070031import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070032import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090033import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.view.IInputContext;
35import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import com.android.internal.view.IInputMethodClient;
37import com.android.internal.view.IInputMethodManager;
38import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070039import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080041import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080042import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
satoke7c6998e2011-06-03 17:57:59 +090044import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090046import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070048import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070049import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070050import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080051import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080052import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070053import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070055import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090056import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090057import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090058import android.app.Notification;
59import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070060import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090061import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080063import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ContentResolver;
65import android.content.Context;
66import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090068import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090070import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070072import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.pm.PackageManager;
75import android.content.pm.ResolveInfo;
76import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070077import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.res.Resources;
79import android.content.res.TypedArray;
80import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080081import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080082import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080083import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070084import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040086import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070087import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090088import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Handler;
90import android.os.IBinder;
91import android.os.IInterface;
92import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070093import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070095import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080097import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.ServiceManager;
99import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900100import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800101import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.provider.Settings;
103import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900104import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700105import android.util.ArrayMap;
106import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700107import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900109import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900110import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.util.PrintWriterPrinter;
112import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900113import android.util.Slog;
114import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900115import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700117import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900118import android.view.LayoutInflater;
119import android.view.View;
120import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700121import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700123import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900124import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700126import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import android.view.inputmethod.InputMethod;
128import android.view.inputmethod.InputMethodInfo;
129import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700130import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900131import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900132import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900133import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900134import android.widget.CompoundButton;
135import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900136import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900137import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900138import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700139import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
satoke7c6998e2011-06-03 17:57:59 +0900141import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900143import java.io.FileInputStream;
144import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.io.IOException;
146import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700147import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100148import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700149import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900151import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.util.HashMap;
153import java.util.List;
154
155/**
156 * This class provides a system service that manages input methods.
157 */
158public class InputMethodManagerService extends IInputMethodManager.Stub
159 implements ServiceConnection, Handler.Callback {
160 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700161 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700162 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700164 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
165 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
166 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 static final int MSG_UNBIND_INPUT = 1000;
169 static final int MSG_BIND_INPUT = 1010;
170 static final int MSG_SHOW_SOFT_INPUT = 1020;
171 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700172 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 static final int MSG_ATTACH_TOKEN = 1040;
174 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_START_INPUT = 2000;
177 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
Yohei Yukawa33e81792015-11-17 21:14:42 -0800179 static final int MSG_UNBIND_CLIENT = 3000;
180 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700181 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700182 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900183 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800184 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
satok01038492012-04-09 21:08:27 +0900186 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
187
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700188 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
189
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700190 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
satokf9f01002011-05-19 21:31:50 +0900192 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
193
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900194 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900195 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900196
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700197 @Retention(SOURCE)
198 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
199 private @interface HardKeyboardBehavior {
200 int WIRELESS_AFFORDANCE = 0;
201 int WIRED_AFFORDANCE = 1;
202 }
satok4e4569d2010-11-19 18:45:53 +0900203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800205 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900207 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700210 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700212 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900213 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900214 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900215 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800216 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900218 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 // All known input methods. mMethodMap also serves as the global
221 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700222 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
223 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900224 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700225 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900226 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700228 // Used to bring IME service up to visible adjustment while it is being shown.
229 final ServiceConnection mVisibleConnection = new ServiceConnection() {
230 @Override public void onServiceConnected(ComponentName name, IBinder service) {
231 }
232
233 @Override public void onServiceDisconnected(ComponentName name) {
234 }
235 };
236 boolean mVisibleBound = false;
237
satok7cfc0ed2011-06-20 21:29:36 +0900238 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700239 private NotificationManager mNotificationManager;
240 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700241 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400242 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700243 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900244 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900245 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900246 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900247
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900248 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final ClientState client;
250 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700251
252 IInputMethodSession session;
253 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 @Override
256 public String toString() {
257 return "SessionState{uid " + client.uid + " pid " + client.pid
258 + " method " + Integer.toHexString(
259 System.identityHashCode(method))
260 + " session " + Integer.toHexString(
261 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700262 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 + "}";
264 }
265
266 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700267 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 client = _client;
269 method = _method;
270 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700271 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 }
273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
Jeff Brownc28867a2013-03-26 15:42:39 -0700275 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 final IInputMethodClient client;
277 final IInputContext inputContext;
278 final int uid;
279 final int pid;
280 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 boolean sessionRequested;
283 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 @Override
286 public String toString() {
287 return "ClientState{" + Integer.toHexString(
288 System.identityHashCode(this)) + " uid " + uid
289 + " pid " + pid + "}";
290 }
291
292 ClientState(IInputMethodClient _client, IInputContext _inputContext,
293 int _uid, int _pid) {
294 client = _client;
295 inputContext = _inputContext;
296 uid = _uid;
297 pid = _pid;
298 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
299 }
300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700302 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700305 * Set once the system is ready to run third party code.
306 */
307 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700309 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700310 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
311 * method. This is to be synchronized with the secure settings keyed with
312 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
313 *
314 * <p>This can be transiently {@code null} when the system is re-initializing input method
315 * settings, e.g., the system locale is just changed.</p>
316 *
317 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
318 * {@link InputMethodManagerService}.</p>
319 *
320 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700322 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * The current binding sequence number, incremented every time there is
327 * a new bind performed.
328 */
329 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * The client that is currently bound to an input method.
333 */
334 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800337 * The last window token that we confirmed to be focused. This is always updated upon reports
338 * from the input method client. If the window state is already changed before the report is
339 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700340 */
341 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700343 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800344 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
345 */
346 ClientState mCurFocusedWindowClient;
347
348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * The input context last provided by the current client.
350 */
351 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700354 * The missing method flags for the input context last provided by the current client.
355 *
356 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
357 */
358 int mCurInputContextMissingMethods;
359
360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 * The attributes last provided by the current client.
362 */
363 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700366 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700368 *
369 * <p>This can be {@code null} when no input method is connected.</p>
370 *
371 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700373 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
satokab751aa2010-09-14 19:17:36 +0900377 * The current subtype of the current input method.
378 */
379 private InputMethodSubtype mCurrentSubtype;
380
satok4e4569d2010-11-19 18:45:53 +0900381 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900382 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700383 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900384
John Spurlocke0980502013-10-25 11:59:29 -0400385 // Was the keyguard locked when this client became current?
386 private boolean mCurClientInKeyguard;
387
satokab751aa2010-09-14 19:17:36 +0900388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * Set to true if our ServiceConnection is currently actively bound to
390 * a service (whether or not we have gotten its IBinder back yet).
391 */
392 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * Set if the client has asked for the input method to be shown.
396 */
397 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 /**
400 * Set if we were explicitly told to show the input method.
401 */
402 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
405 * Set if we were forced to be shown.
406 */
407 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 /**
410 * Set if we last told the input method to show itself.
411 */
412 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * The Intent used to connect to the current input method.
416 */
417 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * The token we have made for the currently active input method, to
421 * identify it in the future.
422 */
423 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * If non-null, this is the input method service we are currently connected
427 * to.
428 */
429 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 /**
432 * Time that we last initiated a bind to the input method, to determine
433 * if we should try to disconnect and reconnect to it.
434 */
435 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 /**
438 * Have we called mCurMethod.bindInput()?
439 */
440 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /**
443 * Currently enabled session. Only touched by service thread, not
444 * protected by a lock.
445 */
446 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700449 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700451 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900453 int mCurUserActionNotificationSequenceNumber = 0;
454
Joe Onorato857fd9b2011-01-27 15:08:35 -0800455 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900456
457 /**
458 * A set of status bits regarding the active IME.
459 *
460 * <p>This value is a combination of following two bits:</p>
461 * <dl>
462 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
463 * <dd>
464 * If this bit is ON, connected IME is ready to accept touch/key events.
465 * </dd>
466 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
467 * <dd>
468 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
469 * </dd>
470 * </dl>
471 * <em>Do not update this value outside of setImeWindowStatus.</em>
472 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800473 int mImeWindowVis;
474
Ken Wakasa05dbb652011-08-22 15:22:43 +0900475 private AlertDialog.Builder mDialogBuilder;
476 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700477 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900478 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700479 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900480 private InputMethodInfo[] mIms;
481 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800482 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700483 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800484 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900485 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
486 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500487 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700488 @HardKeyboardBehavior
489 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700492 int mUserId;
493 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700494 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800495 String mLastEnabled = "";
496
Yohei Yukawa81482972015-06-04 00:58:59 -0700497 /**
498 * <em>This constructor must be called within the lock.</em>
499 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 SettingsObserver(Handler handler) {
501 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700502 }
503
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800504 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700505 if (mRegistered && mUserId == userId) {
506 return;
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700509 if (mRegistered) {
510 mContext.getContentResolver().unregisterContentObserver(this);
511 mRegistered = false;
512 }
513 if (mUserId != userId) {
514 mLastEnabled = "";
515 mUserId = userId;
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700518 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900519 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700520 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900521 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700522 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700523 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700524 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800525 resolver.registerContentObserver(Settings.Secure.getUriFor(
526 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700527 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800529
Michael Wright7b5a96b2014-08-09 19:28:42 -0700530 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800531 final Uri showImeUri = Settings.Secure.getUriFor(
532 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
533 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
534 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700536 if (showImeUri.equals(uri)) {
537 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800538 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
539 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
540 mContext.getContentResolver(),
541 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
542 0, mUserId) == 1;
543 if (mAccessibilityRequestingNoSoftKeyboard) {
544 final boolean showRequested = mShowRequested;
545 hideCurrentInputLocked(0, null);
546 mShowRequested = showRequested;
547 } else if (mShowRequested) {
548 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
549 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700550 } else {
551 boolean enabledChanged = false;
552 String newEnabled = mSettings.getEnabledInputMethodsStr();
553 if (!mLastEnabled.equals(newEnabled)) {
554 mLastEnabled = newEnabled;
555 enabledChanged = true;
556 }
557 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700561
562 @Override
563 public String toString() {
564 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
565 + " mLastEnabled=" + mLastEnabled + "}";
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900569 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900570 @Override
571 public void onReceive(Context context, Intent intent) {
572 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700573 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900574 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700575 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900576 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800577 } else if (Intent.ACTION_USER_ADDED.equals(action)
578 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100579 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800580 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700581 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
582 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
583 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
584 final String prevValue = intent.getStringExtra(
585 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
586 final String newValue = intent.getStringExtra(
587 Intent.EXTRA_SETTING_NEW_VALUE);
588 restoreEnabledInputMethods(mContext, prevValue, newValue);
589 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900590 } else {
591 Slog.w(TAG, "Unexpected intent " + intent);
592 }
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700596 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
597 // does not attempt to validate on the fly with any installed device policy, so must only
598 // be run in the context of initial device setup.
599 //
600 // TODO: Move this method to InputMethodUtils with adding unit tests.
601 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
602 if (DEBUG_RESTORE) {
603 Slog.i(TAG, "Restoring enabled input methods:");
604 Slog.i(TAG, "prev=" + prevValue);
605 Slog.i(TAG, " new=" + newValue);
606 }
607 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900608 ArrayMap<String, ArraySet<String>> prevMap =
609 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
610 ArrayMap<String, ArraySet<String>> newMap =
611 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700612
613 // Merge the restored ime+subtype enabled states into the live state
614 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
615 final String imeId = entry.getKey();
616 ArraySet<String> prevSubtypes = prevMap.get(imeId);
617 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700618 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700619 prevMap.put(imeId, prevSubtypes);
620 }
621 prevSubtypes.addAll(entry.getValue());
622 }
623
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700624 final String mergedImesAndSubtypesString =
625 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700626 if (DEBUG_RESTORE) {
627 Slog.i(TAG, "Merged IME string:");
628 Slog.i(TAG, " " + mergedImesAndSubtypesString);
629 }
630 Settings.Secure.putString(context.getContentResolver(),
631 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
632 }
633
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800634 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 private boolean isChangingPackagesOfCurrentUser() {
636 final int userId = getChangingUserId();
637 final boolean retval = userId == mSettings.getCurrentUserId();
638 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900639 if (!retval) {
640 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
641 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900642 }
643 return retval;
644 }
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800647 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900648 if (!isChangingPackagesOfCurrentUser()) {
649 return false;
650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900652 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 final int N = mMethodList.size();
654 if (curInputMethodId != null) {
655 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 InputMethodInfo imi = mMethodList.get(i);
657 if (imi.getId().equals(curInputMethodId)) {
658 for (String pkg : packages) {
659 if (imi.getPackageName().equals(pkg)) {
660 if (!doit) {
661 return true;
662 }
satok723a27e2010-11-11 14:58:11 +0900663 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800664 chooseNewDefaultIMELocked();
665 return true;
666 }
667 }
668 }
669 }
670 }
671 }
672 return false;
673 }
674
675 @Override
676 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900677 if (!isChangingPackagesOfCurrentUser()) {
678 return;
679 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 synchronized (mMethodMap) {
681 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900682 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800683 final int N = mMethodList.size();
684 if (curInputMethodId != null) {
685 for (int i=0; i<N; i++) {
686 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900687 final String imiId = imi.getId();
688 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800689 curIm = imi;
690 }
satoke7c6998e2011-06-03 17:57:59 +0900691
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900693 if (isPackageModified(imi.getPackageName())) {
694 mFileManager.deleteAllInputMethodSubtypes(imiId);
695 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800696 if (change == PACKAGE_TEMPORARY_CHANGE
697 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800698 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800699 + imi.getComponent());
700 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702 }
703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800704
Yohei Yukawa94e33302016-02-12 19:37:03 -0800705 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800708
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800709 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800710 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800711 if (change == PACKAGE_TEMPORARY_CHANGE
712 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800713 ServiceInfo si = null;
714 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900715 si = mIPackageManager.getServiceInfo(
716 curIm.getComponent(), 0, mSettings.getCurrentUserId());
717 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800718 }
719 if (si == null) {
720 // Uh oh, current input method is no longer around!
721 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800722 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900723 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800724 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800725 changed = true;
726 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800727 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900728 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800729 }
730 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800731 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800732 }
satokab751aa2010-09-14 19:17:36 +0900733
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800734 if (curIm == null) {
735 // We currently don't have a default input method... is
736 // one now available?
737 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700738 } else if (!changed && isPackageModified(curIm.getPackageName())) {
739 // Even if the current input method is still available, mCurrentSubtype could
740 // be obsolete when the package is modified in practice.
741 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800744 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800745 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
748 }
749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750
Jeff Brownc28867a2013-03-26 15:42:39 -0700751 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900752 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700753 private final IInputMethod mMethod;
754 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
Jeff Brownc28867a2013-03-26 15:42:39 -0700756 MethodCallback(InputMethodManagerService imms, IInputMethod method,
757 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900758 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700759 mMethod = method;
760 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
satoke7c6998e2011-06-03 17:57:59 +0900763 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700764 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700765 long ident = Binder.clearCallingIdentity();
766 try {
767 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
768 } finally {
769 Binder.restoreCallingIdentity(ident);
770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
satok01038492012-04-09 21:08:27 +0900774 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700775 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900776 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700777 public void onHardKeyboardStatusChange(boolean available) {
778 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
779 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900780 }
781
Michael Wright7b5a96b2014-08-09 19:28:42 -0700782 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900783 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700784 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900785 }
786 synchronized(mMethodMap) {
787 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
788 && mSwitchingDialog.isShowing()) {
789 mSwitchingDialogTitleView.findViewById(
790 com.android.internal.R.id.hard_keyboard_section).setVisibility(
791 available ? View.VISIBLE : View.GONE);
792 }
793 }
794 }
795 }
796
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800797 public static final class Lifecycle extends SystemService {
798 private InputMethodManagerService mService;
799
800 public Lifecycle(Context context) {
801 super(context);
802 mService = new InputMethodManagerService(context);
803 }
804
805 @Override
806 public void onStart() {
807 LocalServices.addService(InputMethodManagerInternal.class,
808 new LocalServiceImpl(mService.mHandler));
809 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
810 }
811
812 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800813 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700814 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800815 // TODO: Dispatch this to a worker thread as needed.
816 mService.onSwitchUser(userHandle);
817 }
818
819 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800820 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700821 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800822 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800823 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
824 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
825 .getService(Context.STATUS_BAR_SERVICE);
826 mService.systemRunning(statusBarService);
827 }
828 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800829
830 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700831 public void onUnlockUser(final @UserIdInt int userHandle) {
832 // Called on ActivityManager thread.
833 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700834 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800835 }
836 }
837
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800838 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800839 synchronized(mMethodMap) {
840 final int currentUserId = mSettings.getCurrentUserId();
841 if (DEBUG) {
842 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
843 }
844 if (userId != currentUserId) {
845 return;
846 }
847 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
848 // We need to rebuild IMEs.
849 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
850 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
851 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800852 }
853
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800854 void onSwitchUser(@UserIdInt int userId) {
855 synchronized (mMethodMap) {
856 switchUserLocked(userId);
857 }
858 }
859
Seigo Nonaka7309b122015-08-17 18:34:13 -0700860 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900861 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800863 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700865 // Note: SettingsObserver doesn't register observers in its constructor.
866 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 mIWindowManager = IWindowManager.Stub.asInterface(
868 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700869 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800870 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900871 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 public void executeMessage(Message msg) {
873 handleMessage(msg);
874 }
Mita Yuned218c72012-12-06 17:18:25 -0800875 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800876 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800877 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900878 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700879 mHasFeature = context.getPackageManager().hasSystemFeature(
880 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500881 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700882 mHardKeyboardBehavior = mContext.getResources().getInteger(
883 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900884
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400885 Bundle extras = new Bundle();
886 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
887 mImeSwitcherNotification = new Notification.Builder(mContext)
888 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
889 .setWhen(0)
890 .setOngoing(true)
891 .addExtras(extras)
892 .setCategory(Notification.CATEGORY_SYSTEM)
893 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400894
satok7cfc0ed2011-06-20 21:29:36 +0900895 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900896 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900897
898 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900899
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900900 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800902 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
903 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700904 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900905 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906
satok7cfc0ed2011-06-20 21:29:36 +0900907 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 int userId = 0;
909 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800910 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900911 } catch (RemoteException e) {
912 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
913 }
satok81f8b7c2012-12-04 20:42:56 +0900914 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900915
satokd87c2592010-09-29 11:52:06 +0900916 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900917 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800918 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700919
Kenny Guy2a764942014-04-02 13:29:20 +0100920 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900921 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900922 synchronized (mMethodMap) {
923 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
924 mSettings, context);
925 }
satok0a1bcf42012-05-16 19:26:31 +0900926
927 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900928 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900929 if (DEBUG) {
930 Slog.d(TAG, "Initial default ime = " + defaultImiId);
931 }
satok0a1bcf42012-05-16 19:26:31 +0900932 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
933
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900934 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800935 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900936 }
satokd87c2592010-09-29 11:52:06 +0900937 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938
satok0a1bcf42012-05-16 19:26:31 +0900939 if (!mImeSelectedOnBoot) {
940 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900941 synchronized (mMethodMap) {
942 resetDefaultImeLocked(context);
943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800945
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900946 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700947 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900948 updateFromSettingsLocked(true);
949 }
satok5b927c432012-05-01 20:09:34 +0900950
951 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
952 // according to the new system locale.
953 final IntentFilter filter = new IntentFilter();
954 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
955 mContext.registerReceiver(
956 new BroadcastReceiver() {
957 @Override
958 public void onReceive(Context context, Intent intent) {
959 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900960 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900961 }
962 }
963 }, filter);
964 }
965
satok5b927c432012-05-01 20:09:34 +0900966 private void resetDefaultImeLocked(Context context) {
967 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800968 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900969 return;
970 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800971 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
972 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
973 if (suitableImes.isEmpty()) {
974 Slog.i(TAG, "No default found");
975 return;
satok5b927c432012-05-01 20:09:34 +0900976 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800977 final InputMethodInfo defIm = suitableImes.get(0);
978 Slog.i(TAG, "Default found, using " + defIm.getId());
979 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900980 }
981
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900982 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
983 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 if (!mSystemReady) {
985 // not system ready
986 return;
987 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800988 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800990 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 if (!updateOnlyWhenLocaleChanged) {
992 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800993 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900994 }
995 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800996 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900997 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800998 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 if (!updateOnlyWhenLocaleChanged) {
1000 final String selectedImiId = mSettings.getSelectedInputMethod();
1001 if (TextUtils.isEmpty(selectedImiId)) {
1002 // This is the first time of the user switch and
1003 // set the current ime to the proper one.
1004 resetDefaultImeLocked(mContext);
1005 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001006 } else {
1007 // If the locale is changed, needs to reset the default ime
1008 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001009 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001010 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001011 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001012 if (!updateOnlyWhenLocaleChanged) {
1013 try {
1014 startInputInnerLocked();
1015 } catch (RuntimeException e) {
1016 Slog.w(TAG, "Unexpected exception", e);
1017 }
1018 }
1019 }
1020 }
1021
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001022 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001023 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1024 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001025 }
1026
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001027 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001028 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1029 + " currentUserId=" + mSettings.getCurrentUserId());
1030
Yohei Yukawa81482972015-06-04 00:58:59 -07001031 // ContentObserver should be registered again when the user is changed
1032 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001033
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001034 // If the system is not ready or the device is not yed unlocked by the user, then we use
1035 // copy-on-write settings.
1036 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001037 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001038 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001039 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001040 // InputMethodFileManager should be reset when the user is changed
1041 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001042 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001043
1044 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1045 + " defaultImiId=" + defaultImiId);
1046
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001047 // For secondary users, the list of enabled IMEs may not have been updated since the
1048 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1049 // not be empty even if the IME has been uninstalled by the primary user.
1050 // Even in such cases, IMMS works fine because it will find the most applicable
1051 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001052 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001053 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001054 initialUserSwitch /* needsToResetDefaultIme */);
1055 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001056 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1057 mSettings.getEnabledInputMethodListLocked(), newUserId,
1058 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001059 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001060
1061 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1062 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001063 }
1064
Kenny Guy2a764942014-04-02 13:29:20 +01001065 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001066 mSettings.setCurrentProfileIds(
1067 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001068 }
1069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 @Override
1071 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1072 throws RemoteException {
1073 try {
1074 return super.onTransact(code, data, reply, flags);
1075 } catch (RuntimeException e) {
1076 // The input method manager only throws security exceptions, so let's
1077 // log all others.
1078 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001079 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 throw e;
1082 }
1083 }
1084
Svetoslav Ganova0027152013-06-25 14:59:53 -07001085 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001086 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001087 if (DEBUG) {
1088 Slog.d(TAG, "--- systemReady");
1089 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001090 if (!mSystemReady) {
1091 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001092 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001093 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001094 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001095 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1096 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001097 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001098 if (mStatusBar != null) {
1099 mStatusBar.setIconVisibility(mSlotIme, false);
1100 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001101 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001102 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1103 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001104 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001105 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001106 mHardKeyboardListener);
1107 }
satok0a1bcf42012-05-16 19:26:31 +09001108 if (!mImeSelectedOnBoot) {
1109 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001110 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001111 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1112 mSettings.getEnabledInputMethodListLocked(),
1113 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001114 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001115 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001116 try {
1117 startInputInnerLocked();
1118 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001119 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001120 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001121 }
1122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001125 // ---------------------------------------------------------------------------------------
1126 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1127 // 1) it comes from the system process
1128 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1129 private boolean calledFromValidUser() {
1130 final int uid = Binder.getCallingUid();
1131 final int userId = UserHandle.getUserId(uid);
1132 if (DEBUG) {
1133 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1134 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1135 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001136 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1137 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001138 }
Kenny Guy2a764942014-04-02 13:29:20 +01001139 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001140 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001141 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001142
1143 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1144 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1145 // must not manage background users' states in any functions.
1146 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1147 // by a token.
1148 if (mContext.checkCallingOrSelfPermission(
1149 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1150 == PackageManager.PERMISSION_GRANTED) {
1151 if (DEBUG) {
1152 Slog.d(TAG, "--- Access granted because the calling process has "
1153 + "the INTERACT_ACROSS_USERS_FULL permission");
1154 }
1155 return true;
1156 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001157 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1158 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001159 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001160 }
1161
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001162
1163 /**
1164 * Returns true iff the caller is identified to be the current input method with the token.
1165 * @param token The window token given to the input method when it was started.
1166 * @return true if and only if non-null valid token is specified.
1167 */
1168 private boolean calledWithValidToken(IBinder token) {
1169 if (token == null || mCurToken != token) {
1170 return false;
1171 }
1172 return true;
1173 }
1174
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 private boolean bindCurrentInputMethodService(
1176 Intent service, ServiceConnection conn, int flags) {
1177 if (service == null || conn == null) {
1178 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1179 return false;
1180 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001181 return mContext.bindServiceAsUser(service, conn, flags,
1182 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001183 }
1184
satoke7c6998e2011-06-03 17:57:59 +09001185 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001187 // TODO: Make this work even for non-current users?
1188 if (!calledFromValidUser()) {
1189 return Collections.emptyList();
1190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001192 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194 }
1195
satoke7c6998e2011-06-03 17:57:59 +09001196 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001198 // TODO: Make this work even for non-current users?
1199 if (!calledFromValidUser()) {
1200 return Collections.emptyList();
1201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001203 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205 }
1206
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001207 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001208 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001209 * @return enabled subtypes of the specified imi
1210 */
satoke7c6998e2011-06-03 17:57:59 +09001211 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001212 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001213 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001214 // TODO: Make this work even for non-current users?
1215 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001216 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001217 }
satok67ddf9c2010-11-17 09:45:54 +09001218 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001219 final InputMethodInfo imi;
1220 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001221 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001222 } else {
1223 imi = mMethodMap.get(imiId);
1224 }
1225 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001226 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001227 }
1228 return mSettings.getEnabledInputMethodSubtypeListLocked(
1229 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001230 }
1231 }
1232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public void addClient(IInputMethodClient client,
1235 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001236 if (!calledFromValidUser()) {
1237 return;
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 synchronized (mMethodMap) {
1240 mClients.put(client.asBinder(), new ClientState(client,
1241 inputContext, uid, pid));
1242 }
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
satoke7c6998e2011-06-03 17:57:59 +09001245 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001247 if (!calledFromValidUser()) {
1248 return;
1249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001251 ClientState cs = mClients.remove(client.asBinder());
1252 if (cs != null) {
1253 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001254 if (mCurClient == cs) {
1255 mCurClient = null;
1256 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001257 if (mCurFocusedWindowClient == cs) {
1258 mCurFocusedWindowClient = null;
1259 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
1262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 void executeOrSendMessage(IInterface target, Message msg) {
1265 if (target.asBinder() instanceof Binder) {
1266 mCaller.sendMessage(msg);
1267 } else {
1268 handleMessage(msg);
1269 msg.recycle();
1270 }
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
Yohei Yukawa33e81792015-11-17 21:14:42 -08001273 void unbindCurrentClientLocked(
1274 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001276 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 + mCurClient.client.asBinder());
1278 if (mBoundToMethod) {
1279 mBoundToMethod = false;
1280 if (mCurMethod != null) {
1281 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1282 MSG_UNBIND_INPUT, mCurMethod));
1283 }
1284 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001285
1286 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1287 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001288 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1289 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001292
The Android Open Source Project10592532009-03-18 17:39:46 -07001293 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
1295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 private int getImeShowFlags() {
1298 int flags = 0;
1299 if (mShowForced) {
1300 flags |= InputMethod.SHOW_FORCED
1301 | InputMethod.SHOW_EXPLICIT;
1302 } else if (mShowExplicitlyRequested) {
1303 flags |= InputMethod.SHOW_EXPLICIT;
1304 }
1305 return flags;
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 private int getAppShowFlags() {
1309 int flags = 0;
1310 if (mShowForced) {
1311 flags |= InputMethodManager.SHOW_FORCED;
1312 } else if (!mShowExplicitlyRequested) {
1313 flags |= InputMethodManager.SHOW_IMPLICIT;
1314 }
1315 return flags;
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
Dianne Hackborn7663d802012-02-24 13:08:49 -08001318 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 if (!mBoundToMethod) {
1320 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1321 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1322 mBoundToMethod = true;
1323 }
1324 final SessionState session = mCurClient.curSession;
1325 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001326 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1327 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1328 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001330 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1331 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1332 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
1334 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001335 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001338 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001339 (session.channel != null ? session.channel.dup() : null),
1340 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001343 InputBindResult startInputLocked(
1344 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001345 IInputMethodClient client, IInputContext inputContext,
1346 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001347 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 // If no method is currently selected, do nothing.
1349 if (mCurMethodId == null) {
1350 return mNoBinding;
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 ClientState cs = mClients.get(client.asBinder());
1354 if (cs == null) {
1355 throw new IllegalArgumentException("unknown client "
1356 + client.asBinder());
1357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358
Yohei Yukawa74750f22016-03-22 12:54:22 -07001359 if (attribute == null) {
1360 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1361 + " uid=" + cs.uid + " pid=" + cs.pid);
1362 return null;
1363 }
1364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 try {
1366 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1367 // Check with the window manager to make sure this client actually
1368 // has a window with focus. If not, reject. This is thread safe
1369 // because if the focus changes some time before or after, the
1370 // next client receiving focus that has any interest in input will
1371 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001372 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1374 return null;
1375 }
1376 } catch (RemoteException e) {
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001379 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1380 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001381 }
1382
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001383 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001384 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001385 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001386 // If no method is currently selected, do nothing.
1387 if (mCurMethodId == null) {
1388 return mNoBinding;
1389 }
1390
Yohei Yukawad57ba672015-06-08 16:39:46 -07001391 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1392 attribute.packageName)) {
1393 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1394 + " uid=" + cs.uid + " package=" + attribute.packageName);
1395 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001396 }
1397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001399 // Was the keyguard locked when switching over to the new client?
1400 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 // If the client is changing, we need to switch over to the new
1402 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001403 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001404 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001405 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406
1407 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001408 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001409 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001410 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
1412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 // Bump up the sequence for this client and attach it.
1415 mCurSeq++;
1416 if (mCurSeq <= 0) mCurSeq = 1;
1417 mCurClient = cs;
1418 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001419 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 // Check if the input method is changing.
1423 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1424 if (cs.curSession != null) {
1425 // Fast case: if we are already connected to the input method,
1426 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001427 return attachNewInputLocked(
1428 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 if (mHaveConnection) {
1431 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 // Return to client, and we will get back with it when
1433 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001434 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001435 return new InputBindResult(null, null, mCurId, mCurSeq,
1436 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 } else if (SystemClock.uptimeMillis()
1438 < (mLastBindTime+TIME_TO_RECONNECT)) {
1439 // In this case we have connected to the service, but
1440 // don't yet have its interface. If it hasn't been too
1441 // long since we did the connection, we'll return to
1442 // the client and wait to get the service interface so
1443 // we can report back. If it has been too long, we want
1444 // to fall through so we can try a disconnect/reconnect
1445 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001446 return new InputBindResult(null, null, mCurId, mCurSeq,
1447 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1450 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
1452 }
1453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001455 return startInputInnerLocked();
1456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001458 InputBindResult startInputInnerLocked() {
1459 if (mCurMethodId == null) {
1460 return mNoBinding;
1461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001463 if (!mSystemReady) {
1464 // If the system is not yet ready, we shouldn't be running third
1465 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001466 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1467 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1471 if (info == null) {
1472 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001474
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001475 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1478 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001479 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1480 com.android.internal.R.string.input_method_binding_label);
1481 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1482 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001483 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001484 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1485 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 mLastBindTime = SystemClock.uptimeMillis();
1487 mHaveConnection = true;
1488 mCurId = info.getId();
1489 mCurToken = new Binder();
1490 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001491 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001492 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 } catch (RemoteException e) {
1494 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001495 return new InputBindResult(null, null, mCurId, mCurSeq,
1496 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 } else {
1498 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 + mCurIntent);
1501 }
1502 return null;
1503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001505 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001506 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001507 IInputMethodClient client, IInputContext inputContext,
1508 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001509 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001510 if (!calledFromValidUser()) {
1511 return null;
1512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001514 if (DEBUG) {
1515 Slog.v(TAG, "startInput: reason="
1516 + InputMethodClient.getStartInputReason(startInputReason)
1517 + " client = " + client.asBinder()
1518 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001519 + " missingMethods="
1520 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001521 + " attribute=" + attribute
1522 + " controlFlags=#" + Integer.toHexString(controlFlags));
1523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 final long ident = Binder.clearCallingIdentity();
1525 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001526 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1527 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 } finally {
1529 Binder.restoreCallingIdentity(ident);
1530 }
1531 }
1532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001533
satoke7c6998e2011-06-03 17:57:59 +09001534 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 public void finishInput(IInputMethodClient client) {
1536 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001537
satoke7c6998e2011-06-03 17:57:59 +09001538 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 public void onServiceConnected(ComponentName name, IBinder service) {
1540 synchronized (mMethodMap) {
1541 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1542 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001543 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001544 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001545 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001546 return;
1547 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001548 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001549 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1550 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 clearClientSessionLocked(mCurClient);
1553 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
1555 }
1556 }
1557 }
1558
Jeff Brownc28867a2013-03-26 15:42:39 -07001559 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1560 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 synchronized (mMethodMap) {
1562 if (mCurMethod != null && method != null
1563 && mCurMethod.asBinder() == method.asBinder()) {
1564 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001565 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001567 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001568 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 if (res.method != null) {
1570 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001571 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001573 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575 }
1576 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001577
1578 // Session abandoned. Close its associated input channel.
1579 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001582 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001583 if (mVisibleBound) {
1584 mContext.unbindService(mVisibleConnection);
1585 mVisibleBound = false;
1586 }
1587
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001588 if (mHaveConnection) {
1589 mContext.unbindService(this);
1590 mHaveConnection = false;
1591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001592
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001593 if (mCurToken != null) {
1594 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001595 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001596 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001597 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001598 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001599 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001600 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001601 } catch (RemoteException e) {
1602 }
1603 mCurToken = null;
1604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001605
The Android Open Source Project10592532009-03-18 17:39:46 -07001606 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001607 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
Yohei Yukawa33e81792015-11-17 21:14:42 -08001610 void resetCurrentMethodAndClient(
1611 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001612 mCurMethodId = null;
1613 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001614 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001615 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001616
1617 void requestClientSessionLocked(ClientState cs) {
1618 if (!cs.sessionRequested) {
1619 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1620 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1621 cs.sessionRequested = true;
1622 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1623 MSG_CREATE_SESSION, mCurMethod, channels[1],
1624 new MethodCallback(this, mCurMethod, channels[0])));
1625 }
1626 }
1627
1628 void clearClientSessionLocked(ClientState cs) {
1629 finishSessionLocked(cs.curSession);
1630 cs.curSession = null;
1631 cs.sessionRequested = false;
1632 }
1633
1634 private void finishSessionLocked(SessionState sessionState) {
1635 if (sessionState != null) {
1636 if (sessionState.session != null) {
1637 try {
1638 sessionState.session.finishSession();
1639 } catch (RemoteException e) {
1640 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001641 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001642 }
1643 sessionState.session = null;
1644 }
1645 if (sessionState.channel != null) {
1646 sessionState.channel.dispose();
1647 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001648 }
1649 }
1650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001652 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (mCurMethod != null) {
1654 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001655 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001657
Jeff Brownc28867a2013-03-26 15:42:39 -07001658 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001659 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 mCurMethod = null;
1661 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001662 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001663 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
satoke7c6998e2011-06-03 17:57:59 +09001667 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001669 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1670 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1671 // which is irregular but can eventually happen for everyone just by continuing using the
1672 // device. Thus it is important to make sure that all the internal states are properly
1673 // refreshed when this method is called back. Running
1674 // adb install -r <APK that implements the current IME>
1675 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001677 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 + " mCurIntent=" + mCurIntent);
1679 if (mCurMethod != null && mCurIntent != null
1680 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001681 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 // We consider this to be a new bind attempt, since the system
1683 // should now try to restart the service for us.
1684 mLastBindTime = SystemClock.uptimeMillis();
1685 mShowRequested = mInputShown;
1686 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001687 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
1689 }
1690 }
1691
satokf9f01002011-05-19 21:31:50 +09001692 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1694 long ident = Binder.clearCallingIdentity();
1695 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001697 if (!calledWithValidToken(token)) {
1698 final int uid = Binder.getCallingUid();
1699 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1700 + " token:" + token);
1701 return;
1702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001704 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001705 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001706 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001709 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001710 CharSequence contentDescription = null;
1711 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001712 // Use PackageManager to load label
1713 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001714 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001715 mIPackageManager.getApplicationInfo(packageName, 0,
1716 mSettings.getCurrentUserId()));
1717 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001718 /* ignore */
1719 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001720 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001721 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001722 contentDescription != null
1723 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001724 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 }
1728 } finally {
1729 Binder.restoreCallingIdentity(ident);
1730 }
1731 }
1732
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001733 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001734 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001735 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001736 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001737 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001738 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001739 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1740 // When physical keyboard is attached, we show the ime switcher (or notification if
1741 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1742 // exists in the IME switcher dialog. Might be OK to remove this condition once
1743 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1744 return true;
1745 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001746 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1747 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001748 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001749
1750 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1751 final int N = imis.size();
1752 if (N > 2) return true;
1753 if (N < 1) return false;
1754 int nonAuxCount = 0;
1755 int auxCount = 0;
1756 InputMethodSubtype nonAuxSubtype = null;
1757 InputMethodSubtype auxSubtype = null;
1758 for(int i = 0; i < N; ++i) {
1759 final InputMethodInfo imi = imis.get(i);
1760 final List<InputMethodSubtype> subtypes =
1761 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1762 final int subtypeCount = subtypes.size();
1763 if (subtypeCount == 0) {
1764 ++nonAuxCount;
1765 } else {
1766 for (int j = 0; j < subtypeCount; ++j) {
1767 final InputMethodSubtype subtype = subtypes.get(j);
1768 if (!subtype.isAuxiliary()) {
1769 ++nonAuxCount;
1770 nonAuxSubtype = subtype;
1771 } else {
1772 ++auxCount;
1773 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001774 }
1775 }
satok7cfc0ed2011-06-20 21:29:36 +09001776 }
1777 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001778 if (nonAuxCount > 1 || auxCount > 1) {
1779 return true;
1780 } else if (nonAuxCount == 1 && auxCount == 1) {
1781 if (nonAuxSubtype != null && auxSubtype != null
1782 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1783 || auxSubtype.overridesImplicitlyEnabledSubtype()
1784 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1785 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1786 return false;
1787 }
1788 return true;
1789 }
1790 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001791 }
1792
John Spurlocke0980502013-10-25 11:59:29 -04001793 private boolean isKeyguardLocked() {
1794 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1795 }
1796
satokdbf29502011-08-25 15:28:23 +09001797 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001798 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001799 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001800 if (!calledWithValidToken(token)) {
1801 final int uid = Binder.getCallingUid();
1802 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1803 + " token:" + token);
1804 return;
1805 }
1806
1807 synchronized (mMethodMap) {
1808 mImeWindowVis = vis;
1809 mBackDisposition = backDisposition;
1810 updateSystemUiLocked(token, vis, backDisposition);
1811 }
1812 }
1813
1814 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1815 synchronized (mMethodMap) {
1816 updateSystemUiLocked(token, vis, backDisposition);
1817 }
1818 }
1819
1820 // Caution! This method is called in this class. Handle multi-user carefully
1821 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1822 if (!calledWithValidToken(token)) {
1823 final int uid = Binder.getCallingUid();
1824 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1825 + " token:" + token);
1826 return;
1827 }
1828
1829 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1830 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001831 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001832 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001833 // apply policy for binder calls
1834 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1835 vis = 0;
satok06487a52010-10-29 11:37:18 +09001836 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001837 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1838 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1839 if (mStatusBar != null) {
1840 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1841 needsToShowImeSwitcher);
1842 }
1843 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1844 if (imi != null && needsToShowImeSwitcher) {
1845 // Used to load label
1846 final CharSequence title = mRes.getText(
1847 com.android.internal.R.string.select_input_method);
1848 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1849 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001850 mImeSwitcherNotification.setContentTitle(title)
1851 .setContentText(summary)
1852 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001853 try {
1854 if ((mNotificationManager != null)
1855 && !mIWindowManager.hasNavigationBar()) {
1856 if (DEBUG) {
1857 Slog.d(TAG, "--- show notification: label = " + summary);
1858 }
1859 mNotificationManager.notifyAsUser(null,
1860 com.android.internal.R.string.select_input_method,
1861 mImeSwitcherNotification.build(), UserHandle.ALL);
1862 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001863 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001864 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001865 }
1866 } else {
1867 if (mNotificationShown && mNotificationManager != null) {
1868 if (DEBUG) {
1869 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001870 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001871 mNotificationManager.cancelAsUser(null,
1872 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1873 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001874 }
satok06487a52010-10-29 11:37:18 +09001875 }
1876 } finally {
1877 Binder.restoreCallingIdentity(ident);
1878 }
1879 }
1880
satoke7c6998e2011-06-03 17:57:59 +09001881 @Override
satokf9f01002011-05-19 21:31:50 +09001882 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001883 if (!calledFromValidUser()) {
1884 return;
1885 }
satokf9f01002011-05-19 21:31:50 +09001886 synchronized (mMethodMap) {
1887 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1888 for (int i = 0; i < spans.length; ++i) {
1889 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001890 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001891 mSecureSuggestionSpans.put(ss, currentImi);
1892 }
1893 }
1894 }
1895 }
1896
satoke7c6998e2011-06-03 17:57:59 +09001897 @Override
satokf9f01002011-05-19 21:31:50 +09001898 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001899 if (!calledFromValidUser()) {
1900 return false;
1901 }
satokf9f01002011-05-19 21:31:50 +09001902 synchronized (mMethodMap) {
1903 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1904 // TODO: Do not send the intent if the process of the targetImi is already dead.
1905 if (targetImi != null) {
1906 final String[] suggestions = span.getSuggestions();
1907 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001908 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001909 final Intent intent = new Intent();
1910 // Ensures that only a class in the original IME package will receive the
1911 // notification.
satok42c5a162011-05-26 16:46:14 +09001912 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001913 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1914 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1915 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1916 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001917 final long ident = Binder.clearCallingIdentity();
1918 try {
1919 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1920 } finally {
1921 Binder.restoreCallingIdentity(ident);
1922 }
satokf9f01002011-05-19 21:31:50 +09001923 return true;
1924 }
1925 }
1926 return false;
1927 }
1928
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001929 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001930 updateInputMethodsFromSettingsLocked(enabledMayChange);
1931 updateKeyboardFromSettingsLocked();
1932 }
1933
1934 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001935 if (enabledMayChange) {
1936 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1937 for (int i=0; i<enabled.size(); i++) {
1938 // We allow the user to select "disabled until used" apps, so if they
1939 // are enabling one of those here we now need to make it enabled.
1940 InputMethodInfo imm = enabled.get(i);
1941 try {
1942 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1943 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1944 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001945 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001946 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001947 if (DEBUG) {
1948 Slog.d(TAG, "Update state(" + imm.getId()
1949 + "): DISABLED_UNTIL_USED -> DEFAULT");
1950 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001951 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1952 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001953 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1954 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001955 }
1956 } catch (RemoteException e) {
1957 }
1958 }
1959 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001960 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1961 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1962 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1963 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001964 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001965 // There is no input method selected, try to choose new applicable input method.
1966 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001967 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001968 }
1969 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001971 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001973 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001974 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
satokf3db1af2010-11-23 13:34:33 +09001976 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001977 } else {
1978 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001979 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001981 // Here is not the perfect place to reset the switching controller. Ideally
1982 // mSwitchingController and mSettings should be able to share the same state.
1983 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1984 // the same enabled IMEs list.
1985 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001986
1987 }
1988
1989 public void updateKeyboardFromSettingsLocked() {
1990 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1991 if (mSwitchingDialog != null
1992 && mSwitchingDialogTitleView != null
1993 && mSwitchingDialog.isShowing()) {
1994 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1995 com.android.internal.R.id.hard_keyboard_switch);
1996 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1997 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001999
Yohei Yukawab097b822015-12-01 10:43:08 -08002000 private void notifyInputMethodSubtypeChanged(final int userId,
2001 @Nullable final InputMethodInfo inputMethodInfo,
2002 @Nullable final InputMethodSubtype subtype) {
2003 final InputManagerInternal inputManagerInternal =
2004 LocalServices.getService(InputManagerInternal.class);
2005 if (inputManagerInternal != null) {
2006 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2007 }
2008 }
2009
satokab751aa2010-09-14 19:17:36 +09002010 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 InputMethodInfo info = mMethodMap.get(id);
2012 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002013 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002015
satokd81e9502012-05-21 12:58:45 +09002016 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002018 final int subtypeCount = info.getSubtypeCount();
2019 if (subtypeCount <= 0) {
2020 return;
satokcd7cd292010-11-20 15:46:23 +09002021 }
satokd81e9502012-05-21 12:58:45 +09002022 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2023 final InputMethodSubtype newSubtype;
2024 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2025 newSubtype = info.getSubtypeAt(subtypeId);
2026 } else {
2027 // If subtype is null, try to find the most applicable one from
2028 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002029 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002030 }
2031 if (newSubtype == null || oldSubtype == null) {
2032 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2033 + ", new subtype = " + newSubtype);
2034 return;
2035 }
2036 if (newSubtype != oldSubtype) {
2037 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2038 if (mCurMethod != null) {
2039 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002040 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002041 mCurMethod.changeInputMethodSubtype(newSubtype);
2042 } catch (RemoteException e) {
2043 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002044 return;
satokab751aa2010-09-14 19:17:36 +09002045 }
2046 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002047 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 return;
2050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002051
satokd81e9502012-05-21 12:58:45 +09002052 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 final long ident = Binder.clearCallingIdentity();
2054 try {
satokab751aa2010-09-14 19:17:36 +09002055 // Set a subtype to this input method.
2056 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002057 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2058 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2059 // because mCurMethodId is stored as a history in
2060 // setSelectedInputMethodAndSubtypeLocked().
2061 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002063 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002065 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002067 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002069 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 } finally {
2071 Binder.restoreCallingIdentity(ident);
2072 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002073
2074 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2075 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002077
satok42c5a162011-05-26 16:46:14 +09002078 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002079 public boolean showSoftInput(IInputMethodClient client, int flags,
2080 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002081 if (!calledFromValidUser()) {
2082 return false;
2083 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002084 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 long ident = Binder.clearCallingIdentity();
2086 try {
2087 synchronized (mMethodMap) {
2088 if (mCurClient == null || client == null
2089 || mCurClient.client.asBinder() != client.asBinder()) {
2090 try {
2091 // We need to check if this is the current client with
2092 // focus in the window manager, to allow this call to
2093 // be made before input is started in it.
2094 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002095 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002096 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
2098 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002099 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
2101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002102
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002103 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002104 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
2106 } finally {
2107 Binder.restoreCallingIdentity(ident);
2108 }
2109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002110
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002113 if (mAccessibilityRequestingNoSoftKeyboard) {
2114 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 }
Anna Galusza9b278112016-01-04 11:37:37 -08002116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2118 mShowExplicitlyRequested = true;
2119 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002120 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2121 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002123
Dianne Hackborncc278702009-09-02 23:07:23 -07002124 if (!mSystemReady) {
2125 return false;
2126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002127
The Android Open Source Project4df24232009-03-05 14:34:35 -08002128 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002130 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2132 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2133 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002135 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002136 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002137 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002138 | Context.BIND_TREAT_LIKE_ACTIVITY
2139 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002140 mVisibleBound = true;
2141 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002142 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002144 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 // The client has asked to have the input method shown, but
2146 // we have been sitting here too long with a connection to the
2147 // service and no interface received, so let's disconnect/connect
2148 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002149 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002151 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002153 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002154 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002155 } else {
2156 if (DEBUG) {
2157 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2158 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002161
The Android Open Source Project4df24232009-03-05 14:34:35 -08002162 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002164
satok42c5a162011-05-26 16:46:14 +09002165 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002166 public boolean hideSoftInput(IInputMethodClient client, int flags,
2167 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002168 if (!calledFromValidUser()) {
2169 return false;
2170 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002171 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 long ident = Binder.clearCallingIdentity();
2173 try {
2174 synchronized (mMethodMap) {
2175 if (mCurClient == null || client == null
2176 || mCurClient.client.asBinder() != client.asBinder()) {
2177 try {
2178 // We need to check if this is the current client with
2179 // focus in the window manager, to allow this call to
2180 // be made before input is started in it.
2181 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002182 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2183 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002184 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
2186 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002187 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
2189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002190
Joe Onorato8a9b2202010-02-26 18:56:32 -08002191 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002192 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 }
2194 } finally {
2195 Binder.restoreCallingIdentity(ident);
2196 }
2197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002198
The Android Open Source Project4df24232009-03-05 14:34:35 -08002199 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2201 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002202 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 -08002203 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
2205 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002206 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 -08002207 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002209
2210 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2211 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2212 // to be updated with the new value sent from IME process. Even in such a transient state
2213 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2214 // application process as a valid request, and have even promised such a behavior with CTS
2215 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2216 // IMMS#InputShown indicates that the software keyboard is shown.
2217 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2218 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2219 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002220 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002221 if (shouldHideSoftInput) {
2222 // The IME will report its visible state again after the following message finally
2223 // delivered to the IME process as an IPC. Hence the inconsistency between
2224 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2225 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002226 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2227 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2228 res = true;
2229 } else {
2230 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002232 if (mHaveConnection && mVisibleBound) {
2233 mContext.unbindService(mVisibleConnection);
2234 mVisibleBound = false;
2235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 mInputShown = false;
2237 mShowRequested = false;
2238 mShowExplicitlyRequested = false;
2239 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002240 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002242
satok42c5a162011-05-26 16:46:14 +09002243 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002244 public InputBindResult startInputOrWindowGainedFocus(
2245 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2246 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002247 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002248 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002249 if (windowToken != null) {
2250 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002251 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002252 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002253 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2254 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002255 }
2256 }
2257
2258 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002259 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2260 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002261 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2262 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002263 // Needs to check the validity before clearing calling identity
2264 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002265 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 long ident = Binder.clearCallingIdentity();
2267 try {
2268 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002269 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2270 + InputMethodClient.getStartInputReason(startInputReason)
2271 + " client=" + client.asBinder()
2272 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002273 + " missingMethods="
2274 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002275 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002276 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002278 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002279
Dianne Hackborn7663d802012-02-24 13:08:49 -08002280 ClientState cs = mClients.get(client.asBinder());
2281 if (cs == null) {
2282 throw new IllegalArgumentException("unknown client "
2283 + client.asBinder());
2284 }
2285
2286 try {
2287 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2288 // Check with the window manager to make sure this client actually
2289 // has a window with focus. If not, reject. This is thread safe
2290 // because if the focus changes some time before or after, the
2291 // next client receiving focus that has any interest in input will
2292 // be calling through here after that change happens.
2293 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2294 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2295 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002297 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002299
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002300 if (!calledFromValidUser) {
2301 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2302 Slog.w(TAG, "If you want to interect with IME, you need "
2303 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2304 hideCurrentInputLocked(0, null);
2305 return null;
2306 }
2307
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002308 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002309 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002310 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002311 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002312 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2313 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002314 }
2315 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002316 }
2317 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002318 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002319
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002320 // Should we auto-show the IME even if the caller has not
2321 // specified what should be done with it?
2322 // We only do this automatically if the window can resize
2323 // to accommodate the IME (so what the user sees will give
2324 // them good context without input information being obscured
2325 // by the IME) or if running on a large screen where there
2326 // is more room for the target window + IME.
2327 final boolean doAutoShow =
2328 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2329 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2330 || mRes.getConfiguration().isLayoutSizeAtLeast(
2331 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002332 final boolean isTextEditor =
2333 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2334
2335 // We want to start input before showing the IME, but after closing
2336 // it. We want to do this after closing it to help the IME disappear
2337 // more quickly (not get stuck behind it initializing itself for the
2338 // new focused input, even if its window wants to hide the IME).
2339 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2342 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002343 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2345 // There is no focus view, and this window will
2346 // be behind any soft input window, so hide the
2347 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002348 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002349 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002351 } else if (isTextEditor && doAutoShow && (softInputMode &
2352 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 // There is a focus view, and we are navigating forward
2354 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002355 // We only do this automatically if the window can resize
2356 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002357 // them good context without input information being obscured
2358 // by the IME) or if running on a large screen where there
2359 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002360 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002361 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002362 res = startInputUncheckedLocked(cs, inputContext,
2363 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002364 didStart = true;
2365 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002366 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 break;
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2370 // Do nothing.
2371 break;
2372 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2373 if ((softInputMode &
2374 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002375 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002376 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 }
2378 break;
2379 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002380 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002381 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 break;
2383 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2384 if ((softInputMode &
2385 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002386 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002387 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002388 res = startInputUncheckedLocked(cs, inputContext,
2389 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002390 didStart = true;
2391 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002392 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
2394 break;
2395 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002396 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002397 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002398 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2399 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002400 didStart = true;
2401 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002402 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 break;
2404 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002405
2406 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002407 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002408 controlFlags);
2409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
2411 } finally {
2412 Binder.restoreCallingIdentity(ident);
2413 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002414
2415 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002417
satok42c5a162011-05-26 16:46:14 +09002418 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002419 public void showInputMethodPickerFromClient(
2420 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002421 if (!calledFromValidUser()) {
2422 return;
2423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 synchronized (mMethodMap) {
2425 if (mCurClient == null || client == null
2426 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002427 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002428 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430
satok440aab52010-11-25 09:43:11 +09002431 // Always call subtype picker, because subtype picker is a superset of input method
2432 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002433 mHandler.sendMessage(mCaller.obtainMessageI(
2434 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002435 }
2436 }
2437
satok42c5a162011-05-26 16:46:14 +09002438 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002440 if (!calledFromValidUser()) {
2441 return;
2442 }
satok28203512010-11-24 11:06:49 +09002443 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2444 }
2445
satok42c5a162011-05-26 16:46:14 +09002446 @Override
satok28203512010-11-24 11:06:49 +09002447 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002448 if (!calledFromValidUser()) {
2449 return;
2450 }
satok28203512010-11-24 11:06:49 +09002451 synchronized (mMethodMap) {
2452 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002453 setInputMethodWithSubtypeIdLocked(token, id,
2454 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2455 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002456 } else {
2457 setInputMethod(token, id);
2458 }
2459 }
satokab751aa2010-09-14 19:17:36 +09002460 }
2461
satok42c5a162011-05-26 16:46:14 +09002462 @Override
satokb416a712010-11-25 20:42:14 +09002463 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002464 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002465 if (!calledFromValidUser()) {
2466 return;
2467 }
satokb416a712010-11-25 20:42:14 +09002468 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002469 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2470 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002471 }
2472 }
2473
satok4fc87d62011-05-20 16:13:43 +09002474 @Override
satok735cf382010-11-11 20:40:09 +09002475 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002476 if (!calledFromValidUser()) {
2477 return false;
2478 }
satok735cf382010-11-11 20:40:09 +09002479 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002480 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002481 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002482 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002483 lastImi = mMethodMap.get(lastIme.first);
2484 } else {
2485 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002486 }
satok4fc87d62011-05-20 16:13:43 +09002487 String targetLastImiId = null;
2488 int subtypeId = NOT_A_SUBTYPE_ID;
2489 if (lastIme != null && lastImi != null) {
2490 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002491 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002492 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2493 : mCurrentSubtype.hashCode();
2494 // If the last IME is the same as the current IME and the last subtype is not
2495 // defined, there is no need to switch to the last IME.
2496 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2497 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002498 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002499 }
2500 }
2501
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002502 if (TextUtils.isEmpty(targetLastImiId)
2503 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002504 // This is a safety net. If the currentSubtype can't be added to the history
2505 // and the framework couldn't find the last ime, we will make the last ime be
2506 // the most applicable enabled keyboard subtype of the system imes.
2507 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2508 if (enabled != null) {
2509 final int N = enabled.size();
2510 final String locale = mCurrentSubtype == null
2511 ? mRes.getConfiguration().locale.toString()
2512 : mCurrentSubtype.getLocale();
2513 for (int i = 0; i < N; ++i) {
2514 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002515 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002516 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002517 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2518 InputMethodUtils.getSubtypes(imi),
2519 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002520 if (keyboardSubtype != null) {
2521 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002522 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002523 imi, keyboardSubtype.hashCode());
2524 if(keyboardSubtype.getLocale().equals(locale)) {
2525 break;
2526 }
2527 }
2528 }
2529 }
2530 }
2531 }
2532
2533 if (!TextUtils.isEmpty(targetLastImiId)) {
2534 if (DEBUG) {
2535 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2536 + ", from: " + mCurMethodId + ", " + subtypeId);
2537 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002538 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002539 return true;
2540 } else {
2541 return false;
2542 }
satok735cf382010-11-11 20:40:09 +09002543 }
2544 }
2545
satoke7c6998e2011-06-03 17:57:59 +09002546 @Override
satok688bd472012-02-09 20:09:17 +09002547 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002548 if (!calledFromValidUser()) {
2549 return false;
2550 }
satok688bd472012-02-09 20:09:17 +09002551 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002552 if (!calledWithValidToken(token)) {
2553 final int uid = Binder.getCallingUid();
2554 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2555 + " token:" + token);
2556 return false;
2557 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002558 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002559 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2560 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002561 if (nextSubtype == null) {
2562 return false;
2563 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002564 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2565 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002566 return true;
2567 }
2568 }
2569
2570 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002571 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2572 if (!calledFromValidUser()) {
2573 return false;
2574 }
2575 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002576 if (!calledWithValidToken(token)) {
2577 final int uid = Binder.getCallingUid();
2578 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2579 + "token. uid:" + uid + " token:" + token);
2580 return false;
2581 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002582 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002583 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2584 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002585 if (nextSubtype == null) {
2586 return false;
2587 }
2588 return true;
2589 }
2590 }
2591
2592 @Override
satok68f1b782011-04-11 14:26:04 +09002593 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002594 if (!calledFromValidUser()) {
2595 return null;
2596 }
satok68f1b782011-04-11 14:26:04 +09002597 synchronized (mMethodMap) {
2598 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2599 // TODO: Handle the case of the last IME with no subtypes
2600 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2601 || TextUtils.isEmpty(lastIme.second)) return null;
2602 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2603 if (lastImi == null) return null;
2604 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002605 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002606 final int lastSubtypeId =
2607 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002608 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2609 return null;
2610 }
2611 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002612 } catch (NumberFormatException e) {
2613 return null;
2614 }
2615 }
2616 }
2617
satoke7c6998e2011-06-03 17:57:59 +09002618 @Override
satokee5e77c2011-09-02 18:50:15 +09002619 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002620 if (!calledFromValidUser()) {
2621 return;
2622 }
satok91e88122011-07-18 11:11:42 +09002623 // By this IPC call, only a process which shares the same uid with the IME can add
2624 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002625 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002626 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002627 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002628 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002629 final String[] packageInfos;
2630 try {
2631 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2632 } catch (RemoteException e) {
2633 Slog.e(TAG, "Failed to get package infos");
2634 return;
2635 }
satok91e88122011-07-18 11:11:42 +09002636 if (packageInfos != null) {
2637 final int packageNum = packageInfos.length;
2638 for (int i = 0; i < packageNum; ++i) {
2639 if (packageInfos[i].equals(imi.getPackageName())) {
2640 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002641 final long ident = Binder.clearCallingIdentity();
2642 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002643 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002644 } finally {
2645 Binder.restoreCallingIdentity(ident);
2646 }
satokee5e77c2011-09-02 18:50:15 +09002647 return;
satok91e88122011-07-18 11:11:42 +09002648 }
2649 }
2650 }
satoke7c6998e2011-06-03 17:57:59 +09002651 }
satokee5e77c2011-09-02 18:50:15 +09002652 return;
satoke7c6998e2011-06-03 17:57:59 +09002653 }
2654
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002655 @Override
2656 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002657 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002658 }
2659
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002660 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002661 public void clearLastInputMethodWindowForTransition(IBinder token) {
2662 if (!calledFromValidUser()) {
2663 return;
2664 }
2665 final long ident = Binder.clearCallingIdentity();
2666 try {
2667 synchronized (mMethodMap) {
2668 if (!calledWithValidToken(token)) {
2669 final int uid = Binder.getCallingUid();
2670 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2671 + "invalid token. uid:" + uid + " token:" + token);
2672 return;
2673 }
2674 }
2675 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2676 } finally {
2677 Binder.restoreCallingIdentity(ident);
2678 }
2679 }
2680
2681 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002682 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002683 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002684 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002685 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002686 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002687 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2688 if (DEBUG) {
2689 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2690 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2691 + " actual: " + sequenceNumber);
2692 }
2693 return;
2694 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002695 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2696 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002697 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002698 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002699 }
2700 }
2701
satok28203512010-11-24 11:06:49 +09002702 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002704 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2705 }
2706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002708 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2709 if (token == null) {
2710 if (mContext.checkCallingOrSelfPermission(
2711 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2712 != PackageManager.PERMISSION_GRANTED) {
2713 throw new SecurityException(
2714 "Using null token requires permission "
2715 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002717 } else if (mCurToken != token) {
2718 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2719 + " token: " + token);
2720 return;
2721 }
2722
2723 final long ident = Binder.clearCallingIdentity();
2724 try {
2725 setInputMethodLocked(id, subtypeId);
2726 } finally {
2727 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 }
2729 }
2730
satok42c5a162011-05-26 16:46:14 +09002731 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002733 if (!calledFromValidUser()) {
2734 return;
2735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002737 if (!calledWithValidToken(token)) {
2738 final int uid = Binder.getCallingUid();
2739 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2740 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 return;
2742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 long ident = Binder.clearCallingIdentity();
2744 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002745 hideCurrentInputLocked(flags, null);
2746 } finally {
2747 Binder.restoreCallingIdentity(ident);
2748 }
2749 }
2750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002751
satok42c5a162011-05-26 16:46:14 +09002752 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002753 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002754 if (!calledFromValidUser()) {
2755 return;
2756 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002757 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002758 if (!calledWithValidToken(token)) {
2759 final int uid = Binder.getCallingUid();
2760 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2761 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002762 return;
2763 }
2764 long ident = Binder.clearCallingIdentity();
2765 try {
2766 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 } finally {
2768 Binder.restoreCallingIdentity(ident);
2769 }
2770 }
2771 }
2772
2773 void setEnabledSessionInMainThread(SessionState session) {
2774 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002775 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002777 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002778 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 } catch (RemoteException e) {
2780 }
2781 }
2782 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002783 if (mEnabledSession != null && mEnabledSession.session != null) {
2784 try {
2785 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2786 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2787 } catch (RemoteException e) {
2788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 }
2790 }
2791 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002792
satok42c5a162011-05-26 16:46:14 +09002793 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002795 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002797 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002798 final boolean showAuxSubtypes;
2799 switch (msg.arg1) {
2800 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2801 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2802 // implemented so that auxiliary subtypes will be excluded when the soft
2803 // keyboard is invisible.
2804 showAuxSubtypes = mInputShown;
2805 break;
2806 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2807 showAuxSubtypes = true;
2808 break;
2809 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2810 showAuxSubtypes = false;
2811 break;
2812 default:
2813 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2814 return false;
2815 }
2816 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002817 return true;
2818
satok47a44912010-10-06 16:03:58 +09002819 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002820 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002821 return true;
2822
2823 case MSG_SHOW_IM_CONFIG:
2824 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002825 return true;
2826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 case MSG_UNBIND_INPUT:
2830 try {
2831 ((IInputMethod)msg.obj).unbindInput();
2832 } catch (RemoteException e) {
2833 // There is nothing interesting about the method dying.
2834 }
2835 return true;
2836 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002837 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 try {
2839 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2840 } catch (RemoteException e) {
2841 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002842 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 return true;
2844 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002845 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002847 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002848 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002849 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 } catch (RemoteException e) {
2851 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002852 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 return true;
2854 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002855 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002857 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002858 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002859 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 } catch (RemoteException e) {
2861 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002862 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002864 case MSG_HIDE_CURRENT_INPUT_METHOD:
2865 synchronized (mMethodMap) {
2866 hideCurrentInputLocked(0, null);
2867 }
2868 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002870 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002872 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2874 } catch (RemoteException e) {
2875 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002876 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002878 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002879 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002880 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002881 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002883 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002885 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002886 // Dispose the channel if the input method is not local to this process
2887 // because the remote proxy will get its own copy when unparceled.
2888 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002889 channel.dispose();
2890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002892 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002896
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002897 case MSG_START_INPUT: {
2898 int missingMethods = msg.arg1;
2899 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002901 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002903 session.method.startInput((IInputContext) args.arg2, missingMethods,
2904 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 } catch (RemoteException e) {
2906 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002907 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002909 }
2910 case MSG_RESTART_INPUT: {
2911 int missingMethods = msg.arg1;
2912 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002914 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002916 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2917 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 } catch (RemoteException e) {
2919 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002920 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002925
Yohei Yukawa33e81792015-11-17 21:14:42 -08002926 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002928 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 } catch (RemoteException e) {
2930 // There is nothing interesting about the last client dying.
2931 }
2932 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002933 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002934 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002935 IInputMethodClient client = (IInputMethodClient)args.arg1;
2936 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002938 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002940 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002941 } finally {
2942 // Dispose the channel if the input method is not local to this process
2943 // because the remote proxy will get its own copy when unparceled.
2944 if (res.channel != null && Binder.isProxy(client)) {
2945 res.channel.dispose();
2946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002948 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002950 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002951 case MSG_SET_ACTIVE:
2952 try {
2953 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2954 } catch (RemoteException e) {
2955 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2956 + ((ClientState)msg.obj).pid + " uid "
2957 + ((ClientState)msg.obj).uid);
2958 }
2959 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002960 case MSG_SET_INTERACTIVE:
2961 handleSetInteractive(msg.arg1 != 0);
2962 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002963 case MSG_SWITCH_IME:
2964 handleSwitchInputMethod(msg.arg1 != 0);
2965 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002966 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2967 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002968 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002969 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002970 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002971 } catch (RemoteException e) {
2972 Slog.w(TAG, "Got RemoteException sending "
2973 + "setUserActionNotificationSequenceNumber("
2974 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002975 + clientState.pid + " uid "
2976 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002977 }
2978 return true;
2979 }
satok01038492012-04-09 21:08:27 +09002980
2981 // --------------------------------------------------------------
2982 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002983 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002984 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002985 case MSG_SYSTEM_UNLOCK_USER:
2986 final int userId = msg.arg1;
2987 onUnlockUser(userId);
2988 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 }
2990 return false;
2991 }
2992
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002993 private void handleSetInteractive(final boolean interactive) {
2994 synchronized (mMethodMap) {
2995 mIsInteractive = interactive;
2996 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2997
2998 // Inform the current client of the change in active status
2999 if (mCurClient != null && mCurClient.client != null) {
3000 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3001 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
3002 }
3003 }
3004 }
3005
Yohei Yukawaae61f712015-12-09 13:00:10 -08003006 private void handleSwitchInputMethod(final boolean forwardDirection) {
3007 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003008 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003009 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003010 if (nextSubtype == null) {
3011 return;
3012 }
3013 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003014 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3015 if (newInputMethodInfo == null) {
3016 return;
3017 }
3018 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3019 newInputMethodInfo, mCurrentSubtype);
3020 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003021 if (mSubtypeSwitchedByShortCutToast == null) {
3022 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3023 Toast.LENGTH_SHORT);
3024 } else {
3025 mSubtypeSwitchedByShortCutToast.setText(toastText);
3026 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003027 mSubtypeSwitchedByShortCutToast.show();
3028 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003029 }
3030 }
3031
satokdc9ddae2011-10-06 12:22:36 +09003032 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003033 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3034 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003035 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003036 if (DEBUG) {
3037 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3038 }
satok723a27e2010-11-11 14:58:11 +09003039 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003040 return true;
3041 }
3042
3043 return false;
3044 }
3045
Yohei Yukawa94e33302016-02-12 19:37:03 -08003046 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003047 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003048 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003049 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003050 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003051 mMethodList.clear();
3052 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003053
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003054 // Use for queryIntentServicesAsUser
3055 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003057 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3058 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3059 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003060 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003062 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3063 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003064
satoke7c6998e2011-06-03 17:57:59 +09003065 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3066 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 for (int i = 0; i < services.size(); ++i) {
3068 ResolveInfo ri = services.get(i);
3069 ServiceInfo si = ri.serviceInfo;
3070 ComponentName compName = new ComponentName(si.packageName, si.name);
3071 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3072 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003073 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 + ": it does not require the permission "
3075 + android.Manifest.permission.BIND_INPUT_METHOD);
3076 continue;
3077 }
3078
Joe Onorato8a9b2202010-02-26 18:56:32 -08003079 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080
3081 try {
satoke7c6998e2011-06-03 17:57:59 +09003082 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003083 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003084 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003085 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086
3087 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003088 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003090 } catch (Exception e) {
3091 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 }
3093 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003094
Yohei Yukawa859df052016-02-17 07:56:46 -08003095 // TODO: The following code should find better place to live.
3096 if (!resetDefaultEnabledIme) {
3097 boolean enabledImeFound = false;
3098 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3099 final int N = enabledImes.size();
3100 for (int i = 0; i < N; ++i) {
3101 final InputMethodInfo imi = enabledImes.get(i);
3102 if (mMethodList.contains(imi)) {
3103 enabledImeFound = true;
3104 break;
3105 }
3106 }
3107 if (!enabledImeFound) {
3108 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3109 resetDefaultEnabledIme = true;
3110 resetSelectedInputMethodAndSubtypeLocked("");
3111 }
3112 }
3113
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003114 if (resetDefaultEnabledIme) {
3115 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003116 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003117 final int N = defaultEnabledIme.size();
3118 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003119 final InputMethodInfo imi = defaultEnabledIme.get(i);
3120 if (DEBUG) {
3121 Slog.d(TAG, "--- enable ime = " + imi);
3122 }
3123 setInputMethodEnabledLocked(imi.getId(), true);
3124 }
3125 }
3126
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003127 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003128 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003129 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003130 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3131 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003132 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003133 }
3134 } else {
3135 // Double check that the default IME is certainly enabled.
3136 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003137 }
3138 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003139 // Here is not the perfect place to reset the switching controller. Ideally
3140 // mSwitchingController and mSettings should be able to share the same state.
3141 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3142 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003143 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003147
satok217f5482010-12-15 05:19:19 +09003148 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003149 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003150 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003151 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3152 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003153 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003154 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003155 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003156 final int userId;
3157 synchronized (mMethodMap) {
3158 userId = mSettings.getCurrentUserId();
3159 }
3160 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003161 }
3162
3163 private void showConfigureInputMethods() {
3164 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3165 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3166 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3167 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003168 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003169 }
3170
satok2c93efc2012-04-02 19:33:47 +09003171 private boolean isScreenLocked() {
3172 return mKeyguardManager != null
3173 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3174 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003175
Seigo Nonaka14e13912015-05-06 21:04:13 -07003176 private void showInputMethodMenu(boolean showAuxSubtypes) {
3177 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003180 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003181
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003182 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003183 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003184 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003185
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003186 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003187 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003188 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3189 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003190 if (immis == null || immis.size() == 0) {
3191 return;
3192 }
3193
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003194 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195
satok688bd472012-02-09 20:09:17 +09003196 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003197 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003198 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003199
satokc3690562012-01-10 20:14:43 +09003200 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003201 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003202 if (currentSubtype != null) {
3203 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003204 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3205 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003206 }
3207 }
3208
Ken Wakasa761eb372011-03-04 19:06:18 +09003209 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003210 mIms = new InputMethodInfo[N];
3211 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003212 int checkedItem = 0;
3213 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003214 final ImeSubtypeListItem item = imList.get(i);
3215 mIms[i] = item.mImi;
3216 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003217 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003218 int subtypeId = mSubtypeIds[i];
3219 if ((subtypeId == NOT_A_SUBTYPE_ID)
3220 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3221 || (subtypeId == lastInputMethodSubtypeId)) {
3222 checkedItem = i;
3223 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003226
3227 final Context settingsContext = new ContextThemeWrapper(context,
3228 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3229
3230 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003231 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3232 @Override
3233 public void onCancel(DialogInterface dialog) {
3234 hideInputMethodMenu();
3235 }
3236 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003237
3238 final Context dialogContext = mDialogBuilder.getContext();
3239 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3240 com.android.internal.R.styleable.DialogPreference,
3241 com.android.internal.R.attr.alertDialogStyle, 0);
3242 final Drawable dialogIcon = a.getDrawable(
3243 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3244 a.recycle();
3245
3246 mDialogBuilder.setIcon(dialogIcon);
3247
Yohei Yukawad34e1482016-02-11 08:03:52 -08003248 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003249 final View tv = inflater.inflate(
3250 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3251 mDialogBuilder.setCustomTitle(tv);
3252
3253 // Setup layout for a toggle switch of the hardware keyboard
3254 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003255 mSwitchingDialogTitleView
3256 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003257 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003258 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003259 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003260 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003261 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003262 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3263 @Override
3264 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003265 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003266 // Ensure that the input method dialog is dismissed when changing
3267 // the hardware keyboard state.
3268 hideInputMethodMenu();
3269 }
3270 });
3271
Alan Viverette505e3ab2014-11-24 15:22:11 -08003272 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003273 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3274 final OnClickListener choiceListener = new OnClickListener() {
3275 @Override
3276 public void onClick(final DialogInterface dialog, final int which) {
3277 synchronized (mMethodMap) {
3278 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3279 || mSubtypeIds.length <= which) {
3280 return;
satok01038492012-04-09 21:08:27 +09003281 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003282 final InputMethodInfo im = mIms[which];
3283 int subtypeId = mSubtypeIds[which];
3284 adapter.mCheckedItem = which;
3285 adapter.notifyDataSetChanged();
3286 hideInputMethodMenu();
3287 if (im != null) {
3288 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3289 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003290 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003291 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003293 }
3294 }
3295 };
3296 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003299 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003300 final Window w = mSwitchingDialog.getWindow();
3301 final WindowManager.LayoutParams attrs = w.getAttributes();
3302 w.setType(TYPE_INPUT_METHOD_DIALOG);
3303 // Use an alternate token for the dialog for that window manager can group the token
3304 // with other IME windows based on type vs. grouping based on whichever token happens
3305 // to get selected by the system later on.
3306 attrs.token = mSwitchingDialogToken;
3307 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3308 attrs.setTitle("Select input method");
3309 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003310 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 mSwitchingDialog.show();
3312 }
3313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003314
Ken Wakasa05dbb652011-08-22 15:22:43 +09003315 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3316 private final LayoutInflater mInflater;
3317 private final int mTextViewResourceId;
3318 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003319 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003320 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3321 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3322 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003323
Ken Wakasa05dbb652011-08-22 15:22:43 +09003324 mTextViewResourceId = textViewResourceId;
3325 mItemsList = itemsList;
3326 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003327 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003328 }
3329
3330 @Override
3331 public View getView(int position, View convertView, ViewGroup parent) {
3332 final View view = convertView != null ? convertView
3333 : mInflater.inflate(mTextViewResourceId, null);
3334 if (position < 0 || position >= mItemsList.size()) return view;
3335 final ImeSubtypeListItem item = mItemsList.get(position);
3336 final CharSequence imeName = item.mImeName;
3337 final CharSequence subtypeName = item.mSubtypeName;
3338 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3339 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3340 if (TextUtils.isEmpty(subtypeName)) {
3341 firstTextView.setText(imeName);
3342 secondTextView.setVisibility(View.GONE);
3343 } else {
3344 firstTextView.setText(subtypeName);
3345 secondTextView.setText(imeName);
3346 secondTextView.setVisibility(View.VISIBLE);
3347 }
3348 final RadioButton radioButton =
3349 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3350 radioButton.setChecked(position == mCheckedItem);
3351 return view;
3352 }
3353 }
3354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003356 synchronized (mMethodMap) {
3357 hideInputMethodMenuLocked();
3358 }
3359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003360
The Android Open Source Project10592532009-03-18 17:39:46 -07003361 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003362 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363
The Android Open Source Project10592532009-03-18 17:39:46 -07003364 if (mSwitchingDialog != null) {
3365 mSwitchingDialog.dismiss();
3366 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003368
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003369 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003370 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003371 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003375
satok42c5a162011-05-26 16:46:14 +09003376 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003378 // TODO: Make this work even for non-current users?
3379 if (!calledFromValidUser()) {
3380 return false;
3381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 synchronized (mMethodMap) {
3383 if (mContext.checkCallingOrSelfPermission(
3384 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3385 != PackageManager.PERMISSION_GRANTED) {
3386 throw new SecurityException(
3387 "Requires permission "
3388 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3389 }
Anna Galusza9b278112016-01-04 11:37:37 -08003390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 long ident = Binder.clearCallingIdentity();
3392 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003393 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 } finally {
3395 Binder.restoreCallingIdentity(ident);
3396 }
3397 }
3398 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003399
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003400 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3401 // Make sure this is a valid input method.
3402 InputMethodInfo imm = mMethodMap.get(id);
3403 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003404 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003405 }
3406
satokd87c2592010-09-29 11:52:06 +09003407 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3408 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003409
satokd87c2592010-09-29 11:52:06 +09003410 if (enabled) {
3411 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3412 if (pair.first.equals(id)) {
3413 // We are enabling this input method, but it is already enabled.
3414 // Nothing to do. The previous state was enabled.
3415 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003416 }
3417 }
satokd87c2592010-09-29 11:52:06 +09003418 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3419 // Previous state was disabled.
3420 return false;
3421 } else {
3422 StringBuilder builder = new StringBuilder();
3423 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3424 builder, enabledInputMethodsList, id)) {
3425 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003426 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003427 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3428 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3429 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003430 }
3431 // Previous state was enabled.
3432 return true;
3433 } else {
3434 // We are disabling the input method but it is already disabled.
3435 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003436 return false;
3437 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003438 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003439 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003440
satok723a27e2010-11-11 14:58:11 +09003441 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3442 boolean setSubtypeOnly) {
3443 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003444 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003445
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003446 mCurUserActionNotificationSequenceNumber =
3447 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3448 if (DEBUG) {
3449 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3450 + mCurUserActionNotificationSequenceNumber);
3451 }
3452
3453 if (mCurClient != null && mCurClient.client != null) {
3454 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3455 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003456 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003457 }
3458
satok723a27e2010-11-11 14:58:11 +09003459 // Set Subtype here
3460 if (imi == null || subtypeId < 0) {
3461 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003462 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003463 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003464 if (subtypeId < imi.getSubtypeCount()) {
3465 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3466 mSettings.putSelectedSubtype(subtype.hashCode());
3467 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003468 } else {
3469 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003470 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003471 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003472 }
satokab751aa2010-09-14 19:17:36 +09003473 }
satok723a27e2010-11-11 14:58:11 +09003474
Yohei Yukawa68645a62016-02-17 07:54:20 -08003475 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003476 // Set InputMethod here
3477 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3478 }
3479 }
3480
3481 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3482 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3483 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3484 // newDefaultIme is empty when there is no candidate for the selected IME.
3485 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3486 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3487 if (subtypeHashCode != null) {
3488 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003489 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003490 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003491 } catch (NumberFormatException e) {
3492 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3493 }
3494 }
3495 }
3496 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003497 }
3498
satok4e4569d2010-11-19 18:45:53 +09003499 // If there are no selected shortcuts, tries finding the most applicable ones.
3500 private Pair<InputMethodInfo, InputMethodSubtype>
3501 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3502 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3503 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003504 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003505 boolean foundInSystemIME = false;
3506
3507 // Search applicable subtype for each InputMethodInfo
3508 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003509 final String imiId = imi.getId();
3510 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3511 continue;
3512 }
satokcd7cd292010-11-20 15:46:23 +09003513 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003514 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003515 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003516 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003517 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003518 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003519 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003520 }
satokdf31ae62011-01-15 06:19:44 +09003521 // 2. Search by the system locale from enabledSubtypes.
3522 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003523 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003524 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003525 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003526 }
satoka86f5e42011-09-02 17:12:42 +09003527 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003528 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003529 final ArrayList<InputMethodSubtype> subtypesForSearch =
3530 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003531 ? InputMethodUtils.getSubtypes(imi)
3532 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003533 // 4. Search by the current subtype's locale from all subtypes.
3534 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003535 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003536 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003537 }
3538 // 5. Search by the system locale from all subtypes.
3539 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003540 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003541 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003542 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003543 }
satokcd7cd292010-11-20 15:46:23 +09003544 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003545 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003546 // The current input method is the most applicable IME.
3547 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003548 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003549 break;
satok7599a7f2010-12-22 13:45:23 +09003550 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003551 // The system input method is 2nd applicable IME.
3552 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003553 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003554 if ((imi.getServiceInfo().applicationInfo.flags
3555 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3556 foundInSystemIME = true;
3557 }
satok4e4569d2010-11-19 18:45:53 +09003558 }
3559 }
3560 }
3561 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003562 if (mostApplicableIMI != null) {
3563 Slog.w(TAG, "Most applicable shortcut input method was:"
3564 + mostApplicableIMI.getId());
3565 if (mostApplicableSubtype != null) {
3566 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3567 + "," + mostApplicableSubtype.getMode() + ","
3568 + mostApplicableSubtype.getLocale());
3569 }
3570 }
satok4e4569d2010-11-19 18:45:53 +09003571 }
satokcd7cd292010-11-20 15:46:23 +09003572 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003573 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003574 } else {
3575 return null;
3576 }
3577 }
3578
satokab751aa2010-09-14 19:17:36 +09003579 /**
3580 * @return Return the current subtype of this input method.
3581 */
satok42c5a162011-05-26 16:46:14 +09003582 @Override
satokab751aa2010-09-14 19:17:36 +09003583 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003584 // TODO: Make this work even for non-current users?
3585 if (!calledFromValidUser()) {
3586 return null;
3587 }
3588 synchronized (mMethodMap) {
3589 return getCurrentInputMethodSubtypeLocked();
3590 }
3591 }
3592
3593 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003594 if (mCurMethodId == null) {
3595 return null;
3596 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003597 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003598 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3599 if (imi == null || imi.getSubtypeCount() == 0) {
3600 return null;
satok4e4569d2010-11-19 18:45:53 +09003601 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003602 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003603 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3604 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003605 if (subtypeId == NOT_A_SUBTYPE_ID) {
3606 // If there are no selected subtypes, the framework will try to find
3607 // the most applicable subtype from explicitly or implicitly enabled
3608 // subtypes.
3609 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003610 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003611 // If there is only one explicitly or implicitly enabled subtype,
3612 // just returns it.
3613 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3614 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3615 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003616 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003617 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003618 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003619 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003620 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003621 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3622 true);
satok4e4569d2010-11-19 18:45:53 +09003623 }
satok3ef8b292010-11-23 06:06:29 +09003624 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003625 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003626 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003627 }
3628 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003629 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003630 }
3631
satok4e4569d2010-11-19 18:45:53 +09003632 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003633 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003634 @Override
satok4e4569d2010-11-19 18:45:53 +09003635 public List getShortcutInputMethodsAndSubtypes() {
3636 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003637 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003638 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003639 // If there are no selected shortcut subtypes, the framework will try to find
3640 // the most applicable subtype from all subtypes whose mode is
3641 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003642 Pair<InputMethodInfo, InputMethodSubtype> info =
3643 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003644 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003645 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003646 ret.add(info.first);
3647 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003648 }
satok3da92232011-01-11 22:46:30 +09003649 return ret;
satokf3db1af2010-11-23 13:34:33 +09003650 }
satokf3db1af2010-11-23 13:34:33 +09003651 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3652 ret.add(imi);
3653 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3654 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003655 }
3656 }
satokf3db1af2010-11-23 13:34:33 +09003657 return ret;
satok4e4569d2010-11-19 18:45:53 +09003658 }
3659 }
3660
satok42c5a162011-05-26 16:46:14 +09003661 @Override
satokb66d2872010-11-10 01:04:04 +09003662 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003663 // TODO: Make this work even for non-current users?
3664 if (!calledFromValidUser()) {
3665 return false;
3666 }
satokb66d2872010-11-10 01:04:04 +09003667 synchronized (mMethodMap) {
3668 if (subtype != null && mCurMethodId != null) {
3669 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003670 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003671 if (subtypeId != NOT_A_SUBTYPE_ID) {
3672 setInputMethodLocked(mCurMethodId, subtypeId);
3673 return true;
3674 }
3675 }
3676 return false;
3677 }
3678 }
3679
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003680 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003681 private static class InputMethodFileManager {
3682 private static final String SYSTEM_PATH = "system";
3683 private static final String INPUT_METHOD_PATH = "inputmethod";
3684 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3685 private static final String NODE_SUBTYPES = "subtypes";
3686 private static final String NODE_SUBTYPE = "subtype";
3687 private static final String NODE_IMI = "imi";
3688 private static final String ATTR_ID = "id";
3689 private static final String ATTR_LABEL = "label";
3690 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003691 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003692 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003693 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003694 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3695 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3696 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003697 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003698 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3699 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003700 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003701 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003702 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003703 if (methodMap == null) {
3704 throw new NullPointerException("methodMap is null");
3705 }
3706 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003707 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003708 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3709 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003710 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003711 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003712 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3713 }
3714 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3715 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3716 if (!subtypeFile.exists()) {
3717 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003718 writeAdditionalInputMethodSubtypes(
3719 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003720 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003721 readAdditionalInputMethodSubtypes(
3722 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003723 }
3724 }
3725
3726 private void deleteAllInputMethodSubtypes(String imiId) {
3727 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003728 mAdditionalSubtypesMap.remove(imiId);
3729 writeAdditionalInputMethodSubtypes(
3730 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003731 }
3732 }
3733
3734 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003735 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003736 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003737 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003738 final int N = additionalSubtypes.length;
3739 for (int i = 0; i < N; ++i) {
3740 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003741 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003742 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003743 } else {
3744 Slog.w(TAG, "Duplicated subtype definition found: "
3745 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003746 }
3747 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003748 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3749 writeAdditionalInputMethodSubtypes(
3750 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003751 }
3752 }
3753
3754 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3755 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003756 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003757 }
3758 }
3759
3760 private static void writeAdditionalInputMethodSubtypes(
3761 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3762 HashMap<String, InputMethodInfo> methodMap) {
3763 // Safety net for the case that this function is called before methodMap is set.
3764 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3765 FileOutputStream fos = null;
3766 try {
3767 fos = subtypesFile.startWrite();
3768 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003769 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003770 out.startDocument(null, true);
3771 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3772 out.startTag(null, NODE_SUBTYPES);
3773 for (String imiId : allSubtypes.keySet()) {
3774 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3775 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3776 continue;
3777 }
3778 out.startTag(null, NODE_IMI);
3779 out.attribute(null, ATTR_ID, imiId);
3780 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3781 final int N = subtypesList.size();
3782 for (int i = 0; i < N; ++i) {
3783 final InputMethodSubtype subtype = subtypesList.get(i);
3784 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003785 if (subtype.hasSubtypeId()) {
3786 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3787 String.valueOf(subtype.getSubtypeId()));
3788 }
satoke7c6998e2011-06-03 17:57:59 +09003789 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3790 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3791 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003792 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3793 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003794 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3795 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3796 out.attribute(null, ATTR_IS_AUXILIARY,
3797 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003798 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3799 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003800 out.endTag(null, NODE_SUBTYPE);
3801 }
3802 out.endTag(null, NODE_IMI);
3803 }
3804 out.endTag(null, NODE_SUBTYPES);
3805 out.endDocument();
3806 subtypesFile.finishWrite(fos);
3807 } catch (java.io.IOException e) {
3808 Slog.w(TAG, "Error writing subtypes", e);
3809 if (fos != null) {
3810 subtypesFile.failWrite(fos);
3811 }
3812 }
3813 }
3814
3815 private static void readAdditionalInputMethodSubtypes(
3816 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3817 if (allSubtypes == null || subtypesFile == null) return;
3818 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003819 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003820 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003821 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003822 int type = parser.getEventType();
3823 // Skip parsing until START_TAG
3824 while ((type = parser.next()) != XmlPullParser.START_TAG
3825 && type != XmlPullParser.END_DOCUMENT) {}
3826 String firstNodeName = parser.getName();
3827 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3828 throw new XmlPullParserException("Xml doesn't start with subtypes");
3829 }
3830 final int depth =parser.getDepth();
3831 String currentImiId = null;
3832 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3833 while (((type = parser.next()) != XmlPullParser.END_TAG
3834 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3835 if (type != XmlPullParser.START_TAG)
3836 continue;
3837 final String nodeName = parser.getName();
3838 if (NODE_IMI.equals(nodeName)) {
3839 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3840 if (TextUtils.isEmpty(currentImiId)) {
3841 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3842 continue;
3843 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003844 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003845 allSubtypes.put(currentImiId, tempSubtypesArray);
3846 } else if (NODE_SUBTYPE.equals(nodeName)) {
3847 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3848 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3849 continue;
3850 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003851 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003852 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003853 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003854 parser.getAttributeValue(null, ATTR_LABEL));
3855 final String imeSubtypeLocale =
3856 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003857 final String languageTag =
3858 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003859 final String imeSubtypeMode =
3860 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3861 final String imeSubtypeExtraValue =
3862 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003863 final boolean isAuxiliary = "1".equals(String.valueOf(
3864 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003865 final boolean isAsciiCapable = "1".equals(String.valueOf(
3866 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003867 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003868 .setSubtypeNameResId(label)
3869 .setSubtypeIconResId(icon)
3870 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003871 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003872 .setSubtypeMode(imeSubtypeMode)
3873 .setSubtypeExtraValue(imeSubtypeExtraValue)
3874 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003875 .setIsAsciiCapable(isAsciiCapable);
3876 final String subtypeIdString =
3877 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3878 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003879 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003880 }
3881 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003882 }
3883 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003884 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3885 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003886 return;
satoke7c6998e2011-06-03 17:57:59 +09003887 }
3888 }
3889 }
3890
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003891 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3892 @NonNull
3893 private final Handler mHandler;
3894
3895 LocalServiceImpl(@NonNull final Handler handler) {
3896 mHandler = handler;
3897 }
3898
3899 @Override
3900 public void setInteractive(boolean interactive) {
3901 // Do everything in handler so as not to block the caller.
3902 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3903 interactive ? 1 : 0, 0));
3904 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003905
3906 @Override
3907 public void switchInputMethod(boolean forwardDirection) {
3908 // Do everything in handler so as not to block the caller.
3909 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3910 forwardDirection ? 1 : 0, 0));
3911 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003912
3913 @Override
3914 public void hideCurrentInputMethod() {
3915 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3916 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3917 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003918 }
3919
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003920 private static String imeWindowStatusToString(final int imeWindowVis) {
3921 final StringBuilder sb = new StringBuilder();
3922 boolean first = true;
3923 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3924 sb.append("Active");
3925 first = false;
3926 }
3927 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3928 if (!first) {
3929 sb.append("|");
3930 }
3931 sb.append("Visible");
3932 }
3933 return sb.toString();
3934 }
3935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003937 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3938 @Nullable Uri contentUri, @Nullable String packageName) {
3939 if (!calledFromValidUser()) {
3940 return null;
3941 }
3942
3943 if (token == null) {
3944 throw new NullPointerException("token");
3945 }
3946 if (packageName == null) {
3947 throw new NullPointerException("packageName");
3948 }
3949 if (contentUri == null) {
3950 throw new NullPointerException("contentUri");
3951 }
3952 final String contentUriScheme = contentUri.getScheme();
3953 if (!"content".equals(contentUriScheme)) {
3954 throw new InvalidParameterException("contentUri must have content scheme");
3955 }
3956
3957 synchronized (mMethodMap) {
3958 final int uid = Binder.getCallingUid();
3959 if (mCurMethodId == null) {
3960 return null;
3961 }
3962 if (mCurToken != token) {
3963 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3964 + " token=" + token);
3965 return null;
3966 }
3967 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3968 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3969 // nature of our system. Let's compare it with our internal record.
3970 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3971 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3972 + mCurAttribute.packageName + " packageName=" + packageName);
3973 return null;
3974 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003975 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003976 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003977 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003978 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003979 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
3980 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
3981 imeUserId);
3982 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
3983 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
3984 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
3985 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
3986 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
3987 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
3988 // actually allowed to "uid", which is guaranteed to be the IME's one.
3989 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
3990 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07003991 }
3992 }
3993
3994 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3996 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3997 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4000 + Binder.getCallingPid()
4001 + ", uid=" + Binder.getCallingUid());
4002 return;
4003 }
4004
4005 IInputMethod method;
4006 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004007 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 synchronized (mMethodMap) {
4012 p.println("Current Input Method Manager state:");
4013 int N = mMethodList.size();
4014 p.println(" Input Methods:");
4015 for (int i=0; i<N; i++) {
4016 InputMethodInfo info = mMethodList.get(i);
4017 p.println(" InputMethod #" + i + ":");
4018 info.dump(p, " ");
4019 }
4020 p.println(" Clients:");
4021 for (ClientState ci : mClients.values()) {
4022 p.println(" Client " + ci + ":");
4023 p.println(" client=" + ci.client);
4024 p.println(" inputContext=" + ci.inputContext);
4025 p.println(" sessionRequested=" + ci.sessionRequested);
4026 p.println(" curSession=" + ci.curSession);
4027 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004028 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004030 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4031 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004032 focusedWindowClient = mCurFocusedWindowClient;
4033 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4035 + " mBoundToMethod=" + mBoundToMethod);
4036 p.println(" mCurToken=" + mCurToken);
4037 p.println(" mCurIntent=" + mCurIntent);
4038 method = mCurMethod;
4039 p.println(" mCurMethod=" + mCurMethod);
4040 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004041 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 p.println(" mShowRequested=" + mShowRequested
4043 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4044 + " mShowForced=" + mShowForced
4045 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004046 p.println(" mCurUserActionNotificationSequenceNumber="
4047 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004048 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004049 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004050 p.println(" mSwitchingController:");
4051 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004052 p.println(" mSettings:");
4053 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004055
Jeff Brownb88102f2010-09-08 11:49:43 -07004056 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 pw.flush();
4059 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004060 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4061 } catch (IOException | RemoteException e) {
4062 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004064 } else {
4065 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004067
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004068 if (focusedWindowClient != null && client != focusedWindowClient) {
4069 p.println(" ");
4070 p.println("Warning: Current input method client doesn't match the last focused. "
4071 + "window.");
4072 p.println("Dumping input method client in the last focused window just in case.");
4073 p.println(" ");
4074 pw.flush();
4075 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004076 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4077 } catch (IOException | RemoteException e) {
4078 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004079 }
4080 }
4081
Jeff Brownb88102f2010-09-08 11:49:43 -07004082 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 pw.flush();
4085 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004086 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4087 } catch (IOException | RemoteException e) {
4088 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004090 } else {
4091 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 }
4093 }
4094}