blob: 8020f0e42c37d819ead80f1bfa34eacf81444760 [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);
935 Slog.i(TAG, "Default found, using " + defIm.getId());
936 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900937 }
938
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900939 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
940 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900941 if (!mSystemReady) {
942 // not system ready
943 return;
944 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800945 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900946 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800947 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 if (!updateOnlyWhenLocaleChanged) {
949 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800950 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900951 }
952 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800953 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800955 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 if (!updateOnlyWhenLocaleChanged) {
957 final String selectedImiId = mSettings.getSelectedInputMethod();
958 if (TextUtils.isEmpty(selectedImiId)) {
959 // This is the first time of the user switch and
960 // set the current ime to the proper one.
961 resetDefaultImeLocked(mContext);
962 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900963 } else {
964 // If the locale is changed, needs to reset the default ime
965 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900966 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800967 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800968 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900969 if (!updateOnlyWhenLocaleChanged) {
970 try {
971 startInputInnerLocked();
972 } catch (RuntimeException e) {
973 Slog.w(TAG, "Unexpected exception", e);
974 }
975 }
976 }
977 }
978
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900979 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900980 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
981 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900982 }
983
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900984 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700985 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
986 + " currentUserId=" + mSettings.getCurrentUserId());
987
Yohei Yukawa81482972015-06-04 00:58:59 -0700988 // ContentObserver should be registered again when the user is changed
989 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800990
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800991 // If the system is not ready or the device is not yed unlocked by the user, then we use
992 // copy-on-write settings.
993 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -0600994 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800995 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +0100996 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900997 // InputMethodFileManager should be reset when the user is changed
998 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900999 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001000
1001 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1002 + " defaultImiId=" + defaultImiId);
1003
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001004 // For secondary users, the list of enabled IMEs may not have been updated since the
1005 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1006 // not be empty even if the IME has been uninstalled by the primary user.
1007 // Even in such cases, IMMS works fine because it will find the most applicable
1008 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001009 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001010 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001011 initialUserSwitch /* needsToResetDefaultIme */);
1012 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001013 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1014 mSettings.getEnabledInputMethodListLocked(), newUserId,
1015 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001016 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001017
1018 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1019 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001020 }
1021
Kenny Guy2a764942014-04-02 13:29:20 +01001022 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001023 mSettings.setCurrentProfileIds(
1024 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001025 }
1026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 @Override
1028 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1029 throws RemoteException {
1030 try {
1031 return super.onTransact(code, data, reply, flags);
1032 } catch (RuntimeException e) {
1033 // The input method manager only throws security exceptions, so let's
1034 // log all others.
1035 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001036 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038 throw e;
1039 }
1040 }
1041
Svetoslav Ganova0027152013-06-25 14:59:53 -07001042 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001043 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001044 if (DEBUG) {
1045 Slog.d(TAG, "--- systemReady");
1046 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001047 if (!mSystemReady) {
1048 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001049 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001050 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001051 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001052 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001053 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1054 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001055 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001056 if (mStatusBar != null) {
1057 mStatusBar.setIconVisibility(mSlotIme, false);
1058 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001059 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001060 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1061 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001062 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001063 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001064 mHardKeyboardListener);
1065 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001066
1067 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1068 mSettingsObserver.registerContentObserverLocked(currentUserId);
1069
1070 final IntentFilter broadcastFilter = new IntentFilter();
1071 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1072 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1073 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1074 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1075 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1076 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1077
1078 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1079 resetDefaultImeLocked(mContext);
1080 updateFromSettingsLocked(true);
1081 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1082 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1083 mContext.getBasePackageName());
1084
Dianne Hackborncc278702009-09-02 23:07:23 -07001085 try {
1086 startInputInnerLocked();
1087 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001088 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001089 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001090 }
1091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001094 // ---------------------------------------------------------------------------------------
1095 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1096 // 1) it comes from the system process
1097 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1098 private boolean calledFromValidUser() {
1099 final int uid = Binder.getCallingUid();
1100 final int userId = UserHandle.getUserId(uid);
1101 if (DEBUG) {
1102 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1103 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1104 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001105 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1106 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001107 }
Kenny Guy2a764942014-04-02 13:29:20 +01001108 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001109 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001110 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001111
1112 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1113 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1114 // must not manage background users' states in any functions.
1115 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1116 // by a token.
1117 if (mContext.checkCallingOrSelfPermission(
1118 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1119 == PackageManager.PERMISSION_GRANTED) {
1120 if (DEBUG) {
1121 Slog.d(TAG, "--- Access granted because the calling process has "
1122 + "the INTERACT_ACROSS_USERS_FULL permission");
1123 }
1124 return true;
1125 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001126 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1127 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001128 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001129 }
1130
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001131
1132 /**
1133 * Returns true iff the caller is identified to be the current input method with the token.
1134 * @param token The window token given to the input method when it was started.
1135 * @return true if and only if non-null valid token is specified.
1136 */
1137 private boolean calledWithValidToken(IBinder token) {
1138 if (token == null || mCurToken != token) {
1139 return false;
1140 }
1141 return true;
1142 }
1143
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001144 private boolean bindCurrentInputMethodService(
1145 Intent service, ServiceConnection conn, int flags) {
1146 if (service == null || conn == null) {
1147 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1148 return false;
1149 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001150 return mContext.bindServiceAsUser(service, conn, flags,
1151 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001152 }
1153
satoke7c6998e2011-06-03 17:57:59 +09001154 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001156 // TODO: Make this work even for non-current users?
1157 if (!calledFromValidUser()) {
1158 return Collections.emptyList();
1159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001161 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
1163 }
1164
satoke7c6998e2011-06-03 17:57:59 +09001165 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001167 // TODO: Make this work even for non-current users?
1168 if (!calledFromValidUser()) {
1169 return Collections.emptyList();
1170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001172 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 }
1175
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001176 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001177 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001178 * @return enabled subtypes of the specified imi
1179 */
satoke7c6998e2011-06-03 17:57:59 +09001180 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001181 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001182 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001183 // TODO: Make this work even for non-current users?
1184 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001185 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001186 }
satok67ddf9c2010-11-17 09:45:54 +09001187 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001188 final InputMethodInfo imi;
1189 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001190 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001191 } else {
1192 imi = mMethodMap.get(imiId);
1193 }
1194 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001195 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001196 }
1197 return mSettings.getEnabledInputMethodSubtypeListLocked(
1198 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001199 }
1200 }
1201
satoke7c6998e2011-06-03 17:57:59 +09001202 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 public void addClient(IInputMethodClient client,
1204 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001205 if (!calledFromValidUser()) {
1206 return;
1207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 synchronized (mMethodMap) {
1209 mClients.put(client.asBinder(), new ClientState(client,
1210 inputContext, uid, pid));
1211 }
1212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
satoke7c6998e2011-06-03 17:57:59 +09001214 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001216 if (!calledFromValidUser()) {
1217 return;
1218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001220 ClientState cs = mClients.remove(client.asBinder());
1221 if (cs != null) {
1222 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001223 if (mCurClient == cs) {
1224 mCurClient = null;
1225 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001226 if (mCurFocusedWindowClient == cs) {
1227 mCurFocusedWindowClient = null;
1228 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 void executeOrSendMessage(IInterface target, Message msg) {
1234 if (target.asBinder() instanceof Binder) {
1235 mCaller.sendMessage(msg);
1236 } else {
1237 handleMessage(msg);
1238 msg.recycle();
1239 }
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
Yohei Yukawa33e81792015-11-17 21:14:42 -08001242 void unbindCurrentClientLocked(
1243 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001245 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 + mCurClient.client.asBinder());
1247 if (mBoundToMethod) {
1248 mBoundToMethod = false;
1249 if (mCurMethod != null) {
1250 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1251 MSG_UNBIND_INPUT, mCurMethod));
1252 }
1253 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001254
1255 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1256 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001257 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1258 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
The Android Open Source Project10592532009-03-18 17:39:46 -07001262 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 }
1264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 private int getImeShowFlags() {
1267 int flags = 0;
1268 if (mShowForced) {
1269 flags |= InputMethod.SHOW_FORCED
1270 | InputMethod.SHOW_EXPLICIT;
1271 } else if (mShowExplicitlyRequested) {
1272 flags |= InputMethod.SHOW_EXPLICIT;
1273 }
1274 return flags;
1275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 private int getAppShowFlags() {
1278 int flags = 0;
1279 if (mShowForced) {
1280 flags |= InputMethodManager.SHOW_FORCED;
1281 } else if (!mShowExplicitlyRequested) {
1282 flags |= InputMethodManager.SHOW_IMPLICIT;
1283 }
1284 return flags;
1285 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001286
Dianne Hackborn7663d802012-02-24 13:08:49 -08001287 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 if (!mBoundToMethod) {
1289 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1290 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1291 mBoundToMethod = true;
1292 }
1293 final SessionState session = mCurClient.curSession;
1294 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001295 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1296 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1297 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001299 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1300 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1301 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
1303 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001304 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001305 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001307 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001308 (session.channel != null ? session.channel.dup() : null),
1309 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001312 InputBindResult startInputLocked(
1313 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001314 IInputMethodClient client, IInputContext inputContext,
1315 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001316 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 // If no method is currently selected, do nothing.
1318 if (mCurMethodId == null) {
1319 return mNoBinding;
1320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 ClientState cs = mClients.get(client.asBinder());
1323 if (cs == null) {
1324 throw new IllegalArgumentException("unknown client "
1325 + client.asBinder());
1326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001327
Yohei Yukawa74750f22016-03-22 12:54:22 -07001328 if (attribute == null) {
1329 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1330 + " uid=" + cs.uid + " pid=" + cs.pid);
1331 return null;
1332 }
1333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 try {
1335 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1336 // Check with the window manager to make sure this client actually
1337 // has a window with focus. If not, reject. This is thread safe
1338 // because if the focus changes some time before or after, the
1339 // next client receiving focus that has any interest in input will
1340 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001341 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1343 return null;
1344 }
1345 } catch (RemoteException e) {
1346 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001347
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001348 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1349 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001350 }
1351
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001352 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001353 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001354 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001355 // If no method is currently selected, do nothing.
1356 if (mCurMethodId == null) {
1357 return mNoBinding;
1358 }
1359
Yohei Yukawad57ba672015-06-08 16:39:46 -07001360 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1361 attribute.packageName)) {
1362 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1363 + " uid=" + cs.uid + " package=" + attribute.packageName);
1364 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001365 }
1366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001368 // Was the keyguard locked when switching over to the new client?
1369 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 // If the client is changing, we need to switch over to the new
1371 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001372 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001373 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001374 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375
1376 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001377 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001378 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001379 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 // Bump up the sequence for this client and attach it.
1384 mCurSeq++;
1385 if (mCurSeq <= 0) mCurSeq = 1;
1386 mCurClient = cs;
1387 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001388 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 // Check if the input method is changing.
1392 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1393 if (cs.curSession != null) {
1394 // Fast case: if we are already connected to the input method,
1395 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001396 return attachNewInputLocked(
1397 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 if (mHaveConnection) {
1400 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 // Return to client, and we will get back with it when
1402 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001403 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001404 return new InputBindResult(null, null, mCurId, mCurSeq,
1405 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 } else if (SystemClock.uptimeMillis()
1407 < (mLastBindTime+TIME_TO_RECONNECT)) {
1408 // In this case we have connected to the service, but
1409 // don't yet have its interface. If it hasn't been too
1410 // long since we did the connection, we'll return to
1411 // the client and wait to get the service interface so
1412 // we can report back. If it has been too long, we want
1413 // to fall through so we can try a disconnect/reconnect
1414 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001415 return new InputBindResult(null, null, mCurId, mCurSeq,
1416 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1419 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421 }
1422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001423
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001424 return startInputInnerLocked();
1425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001427 InputBindResult startInputInnerLocked() {
1428 if (mCurMethodId == null) {
1429 return mNoBinding;
1430 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001432 if (!mSystemReady) {
1433 // If the system is not yet ready, we shouldn't be running third
1434 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001435 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1436 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1440 if (info == null) {
1441 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001444 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1447 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001448 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1449 com.android.internal.R.string.input_method_binding_label);
1450 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1451 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001452 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001453 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1454 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 mLastBindTime = SystemClock.uptimeMillis();
1456 mHaveConnection = true;
1457 mCurId = info.getId();
1458 mCurToken = new Binder();
1459 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001460 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001461 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 } catch (RemoteException e) {
1463 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001464 return new InputBindResult(null, null, mCurId, mCurSeq,
1465 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 } else {
1467 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001468 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 + mCurIntent);
1470 }
1471 return null;
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001474 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001475 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001476 IInputMethodClient client, IInputContext inputContext,
1477 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001478 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001479 if (!calledFromValidUser()) {
1480 return null;
1481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001483 if (DEBUG) {
1484 Slog.v(TAG, "startInput: reason="
1485 + InputMethodClient.getStartInputReason(startInputReason)
1486 + " client = " + client.asBinder()
1487 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001488 + " missingMethods="
1489 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001490 + " attribute=" + attribute
1491 + " controlFlags=#" + Integer.toHexString(controlFlags));
1492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 final long ident = Binder.clearCallingIdentity();
1494 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001495 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1496 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 } finally {
1498 Binder.restoreCallingIdentity(ident);
1499 }
1500 }
1501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
satoke7c6998e2011-06-03 17:57:59 +09001503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 public void finishInput(IInputMethodClient client) {
1505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506
satoke7c6998e2011-06-03 17:57:59 +09001507 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 public void onServiceConnected(ComponentName name, IBinder service) {
1509 synchronized (mMethodMap) {
1510 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1511 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001512 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001513 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001514 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001515 return;
1516 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001517 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001518 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1519 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001521 clearClientSessionLocked(mCurClient);
1522 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524 }
1525 }
1526 }
1527
Jeff Brownc28867a2013-03-26 15:42:39 -07001528 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1529 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 synchronized (mMethodMap) {
1531 if (mCurMethod != null && method != null
1532 && mCurMethod.asBinder() == method.asBinder()) {
1533 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001534 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001536 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001537 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 if (res.method != null) {
1539 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001540 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001542 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
1544 }
1545 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001546
1547 // Session abandoned. Close its associated input channel.
1548 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001550
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001551 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001552 if (mVisibleBound) {
1553 mContext.unbindService(mVisibleConnection);
1554 mVisibleBound = false;
1555 }
1556
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001557 if (mHaveConnection) {
1558 mContext.unbindService(this);
1559 mHaveConnection = false;
1560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001561
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001562 if (mCurToken != null) {
1563 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001564 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001565 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001566 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001567 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001568 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001569 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001570 } catch (RemoteException e) {
1571 }
1572 mCurToken = null;
1573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
The Android Open Source Project10592532009-03-18 17:39:46 -07001575 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001576 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
Yohei Yukawa33e81792015-11-17 21:14:42 -08001579 void resetCurrentMethodAndClient(
1580 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001581 mCurMethodId = null;
1582 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001583 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001585
1586 void requestClientSessionLocked(ClientState cs) {
1587 if (!cs.sessionRequested) {
1588 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1589 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1590 cs.sessionRequested = true;
1591 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1592 MSG_CREATE_SESSION, mCurMethod, channels[1],
1593 new MethodCallback(this, mCurMethod, channels[0])));
1594 }
1595 }
1596
1597 void clearClientSessionLocked(ClientState cs) {
1598 finishSessionLocked(cs.curSession);
1599 cs.curSession = null;
1600 cs.sessionRequested = false;
1601 }
1602
1603 private void finishSessionLocked(SessionState sessionState) {
1604 if (sessionState != null) {
1605 if (sessionState.session != null) {
1606 try {
1607 sessionState.session.finishSession();
1608 } catch (RemoteException e) {
1609 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001610 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001611 }
1612 sessionState.session = null;
1613 }
1614 if (sessionState.channel != null) {
1615 sessionState.channel.dispose();
1616 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001617 }
1618 }
1619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001620
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001621 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 if (mCurMethod != null) {
1623 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001624 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001626
Jeff Brownc28867a2013-03-26 15:42:39 -07001627 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001628 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 mCurMethod = null;
1630 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001631 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001632 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001635
satoke7c6998e2011-06-03 17:57:59 +09001636 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001638 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1639 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1640 // which is irregular but can eventually happen for everyone just by continuing using the
1641 // device. Thus it is important to make sure that all the internal states are properly
1642 // refreshed when this method is called back. Running
1643 // adb install -r <APK that implements the current IME>
1644 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001646 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 + " mCurIntent=" + mCurIntent);
1648 if (mCurMethod != null && mCurIntent != null
1649 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001650 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 // We consider this to be a new bind attempt, since the system
1652 // should now try to restart the service for us.
1653 mLastBindTime = SystemClock.uptimeMillis();
1654 mShowRequested = mInputShown;
1655 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001656 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
1658 }
1659 }
1660
satokf9f01002011-05-19 21:31:50 +09001661 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001663 synchronized (mMethodMap) {
1664 if (!calledWithValidToken(token)) {
1665 final int uid = Binder.getCallingUid();
1666 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1667 + " token:" + token);
1668 return;
1669 }
1670 final long ident = Binder.clearCallingIdentity();
1671 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001673 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001674 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001675 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001678 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001679 CharSequence contentDescription = null;
1680 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001681 // Use PackageManager to load label
1682 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001683 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001684 mIPackageManager.getApplicationInfo(packageName, 0,
1685 mSettings.getCurrentUserId()));
1686 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001687 /* ignore */
1688 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001689 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001690 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001691 contentDescription != null
1692 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001693 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001696 } finally {
1697 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
1700 }
1701
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001702 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001703 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001704 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001705 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001706 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001707 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001708 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1709 // When physical keyboard is attached, we show the ime switcher (or notification if
1710 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1711 // exists in the IME switcher dialog. Might be OK to remove this condition once
1712 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1713 return true;
1714 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001715 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1716 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001717 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001718
1719 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1720 final int N = imis.size();
1721 if (N > 2) return true;
1722 if (N < 1) return false;
1723 int nonAuxCount = 0;
1724 int auxCount = 0;
1725 InputMethodSubtype nonAuxSubtype = null;
1726 InputMethodSubtype auxSubtype = null;
1727 for(int i = 0; i < N; ++i) {
1728 final InputMethodInfo imi = imis.get(i);
1729 final List<InputMethodSubtype> subtypes =
1730 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1731 final int subtypeCount = subtypes.size();
1732 if (subtypeCount == 0) {
1733 ++nonAuxCount;
1734 } else {
1735 for (int j = 0; j < subtypeCount; ++j) {
1736 final InputMethodSubtype subtype = subtypes.get(j);
1737 if (!subtype.isAuxiliary()) {
1738 ++nonAuxCount;
1739 nonAuxSubtype = subtype;
1740 } else {
1741 ++auxCount;
1742 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001743 }
1744 }
satok7cfc0ed2011-06-20 21:29:36 +09001745 }
1746 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001747 if (nonAuxCount > 1 || auxCount > 1) {
1748 return true;
1749 } else if (nonAuxCount == 1 && auxCount == 1) {
1750 if (nonAuxSubtype != null && auxSubtype != null
1751 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1752 || auxSubtype.overridesImplicitlyEnabledSubtype()
1753 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1754 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1755 return false;
1756 }
1757 return true;
1758 }
1759 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001760 }
1761
John Spurlocke0980502013-10-25 11:59:29 -04001762 private boolean isKeyguardLocked() {
1763 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1764 }
1765
satokdbf29502011-08-25 15:28:23 +09001766 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001767 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001768 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001769 if (!calledWithValidToken(token)) {
1770 final int uid = Binder.getCallingUid();
1771 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1772 + " token:" + token);
1773 return;
1774 }
1775
1776 synchronized (mMethodMap) {
1777 mImeWindowVis = vis;
1778 mBackDisposition = backDisposition;
1779 updateSystemUiLocked(token, vis, backDisposition);
1780 }
1781 }
1782
1783 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1784 synchronized (mMethodMap) {
1785 updateSystemUiLocked(token, vis, backDisposition);
1786 }
1787 }
1788
1789 // Caution! This method is called in this class. Handle multi-user carefully
1790 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1791 if (!calledWithValidToken(token)) {
1792 final int uid = Binder.getCallingUid();
1793 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1794 + " token:" + token);
1795 return;
1796 }
1797
1798 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1799 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001800 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001801 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001802 // apply policy for binder calls
1803 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1804 vis = 0;
satok06487a52010-10-29 11:37:18 +09001805 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001806 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1807 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1808 if (mStatusBar != null) {
1809 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1810 needsToShowImeSwitcher);
1811 }
1812 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1813 if (imi != null && needsToShowImeSwitcher) {
1814 // Used to load label
1815 final CharSequence title = mRes.getText(
1816 com.android.internal.R.string.select_input_method);
1817 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1818 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001819 mImeSwitcherNotification.setContentTitle(title)
1820 .setContentText(summary)
1821 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001822 try {
1823 if ((mNotificationManager != null)
1824 && !mIWindowManager.hasNavigationBar()) {
1825 if (DEBUG) {
1826 Slog.d(TAG, "--- show notification: label = " + summary);
1827 }
1828 mNotificationManager.notifyAsUser(null,
1829 com.android.internal.R.string.select_input_method,
1830 mImeSwitcherNotification.build(), UserHandle.ALL);
1831 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001832 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001833 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001834 }
1835 } else {
1836 if (mNotificationShown && mNotificationManager != null) {
1837 if (DEBUG) {
1838 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001839 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001840 mNotificationManager.cancelAsUser(null,
1841 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1842 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001843 }
satok06487a52010-10-29 11:37:18 +09001844 }
1845 } finally {
1846 Binder.restoreCallingIdentity(ident);
1847 }
1848 }
1849
satoke7c6998e2011-06-03 17:57:59 +09001850 @Override
satokf9f01002011-05-19 21:31:50 +09001851 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001852 if (!calledFromValidUser()) {
1853 return;
1854 }
satokf9f01002011-05-19 21:31:50 +09001855 synchronized (mMethodMap) {
1856 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1857 for (int i = 0; i < spans.length; ++i) {
1858 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001859 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001860 mSecureSuggestionSpans.put(ss, currentImi);
1861 }
1862 }
1863 }
1864 }
1865
satoke7c6998e2011-06-03 17:57:59 +09001866 @Override
satokf9f01002011-05-19 21:31:50 +09001867 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001868 if (!calledFromValidUser()) {
1869 return false;
1870 }
satokf9f01002011-05-19 21:31:50 +09001871 synchronized (mMethodMap) {
1872 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1873 // TODO: Do not send the intent if the process of the targetImi is already dead.
1874 if (targetImi != null) {
1875 final String[] suggestions = span.getSuggestions();
1876 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001877 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001878 final Intent intent = new Intent();
1879 // Ensures that only a class in the original IME package will receive the
1880 // notification.
satok42c5a162011-05-26 16:46:14 +09001881 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001882 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1883 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1884 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1885 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001886 final long ident = Binder.clearCallingIdentity();
1887 try {
1888 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1889 } finally {
1890 Binder.restoreCallingIdentity(ident);
1891 }
satokf9f01002011-05-19 21:31:50 +09001892 return true;
1893 }
1894 }
1895 return false;
1896 }
1897
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001898 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001899 updateInputMethodsFromSettingsLocked(enabledMayChange);
1900 updateKeyboardFromSettingsLocked();
1901 }
1902
1903 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001904 if (enabledMayChange) {
1905 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1906 for (int i=0; i<enabled.size(); i++) {
1907 // We allow the user to select "disabled until used" apps, so if they
1908 // are enabling one of those here we now need to make it enabled.
1909 InputMethodInfo imm = enabled.get(i);
1910 try {
1911 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1912 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1913 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001914 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001915 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001916 if (DEBUG) {
1917 Slog.d(TAG, "Update state(" + imm.getId()
1918 + "): DISABLED_UNTIL_USED -> DEFAULT");
1919 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001920 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1921 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001922 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1923 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001924 }
1925 } catch (RemoteException e) {
1926 }
1927 }
1928 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001929 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1930 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1931 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1932 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001933 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001934 // There is no input method selected, try to choose new applicable input method.
1935 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001936 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001937 }
1938 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001940 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001942 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001943 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 }
satokf3db1af2010-11-23 13:34:33 +09001945 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001946 } else {
1947 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001948 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001950 // Here is not the perfect place to reset the switching controller. Ideally
1951 // mSwitchingController and mSettings should be able to share the same state.
1952 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1953 // the same enabled IMEs list.
1954 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001955
1956 }
1957
1958 public void updateKeyboardFromSettingsLocked() {
1959 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1960 if (mSwitchingDialog != null
1961 && mSwitchingDialogTitleView != null
1962 && mSwitchingDialog.isShowing()) {
1963 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1964 com.android.internal.R.id.hard_keyboard_switch);
1965 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001968
Yohei Yukawab097b822015-12-01 10:43:08 -08001969 private void notifyInputMethodSubtypeChanged(final int userId,
1970 @Nullable final InputMethodInfo inputMethodInfo,
1971 @Nullable final InputMethodSubtype subtype) {
1972 final InputManagerInternal inputManagerInternal =
1973 LocalServices.getService(InputManagerInternal.class);
1974 if (inputManagerInternal != null) {
1975 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1976 }
1977 }
1978
satokab751aa2010-09-14 19:17:36 +09001979 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 InputMethodInfo info = mMethodMap.get(id);
1981 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001982 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001984
satokd81e9502012-05-21 12:58:45 +09001985 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001987 final int subtypeCount = info.getSubtypeCount();
1988 if (subtypeCount <= 0) {
1989 return;
satokcd7cd292010-11-20 15:46:23 +09001990 }
satokd81e9502012-05-21 12:58:45 +09001991 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1992 final InputMethodSubtype newSubtype;
1993 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1994 newSubtype = info.getSubtypeAt(subtypeId);
1995 } else {
1996 // If subtype is null, try to find the most applicable one from
1997 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001998 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001999 }
2000 if (newSubtype == null || oldSubtype == null) {
2001 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2002 + ", new subtype = " + newSubtype);
2003 return;
2004 }
2005 if (newSubtype != oldSubtype) {
2006 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2007 if (mCurMethod != null) {
2008 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002009 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002010 mCurMethod.changeInputMethodSubtype(newSubtype);
2011 } catch (RemoteException e) {
2012 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002013 return;
satokab751aa2010-09-14 19:17:36 +09002014 }
2015 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002016 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 return;
2019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002020
satokd81e9502012-05-21 12:58:45 +09002021 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 final long ident = Binder.clearCallingIdentity();
2023 try {
satokab751aa2010-09-14 19:17:36 +09002024 // Set a subtype to this input method.
2025 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002026 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2027 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2028 // because mCurMethodId is stored as a history in
2029 // setSelectedInputMethodAndSubtypeLocked().
2030 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002032 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002034 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002036 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002038 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 } finally {
2040 Binder.restoreCallingIdentity(ident);
2041 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002042
2043 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2044 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002046
satok42c5a162011-05-26 16:46:14 +09002047 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002048 public boolean showSoftInput(IInputMethodClient client, int flags,
2049 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002050 if (!calledFromValidUser()) {
2051 return false;
2052 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002053 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 long ident = Binder.clearCallingIdentity();
2055 try {
2056 synchronized (mMethodMap) {
2057 if (mCurClient == null || client == null
2058 || mCurClient.client.asBinder() != client.asBinder()) {
2059 try {
2060 // We need to check if this is the current client with
2061 // focus in the window manager, to allow this call to
2062 // be made before input is started in it.
2063 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002064 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002065 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
2067 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002068 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002071
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002072 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002073 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
2075 } finally {
2076 Binder.restoreCallingIdentity(ident);
2077 }
2078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002079
The Android Open Source Project4df24232009-03-05 14:34:35 -08002080 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002082 if (mAccessibilityRequestingNoSoftKeyboard) {
2083 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
Anna Galusza9b278112016-01-04 11:37:37 -08002085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2087 mShowExplicitlyRequested = true;
2088 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002089 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2090 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
Dianne Hackborncc278702009-09-02 23:07:23 -07002093 if (!mSystemReady) {
2094 return false;
2095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002096
The Android Open Source Project4df24232009-03-05 14:34:35 -08002097 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002099 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002100 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2101 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2102 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002104 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002105 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002106 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002107 | Context.BIND_TREAT_LIKE_ACTIVITY
2108 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002109 mVisibleBound = true;
2110 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002113 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 // The client has asked to have the input method shown, but
2115 // we have been sitting here too long with a connection to the
2116 // service and no interface received, so let's disconnect/connect
2117 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002120 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002122 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002123 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002124 } else {
2125 if (DEBUG) {
2126 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2127 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002133
satok42c5a162011-05-26 16:46:14 +09002134 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002135 public boolean hideSoftInput(IInputMethodClient client, int flags,
2136 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002137 if (!calledFromValidUser()) {
2138 return false;
2139 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002140 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 long ident = Binder.clearCallingIdentity();
2142 try {
2143 synchronized (mMethodMap) {
2144 if (mCurClient == null || client == null
2145 || mCurClient.client.asBinder() != client.asBinder()) {
2146 try {
2147 // We need to check if this is the current client with
2148 // focus in the window manager, to allow this call to
2149 // be made before input is started in it.
2150 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002151 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2152 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002153 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 }
2155 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002156 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002159
Joe Onorato8a9b2202010-02-26 18:56:32 -08002160 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002161 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
2163 } finally {
2164 Binder.restoreCallingIdentity(ident);
2165 }
2166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
The Android Open Source Project4df24232009-03-05 14:34:35 -08002168 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2170 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002171 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 -08002172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
2174 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002175 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 -08002176 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002178
2179 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2180 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2181 // to be updated with the new value sent from IME process. Even in such a transient state
2182 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2183 // application process as a valid request, and have even promised such a behavior with CTS
2184 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2185 // IMMS#InputShown indicates that the software keyboard is shown.
2186 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2187 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2188 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002189 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002190 if (shouldHideSoftInput) {
2191 // The IME will report its visible state again after the following message finally
2192 // delivered to the IME process as an IPC. Hence the inconsistency between
2193 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2194 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002195 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2196 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2197 res = true;
2198 } else {
2199 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002201 if (mHaveConnection && mVisibleBound) {
2202 mContext.unbindService(mVisibleConnection);
2203 mVisibleBound = false;
2204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 mInputShown = false;
2206 mShowRequested = false;
2207 mShowExplicitlyRequested = false;
2208 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002209 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002211
satok42c5a162011-05-26 16:46:14 +09002212 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002213 public InputBindResult startInputOrWindowGainedFocus(
2214 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2215 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002216 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002217 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002218 if (windowToken != null) {
2219 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002220 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002221 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002222 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2223 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002224 }
2225 }
2226
2227 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002228 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2229 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002230 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2231 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002232 // Needs to check the validity before clearing calling identity
2233 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002234 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 long ident = Binder.clearCallingIdentity();
2236 try {
2237 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002238 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2239 + InputMethodClient.getStartInputReason(startInputReason)
2240 + " client=" + client.asBinder()
2241 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002242 + " missingMethods="
2243 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002244 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002245 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002247 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002248
Dianne Hackborn7663d802012-02-24 13:08:49 -08002249 ClientState cs = mClients.get(client.asBinder());
2250 if (cs == null) {
2251 throw new IllegalArgumentException("unknown client "
2252 + client.asBinder());
2253 }
2254
2255 try {
2256 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2257 // Check with the window manager to make sure this client actually
2258 // has a window with focus. If not, reject. This is thread safe
2259 // because if the focus changes some time before or after, the
2260 // next client receiving focus that has any interest in input will
2261 // be calling through here after that change happens.
2262 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2263 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2264 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002268
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002269 if (!calledFromValidUser) {
2270 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2271 Slog.w(TAG, "If you want to interect with IME, you need "
2272 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2273 hideCurrentInputLocked(0, null);
2274 return null;
2275 }
2276
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002277 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002278 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002279 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002280 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002281 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2282 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283 }
2284 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002285 }
2286 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002287 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002288
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002289 // Should we auto-show the IME even if the caller has not
2290 // specified what should be done with it?
2291 // We only do this automatically if the window can resize
2292 // to accommodate the IME (so what the user sees will give
2293 // them good context without input information being obscured
2294 // by the IME) or if running on a large screen where there
2295 // is more room for the target window + IME.
2296 final boolean doAutoShow =
2297 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2298 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2299 || mRes.getConfiguration().isLayoutSizeAtLeast(
2300 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002301 final boolean isTextEditor =
2302 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2303
2304 // We want to start input before showing the IME, but after closing
2305 // it. We want to do this after closing it to help the IME disappear
2306 // more quickly (not get stuck behind it initializing itself for the
2307 // new focused input, even if its window wants to hide the IME).
2308 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2311 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002312 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2314 // There is no focus view, and this window will
2315 // be behind any soft input window, so hide the
2316 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002317 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002318 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002320 } else if (isTextEditor && doAutoShow && (softInputMode &
2321 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 // There is a focus view, and we are navigating forward
2323 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002324 // We only do this automatically if the window can resize
2325 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002326 // them good context without input information being obscured
2327 // by the IME) or if running on a large screen where there
2328 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002329 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002330 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002331 res = startInputUncheckedLocked(cs, inputContext,
2332 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002333 didStart = true;
2334 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002335 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337 break;
2338 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2339 // Do nothing.
2340 break;
2341 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2342 if ((softInputMode &
2343 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002344 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002345 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
2347 break;
2348 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002349 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002350 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 break;
2352 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2353 if ((softInputMode &
2354 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002355 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002356 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002357 res = startInputUncheckedLocked(cs, inputContext,
2358 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002359 didStart = true;
2360 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002361 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 }
2363 break;
2364 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002366 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002367 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2368 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002369 didStart = true;
2370 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002371 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 break;
2373 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002374
2375 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002376 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002377 controlFlags);
2378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380 } finally {
2381 Binder.restoreCallingIdentity(ident);
2382 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002383
2384 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002386
satok42c5a162011-05-26 16:46:14 +09002387 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002388 public void showInputMethodPickerFromClient(
2389 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002390 if (!calledFromValidUser()) {
2391 return;
2392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 synchronized (mMethodMap) {
2394 if (mCurClient == null || client == null
2395 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002396 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002397 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 }
2399
satok440aab52010-11-25 09:43:11 +09002400 // Always call subtype picker, because subtype picker is a superset of input method
2401 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002402 mHandler.sendMessage(mCaller.obtainMessageI(
2403 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002404 }
2405 }
2406
satok42c5a162011-05-26 16:46:14 +09002407 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002409 if (!calledFromValidUser()) {
2410 return;
2411 }
satok28203512010-11-24 11:06:49 +09002412 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2413 }
2414
satok42c5a162011-05-26 16:46:14 +09002415 @Override
satok28203512010-11-24 11:06:49 +09002416 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002417 if (!calledFromValidUser()) {
2418 return;
2419 }
satok28203512010-11-24 11:06:49 +09002420 synchronized (mMethodMap) {
2421 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002422 setInputMethodWithSubtypeIdLocked(token, id,
2423 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2424 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002425 } else {
2426 setInputMethod(token, id);
2427 }
2428 }
satokab751aa2010-09-14 19:17:36 +09002429 }
2430
satok42c5a162011-05-26 16:46:14 +09002431 @Override
satokb416a712010-11-25 20:42:14 +09002432 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002433 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002434 if (!calledFromValidUser()) {
2435 return;
2436 }
satokb416a712010-11-25 20:42:14 +09002437 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002438 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2439 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002440 }
2441 }
2442
satok4fc87d62011-05-20 16:13:43 +09002443 @Override
satok735cf382010-11-11 20:40:09 +09002444 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002445 if (!calledFromValidUser()) {
2446 return false;
2447 }
satok735cf382010-11-11 20:40:09 +09002448 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002449 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002450 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002451 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002452 lastImi = mMethodMap.get(lastIme.first);
2453 } else {
2454 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002455 }
satok4fc87d62011-05-20 16:13:43 +09002456 String targetLastImiId = null;
2457 int subtypeId = NOT_A_SUBTYPE_ID;
2458 if (lastIme != null && lastImi != null) {
2459 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002460 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002461 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2462 : mCurrentSubtype.hashCode();
2463 // If the last IME is the same as the current IME and the last subtype is not
2464 // defined, there is no need to switch to the last IME.
2465 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2466 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002467 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002468 }
2469 }
2470
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002471 if (TextUtils.isEmpty(targetLastImiId)
2472 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002473 // This is a safety net. If the currentSubtype can't be added to the history
2474 // and the framework couldn't find the last ime, we will make the last ime be
2475 // the most applicable enabled keyboard subtype of the system imes.
2476 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2477 if (enabled != null) {
2478 final int N = enabled.size();
2479 final String locale = mCurrentSubtype == null
2480 ? mRes.getConfiguration().locale.toString()
2481 : mCurrentSubtype.getLocale();
2482 for (int i = 0; i < N; ++i) {
2483 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002484 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002485 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002486 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2487 InputMethodUtils.getSubtypes(imi),
2488 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002489 if (keyboardSubtype != null) {
2490 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002491 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002492 imi, keyboardSubtype.hashCode());
2493 if(keyboardSubtype.getLocale().equals(locale)) {
2494 break;
2495 }
2496 }
2497 }
2498 }
2499 }
2500 }
2501
2502 if (!TextUtils.isEmpty(targetLastImiId)) {
2503 if (DEBUG) {
2504 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2505 + ", from: " + mCurMethodId + ", " + subtypeId);
2506 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002507 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002508 return true;
2509 } else {
2510 return false;
2511 }
satok735cf382010-11-11 20:40:09 +09002512 }
2513 }
2514
satoke7c6998e2011-06-03 17:57:59 +09002515 @Override
satok688bd472012-02-09 20:09:17 +09002516 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002517 if (!calledFromValidUser()) {
2518 return false;
2519 }
satok688bd472012-02-09 20:09:17 +09002520 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002521 if (!calledWithValidToken(token)) {
2522 final int uid = Binder.getCallingUid();
2523 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2524 + " token:" + token);
2525 return false;
2526 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002527 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002528 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2529 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002530 if (nextSubtype == null) {
2531 return false;
2532 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002533 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2534 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002535 return true;
2536 }
2537 }
2538
2539 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002540 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2541 if (!calledFromValidUser()) {
2542 return false;
2543 }
2544 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002545 if (!calledWithValidToken(token)) {
2546 final int uid = Binder.getCallingUid();
2547 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2548 + "token. uid:" + uid + " token:" + token);
2549 return false;
2550 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002551 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002552 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2553 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002554 if (nextSubtype == null) {
2555 return false;
2556 }
2557 return true;
2558 }
2559 }
2560
2561 @Override
satok68f1b782011-04-11 14:26:04 +09002562 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002563 if (!calledFromValidUser()) {
2564 return null;
2565 }
satok68f1b782011-04-11 14:26:04 +09002566 synchronized (mMethodMap) {
2567 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2568 // TODO: Handle the case of the last IME with no subtypes
2569 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2570 || TextUtils.isEmpty(lastIme.second)) return null;
2571 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2572 if (lastImi == null) return null;
2573 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002574 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002575 final int lastSubtypeId =
2576 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002577 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2578 return null;
2579 }
2580 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002581 } catch (NumberFormatException e) {
2582 return null;
2583 }
2584 }
2585 }
2586
satoke7c6998e2011-06-03 17:57:59 +09002587 @Override
satokee5e77c2011-09-02 18:50:15 +09002588 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002589 if (!calledFromValidUser()) {
2590 return;
2591 }
satok91e88122011-07-18 11:11:42 +09002592 // By this IPC call, only a process which shares the same uid with the IME can add
2593 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002594 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002595 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002596 if (!mSystemReady) {
2597 return;
2598 }
satok91e88122011-07-18 11:11:42 +09002599 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002600 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002601 final String[] packageInfos;
2602 try {
2603 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2604 } catch (RemoteException e) {
2605 Slog.e(TAG, "Failed to get package infos");
2606 return;
2607 }
satok91e88122011-07-18 11:11:42 +09002608 if (packageInfos != null) {
2609 final int packageNum = packageInfos.length;
2610 for (int i = 0; i < packageNum; ++i) {
2611 if (packageInfos[i].equals(imi.getPackageName())) {
2612 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002613 final long ident = Binder.clearCallingIdentity();
2614 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002615 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002616 } finally {
2617 Binder.restoreCallingIdentity(ident);
2618 }
satokee5e77c2011-09-02 18:50:15 +09002619 return;
satok91e88122011-07-18 11:11:42 +09002620 }
2621 }
2622 }
satoke7c6998e2011-06-03 17:57:59 +09002623 }
satokee5e77c2011-09-02 18:50:15 +09002624 return;
satoke7c6998e2011-06-03 17:57:59 +09002625 }
2626
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002627 @Override
2628 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002629 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002630 }
2631
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002632 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002633 public void clearLastInputMethodWindowForTransition(IBinder token) {
2634 if (!calledFromValidUser()) {
2635 return;
2636 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002637 synchronized (mMethodMap) {
2638 if (!calledWithValidToken(token)) {
2639 final int uid = Binder.getCallingUid();
2640 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2641 + "invalid token. uid:" + uid + " token:" + token);
2642 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002643 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002644 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002645 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002646 }
2647
2648 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002649 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002650 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002651 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002652 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002653 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002654 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2655 if (DEBUG) {
2656 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2657 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2658 + " actual: " + sequenceNumber);
2659 }
2660 return;
2661 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002662 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2663 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002664 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002665 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002666 }
2667 }
2668
satok28203512010-11-24 11:06:49 +09002669 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002671 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2672 }
2673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002675 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2676 if (token == null) {
2677 if (mContext.checkCallingOrSelfPermission(
2678 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2679 != PackageManager.PERMISSION_GRANTED) {
2680 throw new SecurityException(
2681 "Using null token requires permission "
2682 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002684 } else if (mCurToken != token) {
2685 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2686 + " token: " + token);
2687 return;
2688 }
2689
2690 final long ident = Binder.clearCallingIdentity();
2691 try {
2692 setInputMethodLocked(id, subtypeId);
2693 } finally {
2694 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 }
2696 }
2697
satok42c5a162011-05-26 16:46:14 +09002698 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002700 if (!calledFromValidUser()) {
2701 return;
2702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002704 if (!calledWithValidToken(token)) {
2705 final int uid = Binder.getCallingUid();
2706 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2707 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 return;
2709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 long ident = Binder.clearCallingIdentity();
2711 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002712 hideCurrentInputLocked(flags, null);
2713 } finally {
2714 Binder.restoreCallingIdentity(ident);
2715 }
2716 }
2717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002718
satok42c5a162011-05-26 16:46:14 +09002719 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002720 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002721 if (!calledFromValidUser()) {
2722 return;
2723 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002724 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002725 if (!calledWithValidToken(token)) {
2726 final int uid = Binder.getCallingUid();
2727 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2728 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002729 return;
2730 }
2731 long ident = Binder.clearCallingIdentity();
2732 try {
2733 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 } finally {
2735 Binder.restoreCallingIdentity(ident);
2736 }
2737 }
2738 }
2739
2740 void setEnabledSessionInMainThread(SessionState session) {
2741 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002742 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002744 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002745 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 } catch (RemoteException e) {
2747 }
2748 }
2749 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002750 if (mEnabledSession != null && mEnabledSession.session != null) {
2751 try {
2752 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2753 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2754 } catch (RemoteException e) {
2755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
2757 }
2758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002759
satok42c5a162011-05-26 16:46:14 +09002760 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002762 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002764 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002765 final boolean showAuxSubtypes;
2766 switch (msg.arg1) {
2767 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2768 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2769 // implemented so that auxiliary subtypes will be excluded when the soft
2770 // keyboard is invisible.
2771 showAuxSubtypes = mInputShown;
2772 break;
2773 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2774 showAuxSubtypes = true;
2775 break;
2776 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2777 showAuxSubtypes = false;
2778 break;
2779 default:
2780 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2781 return false;
2782 }
2783 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002784 return true;
2785
satok47a44912010-10-06 16:03:58 +09002786 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002787 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002788 return true;
2789
2790 case MSG_SHOW_IM_CONFIG:
2791 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002792 return true;
2793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 case MSG_UNBIND_INPUT:
2797 try {
2798 ((IInputMethod)msg.obj).unbindInput();
2799 } catch (RemoteException e) {
2800 // There is nothing interesting about the method dying.
2801 }
2802 return true;
2803 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002804 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 try {
2806 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2807 } catch (RemoteException e) {
2808 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002809 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 return true;
2811 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002812 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002814 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002815 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002816 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 } catch (RemoteException e) {
2818 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002819 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 return true;
2821 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002822 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002824 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002825 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002826 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 } catch (RemoteException e) {
2828 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002829 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002831 case MSG_HIDE_CURRENT_INPUT_METHOD:
2832 synchronized (mMethodMap) {
2833 hideCurrentInputLocked(0, null);
2834 }
2835 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002837 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002839 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2841 } catch (RemoteException e) {
2842 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002843 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002845 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002846 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002847 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002848 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002850 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002852 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002853 // Dispose the channel if the input method is not local to this process
2854 // because the remote proxy will get its own copy when unparceled.
2855 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002856 channel.dispose();
2857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002859 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002863
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002864 case MSG_START_INPUT: {
2865 int missingMethods = msg.arg1;
2866 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002868 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002870 session.method.startInput((IInputContext) args.arg2, missingMethods,
2871 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 } catch (RemoteException e) {
2873 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002874 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002876 }
2877 case MSG_RESTART_INPUT: {
2878 int missingMethods = msg.arg1;
2879 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002881 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002883 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2884 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 } catch (RemoteException e) {
2886 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002887 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002889 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002892
Yohei Yukawa33e81792015-11-17 21:14:42 -08002893 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002895 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } catch (RemoteException e) {
2897 // There is nothing interesting about the last client dying.
2898 }
2899 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002900 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002901 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002902 IInputMethodClient client = (IInputMethodClient)args.arg1;
2903 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002905 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002907 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002908 } finally {
2909 // Dispose the channel if the input method is not local to this process
2910 // because the remote proxy will get its own copy when unparceled.
2911 if (res.channel != null && Binder.isProxy(client)) {
2912 res.channel.dispose();
2913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002915 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002917 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002918 case MSG_SET_ACTIVE:
2919 try {
2920 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2921 } catch (RemoteException e) {
2922 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2923 + ((ClientState)msg.obj).pid + " uid "
2924 + ((ClientState)msg.obj).uid);
2925 }
2926 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002927 case MSG_SET_INTERACTIVE:
2928 handleSetInteractive(msg.arg1 != 0);
2929 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002930 case MSG_SWITCH_IME:
2931 handleSwitchInputMethod(msg.arg1 != 0);
2932 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002933 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2934 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002935 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002936 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002937 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002938 } catch (RemoteException e) {
2939 Slog.w(TAG, "Got RemoteException sending "
2940 + "setUserActionNotificationSequenceNumber("
2941 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002942 + clientState.pid + " uid "
2943 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002944 }
2945 return true;
2946 }
satok01038492012-04-09 21:08:27 +09002947
2948 // --------------------------------------------------------------
2949 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002950 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002951 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002952 case MSG_SYSTEM_UNLOCK_USER:
2953 final int userId = msg.arg1;
2954 onUnlockUser(userId);
2955 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 }
2957 return false;
2958 }
2959
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002960 private void handleSetInteractive(final boolean interactive) {
2961 synchronized (mMethodMap) {
2962 mIsInteractive = interactive;
2963 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2964
2965 // Inform the current client of the change in active status
2966 if (mCurClient != null && mCurClient.client != null) {
2967 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2968 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2969 }
2970 }
2971 }
2972
Yohei Yukawaae61f712015-12-09 13:00:10 -08002973 private void handleSwitchInputMethod(final boolean forwardDirection) {
2974 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002975 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002976 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002977 if (nextSubtype == null) {
2978 return;
2979 }
2980 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002981 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2982 if (newInputMethodInfo == null) {
2983 return;
2984 }
2985 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
2986 newInputMethodInfo, mCurrentSubtype);
2987 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07002988 if (mSubtypeSwitchedByShortCutToast == null) {
2989 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
2990 Toast.LENGTH_SHORT);
2991 } else {
2992 mSubtypeSwitchedByShortCutToast.setText(toastText);
2993 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002994 mSubtypeSwitchedByShortCutToast.show();
2995 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08002996 }
2997 }
2998
satokdc9ddae2011-10-06 12:22:36 +09002999 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003000 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3001 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003002 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003003 if (DEBUG) {
3004 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3005 }
satok723a27e2010-11-11 14:58:11 +09003006 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003007 return true;
3008 }
3009
3010 return false;
3011 }
3012
Yohei Yukawa94e33302016-02-12 19:37:03 -08003013 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003014 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003015 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003016 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003017 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003018 if (!mSystemReady) {
3019 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3020 return;
3021 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003022 mMethodList.clear();
3023 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003024
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003025 // Use for queryIntentServicesAsUser
3026 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003028 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3029 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3030 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003031 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003033 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3034 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003035
satoke7c6998e2011-06-03 17:57:59 +09003036 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3037 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 for (int i = 0; i < services.size(); ++i) {
3039 ResolveInfo ri = services.get(i);
3040 ServiceInfo si = ri.serviceInfo;
3041 ComponentName compName = new ComponentName(si.packageName, si.name);
3042 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3043 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003044 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 + ": it does not require the permission "
3046 + android.Manifest.permission.BIND_INPUT_METHOD);
3047 continue;
3048 }
3049
Joe Onorato8a9b2202010-02-26 18:56:32 -08003050 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051
3052 try {
satoke7c6998e2011-06-03 17:57:59 +09003053 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003054 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003055 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003056 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057
3058 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003059 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003061 } catch (Exception e) {
3062 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 }
3064 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003065
Yohei Yukawa859df052016-02-17 07:56:46 -08003066 // TODO: The following code should find better place to live.
3067 if (!resetDefaultEnabledIme) {
3068 boolean enabledImeFound = false;
3069 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3070 final int N = enabledImes.size();
3071 for (int i = 0; i < N; ++i) {
3072 final InputMethodInfo imi = enabledImes.get(i);
3073 if (mMethodList.contains(imi)) {
3074 enabledImeFound = true;
3075 break;
3076 }
3077 }
3078 if (!enabledImeFound) {
3079 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3080 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}