blob: 08cb1094ae38ba9620b57592aacd55c3e6476cdb [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;
246
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900247 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 final ClientState client;
249 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700250
251 IInputMethodSession session;
252 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 @Override
255 public String toString() {
256 return "SessionState{uid " + client.uid + " pid " + client.pid
257 + " method " + Integer.toHexString(
258 System.identityHashCode(method))
259 + " session " + Integer.toHexString(
260 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700261 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 + "}";
263 }
264
265 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700266 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 client = _client;
268 method = _method;
269 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700270 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800273
Jeff Brownc28867a2013-03-26 15:42:39 -0700274 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 final IInputMethodClient client;
276 final IInputContext inputContext;
277 final int uid;
278 final int pid;
279 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 boolean sessionRequested;
282 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 @Override
285 public String toString() {
286 return "ClientState{" + Integer.toHexString(
287 System.identityHashCode(this)) + " uid " + uid
288 + " pid " + pid + "}";
289 }
290
291 ClientState(IInputMethodClient _client, IInputContext _inputContext,
292 int _uid, int _pid) {
293 client = _client;
294 inputContext = _inputContext;
295 uid = _uid;
296 pid = _pid;
297 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
298 }
299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700301 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700304 * Set once the system is ready to run third party code.
305 */
306 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700308 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700309 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
310 * method. This is to be synchronized with the secure settings keyed with
311 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
312 *
313 * <p>This can be transiently {@code null} when the system is re-initializing input method
314 * settings, e.g., the system locale is just changed.</p>
315 *
316 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
317 * {@link InputMethodManagerService}.</p>
318 *
319 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700321 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * The current binding sequence number, incremented every time there is
326 * a new bind performed.
327 */
328 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * The client that is currently bound to an input method.
332 */
333 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800336 * The last window token that we confirmed to be focused. This is always updated upon reports
337 * from the input method client. If the window state is already changed before the report is
338 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700339 */
340 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700342 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800343 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
344 */
345 ClientState mCurFocusedWindowClient;
346
347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 * The input context last provided by the current client.
349 */
350 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700353 * The missing method flags for the input context last provided by the current client.
354 *
355 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
356 */
357 int mCurInputContextMissingMethods;
358
359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * The attributes last provided by the current client.
361 */
362 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700365 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700367 *
368 * <p>This can be {@code null} when no input method is connected.</p>
369 *
370 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700372 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 /**
satokab751aa2010-09-14 19:17:36 +0900376 * The current subtype of the current input method.
377 */
378 private InputMethodSubtype mCurrentSubtype;
379
satok4e4569d2010-11-19 18:45:53 +0900380 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900381 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700382 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900383
John Spurlocke0980502013-10-25 11:59:29 -0400384 // Was the keyguard locked when this client became current?
385 private boolean mCurClientInKeyguard;
386
satokab751aa2010-09-14 19:17:36 +0900387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * Set to true if our ServiceConnection is currently actively bound to
389 * a service (whether or not we have gotten its IBinder back yet).
390 */
391 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
394 * Set if the client has asked for the input method to be shown.
395 */
396 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 /**
399 * Set if we were explicitly told to show the input method.
400 */
401 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
404 * Set if we were forced to be shown.
405 */
406 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Set if we last told the input method to show itself.
410 */
411 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * The Intent used to connect to the current input method.
415 */
416 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
419 * The token we have made for the currently active input method, to
420 * identify it in the future.
421 */
422 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * If non-null, this is the input method service we are currently connected
426 * to.
427 */
428 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * Time that we last initiated a bind to the input method, to determine
432 * if we should try to disconnect and reconnect to it.
433 */
434 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Have we called mCurMethod.bindInput()?
438 */
439 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
442 * Currently enabled session. Only touched by service thread, not
443 * protected by a lock.
444 */
445 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700448 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700450 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900452 int mCurUserActionNotificationSequenceNumber = 0;
453
Joe Onorato857fd9b2011-01-27 15:08:35 -0800454 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900455
456 /**
457 * A set of status bits regarding the active IME.
458 *
459 * <p>This value is a combination of following two bits:</p>
460 * <dl>
461 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
462 * <dd>
463 * If this bit is ON, connected IME is ready to accept touch/key events.
464 * </dd>
465 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
466 * <dd>
467 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
468 * </dd>
469 * </dl>
470 * <em>Do not update this value outside of setImeWindowStatus.</em>
471 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800472 int mImeWindowVis;
473
Ken Wakasa05dbb652011-08-22 15:22:43 +0900474 private AlertDialog.Builder mDialogBuilder;
475 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700476 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900477 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700478 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900479 private InputMethodInfo[] mIms;
480 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800481 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700482 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800483 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900484 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
485 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500486 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700487 @HardKeyboardBehavior
488 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 int mUserId;
492 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700493 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800494 String mLastEnabled = "";
495
Yohei Yukawa81482972015-06-04 00:58:59 -0700496 /**
497 * <em>This constructor must be called within the lock.</em>
498 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 SettingsObserver(Handler handler) {
500 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700501 }
502
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800503 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700504 if (mRegistered && mUserId == userId) {
505 return;
506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700508 if (mRegistered) {
509 mContext.getContentResolver().unregisterContentObserver(this);
510 mRegistered = false;
511 }
512 if (mUserId != userId) {
513 mLastEnabled = "";
514 mUserId = userId;
515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700517 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900518 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700519 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900520 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700521 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700522 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700523 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800524 resolver.registerContentObserver(Settings.Secure.getUriFor(
525 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700526 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800528
Michael Wright7b5a96b2014-08-09 19:28:42 -0700529 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800530 final Uri showImeUri = Settings.Secure.getUriFor(
531 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
532 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
533 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700535 if (showImeUri.equals(uri)) {
536 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800537 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
538 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
539 mContext.getContentResolver(),
540 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
541 0, mUserId) == 1;
542 if (mAccessibilityRequestingNoSoftKeyboard) {
543 final boolean showRequested = mShowRequested;
544 hideCurrentInputLocked(0, null);
545 mShowRequested = showRequested;
546 } else if (mShowRequested) {
547 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
548 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700549 } else {
550 boolean enabledChanged = false;
551 String newEnabled = mSettings.getEnabledInputMethodsStr();
552 if (!mLastEnabled.equals(newEnabled)) {
553 mLastEnabled = newEnabled;
554 enabledChanged = true;
555 }
556 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700560
561 @Override
562 public String toString() {
563 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
564 + " mLastEnabled=" + mLastEnabled + "}";
565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800567
Yohei Yukawa79247822017-01-23 15:26:15 -0800568 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900569 @Override
570 public void onReceive(Context context, Intent intent) {
571 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700572 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900573 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700574 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900575 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800576 } else if (Intent.ACTION_USER_ADDED.equals(action)
577 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100578 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800579 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700580 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
581 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
582 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
583 final String prevValue = intent.getStringExtra(
584 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
585 final String newValue = intent.getStringExtra(
586 Intent.EXTRA_SETTING_NEW_VALUE);
587 restoreEnabledInputMethods(mContext, prevValue, newValue);
588 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800589 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
590 synchronized (mMethodMap) {
591 resetStateIfCurrentLocaleChangedLocked();
592 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900593 } else {
594 Slog.w(TAG, "Unexpected intent " + intent);
595 }
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800598
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700599 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
600 // does not attempt to validate on the fly with any installed device policy, so must only
601 // be run in the context of initial device setup.
602 //
603 // TODO: Move this method to InputMethodUtils with adding unit tests.
604 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
605 if (DEBUG_RESTORE) {
606 Slog.i(TAG, "Restoring enabled input methods:");
607 Slog.i(TAG, "prev=" + prevValue);
608 Slog.i(TAG, " new=" + newValue);
609 }
610 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900611 ArrayMap<String, ArraySet<String>> prevMap =
612 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
613 ArrayMap<String, ArraySet<String>> newMap =
614 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700615
616 // Merge the restored ime+subtype enabled states into the live state
617 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
618 final String imeId = entry.getKey();
619 ArraySet<String> prevSubtypes = prevMap.get(imeId);
620 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700621 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700622 prevMap.put(imeId, prevSubtypes);
623 }
624 prevSubtypes.addAll(entry.getValue());
625 }
626
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700627 final String mergedImesAndSubtypesString =
628 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700629 if (DEBUG_RESTORE) {
630 Slog.i(TAG, "Merged IME string:");
631 Slog.i(TAG, " " + mergedImesAndSubtypesString);
632 }
633 Settings.Secure.putString(context.getContentResolver(),
634 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
635 }
636
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800637 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900638 private boolean isChangingPackagesOfCurrentUser() {
639 final int userId = getChangingUserId();
640 final boolean retval = userId == mSettings.getCurrentUserId();
641 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900642 if (!retval) {
643 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
644 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 }
646 return retval;
647 }
648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800650 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900651 if (!isChangingPackagesOfCurrentUser()) {
652 return false;
653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 final int N = mMethodList.size();
657 if (curInputMethodId != null) {
658 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800659 InputMethodInfo imi = mMethodList.get(i);
660 if (imi.getId().equals(curInputMethodId)) {
661 for (String pkg : packages) {
662 if (imi.getPackageName().equals(pkg)) {
663 if (!doit) {
664 return true;
665 }
satok723a27e2010-11-11 14:58:11 +0900666 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800667 chooseNewDefaultIMELocked();
668 return true;
669 }
670 }
671 }
672 }
673 }
674 }
675 return false;
676 }
677
678 @Override
679 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900680 if (!isChangingPackagesOfCurrentUser()) {
681 return;
682 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800683 synchronized (mMethodMap) {
684 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900685 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800686 final int N = mMethodList.size();
687 if (curInputMethodId != null) {
688 for (int i=0; i<N; i++) {
689 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900690 final String imiId = imi.getId();
691 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692 curIm = imi;
693 }
satoke7c6998e2011-06-03 17:57:59 +0900694
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800695 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900696 if (isPackageModified(imi.getPackageName())) {
697 mFileManager.deleteAllInputMethodSubtypes(imiId);
698 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800699 if (change == PACKAGE_TEMPORARY_CHANGE
700 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800701 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800702 + imi.getComponent());
703 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705 }
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
Yohei Yukawa94e33302016-02-12 19:37:03 -0800708 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800712 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800713 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800714 if (change == PACKAGE_TEMPORARY_CHANGE
715 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800716 ServiceInfo si = null;
717 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900718 si = mIPackageManager.getServiceInfo(
719 curIm.getComponent(), 0, mSettings.getCurrentUserId());
720 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800721 }
722 if (si == null) {
723 // Uh oh, current input method is no longer around!
724 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800725 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900726 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800727 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800728 changed = true;
729 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800730 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900731 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800732 }
733 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800734 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800735 }
satokab751aa2010-09-14 19:17:36 +0900736
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800737 if (curIm == null) {
738 // We currently don't have a default input method... is
739 // one now available?
740 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700741 } else if (!changed && isPackageModified(curIm.getPackageName())) {
742 // Even if the current input method is still available, mCurrentSubtype could
743 // be obsolete when the package is modified in practice.
744 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800747 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800748 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750 }
751 }
752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
Jeff Brownc28867a2013-03-26 15:42:39 -0700754 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900755 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700756 private final IInputMethod mMethod;
757 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800758
Jeff Brownc28867a2013-03-26 15:42:39 -0700759 MethodCallback(InputMethodManagerService imms, IInputMethod method,
760 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900761 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700762 mMethod = method;
763 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800765
satoke7c6998e2011-06-03 17:57:59 +0900766 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700767 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700768 long ident = Binder.clearCallingIdentity();
769 try {
770 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
771 } finally {
772 Binder.restoreCallingIdentity(ident);
773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
satok01038492012-04-09 21:08:27 +0900777 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700778 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900779 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700780 public void onHardKeyboardStatusChange(boolean available) {
781 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
782 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900783 }
784
Michael Wright7b5a96b2014-08-09 19:28:42 -0700785 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900786 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700787 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900788 }
789 synchronized(mMethodMap) {
790 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
791 && mSwitchingDialog.isShowing()) {
792 mSwitchingDialogTitleView.findViewById(
793 com.android.internal.R.id.hard_keyboard_section).setVisibility(
794 available ? View.VISIBLE : View.GONE);
795 }
796 }
797 }
798 }
799
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800800 public static final class Lifecycle extends SystemService {
801 private InputMethodManagerService mService;
802
803 public Lifecycle(Context context) {
804 super(context);
805 mService = new InputMethodManagerService(context);
806 }
807
808 @Override
809 public void onStart() {
810 LocalServices.addService(InputMethodManagerInternal.class,
811 new LocalServiceImpl(mService.mHandler));
812 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
813 }
814
815 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800816 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700817 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800818 // TODO: Dispatch this to a worker thread as needed.
819 mService.onSwitchUser(userHandle);
820 }
821
822 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800823 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700824 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800825 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800826 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
827 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
828 .getService(Context.STATUS_BAR_SERVICE);
829 mService.systemRunning(statusBarService);
830 }
831 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800832
833 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700834 public void onUnlockUser(final @UserIdInt int userHandle) {
835 // Called on ActivityManager thread.
836 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700837 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800838 }
839 }
840
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800841 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800842 synchronized(mMethodMap) {
843 final int currentUserId = mSettings.getCurrentUserId();
844 if (DEBUG) {
845 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
846 }
847 if (userId != currentUserId) {
848 return;
849 }
850 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800851 if (mSystemReady) {
852 // We need to rebuild IMEs.
853 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
854 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
855 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800856 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800857 }
858
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800859 void onSwitchUser(@UserIdInt int userId) {
860 synchronized (mMethodMap) {
861 switchUserLocked(userId);
862 }
863 }
864
Seigo Nonaka7309b122015-08-17 18:34:13 -0700865 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900866 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800868 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700870 // Note: SettingsObserver doesn't register observers in its constructor.
871 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 mIWindowManager = IWindowManager.Stub.asInterface(
873 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700874 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800875 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900876 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 public void executeMessage(Message msg) {
878 handleMessage(msg);
879 }
Mita Yuned218c72012-12-06 17:18:25 -0800880 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800881 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800882 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900883 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700884 mHasFeature = context.getPackageManager().hasSystemFeature(
885 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500886 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700887 mHardKeyboardBehavior = mContext.getResources().getInteger(
888 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900889
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400890 Bundle extras = new Bundle();
891 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
892 mImeSwitcherNotification = new Notification.Builder(mContext)
893 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
894 .setWhen(0)
895 .setOngoing(true)
896 .addExtras(extras)
897 .setCategory(Notification.CATEGORY_SYSTEM)
898 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400899
satok7cfc0ed2011-06-20 21:29:36 +0900900 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900901 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900902
903 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900904
satok7cfc0ed2011-06-20 21:29:36 +0900905 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900906 int userId = 0;
907 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800908 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900909 } catch (RemoteException e) {
910 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
911 }
satok913a8922010-08-26 21:53:41 +0900912
satokd87c2592010-09-29 11:52:06 +0900913 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900914 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800915 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700916
Kenny Guy2a764942014-04-02 13:29:20 +0100917 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900918 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800919 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
920 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900921 }
922
satok5b927c432012-05-01 20:09:34 +0900923 private void resetDefaultImeLocked(Context context) {
924 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800925 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900926 return;
927 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800928 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800929 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800930 if (suitableImes.isEmpty()) {
931 Slog.i(TAG, "No default found");
932 return;
satok5b927c432012-05-01 20:09:34 +0900933 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800934 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -0800935 if (DEBUG) {
936 Slog.i(TAG, "Default found, using " + defIm.getId());
937 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800938 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900939 }
940
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900941 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
942 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 if (!mSystemReady) {
944 // not system ready
945 return;
946 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800947 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800949 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900950 if (!updateOnlyWhenLocaleChanged) {
951 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800952 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900953 }
954 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800955 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800957 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900958 if (!updateOnlyWhenLocaleChanged) {
959 final String selectedImiId = mSettings.getSelectedInputMethod();
960 if (TextUtils.isEmpty(selectedImiId)) {
961 // This is the first time of the user switch and
962 // set the current ime to the proper one.
963 resetDefaultImeLocked(mContext);
964 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900965 } else {
966 // If the locale is changed, needs to reset the default ime
967 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800969 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800970 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 if (!updateOnlyWhenLocaleChanged) {
972 try {
973 startInputInnerLocked();
974 } catch (RuntimeException e) {
975 Slog.w(TAG, "Unexpected exception", e);
976 }
977 }
978 }
979 }
980
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900981 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900982 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
983 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 }
985
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900986 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700987 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
988 + " currentUserId=" + mSettings.getCurrentUserId());
989
Yohei Yukawa81482972015-06-04 00:58:59 -0700990 // ContentObserver should be registered again when the user is changed
991 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800992
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800993 // If the system is not ready or the device is not yed unlocked by the user, then we use
994 // copy-on-write settings.
995 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -0600996 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800997 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +0100998 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900999 // InputMethodFileManager should be reset when the user is changed
1000 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001001 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001002
1003 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1004 + " defaultImiId=" + defaultImiId);
1005
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001006 // For secondary users, the list of enabled IMEs may not have been updated since the
1007 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1008 // not be empty even if the IME has been uninstalled by the primary user.
1009 // Even in such cases, IMMS works fine because it will find the most applicable
1010 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001011 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001012 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001013 initialUserSwitch /* needsToResetDefaultIme */);
1014 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001015 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1016 mSettings.getEnabledInputMethodListLocked(), newUserId,
1017 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001018 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001019
1020 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1021 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001022 }
1023
Kenny Guy2a764942014-04-02 13:29:20 +01001024 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001025 mSettings.setCurrentProfileIds(
1026 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001027 }
1028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 @Override
1030 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1031 throws RemoteException {
1032 try {
1033 return super.onTransact(code, data, reply, flags);
1034 } catch (RuntimeException e) {
1035 // The input method manager only throws security exceptions, so let's
1036 // log all others.
1037 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001038 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040 throw e;
1041 }
1042 }
1043
Svetoslav Ganova0027152013-06-25 14:59:53 -07001044 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001045 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001046 if (DEBUG) {
1047 Slog.d(TAG, "--- systemReady");
1048 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001049 if (!mSystemReady) {
1050 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001051 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001052 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001053 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001054 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001055 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1056 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001057 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001058 if (mStatusBar != null) {
1059 mStatusBar.setIconVisibility(mSlotIme, false);
1060 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001061 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001062 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1063 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001064 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001065 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001066 mHardKeyboardListener);
1067 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001068
1069 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1070 mSettingsObserver.registerContentObserverLocked(currentUserId);
1071
1072 final IntentFilter broadcastFilter = new IntentFilter();
1073 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1074 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1075 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1076 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1077 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1078 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1079
1080 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1081 resetDefaultImeLocked(mContext);
1082 updateFromSettingsLocked(true);
1083 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1084 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1085 mContext.getBasePackageName());
1086
Dianne Hackborncc278702009-09-02 23:07:23 -07001087 try {
1088 startInputInnerLocked();
1089 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001091 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001092 }
1093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001096 // ---------------------------------------------------------------------------------------
1097 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1098 // 1) it comes from the system process
1099 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1100 private boolean calledFromValidUser() {
1101 final int uid = Binder.getCallingUid();
1102 final int userId = UserHandle.getUserId(uid);
1103 if (DEBUG) {
1104 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1105 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1106 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001107 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1108 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001109 }
Kenny Guy2a764942014-04-02 13:29:20 +01001110 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001111 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001113
1114 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1115 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1116 // must not manage background users' states in any functions.
1117 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1118 // by a token.
1119 if (mContext.checkCallingOrSelfPermission(
1120 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1121 == PackageManager.PERMISSION_GRANTED) {
1122 if (DEBUG) {
1123 Slog.d(TAG, "--- Access granted because the calling process has "
1124 + "the INTERACT_ACROSS_USERS_FULL permission");
1125 }
1126 return true;
1127 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001128 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001129 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1130 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001131 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001132 }
1133
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001134
1135 /**
1136 * Returns true iff the caller is identified to be the current input method with the token.
1137 * @param token The window token given to the input method when it was started.
1138 * @return true if and only if non-null valid token is specified.
1139 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001140 private boolean calledWithValidToken(@Nullable IBinder token) {
1141 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1142 if (DEBUG) {
1143 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1144 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1145 }
1146 return false;
1147 }
1148 if (token == null || token != mCurToken) {
1149 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1150 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1151 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001152 return false;
1153 }
1154 return true;
1155 }
1156
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001157 private boolean bindCurrentInputMethodService(
1158 Intent service, ServiceConnection conn, int flags) {
1159 if (service == null || conn == null) {
1160 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1161 return false;
1162 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001163 return mContext.bindServiceAsUser(service, conn, flags,
1164 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001165 }
1166
satoke7c6998e2011-06-03 17:57:59 +09001167 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001169 // TODO: Make this work even for non-current users?
1170 if (!calledFromValidUser()) {
1171 return Collections.emptyList();
1172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001174 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 }
1177
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 // TODO: Make this work even for non-current users?
1181 if (!calledFromValidUser()) {
1182 return Collections.emptyList();
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001185 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 }
1188
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001189 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001190 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001191 * @return enabled subtypes of the specified imi
1192 */
satoke7c6998e2011-06-03 17:57:59 +09001193 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001194 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001195 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001196 // TODO: Make this work even for non-current users?
1197 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001198 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001199 }
satok67ddf9c2010-11-17 09:45:54 +09001200 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001201 final InputMethodInfo imi;
1202 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001203 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001204 } else {
1205 imi = mMethodMap.get(imiId);
1206 }
1207 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001208 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001209 }
1210 return mSettings.getEnabledInputMethodSubtypeListLocked(
1211 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001212 }
1213 }
1214
satoke7c6998e2011-06-03 17:57:59 +09001215 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 public void addClient(IInputMethodClient client,
1217 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001218 if (!calledFromValidUser()) {
1219 return;
1220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 synchronized (mMethodMap) {
1222 mClients.put(client.asBinder(), new ClientState(client,
1223 inputContext, uid, pid));
1224 }
1225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001226
satoke7c6998e2011-06-03 17:57:59 +09001227 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001229 if (!calledFromValidUser()) {
1230 return;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001233 ClientState cs = mClients.remove(client.asBinder());
1234 if (cs != null) {
1235 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001236 if (mCurClient == cs) {
1237 mCurClient = null;
1238 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001239 if (mCurFocusedWindowClient == cs) {
1240 mCurFocusedWindowClient = null;
1241 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 void executeOrSendMessage(IInterface target, Message msg) {
1247 if (target.asBinder() instanceof Binder) {
1248 mCaller.sendMessage(msg);
1249 } else {
1250 handleMessage(msg);
1251 msg.recycle();
1252 }
1253 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001254
Yohei Yukawa33e81792015-11-17 21:14:42 -08001255 void unbindCurrentClientLocked(
1256 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001258 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 + mCurClient.client.asBinder());
1260 if (mBoundToMethod) {
1261 mBoundToMethod = false;
1262 if (mCurMethod != null) {
1263 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1264 MSG_UNBIND_INPUT, mCurMethod));
1265 }
1266 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001267
1268 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1269 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001270 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1271 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project10592532009-03-18 17:39:46 -07001275 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 private int getImeShowFlags() {
1280 int flags = 0;
1281 if (mShowForced) {
1282 flags |= InputMethod.SHOW_FORCED
1283 | InputMethod.SHOW_EXPLICIT;
1284 } else if (mShowExplicitlyRequested) {
1285 flags |= InputMethod.SHOW_EXPLICIT;
1286 }
1287 return flags;
1288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 private int getAppShowFlags() {
1291 int flags = 0;
1292 if (mShowForced) {
1293 flags |= InputMethodManager.SHOW_FORCED;
1294 } else if (!mShowExplicitlyRequested) {
1295 flags |= InputMethodManager.SHOW_IMPLICIT;
1296 }
1297 return flags;
1298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001300 InputBindResult attachNewInputLocked(
1301 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 if (!mBoundToMethod) {
1303 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1304 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1305 mBoundToMethod = true;
1306 }
1307 final SessionState session = mCurClient.curSession;
1308 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001309 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1310 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1311 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001313 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1314 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1315 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001318 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001319 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001321 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001322 (session.channel != null ? session.channel.dup() : null),
1323 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001325
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001326 InputBindResult startInputLocked(
1327 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001328 IInputMethodClient client, IInputContext inputContext,
1329 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001330 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 // If no method is currently selected, do nothing.
1332 if (mCurMethodId == null) {
1333 return mNoBinding;
1334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 ClientState cs = mClients.get(client.asBinder());
1337 if (cs == null) {
1338 throw new IllegalArgumentException("unknown client "
1339 + client.asBinder());
1340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001341
Yohei Yukawa74750f22016-03-22 12:54:22 -07001342 if (attribute == null) {
1343 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1344 + " uid=" + cs.uid + " pid=" + cs.pid);
1345 return null;
1346 }
1347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 try {
1349 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1350 // Check with the window manager to make sure this client actually
1351 // has a window with focus. If not, reject. This is thread safe
1352 // because if the focus changes some time before or after, the
1353 // next client receiving focus that has any interest in input will
1354 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001355 if (DEBUG) {
1356 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1357 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 return null;
1360 }
1361 } catch (RemoteException e) {
1362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001363
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001364 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001365 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001366 }
1367
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001368 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001369 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001370 @NonNull EditorInfo attribute, int controlFlags,
1371 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001372 // If no method is currently selected, do nothing.
1373 if (mCurMethodId == null) {
1374 return mNoBinding;
1375 }
1376
Yohei Yukawad57ba672015-06-08 16:39:46 -07001377 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1378 attribute.packageName)) {
1379 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1380 + " uid=" + cs.uid + " package=" + attribute.packageName);
1381 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001382 }
1383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001385 // Was the keyguard locked when switching over to the new client?
1386 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 // If the client is changing, we need to switch over to the new
1388 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001389 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001390 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001391 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392
1393 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001394 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001395 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001396 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
1398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 // Bump up the sequence for this client and attach it.
1401 mCurSeq++;
1402 if (mCurSeq <= 0) mCurSeq = 1;
1403 mCurClient = cs;
1404 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001405 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 // Check if the input method is changing.
1409 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1410 if (cs.curSession != null) {
1411 // Fast case: if we are already connected to the input method,
1412 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001413 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001414 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 if (mHaveConnection) {
1417 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 // Return to client, and we will get back with it when
1419 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001420 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001421 return new InputBindResult(null, null, mCurId, mCurSeq,
1422 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 } else if (SystemClock.uptimeMillis()
1424 < (mLastBindTime+TIME_TO_RECONNECT)) {
1425 // In this case we have connected to the service, but
1426 // don't yet have its interface. If it hasn't been too
1427 // long since we did the connection, we'll return to
1428 // the client and wait to get the service interface so
1429 // we can report back. If it has been too long, we want
1430 // to fall through so we can try a disconnect/reconnect
1431 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001432 return new InputBindResult(null, null, mCurId, mCurSeq,
1433 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1436 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438 }
1439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001440
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001441 return startInputInnerLocked();
1442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001444 InputBindResult startInputInnerLocked() {
1445 if (mCurMethodId == null) {
1446 return mNoBinding;
1447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001449 if (!mSystemReady) {
1450 // If the system is not yet ready, we shouldn't be running third
1451 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001452 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1453 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1457 if (info == null) {
1458 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001460
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001461 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1464 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001465 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1466 com.android.internal.R.string.input_method_binding_label);
1467 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1468 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001469 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001470 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1471 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 mLastBindTime = SystemClock.uptimeMillis();
1473 mHaveConnection = true;
1474 mCurId = info.getId();
1475 mCurToken = new Binder();
1476 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001477 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001478 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 } catch (RemoteException e) {
1480 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001481 return new InputBindResult(null, null, mCurId, mCurSeq,
1482 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 } else {
1484 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001485 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 + mCurIntent);
1487 }
1488 return null;
1489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001491 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001492 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001493 IInputMethodClient client, IInputContext inputContext,
1494 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001495 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001496 if (!calledFromValidUser()) {
1497 return null;
1498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001500 if (DEBUG) {
1501 Slog.v(TAG, "startInput: reason="
1502 + InputMethodClient.getStartInputReason(startInputReason)
1503 + " client = " + client.asBinder()
1504 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001505 + " missingMethods="
1506 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001507 + " attribute=" + attribute
1508 + " controlFlags=#" + Integer.toHexString(controlFlags));
1509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 final long ident = Binder.clearCallingIdentity();
1511 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001512 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1513 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 } finally {
1515 Binder.restoreCallingIdentity(ident);
1516 }
1517 }
1518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
satoke7c6998e2011-06-03 17:57:59 +09001520 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 public void finishInput(IInputMethodClient client) {
1522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001523
satoke7c6998e2011-06-03 17:57:59 +09001524 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 public void onServiceConnected(ComponentName name, IBinder service) {
1526 synchronized (mMethodMap) {
1527 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1528 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001529 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001530 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001531 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001532 return;
1533 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001534 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001535 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1536 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001538 clearClientSessionLocked(mCurClient);
1539 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541 }
1542 }
1543 }
1544
Jeff Brownc28867a2013-03-26 15:42:39 -07001545 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1546 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 synchronized (mMethodMap) {
1548 if (mCurMethod != null && method != null
1549 && mCurMethod.asBinder() == method.asBinder()) {
1550 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001551 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001553 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001554 InputBindResult res = attachNewInputLocked(
1555 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 if (res.method != null) {
1557 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001558 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001560 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
1562 }
1563 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001564
1565 // Session abandoned. Close its associated input channel.
1566 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001568
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001569 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001570 if (mVisibleBound) {
1571 mContext.unbindService(mVisibleConnection);
1572 mVisibleBound = false;
1573 }
1574
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001575 if (mHaveConnection) {
1576 mContext.unbindService(this);
1577 mHaveConnection = false;
1578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001579
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001580 if (mCurToken != null) {
1581 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001582 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001583 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001584 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001585 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001586 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001587 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001588 } catch (RemoteException e) {
1589 }
1590 mCurToken = null;
1591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001592
The Android Open Source Project10592532009-03-18 17:39:46 -07001593 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001594 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
Yohei Yukawa33e81792015-11-17 21:14:42 -08001597 void resetCurrentMethodAndClient(
1598 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001599 mCurMethodId = null;
1600 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001601 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001602 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001603
1604 void requestClientSessionLocked(ClientState cs) {
1605 if (!cs.sessionRequested) {
1606 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1607 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1608 cs.sessionRequested = true;
1609 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1610 MSG_CREATE_SESSION, mCurMethod, channels[1],
1611 new MethodCallback(this, mCurMethod, channels[0])));
1612 }
1613 }
1614
1615 void clearClientSessionLocked(ClientState cs) {
1616 finishSessionLocked(cs.curSession);
1617 cs.curSession = null;
1618 cs.sessionRequested = false;
1619 }
1620
1621 private void finishSessionLocked(SessionState sessionState) {
1622 if (sessionState != null) {
1623 if (sessionState.session != null) {
1624 try {
1625 sessionState.session.finishSession();
1626 } catch (RemoteException e) {
1627 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001628 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001629 }
1630 sessionState.session = null;
1631 }
1632 if (sessionState.channel != null) {
1633 sessionState.channel.dispose();
1634 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001635 }
1636 }
1637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001639 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 if (mCurMethod != null) {
1641 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001642 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001644
Jeff Brownc28867a2013-03-26 15:42:39 -07001645 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001646 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 mCurMethod = null;
1648 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001649 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001650 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001653
satoke7c6998e2011-06-03 17:57:59 +09001654 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001656 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1657 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1658 // which is irregular but can eventually happen for everyone just by continuing using the
1659 // device. Thus it is important to make sure that all the internal states are properly
1660 // refreshed when this method is called back. Running
1661 // adb install -r <APK that implements the current IME>
1662 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001664 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 + " mCurIntent=" + mCurIntent);
1666 if (mCurMethod != null && mCurIntent != null
1667 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001668 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 // We consider this to be a new bind attempt, since the system
1670 // should now try to restart the service for us.
1671 mLastBindTime = SystemClock.uptimeMillis();
1672 mShowRequested = mInputShown;
1673 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001674 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
1676 }
1677 }
1678
satokf9f01002011-05-19 21:31:50 +09001679 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001681 synchronized (mMethodMap) {
1682 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001683 return;
1684 }
1685 final long ident = Binder.clearCallingIdentity();
1686 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001688 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001689 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001690 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001693 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001694 CharSequence contentDescription = null;
1695 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001696 // Use PackageManager to load label
1697 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001698 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001699 mIPackageManager.getApplicationInfo(packageName, 0,
1700 mSettings.getCurrentUserId()));
1701 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001702 /* ignore */
1703 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001704 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001705 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001706 contentDescription != null
1707 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001708 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001711 } finally {
1712 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
1715 }
1716
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001717 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001718 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001719 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001720 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001721 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001722 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001723 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1724 // When physical keyboard is attached, we show the ime switcher (or notification if
1725 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1726 // exists in the IME switcher dialog. Might be OK to remove this condition once
1727 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1728 return true;
1729 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001730 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1731 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001732 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001733
1734 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1735 final int N = imis.size();
1736 if (N > 2) return true;
1737 if (N < 1) return false;
1738 int nonAuxCount = 0;
1739 int auxCount = 0;
1740 InputMethodSubtype nonAuxSubtype = null;
1741 InputMethodSubtype auxSubtype = null;
1742 for(int i = 0; i < N; ++i) {
1743 final InputMethodInfo imi = imis.get(i);
1744 final List<InputMethodSubtype> subtypes =
1745 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1746 final int subtypeCount = subtypes.size();
1747 if (subtypeCount == 0) {
1748 ++nonAuxCount;
1749 } else {
1750 for (int j = 0; j < subtypeCount; ++j) {
1751 final InputMethodSubtype subtype = subtypes.get(j);
1752 if (!subtype.isAuxiliary()) {
1753 ++nonAuxCount;
1754 nonAuxSubtype = subtype;
1755 } else {
1756 ++auxCount;
1757 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001758 }
1759 }
satok7cfc0ed2011-06-20 21:29:36 +09001760 }
1761 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001762 if (nonAuxCount > 1 || auxCount > 1) {
1763 return true;
1764 } else if (nonAuxCount == 1 && auxCount == 1) {
1765 if (nonAuxSubtype != null && auxSubtype != null
1766 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1767 || auxSubtype.overridesImplicitlyEnabledSubtype()
1768 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1769 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1770 return false;
1771 }
1772 return true;
1773 }
1774 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001775 }
1776
John Spurlocke0980502013-10-25 11:59:29 -04001777 private boolean isKeyguardLocked() {
1778 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1779 }
1780
satokdbf29502011-08-25 15:28:23 +09001781 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001782 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001783 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001784 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001785 return;
1786 }
1787
1788 synchronized (mMethodMap) {
1789 mImeWindowVis = vis;
1790 mBackDisposition = backDisposition;
1791 updateSystemUiLocked(token, vis, backDisposition);
1792 }
1793 }
1794
1795 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1796 synchronized (mMethodMap) {
1797 updateSystemUiLocked(token, vis, backDisposition);
1798 }
1799 }
1800
1801 // Caution! This method is called in this class. Handle multi-user carefully
1802 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1803 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001804 return;
1805 }
1806
1807 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1808 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001809 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001810 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001811 // apply policy for binder calls
1812 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1813 vis = 0;
satok06487a52010-10-29 11:37:18 +09001814 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001815 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1816 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1817 if (mStatusBar != null) {
1818 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1819 needsToShowImeSwitcher);
1820 }
1821 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1822 if (imi != null && needsToShowImeSwitcher) {
1823 // Used to load label
1824 final CharSequence title = mRes.getText(
1825 com.android.internal.R.string.select_input_method);
1826 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1827 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001828 mImeSwitcherNotification.setContentTitle(title)
1829 .setContentText(summary)
1830 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001831 try {
1832 if ((mNotificationManager != null)
1833 && !mIWindowManager.hasNavigationBar()) {
1834 if (DEBUG) {
1835 Slog.d(TAG, "--- show notification: label = " + summary);
1836 }
1837 mNotificationManager.notifyAsUser(null,
1838 com.android.internal.R.string.select_input_method,
1839 mImeSwitcherNotification.build(), UserHandle.ALL);
1840 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001841 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001842 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001843 }
1844 } else {
1845 if (mNotificationShown && mNotificationManager != null) {
1846 if (DEBUG) {
1847 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001848 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001849 mNotificationManager.cancelAsUser(null,
1850 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1851 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001852 }
satok06487a52010-10-29 11:37:18 +09001853 }
1854 } finally {
1855 Binder.restoreCallingIdentity(ident);
1856 }
1857 }
1858
satoke7c6998e2011-06-03 17:57:59 +09001859 @Override
satokf9f01002011-05-19 21:31:50 +09001860 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001861 if (!calledFromValidUser()) {
1862 return;
1863 }
satokf9f01002011-05-19 21:31:50 +09001864 synchronized (mMethodMap) {
1865 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1866 for (int i = 0; i < spans.length; ++i) {
1867 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001868 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001869 mSecureSuggestionSpans.put(ss, currentImi);
1870 }
1871 }
1872 }
1873 }
1874
satoke7c6998e2011-06-03 17:57:59 +09001875 @Override
satokf9f01002011-05-19 21:31:50 +09001876 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001877 if (!calledFromValidUser()) {
1878 return false;
1879 }
satokf9f01002011-05-19 21:31:50 +09001880 synchronized (mMethodMap) {
1881 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1882 // TODO: Do not send the intent if the process of the targetImi is already dead.
1883 if (targetImi != null) {
1884 final String[] suggestions = span.getSuggestions();
1885 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001886 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001887 final Intent intent = new Intent();
1888 // Ensures that only a class in the original IME package will receive the
1889 // notification.
satok42c5a162011-05-26 16:46:14 +09001890 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001891 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1892 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1893 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1894 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001895 final long ident = Binder.clearCallingIdentity();
1896 try {
1897 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1898 } finally {
1899 Binder.restoreCallingIdentity(ident);
1900 }
satokf9f01002011-05-19 21:31:50 +09001901 return true;
1902 }
1903 }
1904 return false;
1905 }
1906
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001907 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001908 updateInputMethodsFromSettingsLocked(enabledMayChange);
1909 updateKeyboardFromSettingsLocked();
1910 }
1911
1912 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001913 if (enabledMayChange) {
1914 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1915 for (int i=0; i<enabled.size(); i++) {
1916 // We allow the user to select "disabled until used" apps, so if they
1917 // are enabling one of those here we now need to make it enabled.
1918 InputMethodInfo imm = enabled.get(i);
1919 try {
1920 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1921 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1922 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001923 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001924 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001925 if (DEBUG) {
1926 Slog.d(TAG, "Update state(" + imm.getId()
1927 + "): DISABLED_UNTIL_USED -> DEFAULT");
1928 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001929 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1930 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001931 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1932 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001933 }
1934 } catch (RemoteException e) {
1935 }
1936 }
1937 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001938 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1939 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1940 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1941 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001942 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001943 // There is no input method selected, try to choose new applicable input method.
1944 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001945 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001946 }
1947 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001949 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001951 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001952 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
satokf3db1af2010-11-23 13:34:33 +09001954 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001955 } else {
1956 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001957 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001959 // Here is not the perfect place to reset the switching controller. Ideally
1960 // mSwitchingController and mSettings should be able to share the same state.
1961 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1962 // the same enabled IMEs list.
1963 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001964
1965 }
1966
1967 public void updateKeyboardFromSettingsLocked() {
1968 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1969 if (mSwitchingDialog != null
1970 && mSwitchingDialogTitleView != null
1971 && mSwitchingDialog.isShowing()) {
1972 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1973 com.android.internal.R.id.hard_keyboard_switch);
1974 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001977
Yohei Yukawab097b822015-12-01 10:43:08 -08001978 private void notifyInputMethodSubtypeChanged(final int userId,
1979 @Nullable final InputMethodInfo inputMethodInfo,
1980 @Nullable final InputMethodSubtype subtype) {
1981 final InputManagerInternal inputManagerInternal =
1982 LocalServices.getService(InputManagerInternal.class);
1983 if (inputManagerInternal != null) {
1984 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1985 }
1986 }
1987
satokab751aa2010-09-14 19:17:36 +09001988 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 InputMethodInfo info = mMethodMap.get(id);
1990 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001991 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001993
satokd81e9502012-05-21 12:58:45 +09001994 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001996 final int subtypeCount = info.getSubtypeCount();
1997 if (subtypeCount <= 0) {
1998 return;
satokcd7cd292010-11-20 15:46:23 +09001999 }
satokd81e9502012-05-21 12:58:45 +09002000 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2001 final InputMethodSubtype newSubtype;
2002 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2003 newSubtype = info.getSubtypeAt(subtypeId);
2004 } else {
2005 // If subtype is null, try to find the most applicable one from
2006 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002007 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002008 }
2009 if (newSubtype == null || oldSubtype == null) {
2010 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2011 + ", new subtype = " + newSubtype);
2012 return;
2013 }
2014 if (newSubtype != oldSubtype) {
2015 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2016 if (mCurMethod != null) {
2017 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002018 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002019 mCurMethod.changeInputMethodSubtype(newSubtype);
2020 } catch (RemoteException e) {
2021 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002022 return;
satokab751aa2010-09-14 19:17:36 +09002023 }
2024 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002025 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 return;
2028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002029
satokd81e9502012-05-21 12:58:45 +09002030 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 final long ident = Binder.clearCallingIdentity();
2032 try {
satokab751aa2010-09-14 19:17:36 +09002033 // Set a subtype to this input method.
2034 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002035 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2036 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2037 // because mCurMethodId is stored as a history in
2038 // setSelectedInputMethodAndSubtypeLocked().
2039 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002041 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002043 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002045 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002047 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 } finally {
2049 Binder.restoreCallingIdentity(ident);
2050 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002051
2052 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2053 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002055
satok42c5a162011-05-26 16:46:14 +09002056 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002057 public boolean showSoftInput(IInputMethodClient client, int flags,
2058 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002059 if (!calledFromValidUser()) {
2060 return false;
2061 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002062 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 long ident = Binder.clearCallingIdentity();
2064 try {
2065 synchronized (mMethodMap) {
2066 if (mCurClient == null || client == null
2067 || mCurClient.client.asBinder() != client.asBinder()) {
2068 try {
2069 // We need to check if this is the current client with
2070 // focus in the window manager, to allow this call to
2071 // be made before input is started in it.
2072 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002073 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002074 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002077 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
2079 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002080
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002081 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002082 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084 } finally {
2085 Binder.restoreCallingIdentity(ident);
2086 }
2087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002088
The Android Open Source Project4df24232009-03-05 14:34:35 -08002089 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002091 if (mAccessibilityRequestingNoSoftKeyboard) {
2092 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
Anna Galusza9b278112016-01-04 11:37:37 -08002094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2096 mShowExplicitlyRequested = true;
2097 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002098 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2099 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002101
Dianne Hackborncc278702009-09-02 23:07:23 -07002102 if (!mSystemReady) {
2103 return false;
2104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002105
The Android Open Source Project4df24232009-03-05 14:34:35 -08002106 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002108 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002109 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2110 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2111 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002113 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002114 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002115 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002116 | Context.BIND_TREAT_LIKE_ACTIVITY
2117 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002118 mVisibleBound = true;
2119 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002120 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002122 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 // The client has asked to have the input method shown, but
2124 // we have been sitting here too long with a connection to the
2125 // service and no interface received, so let's disconnect/connect
2126 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002127 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002129 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002131 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002132 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002133 } else {
2134 if (DEBUG) {
2135 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2136 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139
The Android Open Source Project4df24232009-03-05 14:34:35 -08002140 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
satok42c5a162011-05-26 16:46:14 +09002143 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002144 public boolean hideSoftInput(IInputMethodClient client, int flags,
2145 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002146 if (!calledFromValidUser()) {
2147 return false;
2148 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002149 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 long ident = Binder.clearCallingIdentity();
2151 try {
2152 synchronized (mMethodMap) {
2153 if (mCurClient == null || client == null
2154 || mCurClient.client.asBinder() != client.asBinder()) {
2155 try {
2156 // We need to check if this is the current client with
2157 // focus in the window manager, to allow this call to
2158 // be made before input is started in it.
2159 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002160 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2161 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002162 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
2164 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002165 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
2167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002168
Joe Onorato8a9b2202010-02-26 18:56:32 -08002169 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002170 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
2172 } finally {
2173 Binder.restoreCallingIdentity(ident);
2174 }
2175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002176
The Android Open Source Project4df24232009-03-05 14:34:35 -08002177 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2179 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002180 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 -08002181 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
2183 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002184 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 -08002185 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002187
2188 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2189 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2190 // to be updated with the new value sent from IME process. Even in such a transient state
2191 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2192 // application process as a valid request, and have even promised such a behavior with CTS
2193 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2194 // IMMS#InputShown indicates that the software keyboard is shown.
2195 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2196 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2197 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002198 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002199 if (shouldHideSoftInput) {
2200 // The IME will report its visible state again after the following message finally
2201 // delivered to the IME process as an IPC. Hence the inconsistency between
2202 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2203 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002204 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2205 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2206 res = true;
2207 } else {
2208 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002210 if (mHaveConnection && mVisibleBound) {
2211 mContext.unbindService(mVisibleConnection);
2212 mVisibleBound = false;
2213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 mInputShown = false;
2215 mShowRequested = false;
2216 mShowExplicitlyRequested = false;
2217 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002218 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002220
satok42c5a162011-05-26 16:46:14 +09002221 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002222 public InputBindResult startInputOrWindowGainedFocus(
2223 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2224 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002225 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002226 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002227 if (windowToken != null) {
2228 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002229 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002230 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002231 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2232 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002233 }
2234 }
2235
2236 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002237 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2238 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002239 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2240 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002241 // Needs to check the validity before clearing calling identity
2242 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002243 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 long ident = Binder.clearCallingIdentity();
2245 try {
2246 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002247 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2248 + InputMethodClient.getStartInputReason(startInputReason)
2249 + " client=" + client.asBinder()
2250 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002251 + " missingMethods="
2252 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002253 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002254 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002256 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002257
Dianne Hackborn7663d802012-02-24 13:08:49 -08002258 ClientState cs = mClients.get(client.asBinder());
2259 if (cs == null) {
2260 throw new IllegalArgumentException("unknown client "
2261 + client.asBinder());
2262 }
2263
2264 try {
2265 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2266 // Check with the window manager to make sure this client actually
2267 // has a window with focus. If not, reject. This is thread safe
2268 // because if the focus changes some time before or after, the
2269 // next client receiving focus that has any interest in input will
2270 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002271 if (DEBUG) {
2272 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2273 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2274 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002275 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002277 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002279
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002280 if (!calledFromValidUser) {
2281 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2282 Slog.w(TAG, "If you want to interect with IME, you need "
2283 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2284 hideCurrentInputLocked(0, null);
2285 return null;
2286 }
2287
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002288 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002289 if (DEBUG) {
2290 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2291 + " attribute=" + attribute + ", token = " + windowToken);
2292 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002293 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002294 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002295 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002296 }
2297 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002298 }
2299 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002300 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002301
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002302 // Should we auto-show the IME even if the caller has not
2303 // specified what should be done with it?
2304 // We only do this automatically if the window can resize
2305 // to accommodate the IME (so what the user sees will give
2306 // them good context without input information being obscured
2307 // by the IME) or if running on a large screen where there
2308 // is more room for the target window + IME.
2309 final boolean doAutoShow =
2310 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2311 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2312 || mRes.getConfiguration().isLayoutSizeAtLeast(
2313 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002314 final boolean isTextEditor =
2315 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2316
2317 // We want to start input before showing the IME, but after closing
2318 // it. We want to do this after closing it to help the IME disappear
2319 // more quickly (not get stuck behind it initializing itself for the
2320 // new focused input, even if its window wants to hide the IME).
2321 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2324 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002325 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2327 // There is no focus view, and this window will
2328 // be behind any soft input window, so hide the
2329 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002330 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002331 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002333 } else if (isTextEditor && doAutoShow && (softInputMode &
2334 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 // There is a focus view, and we are navigating forward
2336 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002337 // We only do this automatically if the window can resize
2338 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002339 // them good context without input information being obscured
2340 // by the IME) or if running on a large screen where there
2341 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002342 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002343 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002344 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002345 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002346 didStart = true;
2347 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002348 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
2350 break;
2351 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2352 // Do nothing.
2353 break;
2354 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2355 if ((softInputMode &
2356 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002357 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002358 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360 break;
2361 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002362 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002363 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 break;
2365 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2366 if ((softInputMode &
2367 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002368 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002369 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002370 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002371 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002372 didStart = true;
2373 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002374 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
2376 break;
2377 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002378 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002379 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002380 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002381 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002382 didStart = true;
2383 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002384 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 break;
2386 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002387
2388 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002389 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002390 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
2393 } finally {
2394 Binder.restoreCallingIdentity(ident);
2395 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002396
2397 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002399
satok42c5a162011-05-26 16:46:14 +09002400 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002401 public void showInputMethodPickerFromClient(
2402 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002403 if (!calledFromValidUser()) {
2404 return;
2405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 synchronized (mMethodMap) {
2407 if (mCurClient == null || client == null
2408 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002409 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002410 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
2412
satok440aab52010-11-25 09:43:11 +09002413 // Always call subtype picker, because subtype picker is a superset of input method
2414 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002415 mHandler.sendMessage(mCaller.obtainMessageI(
2416 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002417 }
2418 }
2419
satok42c5a162011-05-26 16:46:14 +09002420 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002422 if (!calledFromValidUser()) {
2423 return;
2424 }
satok28203512010-11-24 11:06:49 +09002425 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2426 }
2427
satok42c5a162011-05-26 16:46:14 +09002428 @Override
satok28203512010-11-24 11:06:49 +09002429 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002430 if (!calledFromValidUser()) {
2431 return;
2432 }
satok28203512010-11-24 11:06:49 +09002433 synchronized (mMethodMap) {
2434 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002435 setInputMethodWithSubtypeIdLocked(token, id,
2436 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2437 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002438 } else {
2439 setInputMethod(token, id);
2440 }
2441 }
satokab751aa2010-09-14 19:17:36 +09002442 }
2443
satok42c5a162011-05-26 16:46:14 +09002444 @Override
satokb416a712010-11-25 20:42:14 +09002445 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002446 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002447 if (!calledFromValidUser()) {
2448 return;
2449 }
satokb416a712010-11-25 20:42:14 +09002450 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002451 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2452 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002453 }
2454 }
2455
satok4fc87d62011-05-20 16:13:43 +09002456 @Override
satok735cf382010-11-11 20:40:09 +09002457 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002458 if (!calledFromValidUser()) {
2459 return false;
2460 }
satok735cf382010-11-11 20:40:09 +09002461 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002462 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002463 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002464 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002465 lastImi = mMethodMap.get(lastIme.first);
2466 } else {
2467 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002468 }
satok4fc87d62011-05-20 16:13:43 +09002469 String targetLastImiId = null;
2470 int subtypeId = NOT_A_SUBTYPE_ID;
2471 if (lastIme != null && lastImi != null) {
2472 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002473 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002474 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2475 : mCurrentSubtype.hashCode();
2476 // If the last IME is the same as the current IME and the last subtype is not
2477 // defined, there is no need to switch to the last IME.
2478 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2479 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002480 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002481 }
2482 }
2483
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002484 if (TextUtils.isEmpty(targetLastImiId)
2485 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002486 // This is a safety net. If the currentSubtype can't be added to the history
2487 // and the framework couldn't find the last ime, we will make the last ime be
2488 // the most applicable enabled keyboard subtype of the system imes.
2489 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2490 if (enabled != null) {
2491 final int N = enabled.size();
2492 final String locale = mCurrentSubtype == null
2493 ? mRes.getConfiguration().locale.toString()
2494 : mCurrentSubtype.getLocale();
2495 for (int i = 0; i < N; ++i) {
2496 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002497 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002498 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002499 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2500 InputMethodUtils.getSubtypes(imi),
2501 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002502 if (keyboardSubtype != null) {
2503 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002504 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002505 imi, keyboardSubtype.hashCode());
2506 if(keyboardSubtype.getLocale().equals(locale)) {
2507 break;
2508 }
2509 }
2510 }
2511 }
2512 }
2513 }
2514
2515 if (!TextUtils.isEmpty(targetLastImiId)) {
2516 if (DEBUG) {
2517 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2518 + ", from: " + mCurMethodId + ", " + subtypeId);
2519 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002520 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002521 return true;
2522 } else {
2523 return false;
2524 }
satok735cf382010-11-11 20:40:09 +09002525 }
2526 }
2527
satoke7c6998e2011-06-03 17:57:59 +09002528 @Override
satok688bd472012-02-09 20:09:17 +09002529 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002530 if (!calledFromValidUser()) {
2531 return false;
2532 }
satok688bd472012-02-09 20:09:17 +09002533 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002534 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002535 return false;
2536 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002537 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002538 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2539 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002540 if (nextSubtype == null) {
2541 return false;
2542 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002543 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2544 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002545 return true;
2546 }
2547 }
2548
2549 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002550 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2551 if (!calledFromValidUser()) {
2552 return false;
2553 }
2554 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002555 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002556 return false;
2557 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002558 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002559 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2560 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002561 if (nextSubtype == null) {
2562 return false;
2563 }
2564 return true;
2565 }
2566 }
2567
2568 @Override
satok68f1b782011-04-11 14:26:04 +09002569 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002570 if (!calledFromValidUser()) {
2571 return null;
2572 }
satok68f1b782011-04-11 14:26:04 +09002573 synchronized (mMethodMap) {
2574 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2575 // TODO: Handle the case of the last IME with no subtypes
2576 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2577 || TextUtils.isEmpty(lastIme.second)) return null;
2578 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2579 if (lastImi == null) return null;
2580 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002581 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002582 final int lastSubtypeId =
2583 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002584 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2585 return null;
2586 }
2587 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002588 } catch (NumberFormatException e) {
2589 return null;
2590 }
2591 }
2592 }
2593
satoke7c6998e2011-06-03 17:57:59 +09002594 @Override
satokee5e77c2011-09-02 18:50:15 +09002595 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002596 if (!calledFromValidUser()) {
2597 return;
2598 }
satok91e88122011-07-18 11:11:42 +09002599 // By this IPC call, only a process which shares the same uid with the IME can add
2600 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002601 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002602 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002603 if (!mSystemReady) {
2604 return;
2605 }
satok91e88122011-07-18 11:11:42 +09002606 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002607 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002608 final String[] packageInfos;
2609 try {
2610 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2611 } catch (RemoteException e) {
2612 Slog.e(TAG, "Failed to get package infos");
2613 return;
2614 }
satok91e88122011-07-18 11:11:42 +09002615 if (packageInfos != null) {
2616 final int packageNum = packageInfos.length;
2617 for (int i = 0; i < packageNum; ++i) {
2618 if (packageInfos[i].equals(imi.getPackageName())) {
2619 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002620 final long ident = Binder.clearCallingIdentity();
2621 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002622 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002623 } finally {
2624 Binder.restoreCallingIdentity(ident);
2625 }
satokee5e77c2011-09-02 18:50:15 +09002626 return;
satok91e88122011-07-18 11:11:42 +09002627 }
2628 }
2629 }
satoke7c6998e2011-06-03 17:57:59 +09002630 }
satokee5e77c2011-09-02 18:50:15 +09002631 return;
satoke7c6998e2011-06-03 17:57:59 +09002632 }
2633
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002634 @Override
2635 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002636 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002637 }
2638
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002639 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002640 public void clearLastInputMethodWindowForTransition(IBinder token) {
2641 if (!calledFromValidUser()) {
2642 return;
2643 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002644 synchronized (mMethodMap) {
2645 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002646 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002647 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002648 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002649 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002650 }
2651
2652 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002653 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002654 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002655 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002656 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002657 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002658 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2659 if (DEBUG) {
2660 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2661 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2662 + " actual: " + sequenceNumber);
2663 }
2664 return;
2665 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002666 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2667 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002668 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002669 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002670 }
2671 }
2672
satok28203512010-11-24 11:06:49 +09002673 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002675 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2676 }
2677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002679 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2680 if (token == null) {
2681 if (mContext.checkCallingOrSelfPermission(
2682 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2683 != PackageManager.PERMISSION_GRANTED) {
2684 throw new SecurityException(
2685 "Using null token requires permission "
2686 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002688 } else if (mCurToken != token) {
2689 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2690 + " token: " + token);
2691 return;
2692 }
2693
2694 final long ident = Binder.clearCallingIdentity();
2695 try {
2696 setInputMethodLocked(id, subtypeId);
2697 } finally {
2698 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 }
2700 }
2701
satok42c5a162011-05-26 16:46:14 +09002702 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002704 if (!calledFromValidUser()) {
2705 return;
2706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002708 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 return;
2710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 long ident = Binder.clearCallingIdentity();
2712 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002713 hideCurrentInputLocked(flags, null);
2714 } finally {
2715 Binder.restoreCallingIdentity(ident);
2716 }
2717 }
2718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002719
satok42c5a162011-05-26 16:46:14 +09002720 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002721 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002722 if (!calledFromValidUser()) {
2723 return;
2724 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002725 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002726 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002727 return;
2728 }
2729 long ident = Binder.clearCallingIdentity();
2730 try {
2731 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 } finally {
2733 Binder.restoreCallingIdentity(ident);
2734 }
2735 }
2736 }
2737
2738 void setEnabledSessionInMainThread(SessionState session) {
2739 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002740 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002742 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002743 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 } catch (RemoteException e) {
2745 }
2746 }
2747 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002748 if (mEnabledSession != null && mEnabledSession.session != null) {
2749 try {
2750 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2751 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2752 } catch (RemoteException e) {
2753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 }
2755 }
2756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002757
satok42c5a162011-05-26 16:46:14 +09002758 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002760 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002762 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002763 final boolean showAuxSubtypes;
2764 switch (msg.arg1) {
2765 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2766 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2767 // implemented so that auxiliary subtypes will be excluded when the soft
2768 // keyboard is invisible.
2769 showAuxSubtypes = mInputShown;
2770 break;
2771 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2772 showAuxSubtypes = true;
2773 break;
2774 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2775 showAuxSubtypes = false;
2776 break;
2777 default:
2778 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2779 return false;
2780 }
2781 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002782 return true;
2783
satok47a44912010-10-06 16:03:58 +09002784 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002785 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002786 return true;
2787
2788 case MSG_SHOW_IM_CONFIG:
2789 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002790 return true;
2791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 case MSG_UNBIND_INPUT:
2795 try {
2796 ((IInputMethod)msg.obj).unbindInput();
2797 } catch (RemoteException e) {
2798 // There is nothing interesting about the method dying.
2799 }
2800 return true;
2801 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002802 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 try {
2804 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2805 } catch (RemoteException e) {
2806 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002807 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 return true;
2809 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002810 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002812 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002813 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002814 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 } catch (RemoteException e) {
2816 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002817 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 return true;
2819 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002820 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002822 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002823 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002824 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 } catch (RemoteException e) {
2826 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002827 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002829 case MSG_HIDE_CURRENT_INPUT_METHOD:
2830 synchronized (mMethodMap) {
2831 hideCurrentInputLocked(0, null);
2832 }
2833 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002835 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002837 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2839 } catch (RemoteException e) {
2840 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002841 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002843 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002844 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002845 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002846 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002848 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002850 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002851 // Dispose the channel if the input method is not local to this process
2852 // because the remote proxy will get its own copy when unparceled.
2853 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002854 channel.dispose();
2855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002857 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002861
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002862 case MSG_START_INPUT: {
2863 int missingMethods = msg.arg1;
2864 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002866 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002868 session.method.startInput((IInputContext) args.arg2, missingMethods,
2869 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 } catch (RemoteException e) {
2871 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002872 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002874 }
2875 case MSG_RESTART_INPUT: {
2876 int missingMethods = msg.arg1;
2877 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002879 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002881 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2882 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 } catch (RemoteException e) {
2884 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002885 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002890
Yohei Yukawa33e81792015-11-17 21:14:42 -08002891 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002893 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 } catch (RemoteException e) {
2895 // There is nothing interesting about the last client dying.
2896 }
2897 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002898 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002899 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002900 IInputMethodClient client = (IInputMethodClient)args.arg1;
2901 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002903 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002905 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002906 } finally {
2907 // Dispose the channel if the input method is not local to this process
2908 // because the remote proxy will get its own copy when unparceled.
2909 if (res.channel != null && Binder.isProxy(client)) {
2910 res.channel.dispose();
2911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002913 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002915 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002916 case MSG_SET_ACTIVE:
2917 try {
2918 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2919 } catch (RemoteException e) {
2920 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2921 + ((ClientState)msg.obj).pid + " uid "
2922 + ((ClientState)msg.obj).uid);
2923 }
2924 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002925 case MSG_SET_INTERACTIVE:
2926 handleSetInteractive(msg.arg1 != 0);
2927 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002928 case MSG_SWITCH_IME:
2929 handleSwitchInputMethod(msg.arg1 != 0);
2930 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002931 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2932 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002933 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002934 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002935 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002936 } catch (RemoteException e) {
2937 Slog.w(TAG, "Got RemoteException sending "
2938 + "setUserActionNotificationSequenceNumber("
2939 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002940 + clientState.pid + " uid "
2941 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002942 }
2943 return true;
2944 }
satok01038492012-04-09 21:08:27 +09002945
2946 // --------------------------------------------------------------
2947 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002948 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002949 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002950 case MSG_SYSTEM_UNLOCK_USER:
2951 final int userId = msg.arg1;
2952 onUnlockUser(userId);
2953 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 }
2955 return false;
2956 }
2957
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002958 private void handleSetInteractive(final boolean interactive) {
2959 synchronized (mMethodMap) {
2960 mIsInteractive = interactive;
2961 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2962
2963 // Inform the current client of the change in active status
2964 if (mCurClient != null && mCurClient.client != null) {
2965 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2966 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2967 }
2968 }
2969 }
2970
Yohei Yukawaae61f712015-12-09 13:00:10 -08002971 private void handleSwitchInputMethod(final boolean forwardDirection) {
2972 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002973 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002974 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002975 if (nextSubtype == null) {
2976 return;
2977 }
2978 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002979 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2980 if (newInputMethodInfo == null) {
2981 return;
2982 }
2983 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
2984 newInputMethodInfo, mCurrentSubtype);
2985 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07002986 if (mSubtypeSwitchedByShortCutToast == null) {
2987 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
2988 Toast.LENGTH_SHORT);
2989 } else {
2990 mSubtypeSwitchedByShortCutToast.setText(toastText);
2991 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002992 mSubtypeSwitchedByShortCutToast.show();
2993 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08002994 }
2995 }
2996
satokdc9ddae2011-10-06 12:22:36 +09002997 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002998 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2999 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003000 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003001 if (DEBUG) {
3002 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3003 }
satok723a27e2010-11-11 14:58:11 +09003004 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003005 return true;
3006 }
3007
3008 return false;
3009 }
3010
Yohei Yukawa94e33302016-02-12 19:37:03 -08003011 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003012 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003013 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003014 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003015 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003016 if (!mSystemReady) {
3017 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3018 return;
3019 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003020 mMethodList.clear();
3021 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003022
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003023 // Use for queryIntentServicesAsUser
3024 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003026 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3027 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3028 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003029 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003031 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3032 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003033
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003034 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003035 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 for (int i = 0; i < services.size(); ++i) {
3037 ResolveInfo ri = services.get(i);
3038 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003039 final String imeId = InputMethodInfo.computeId(ri);
3040 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3041 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 + ": it does not require the permission "
3043 + android.Manifest.permission.BIND_INPUT_METHOD);
3044 continue;
3045 }
3046
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003047 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003049 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 try {
satoke7c6998e2011-06-03 17:57:59 +09003051 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003052 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003053 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003054 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055
3056 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003057 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003059 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003060 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003061 }
3062 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003063
Yohei Yukawa859df052016-02-17 07:56:46 -08003064 // TODO: The following code should find better place to live.
3065 if (!resetDefaultEnabledIme) {
3066 boolean enabledImeFound = false;
3067 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3068 final int N = enabledImes.size();
3069 for (int i = 0; i < N; ++i) {
3070 final InputMethodInfo imi = enabledImes.get(i);
3071 if (mMethodList.contains(imi)) {
3072 enabledImeFound = true;
3073 break;
3074 }
3075 }
3076 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003077 if (DEBUG) {
3078 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3079 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003080 resetDefaultEnabledIme = true;
3081 resetSelectedInputMethodAndSubtypeLocked("");
3082 }
3083 }
3084
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003085 if (resetDefaultEnabledIme) {
3086 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003087 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003088 final int N = defaultEnabledIme.size();
3089 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003090 final InputMethodInfo imi = defaultEnabledIme.get(i);
3091 if (DEBUG) {
3092 Slog.d(TAG, "--- enable ime = " + imi);
3093 }
3094 setInputMethodEnabledLocked(imi.getId(), true);
3095 }
3096 }
3097
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003098 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003099 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003100 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003101 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3102 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003103 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003104 }
3105 } else {
3106 // Double check that the default IME is certainly enabled.
3107 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003108 }
3109 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003110 // Here is not the perfect place to reset the switching controller. Ideally
3111 // mSwitchingController and mSettings should be able to share the same state.
3112 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3113 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003114 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003118
satok217f5482010-12-15 05:19:19 +09003119 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003120 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003121 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003122 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3123 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003124 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003125 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003126 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003127 final int userId;
3128 synchronized (mMethodMap) {
3129 userId = mSettings.getCurrentUserId();
3130 }
3131 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003132 }
3133
3134 private void showConfigureInputMethods() {
3135 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3136 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3137 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3138 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003139 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003140 }
3141
satok2c93efc2012-04-02 19:33:47 +09003142 private boolean isScreenLocked() {
3143 return mKeyguardManager != null
3144 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3145 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003146
Seigo Nonaka14e13912015-05-06 21:04:13 -07003147 private void showInputMethodMenu(boolean showAuxSubtypes) {
3148 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003151 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003152
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003153 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003154 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003155 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003156
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003157 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003158 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003159 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3160 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003161 if (immis == null || immis.size() == 0) {
3162 return;
3163 }
3164
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003165 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166
satok688bd472012-02-09 20:09:17 +09003167 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003168 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003169 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003170
satokc3690562012-01-10 20:14:43 +09003171 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003172 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003173 if (currentSubtype != null) {
3174 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003175 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3176 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003177 }
3178 }
3179
Ken Wakasa761eb372011-03-04 19:06:18 +09003180 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003181 mIms = new InputMethodInfo[N];
3182 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003183 int checkedItem = 0;
3184 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003185 final ImeSubtypeListItem item = imList.get(i);
3186 mIms[i] = item.mImi;
3187 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003188 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003189 int subtypeId = mSubtypeIds[i];
3190 if ((subtypeId == NOT_A_SUBTYPE_ID)
3191 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3192 || (subtypeId == lastInputMethodSubtypeId)) {
3193 checkedItem = i;
3194 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003197
3198 final Context settingsContext = new ContextThemeWrapper(context,
3199 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3200
3201 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003202 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3203 @Override
3204 public void onCancel(DialogInterface dialog) {
3205 hideInputMethodMenu();
3206 }
3207 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003208
3209 final Context dialogContext = mDialogBuilder.getContext();
3210 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3211 com.android.internal.R.styleable.DialogPreference,
3212 com.android.internal.R.attr.alertDialogStyle, 0);
3213 final Drawable dialogIcon = a.getDrawable(
3214 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3215 a.recycle();
3216
3217 mDialogBuilder.setIcon(dialogIcon);
3218
Yohei Yukawad34e1482016-02-11 08:03:52 -08003219 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003220 final View tv = inflater.inflate(
3221 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3222 mDialogBuilder.setCustomTitle(tv);
3223
3224 // Setup layout for a toggle switch of the hardware keyboard
3225 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003226 mSwitchingDialogTitleView
3227 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003228 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003229 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003230 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003231 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003232 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003233 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3234 @Override
3235 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003236 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003237 // Ensure that the input method dialog is dismissed when changing
3238 // the hardware keyboard state.
3239 hideInputMethodMenu();
3240 }
3241 });
3242
Alan Viverette505e3ab2014-11-24 15:22:11 -08003243 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003244 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3245 final OnClickListener choiceListener = new OnClickListener() {
3246 @Override
3247 public void onClick(final DialogInterface dialog, final int which) {
3248 synchronized (mMethodMap) {
3249 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3250 || mSubtypeIds.length <= which) {
3251 return;
satok01038492012-04-09 21:08:27 +09003252 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003253 final InputMethodInfo im = mIms[which];
3254 int subtypeId = mSubtypeIds[which];
3255 adapter.mCheckedItem = which;
3256 adapter.notifyDataSetChanged();
3257 hideInputMethodMenu();
3258 if (im != null) {
3259 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3260 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003261 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003262 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003264 }
3265 }
3266 };
3267 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003270 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003271 final Window w = mSwitchingDialog.getWindow();
3272 final WindowManager.LayoutParams attrs = w.getAttributes();
3273 w.setType(TYPE_INPUT_METHOD_DIALOG);
3274 // Use an alternate token for the dialog for that window manager can group the token
3275 // with other IME windows based on type vs. grouping based on whichever token happens
3276 // to get selected by the system later on.
3277 attrs.token = mSwitchingDialogToken;
3278 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3279 attrs.setTitle("Select input method");
3280 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003281 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 mSwitchingDialog.show();
3283 }
3284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003285
Ken Wakasa05dbb652011-08-22 15:22:43 +09003286 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3287 private final LayoutInflater mInflater;
3288 private final int mTextViewResourceId;
3289 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003290 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003291 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3292 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3293 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003294
Ken Wakasa05dbb652011-08-22 15:22:43 +09003295 mTextViewResourceId = textViewResourceId;
3296 mItemsList = itemsList;
3297 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003298 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003299 }
3300
3301 @Override
3302 public View getView(int position, View convertView, ViewGroup parent) {
3303 final View view = convertView != null ? convertView
3304 : mInflater.inflate(mTextViewResourceId, null);
3305 if (position < 0 || position >= mItemsList.size()) return view;
3306 final ImeSubtypeListItem item = mItemsList.get(position);
3307 final CharSequence imeName = item.mImeName;
3308 final CharSequence subtypeName = item.mSubtypeName;
3309 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3310 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3311 if (TextUtils.isEmpty(subtypeName)) {
3312 firstTextView.setText(imeName);
3313 secondTextView.setVisibility(View.GONE);
3314 } else {
3315 firstTextView.setText(subtypeName);
3316 secondTextView.setText(imeName);
3317 secondTextView.setVisibility(View.VISIBLE);
3318 }
3319 final RadioButton radioButton =
3320 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3321 radioButton.setChecked(position == mCheckedItem);
3322 return view;
3323 }
3324 }
3325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003327 synchronized (mMethodMap) {
3328 hideInputMethodMenuLocked();
3329 }
3330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003331
The Android Open Source Project10592532009-03-18 17:39:46 -07003332 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003333 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334
The Android Open Source Project10592532009-03-18 17:39:46 -07003335 if (mSwitchingDialog != null) {
3336 mSwitchingDialog.dismiss();
3337 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003339
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003340 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003341 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003342 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003346
satok42c5a162011-05-26 16:46:14 +09003347 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003349 // TODO: Make this work even for non-current users?
3350 if (!calledFromValidUser()) {
3351 return false;
3352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 synchronized (mMethodMap) {
3354 if (mContext.checkCallingOrSelfPermission(
3355 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3356 != PackageManager.PERMISSION_GRANTED) {
3357 throw new SecurityException(
3358 "Requires permission "
3359 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3360 }
Anna Galusza9b278112016-01-04 11:37:37 -08003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 long ident = Binder.clearCallingIdentity();
3363 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003364 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 } finally {
3366 Binder.restoreCallingIdentity(ident);
3367 }
3368 }
3369 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003370
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003371 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3372 // Make sure this is a valid input method.
3373 InputMethodInfo imm = mMethodMap.get(id);
3374 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003375 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003376 }
3377
satokd87c2592010-09-29 11:52:06 +09003378 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3379 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003380
satokd87c2592010-09-29 11:52:06 +09003381 if (enabled) {
3382 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3383 if (pair.first.equals(id)) {
3384 // We are enabling this input method, but it is already enabled.
3385 // Nothing to do. The previous state was enabled.
3386 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003387 }
3388 }
satokd87c2592010-09-29 11:52:06 +09003389 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3390 // Previous state was disabled.
3391 return false;
3392 } else {
3393 StringBuilder builder = new StringBuilder();
3394 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3395 builder, enabledInputMethodsList, id)) {
3396 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003397 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003398 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3399 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3400 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003401 }
3402 // Previous state was enabled.
3403 return true;
3404 } else {
3405 // We are disabling the input method but it is already disabled.
3406 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003407 return false;
3408 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003409 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003410 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003411
satok723a27e2010-11-11 14:58:11 +09003412 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3413 boolean setSubtypeOnly) {
3414 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003415 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003416
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003417 mCurUserActionNotificationSequenceNumber =
3418 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3419 if (DEBUG) {
3420 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3421 + mCurUserActionNotificationSequenceNumber);
3422 }
3423
3424 if (mCurClient != null && mCurClient.client != null) {
3425 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3426 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003427 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003428 }
3429
satok723a27e2010-11-11 14:58:11 +09003430 // Set Subtype here
3431 if (imi == null || subtypeId < 0) {
3432 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003433 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003434 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003435 if (subtypeId < imi.getSubtypeCount()) {
3436 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3437 mSettings.putSelectedSubtype(subtype.hashCode());
3438 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003439 } else {
3440 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003441 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003442 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003443 }
satokab751aa2010-09-14 19:17:36 +09003444 }
satok723a27e2010-11-11 14:58:11 +09003445
Yohei Yukawa68645a62016-02-17 07:54:20 -08003446 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003447 // Set InputMethod here
3448 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3449 }
3450 }
3451
3452 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3453 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3454 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3455 // newDefaultIme is empty when there is no candidate for the selected IME.
3456 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3457 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3458 if (subtypeHashCode != null) {
3459 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003460 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003461 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003462 } catch (NumberFormatException e) {
3463 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3464 }
3465 }
3466 }
3467 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003468 }
3469
satok4e4569d2010-11-19 18:45:53 +09003470 // If there are no selected shortcuts, tries finding the most applicable ones.
3471 private Pair<InputMethodInfo, InputMethodSubtype>
3472 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3473 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3474 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003475 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003476 boolean foundInSystemIME = false;
3477
3478 // Search applicable subtype for each InputMethodInfo
3479 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003480 final String imiId = imi.getId();
3481 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3482 continue;
3483 }
satokcd7cd292010-11-20 15:46:23 +09003484 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003485 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003486 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003487 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003488 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003489 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003490 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003491 }
satokdf31ae62011-01-15 06:19:44 +09003492 // 2. Search by the system locale from enabledSubtypes.
3493 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003494 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003495 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003496 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003497 }
satoka86f5e42011-09-02 17:12:42 +09003498 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003499 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003500 final ArrayList<InputMethodSubtype> subtypesForSearch =
3501 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003502 ? InputMethodUtils.getSubtypes(imi)
3503 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003504 // 4. Search by the current subtype's locale from all subtypes.
3505 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003506 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003507 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003508 }
3509 // 5. Search by the system locale from all subtypes.
3510 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003511 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003512 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003513 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003514 }
satokcd7cd292010-11-20 15:46:23 +09003515 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003516 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003517 // The current input method is the most applicable IME.
3518 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003519 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003520 break;
satok7599a7f2010-12-22 13:45:23 +09003521 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003522 // The system input method is 2nd applicable IME.
3523 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003524 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003525 if ((imi.getServiceInfo().applicationInfo.flags
3526 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3527 foundInSystemIME = true;
3528 }
satok4e4569d2010-11-19 18:45:53 +09003529 }
3530 }
3531 }
3532 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003533 if (mostApplicableIMI != null) {
3534 Slog.w(TAG, "Most applicable shortcut input method was:"
3535 + mostApplicableIMI.getId());
3536 if (mostApplicableSubtype != null) {
3537 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3538 + "," + mostApplicableSubtype.getMode() + ","
3539 + mostApplicableSubtype.getLocale());
3540 }
3541 }
satok4e4569d2010-11-19 18:45:53 +09003542 }
satokcd7cd292010-11-20 15:46:23 +09003543 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003544 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003545 } else {
3546 return null;
3547 }
3548 }
3549
satokab751aa2010-09-14 19:17:36 +09003550 /**
3551 * @return Return the current subtype of this input method.
3552 */
satok42c5a162011-05-26 16:46:14 +09003553 @Override
satokab751aa2010-09-14 19:17:36 +09003554 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003555 // TODO: Make this work even for non-current users?
3556 if (!calledFromValidUser()) {
3557 return null;
3558 }
3559 synchronized (mMethodMap) {
3560 return getCurrentInputMethodSubtypeLocked();
3561 }
3562 }
3563
3564 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003565 if (mCurMethodId == null) {
3566 return null;
3567 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003568 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003569 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3570 if (imi == null || imi.getSubtypeCount() == 0) {
3571 return null;
satok4e4569d2010-11-19 18:45:53 +09003572 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003573 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003574 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3575 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003576 if (subtypeId == NOT_A_SUBTYPE_ID) {
3577 // If there are no selected subtypes, the framework will try to find
3578 // the most applicable subtype from explicitly or implicitly enabled
3579 // subtypes.
3580 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003581 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003582 // If there is only one explicitly or implicitly enabled subtype,
3583 // just returns it.
3584 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3585 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3586 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003587 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003588 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003589 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003590 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003591 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003592 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3593 true);
satok4e4569d2010-11-19 18:45:53 +09003594 }
satok3ef8b292010-11-23 06:06:29 +09003595 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003596 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003597 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003598 }
3599 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003600 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003601 }
3602
satok4e4569d2010-11-19 18:45:53 +09003603 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003604 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003605 @Override
satok4e4569d2010-11-19 18:45:53 +09003606 public List getShortcutInputMethodsAndSubtypes() {
3607 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003608 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003609 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003610 // If there are no selected shortcut subtypes, the framework will try to find
3611 // the most applicable subtype from all subtypes whose mode is
3612 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003613 Pair<InputMethodInfo, InputMethodSubtype> info =
3614 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003615 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003616 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003617 ret.add(info.first);
3618 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003619 }
satok3da92232011-01-11 22:46:30 +09003620 return ret;
satokf3db1af2010-11-23 13:34:33 +09003621 }
satokf3db1af2010-11-23 13:34:33 +09003622 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3623 ret.add(imi);
3624 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3625 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003626 }
3627 }
satokf3db1af2010-11-23 13:34:33 +09003628 return ret;
satok4e4569d2010-11-19 18:45:53 +09003629 }
3630 }
3631
satok42c5a162011-05-26 16:46:14 +09003632 @Override
satokb66d2872010-11-10 01:04:04 +09003633 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003634 // TODO: Make this work even for non-current users?
3635 if (!calledFromValidUser()) {
3636 return false;
3637 }
satokb66d2872010-11-10 01:04:04 +09003638 synchronized (mMethodMap) {
3639 if (subtype != null && mCurMethodId != null) {
3640 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003641 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003642 if (subtypeId != NOT_A_SUBTYPE_ID) {
3643 setInputMethodLocked(mCurMethodId, subtypeId);
3644 return true;
3645 }
3646 }
3647 return false;
3648 }
3649 }
3650
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003651 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003652 private static class InputMethodFileManager {
3653 private static final String SYSTEM_PATH = "system";
3654 private static final String INPUT_METHOD_PATH = "inputmethod";
3655 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3656 private static final String NODE_SUBTYPES = "subtypes";
3657 private static final String NODE_SUBTYPE = "subtype";
3658 private static final String NODE_IMI = "imi";
3659 private static final String ATTR_ID = "id";
3660 private static final String ATTR_LABEL = "label";
3661 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003662 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003663 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003664 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003665 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3666 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3667 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003668 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003669 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3670 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003671 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003672 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003673 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003674 if (methodMap == null) {
3675 throw new NullPointerException("methodMap is null");
3676 }
3677 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003678 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003679 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3680 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003681 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003682 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003683 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3684 }
3685 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3686 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3687 if (!subtypeFile.exists()) {
3688 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003689 writeAdditionalInputMethodSubtypes(
3690 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003691 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003692 readAdditionalInputMethodSubtypes(
3693 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003694 }
3695 }
3696
3697 private void deleteAllInputMethodSubtypes(String imiId) {
3698 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003699 mAdditionalSubtypesMap.remove(imiId);
3700 writeAdditionalInputMethodSubtypes(
3701 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003702 }
3703 }
3704
3705 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003706 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003707 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003708 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003709 final int N = additionalSubtypes.length;
3710 for (int i = 0; i < N; ++i) {
3711 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003712 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003713 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003714 } else {
3715 Slog.w(TAG, "Duplicated subtype definition found: "
3716 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003717 }
3718 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003719 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3720 writeAdditionalInputMethodSubtypes(
3721 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003722 }
3723 }
3724
3725 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3726 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003727 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003728 }
3729 }
3730
3731 private static void writeAdditionalInputMethodSubtypes(
3732 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3733 HashMap<String, InputMethodInfo> methodMap) {
3734 // Safety net for the case that this function is called before methodMap is set.
3735 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3736 FileOutputStream fos = null;
3737 try {
3738 fos = subtypesFile.startWrite();
3739 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003740 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003741 out.startDocument(null, true);
3742 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3743 out.startTag(null, NODE_SUBTYPES);
3744 for (String imiId : allSubtypes.keySet()) {
3745 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3746 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3747 continue;
3748 }
3749 out.startTag(null, NODE_IMI);
3750 out.attribute(null, ATTR_ID, imiId);
3751 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3752 final int N = subtypesList.size();
3753 for (int i = 0; i < N; ++i) {
3754 final InputMethodSubtype subtype = subtypesList.get(i);
3755 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003756 if (subtype.hasSubtypeId()) {
3757 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3758 String.valueOf(subtype.getSubtypeId()));
3759 }
satoke7c6998e2011-06-03 17:57:59 +09003760 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3761 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3762 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003763 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3764 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003765 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3766 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3767 out.attribute(null, ATTR_IS_AUXILIARY,
3768 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003769 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3770 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003771 out.endTag(null, NODE_SUBTYPE);
3772 }
3773 out.endTag(null, NODE_IMI);
3774 }
3775 out.endTag(null, NODE_SUBTYPES);
3776 out.endDocument();
3777 subtypesFile.finishWrite(fos);
3778 } catch (java.io.IOException e) {
3779 Slog.w(TAG, "Error writing subtypes", e);
3780 if (fos != null) {
3781 subtypesFile.failWrite(fos);
3782 }
3783 }
3784 }
3785
3786 private static void readAdditionalInputMethodSubtypes(
3787 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3788 if (allSubtypes == null || subtypesFile == null) return;
3789 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003790 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003791 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003792 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003793 int type = parser.getEventType();
3794 // Skip parsing until START_TAG
3795 while ((type = parser.next()) != XmlPullParser.START_TAG
3796 && type != XmlPullParser.END_DOCUMENT) {}
3797 String firstNodeName = parser.getName();
3798 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3799 throw new XmlPullParserException("Xml doesn't start with subtypes");
3800 }
3801 final int depth =parser.getDepth();
3802 String currentImiId = null;
3803 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3804 while (((type = parser.next()) != XmlPullParser.END_TAG
3805 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3806 if (type != XmlPullParser.START_TAG)
3807 continue;
3808 final String nodeName = parser.getName();
3809 if (NODE_IMI.equals(nodeName)) {
3810 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3811 if (TextUtils.isEmpty(currentImiId)) {
3812 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3813 continue;
3814 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003815 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003816 allSubtypes.put(currentImiId, tempSubtypesArray);
3817 } else if (NODE_SUBTYPE.equals(nodeName)) {
3818 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3819 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3820 continue;
3821 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003822 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003823 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003824 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003825 parser.getAttributeValue(null, ATTR_LABEL));
3826 final String imeSubtypeLocale =
3827 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003828 final String languageTag =
3829 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003830 final String imeSubtypeMode =
3831 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3832 final String imeSubtypeExtraValue =
3833 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003834 final boolean isAuxiliary = "1".equals(String.valueOf(
3835 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003836 final boolean isAsciiCapable = "1".equals(String.valueOf(
3837 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003838 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003839 .setSubtypeNameResId(label)
3840 .setSubtypeIconResId(icon)
3841 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003842 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003843 .setSubtypeMode(imeSubtypeMode)
3844 .setSubtypeExtraValue(imeSubtypeExtraValue)
3845 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003846 .setIsAsciiCapable(isAsciiCapable);
3847 final String subtypeIdString =
3848 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3849 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003850 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003851 }
3852 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003853 }
3854 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003855 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3856 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003857 return;
satoke7c6998e2011-06-03 17:57:59 +09003858 }
3859 }
3860 }
3861
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003862 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3863 @NonNull
3864 private final Handler mHandler;
3865
3866 LocalServiceImpl(@NonNull final Handler handler) {
3867 mHandler = handler;
3868 }
3869
3870 @Override
3871 public void setInteractive(boolean interactive) {
3872 // Do everything in handler so as not to block the caller.
3873 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3874 interactive ? 1 : 0, 0));
3875 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003876
3877 @Override
3878 public void switchInputMethod(boolean forwardDirection) {
3879 // Do everything in handler so as not to block the caller.
3880 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3881 forwardDirection ? 1 : 0, 0));
3882 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003883
3884 @Override
3885 public void hideCurrentInputMethod() {
3886 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3887 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3888 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003889 }
3890
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003891 private static String imeWindowStatusToString(final int imeWindowVis) {
3892 final StringBuilder sb = new StringBuilder();
3893 boolean first = true;
3894 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3895 sb.append("Active");
3896 first = false;
3897 }
3898 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3899 if (!first) {
3900 sb.append("|");
3901 }
3902 sb.append("Visible");
3903 }
3904 return sb.toString();
3905 }
3906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003908 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3909 @Nullable Uri contentUri, @Nullable String packageName) {
3910 if (!calledFromValidUser()) {
3911 return null;
3912 }
3913
3914 if (token == null) {
3915 throw new NullPointerException("token");
3916 }
3917 if (packageName == null) {
3918 throw new NullPointerException("packageName");
3919 }
3920 if (contentUri == null) {
3921 throw new NullPointerException("contentUri");
3922 }
3923 final String contentUriScheme = contentUri.getScheme();
3924 if (!"content".equals(contentUriScheme)) {
3925 throw new InvalidParameterException("contentUri must have content scheme");
3926 }
3927
3928 synchronized (mMethodMap) {
3929 final int uid = Binder.getCallingUid();
3930 if (mCurMethodId == null) {
3931 return null;
3932 }
3933 if (mCurToken != token) {
3934 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3935 + " token=" + token);
3936 return null;
3937 }
3938 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3939 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3940 // nature of our system. Let's compare it with our internal record.
3941 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3942 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3943 + mCurAttribute.packageName + " packageName=" + packageName);
3944 return null;
3945 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003946 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003947 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003948 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003949 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003950 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
3951 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
3952 imeUserId);
3953 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
3954 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
3955 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
3956 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
3957 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
3958 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
3959 // actually allowed to "uid", which is guaranteed to be the IME's one.
3960 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
3961 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07003962 }
3963 }
3964
3965 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3967 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3968 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3971 + Binder.getCallingPid()
3972 + ", uid=" + Binder.getCallingUid());
3973 return;
3974 }
3975
3976 IInputMethod method;
3977 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003978 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 synchronized (mMethodMap) {
3983 p.println("Current Input Method Manager state:");
3984 int N = mMethodList.size();
3985 p.println(" Input Methods:");
3986 for (int i=0; i<N; i++) {
3987 InputMethodInfo info = mMethodList.get(i);
3988 p.println(" InputMethod #" + i + ":");
3989 info.dump(p, " ");
3990 }
3991 p.println(" Clients:");
3992 for (ClientState ci : mClients.values()) {
3993 p.println(" Client " + ci + ":");
3994 p.println(" client=" + ci.client);
3995 p.println(" inputContext=" + ci.inputContext);
3996 p.println(" sessionRequested=" + ci.sessionRequested);
3997 p.println(" curSession=" + ci.curSession);
3998 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003999 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004001 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4002 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004003 focusedWindowClient = mCurFocusedWindowClient;
4004 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4006 + " mBoundToMethod=" + mBoundToMethod);
4007 p.println(" mCurToken=" + mCurToken);
4008 p.println(" mCurIntent=" + mCurIntent);
4009 method = mCurMethod;
4010 p.println(" mCurMethod=" + mCurMethod);
4011 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004012 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 p.println(" mShowRequested=" + mShowRequested
4014 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4015 + " mShowForced=" + mShowForced
4016 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004017 p.println(" mCurUserActionNotificationSequenceNumber="
4018 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004019 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004020 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004021 p.println(" mSwitchingController:");
4022 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004023 p.println(" mSettings:");
4024 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004026
Jeff Brownb88102f2010-09-08 11:49:43 -07004027 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 pw.flush();
4030 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004031 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4032 } catch (IOException | RemoteException e) {
4033 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004035 } else {
4036 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004038
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004039 if (focusedWindowClient != null && client != focusedWindowClient) {
4040 p.println(" ");
4041 p.println("Warning: Current input method client doesn't match the last focused. "
4042 + "window.");
4043 p.println("Dumping input method client in the last focused window just in case.");
4044 p.println(" ");
4045 pw.flush();
4046 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004047 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4048 } catch (IOException | RemoteException e) {
4049 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004050 }
4051 }
4052
Jeff Brownb88102f2010-09-08 11:49:43 -07004053 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 pw.flush();
4056 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004057 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4058 } catch (IOException | RemoteException e) {
4059 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004061 } else {
4062 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
4064 }
4065}