blob: 2bf5866e22f5415539fbf17f0193294df2f8ee88 [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 Shankafc46e9b2016-10-21 17:55:27 -070052import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.app.ActivityManagerNative;
54import 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;
63import android.content.ContentResolver;
64import android.content.Context;
65import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090067import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090069import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070071import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090072import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.pm.PackageManager;
74import android.content.pm.ResolveInfo;
75import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070076import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.content.res.Resources;
78import android.content.res.TypedArray;
79import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080080import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080081import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080082import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070083import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040085import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070086import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090087import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.Handler;
89import android.os.IBinder;
90import android.os.IInterface;
91import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070092import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070094import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080096import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.ServiceManager;
98import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090099import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800100import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.provider.Settings;
102import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900103import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700104import android.util.ArrayMap;
105import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700106import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900108import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900109import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.util.PrintWriterPrinter;
111import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900112import android.util.Slog;
113import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900114import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700116import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900117import android.view.LayoutInflater;
118import android.view.View;
119import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700120import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700122import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900123import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700125import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.view.inputmethod.InputMethod;
127import android.view.inputmethod.InputMethodInfo;
128import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700129import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900130import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900131import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900132import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900133import android.widget.CompoundButton;
134import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900135import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900136import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900137import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700138import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
satoke7c6998e2011-06-03 17:57:59 +0900140import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900142import java.io.FileInputStream;
143import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144import java.io.IOException;
145import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700146import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100147import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700148import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900150import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151import java.util.HashMap;
152import java.util.List;
153
154/**
155 * This class provides a system service that manages input methods.
156 */
157public class InputMethodManagerService extends IInputMethodManager.Stub
158 implements ServiceConnection, Handler.Callback {
159 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700160 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700161 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700163 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
164 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
165 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 static final int MSG_UNBIND_INPUT = 1000;
168 static final int MSG_BIND_INPUT = 1010;
169 static final int MSG_SHOW_SOFT_INPUT = 1020;
170 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700171 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 static final int MSG_ATTACH_TOKEN = 1040;
173 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 static final int MSG_START_INPUT = 2000;
176 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
Yohei Yukawa33e81792015-11-17 21:14:42 -0800178 static final int MSG_UNBIND_CLIENT = 3000;
179 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700180 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700181 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900182 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800183 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
satok01038492012-04-09 21:08:27 +0900185 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
186
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700187 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
188
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700189 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
satokf9f01002011-05-19 21:31:50 +0900191 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
192
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900193 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900194 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900195
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700196 @Retention(SOURCE)
197 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
198 private @interface HardKeyboardBehavior {
199 int WIRELESS_AFFORDANCE = 0;
200 int WIRED_AFFORDANCE = 1;
201 }
satok4e4569d2010-11-19 18:45:53 +0900202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800204 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900206 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700209 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700211 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900212 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900213 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900214 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800215 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900217 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 // All known input methods. mMethodMap also serves as the global
220 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700221 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
222 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900223 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700224 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900225 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700227 // Used to bring IME service up to visible adjustment while it is being shown.
228 final ServiceConnection mVisibleConnection = new ServiceConnection() {
229 @Override public void onServiceConnected(ComponentName name, IBinder service) {
230 }
231
232 @Override public void onServiceDisconnected(ComponentName name) {
233 }
234 };
235 boolean mVisibleBound = false;
236
satok7cfc0ed2011-06-20 21:29:36 +0900237 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700238 private NotificationManager mNotificationManager;
239 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700240 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400241 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700242 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900243 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900244 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900245 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900246
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
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900568 class ImmsBroadcastReceiver extends android.content.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 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900589 } else {
590 Slog.w(TAG, "Unexpected intent " + intent);
591 }
592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800594
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700595 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
596 // does not attempt to validate on the fly with any installed device policy, so must only
597 // be run in the context of initial device setup.
598 //
599 // TODO: Move this method to InputMethodUtils with adding unit tests.
600 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
601 if (DEBUG_RESTORE) {
602 Slog.i(TAG, "Restoring enabled input methods:");
603 Slog.i(TAG, "prev=" + prevValue);
604 Slog.i(TAG, " new=" + newValue);
605 }
606 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900607 ArrayMap<String, ArraySet<String>> prevMap =
608 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
609 ArrayMap<String, ArraySet<String>> newMap =
610 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700611
612 // Merge the restored ime+subtype enabled states into the live state
613 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
614 final String imeId = entry.getKey();
615 ArraySet<String> prevSubtypes = prevMap.get(imeId);
616 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700617 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700618 prevMap.put(imeId, prevSubtypes);
619 }
620 prevSubtypes.addAll(entry.getValue());
621 }
622
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700623 final String mergedImesAndSubtypesString =
624 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700625 if (DEBUG_RESTORE) {
626 Slog.i(TAG, "Merged IME string:");
627 Slog.i(TAG, " " + mergedImesAndSubtypesString);
628 }
629 Settings.Secure.putString(context.getContentResolver(),
630 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
631 }
632
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800633 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900634 private boolean isChangingPackagesOfCurrentUser() {
635 final int userId = getChangingUserId();
636 final boolean retval = userId == mSettings.getCurrentUserId();
637 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900638 if (!retval) {
639 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
640 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900641 }
642 return retval;
643 }
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800646 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900647 if (!isChangingPackagesOfCurrentUser()) {
648 return false;
649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900651 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 final int N = mMethodList.size();
653 if (curInputMethodId != null) {
654 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800655 InputMethodInfo imi = mMethodList.get(i);
656 if (imi.getId().equals(curInputMethodId)) {
657 for (String pkg : packages) {
658 if (imi.getPackageName().equals(pkg)) {
659 if (!doit) {
660 return true;
661 }
satok723a27e2010-11-11 14:58:11 +0900662 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800663 chooseNewDefaultIMELocked();
664 return true;
665 }
666 }
667 }
668 }
669 }
670 }
671 return false;
672 }
673
674 @Override
675 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900676 if (!isChangingPackagesOfCurrentUser()) {
677 return;
678 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679 synchronized (mMethodMap) {
680 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900681 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800682 final int N = mMethodList.size();
683 if (curInputMethodId != null) {
684 for (int i=0; i<N; i++) {
685 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900686 final String imiId = imi.getId();
687 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800688 curIm = imi;
689 }
satoke7c6998e2011-06-03 17:57:59 +0900690
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800691 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900692 if (isPackageModified(imi.getPackageName())) {
693 mFileManager.deleteAllInputMethodSubtypes(imiId);
694 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800695 if (change == PACKAGE_TEMPORARY_CHANGE
696 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800697 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698 + imi.getComponent());
699 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701 }
702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800703
Yohei Yukawa94e33302016-02-12 19:37:03 -0800704 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800708 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800709 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800710 if (change == PACKAGE_TEMPORARY_CHANGE
711 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800712 ServiceInfo si = null;
713 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900714 si = mIPackageManager.getServiceInfo(
715 curIm.getComponent(), 0, mSettings.getCurrentUserId());
716 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800717 }
718 if (si == null) {
719 // Uh oh, current input method is no longer around!
720 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800721 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900722 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800723 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800724 changed = true;
725 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900727 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800728 }
729 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800730 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800731 }
satokab751aa2010-09-14 19:17:36 +0900732
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800733 if (curIm == null) {
734 // We currently don't have a default input method... is
735 // one now available?
736 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700737 } else if (!changed && isPackageModified(curIm.getPackageName())) {
738 // Even if the current input method is still available, mCurrentSubtype could
739 // be obsolete when the package is modified in practice.
740 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800742
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800743 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800744 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746 }
747 }
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
Jeff Brownc28867a2013-03-26 15:42:39 -0700750 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900751 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700752 private final IInputMethod mMethod;
753 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754
Jeff Brownc28867a2013-03-26 15:42:39 -0700755 MethodCallback(InputMethodManagerService imms, IInputMethod method,
756 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900757 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700758 mMethod = method;
759 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800761
satoke7c6998e2011-06-03 17:57:59 +0900762 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700763 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700764 long ident = Binder.clearCallingIdentity();
765 try {
766 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
767 } finally {
768 Binder.restoreCallingIdentity(ident);
769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
satok01038492012-04-09 21:08:27 +0900773 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700774 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900775 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700776 public void onHardKeyboardStatusChange(boolean available) {
777 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
778 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900779 }
780
Michael Wright7b5a96b2014-08-09 19:28:42 -0700781 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900782 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700783 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900784 }
785 synchronized(mMethodMap) {
786 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
787 && mSwitchingDialog.isShowing()) {
788 mSwitchingDialogTitleView.findViewById(
789 com.android.internal.R.id.hard_keyboard_section).setVisibility(
790 available ? View.VISIBLE : View.GONE);
791 }
792 }
793 }
794 }
795
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800796 public static final class Lifecycle extends SystemService {
797 private InputMethodManagerService mService;
798
799 public Lifecycle(Context context) {
800 super(context);
801 mService = new InputMethodManagerService(context);
802 }
803
804 @Override
805 public void onStart() {
806 LocalServices.addService(InputMethodManagerInternal.class,
807 new LocalServiceImpl(mService.mHandler));
808 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
809 }
810
811 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800812 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700813 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800814 // TODO: Dispatch this to a worker thread as needed.
815 mService.onSwitchUser(userHandle);
816 }
817
818 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800819 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700820 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800821 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800822 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
823 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
824 .getService(Context.STATUS_BAR_SERVICE);
825 mService.systemRunning(statusBarService);
826 }
827 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800828
829 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700830 public void onUnlockUser(final @UserIdInt int userHandle) {
831 // Called on ActivityManager thread.
832 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700833 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800834 }
835 }
836
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800837 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800838 synchronized(mMethodMap) {
839 final int currentUserId = mSettings.getCurrentUserId();
840 if (DEBUG) {
841 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
842 }
843 if (userId != currentUserId) {
844 return;
845 }
846 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
847 // We need to rebuild IMEs.
848 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
849 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
850 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800851 }
852
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800853 void onSwitchUser(@UserIdInt int userId) {
854 synchronized (mMethodMap) {
855 switchUserLocked(userId);
856 }
857 }
858
Seigo Nonaka7309b122015-08-17 18:34:13 -0700859 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900860 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800862 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700864 // Note: SettingsObserver doesn't register observers in its constructor.
865 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 mIWindowManager = IWindowManager.Stub.asInterface(
867 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700868 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800869 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900870 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 public void executeMessage(Message msg) {
872 handleMessage(msg);
873 }
Mita Yuned218c72012-12-06 17:18:25 -0800874 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800875 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800876 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900877 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700878 mHasFeature = context.getPackageManager().hasSystemFeature(
879 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500880 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700881 mHardKeyboardBehavior = mContext.getResources().getInteger(
882 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900883
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400884 Bundle extras = new Bundle();
885 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
886 mImeSwitcherNotification = new Notification.Builder(mContext)
887 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
888 .setWhen(0)
889 .setOngoing(true)
890 .addExtras(extras)
891 .setCategory(Notification.CATEGORY_SYSTEM)
892 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400893
satok7cfc0ed2011-06-20 21:29:36 +0900894 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900895 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900896
897 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900898
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900899 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900900 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800901 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
902 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700903 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900904 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800905
satok7cfc0ed2011-06-20 21:29:36 +0900906 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 int userId = 0;
908 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900909 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
910 } catch (RemoteException e) {
911 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
912 }
satok81f8b7c2012-12-04 20:42:56 +0900913 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900914
satokd87c2592010-09-29 11:52:06 +0900915 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900916 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800917 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700918
Kenny Guy2a764942014-04-02 13:29:20 +0100919 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900920 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900921 synchronized (mMethodMap) {
922 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
923 mSettings, context);
924 }
satok0a1bcf42012-05-16 19:26:31 +0900925
926 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900927 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900928 if (DEBUG) {
929 Slog.d(TAG, "Initial default ime = " + defaultImiId);
930 }
satok0a1bcf42012-05-16 19:26:31 +0900931 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
932
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900933 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800934 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900935 }
satokd87c2592010-09-29 11:52:06 +0900936 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937
satok0a1bcf42012-05-16 19:26:31 +0900938 if (!mImeSelectedOnBoot) {
939 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900940 synchronized (mMethodMap) {
941 resetDefaultImeLocked(context);
942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800944
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900945 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700946 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900947 updateFromSettingsLocked(true);
948 }
satok5b927c432012-05-01 20:09:34 +0900949
950 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
951 // according to the new system locale.
952 final IntentFilter filter = new IntentFilter();
953 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
954 mContext.registerReceiver(
955 new BroadcastReceiver() {
956 @Override
957 public void onReceive(Context context, Intent intent) {
958 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900959 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900960 }
961 }
962 }, filter);
963 }
964
satok5b927c432012-05-01 20:09:34 +0900965 private void resetDefaultImeLocked(Context context) {
966 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800967 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900968 return;
969 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800970 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
971 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
972 if (suitableImes.isEmpty()) {
973 Slog.i(TAG, "No default found");
974 return;
satok5b927c432012-05-01 20:09:34 +0900975 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800976 final InputMethodInfo defIm = suitableImes.get(0);
977 Slog.i(TAG, "Default found, using " + defIm.getId());
978 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900979 }
980
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900981 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
982 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 if (!mSystemReady) {
984 // not system ready
985 return;
986 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800987 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800989 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 if (!updateOnlyWhenLocaleChanged) {
991 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800992 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900993 }
994 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800995 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800997 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 if (!updateOnlyWhenLocaleChanged) {
999 final String selectedImiId = mSettings.getSelectedInputMethod();
1000 if (TextUtils.isEmpty(selectedImiId)) {
1001 // This is the first time of the user switch and
1002 // set the current ime to the proper one.
1003 resetDefaultImeLocked(mContext);
1004 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001005 } else {
1006 // If the locale is changed, needs to reset the default ime
1007 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001008 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001009 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001010 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001011 if (!updateOnlyWhenLocaleChanged) {
1012 try {
1013 startInputInnerLocked();
1014 } catch (RuntimeException e) {
1015 Slog.w(TAG, "Unexpected exception", e);
1016 }
1017 }
1018 }
1019 }
1020
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001021 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001022 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1023 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001024 }
1025
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001026 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001027 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1028 + " currentUserId=" + mSettings.getCurrentUserId());
1029
Yohei Yukawa81482972015-06-04 00:58:59 -07001030 // ContentObserver should be registered again when the user is changed
1031 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001032
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001033 // If the system is not ready or the device is not yed unlocked by the user, then we use
1034 // copy-on-write settings.
1035 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001036 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001037 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001038 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001039 // InputMethodFileManager should be reset when the user is changed
1040 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001041 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001042
1043 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1044 + " defaultImiId=" + defaultImiId);
1045
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001046 // For secondary users, the list of enabled IMEs may not have been updated since the
1047 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1048 // not be empty even if the IME has been uninstalled by the primary user.
1049 // Even in such cases, IMMS works fine because it will find the most applicable
1050 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001051 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001052 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001053 initialUserSwitch /* needsToResetDefaultIme */);
1054 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001055 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1056 mSettings.getEnabledInputMethodListLocked(), newUserId,
1057 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001058 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001059
1060 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1061 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001062 }
1063
Kenny Guy2a764942014-04-02 13:29:20 +01001064 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001065 mSettings.setCurrentProfileIds(
1066 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001067 }
1068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 @Override
1070 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1071 throws RemoteException {
1072 try {
1073 return super.onTransact(code, data, reply, flags);
1074 } catch (RuntimeException e) {
1075 // The input method manager only throws security exceptions, so let's
1076 // log all others.
1077 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001078 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 throw e;
1081 }
1082 }
1083
Svetoslav Ganova0027152013-06-25 14:59:53 -07001084 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001085 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001086 if (DEBUG) {
1087 Slog.d(TAG, "--- systemReady");
1088 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001089 if (!mSystemReady) {
1090 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001091 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001092 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001093 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001094 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1095 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001096 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001097 if (mStatusBar != null) {
1098 mStatusBar.setIconVisibility(mSlotIme, false);
1099 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001100 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001101 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1102 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001103 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001104 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001105 mHardKeyboardListener);
1106 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001107 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001108 if (!mImeSelectedOnBoot) {
1109 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001110 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001111 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1112 mSettings.getEnabledInputMethodListLocked(),
1113 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001114 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001115 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001116 try {
1117 startInputInnerLocked();
1118 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001119 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001120 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001121 }
1122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001124
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001125 // ---------------------------------------------------------------------------------------
1126 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1127 // 1) it comes from the system process
1128 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1129 private boolean calledFromValidUser() {
1130 final int uid = Binder.getCallingUid();
1131 final int userId = UserHandle.getUserId(uid);
1132 if (DEBUG) {
1133 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1134 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1135 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001136 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1137 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001138 }
Kenny Guy2a764942014-04-02 13:29:20 +01001139 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001140 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001141 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001142
1143 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1144 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1145 // must not manage background users' states in any functions.
1146 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1147 // by a token.
1148 if (mContext.checkCallingOrSelfPermission(
1149 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1150 == PackageManager.PERMISSION_GRANTED) {
1151 if (DEBUG) {
1152 Slog.d(TAG, "--- Access granted because the calling process has "
1153 + "the INTERACT_ACROSS_USERS_FULL permission");
1154 }
1155 return true;
1156 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001157 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1158 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001159 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001160 }
1161
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001162
1163 /**
1164 * Returns true iff the caller is identified to be the current input method with the token.
1165 * @param token The window token given to the input method when it was started.
1166 * @return true if and only if non-null valid token is specified.
1167 */
1168 private boolean calledWithValidToken(IBinder token) {
1169 if (token == null || mCurToken != token) {
1170 return false;
1171 }
1172 return true;
1173 }
1174
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 private boolean bindCurrentInputMethodService(
1176 Intent service, ServiceConnection conn, int flags) {
1177 if (service == null || conn == null) {
1178 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1179 return false;
1180 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001181 return mContext.bindServiceAsUser(service, conn, flags,
1182 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001183 }
1184
satoke7c6998e2011-06-03 17:57:59 +09001185 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001187 // TODO: Make this work even for non-current users?
1188 if (!calledFromValidUser()) {
1189 return Collections.emptyList();
1190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001192 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194 }
1195
satoke7c6998e2011-06-03 17:57:59 +09001196 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001198 // TODO: Make this work even for non-current users?
1199 if (!calledFromValidUser()) {
1200 return Collections.emptyList();
1201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001203 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205 }
1206
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001207 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001208 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001209 * @return enabled subtypes of the specified imi
1210 */
satoke7c6998e2011-06-03 17:57:59 +09001211 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001212 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001213 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001214 // TODO: Make this work even for non-current users?
1215 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001216 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001217 }
satok67ddf9c2010-11-17 09:45:54 +09001218 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001219 final InputMethodInfo imi;
1220 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001221 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001222 } else {
1223 imi = mMethodMap.get(imiId);
1224 }
1225 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001226 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001227 }
1228 return mSettings.getEnabledInputMethodSubtypeListLocked(
1229 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001230 }
1231 }
1232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public void addClient(IInputMethodClient client,
1235 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001236 if (!calledFromValidUser()) {
1237 return;
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 synchronized (mMethodMap) {
1240 mClients.put(client.asBinder(), new ClientState(client,
1241 inputContext, uid, pid));
1242 }
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
satoke7c6998e2011-06-03 17:57:59 +09001245 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001247 if (!calledFromValidUser()) {
1248 return;
1249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001251 ClientState cs = mClients.remove(client.asBinder());
1252 if (cs != null) {
1253 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001254 if (mCurClient == cs) {
1255 mCurClient = null;
1256 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001257 if (mCurFocusedWindowClient == cs) {
1258 mCurFocusedWindowClient = null;
1259 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
1262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 void executeOrSendMessage(IInterface target, Message msg) {
1265 if (target.asBinder() instanceof Binder) {
1266 mCaller.sendMessage(msg);
1267 } else {
1268 handleMessage(msg);
1269 msg.recycle();
1270 }
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
Yohei Yukawa33e81792015-11-17 21:14:42 -08001273 void unbindCurrentClientLocked(
1274 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001276 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 + mCurClient.client.asBinder());
1278 if (mBoundToMethod) {
1279 mBoundToMethod = false;
1280 if (mCurMethod != null) {
1281 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1282 MSG_UNBIND_INPUT, mCurMethod));
1283 }
1284 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001285
1286 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1287 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001288 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1289 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001292
The Android Open Source Project10592532009-03-18 17:39:46 -07001293 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
1295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 private int getImeShowFlags() {
1298 int flags = 0;
1299 if (mShowForced) {
1300 flags |= InputMethod.SHOW_FORCED
1301 | InputMethod.SHOW_EXPLICIT;
1302 } else if (mShowExplicitlyRequested) {
1303 flags |= InputMethod.SHOW_EXPLICIT;
1304 }
1305 return flags;
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 private int getAppShowFlags() {
1309 int flags = 0;
1310 if (mShowForced) {
1311 flags |= InputMethodManager.SHOW_FORCED;
1312 } else if (!mShowExplicitlyRequested) {
1313 flags |= InputMethodManager.SHOW_IMPLICIT;
1314 }
1315 return flags;
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
Dianne Hackborn7663d802012-02-24 13:08:49 -08001318 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 if (!mBoundToMethod) {
1320 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1321 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1322 mBoundToMethod = true;
1323 }
1324 final SessionState session = mCurClient.curSession;
1325 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001326 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1327 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1328 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001330 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1331 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1332 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
1334 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001335 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001338 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001339 (session.channel != null ? session.channel.dup() : null),
1340 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001343 InputBindResult startInputLocked(
1344 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001345 IInputMethodClient client, IInputContext inputContext,
1346 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001347 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 // If no method is currently selected, do nothing.
1349 if (mCurMethodId == null) {
1350 return mNoBinding;
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 ClientState cs = mClients.get(client.asBinder());
1354 if (cs == null) {
1355 throw new IllegalArgumentException("unknown client "
1356 + client.asBinder());
1357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358
Yohei Yukawa74750f22016-03-22 12:54:22 -07001359 if (attribute == null) {
1360 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1361 + " uid=" + cs.uid + " pid=" + cs.pid);
1362 return null;
1363 }
1364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 try {
1366 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1367 // Check with the window manager to make sure this client actually
1368 // has a window with focus. If not, reject. This is thread safe
1369 // because if the focus changes some time before or after, the
1370 // next client receiving focus that has any interest in input will
1371 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001372 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1374 return null;
1375 }
1376 } catch (RemoteException e) {
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001379 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1380 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001381 }
1382
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001383 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001384 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001385 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001386 // If no method is currently selected, do nothing.
1387 if (mCurMethodId == null) {
1388 return mNoBinding;
1389 }
1390
Yohei Yukawad57ba672015-06-08 16:39:46 -07001391 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1392 attribute.packageName)) {
1393 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1394 + " uid=" + cs.uid + " package=" + attribute.packageName);
1395 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001396 }
1397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001399 // Was the keyguard locked when switching over to the new client?
1400 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 // If the client is changing, we need to switch over to the new
1402 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001403 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001404 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001405 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406
1407 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001408 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001409 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001410 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
1412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 // Bump up the sequence for this client and attach it.
1415 mCurSeq++;
1416 if (mCurSeq <= 0) mCurSeq = 1;
1417 mCurClient = cs;
1418 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001419 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 // Check if the input method is changing.
1423 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1424 if (cs.curSession != null) {
1425 // Fast case: if we are already connected to the input method,
1426 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001427 return attachNewInputLocked(
1428 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 if (mHaveConnection) {
1431 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 // Return to client, and we will get back with it when
1433 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001434 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001435 return new InputBindResult(null, null, mCurId, mCurSeq,
1436 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 } else if (SystemClock.uptimeMillis()
1438 < (mLastBindTime+TIME_TO_RECONNECT)) {
1439 // In this case we have connected to the service, but
1440 // don't yet have its interface. If it hasn't been too
1441 // long since we did the connection, we'll return to
1442 // the client and wait to get the service interface so
1443 // we can report back. If it has been too long, we want
1444 // to fall through so we can try a disconnect/reconnect
1445 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001446 return new InputBindResult(null, null, mCurId, mCurSeq,
1447 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1450 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
1452 }
1453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001455 return startInputInnerLocked();
1456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001458 InputBindResult startInputInnerLocked() {
1459 if (mCurMethodId == null) {
1460 return mNoBinding;
1461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001463 if (!mSystemReady) {
1464 // If the system is not yet ready, we shouldn't be running third
1465 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001466 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1467 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1471 if (info == null) {
1472 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1473 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001474
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001475 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1478 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001479 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1480 com.android.internal.R.string.input_method_binding_label);
1481 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1482 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001483 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001484 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1485 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 mLastBindTime = SystemClock.uptimeMillis();
1487 mHaveConnection = true;
1488 mCurId = info.getId();
1489 mCurToken = new Binder();
1490 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001491 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001492 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 } catch (RemoteException e) {
1494 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001495 return new InputBindResult(null, null, mCurId, mCurSeq,
1496 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 } else {
1498 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 + mCurIntent);
1501 }
1502 return null;
1503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001505 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001506 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001507 IInputMethodClient client, IInputContext inputContext,
1508 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001509 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001510 if (!calledFromValidUser()) {
1511 return null;
1512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001514 if (DEBUG) {
1515 Slog.v(TAG, "startInput: reason="
1516 + InputMethodClient.getStartInputReason(startInputReason)
1517 + " client = " + client.asBinder()
1518 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001519 + " missingMethods="
1520 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001521 + " attribute=" + attribute
1522 + " controlFlags=#" + Integer.toHexString(controlFlags));
1523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 final long ident = Binder.clearCallingIdentity();
1525 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001526 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1527 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 } finally {
1529 Binder.restoreCallingIdentity(ident);
1530 }
1531 }
1532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001533
satoke7c6998e2011-06-03 17:57:59 +09001534 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 public void finishInput(IInputMethodClient client) {
1536 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001537
satoke7c6998e2011-06-03 17:57:59 +09001538 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 public void onServiceConnected(ComponentName name, IBinder service) {
1540 synchronized (mMethodMap) {
1541 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1542 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001543 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001544 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001545 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001546 return;
1547 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001548 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001549 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1550 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 clearClientSessionLocked(mCurClient);
1553 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
1555 }
1556 }
1557 }
1558
Jeff Brownc28867a2013-03-26 15:42:39 -07001559 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1560 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 synchronized (mMethodMap) {
1562 if (mCurMethod != null && method != null
1563 && mCurMethod.asBinder() == method.asBinder()) {
1564 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001565 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001567 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001568 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 if (res.method != null) {
1570 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001571 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001573 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
1575 }
1576 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001577
1578 // Session abandoned. Close its associated input channel.
1579 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001582 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001583 if (mVisibleBound) {
1584 mContext.unbindService(mVisibleConnection);
1585 mVisibleBound = false;
1586 }
1587
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001588 if (mHaveConnection) {
1589 mContext.unbindService(this);
1590 mHaveConnection = false;
1591 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001592
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001593 if (mCurToken != null) {
1594 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001595 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001596 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001597 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001598 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001599 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001600 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001601 } catch (RemoteException e) {
1602 }
1603 mCurToken = null;
1604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001605
The Android Open Source Project10592532009-03-18 17:39:46 -07001606 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001607 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
Yohei Yukawa33e81792015-11-17 21:14:42 -08001610 void resetCurrentMethodAndClient(
1611 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001612 mCurMethodId = null;
1613 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001614 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001615 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001616
1617 void requestClientSessionLocked(ClientState cs) {
1618 if (!cs.sessionRequested) {
1619 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1620 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1621 cs.sessionRequested = true;
1622 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1623 MSG_CREATE_SESSION, mCurMethod, channels[1],
1624 new MethodCallback(this, mCurMethod, channels[0])));
1625 }
1626 }
1627
1628 void clearClientSessionLocked(ClientState cs) {
1629 finishSessionLocked(cs.curSession);
1630 cs.curSession = null;
1631 cs.sessionRequested = false;
1632 }
1633
1634 private void finishSessionLocked(SessionState sessionState) {
1635 if (sessionState != null) {
1636 if (sessionState.session != null) {
1637 try {
1638 sessionState.session.finishSession();
1639 } catch (RemoteException e) {
1640 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001641 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001642 }
1643 sessionState.session = null;
1644 }
1645 if (sessionState.channel != null) {
1646 sessionState.channel.dispose();
1647 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001648 }
1649 }
1650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001652 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (mCurMethod != null) {
1654 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001655 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001657
Jeff Brownc28867a2013-03-26 15:42:39 -07001658 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001659 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 mCurMethod = null;
1661 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001662 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001663 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
satoke7c6998e2011-06-03 17:57:59 +09001667 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 public void onServiceDisconnected(ComponentName name) {
1669 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001670 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 + " mCurIntent=" + mCurIntent);
1672 if (mCurMethod != null && mCurIntent != null
1673 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001674 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 // We consider this to be a new bind attempt, since the system
1676 // should now try to restart the service for us.
1677 mLastBindTime = SystemClock.uptimeMillis();
1678 mShowRequested = mInputShown;
1679 mInputShown = false;
1680 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001681 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1682 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1683 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685 }
1686 }
1687 }
1688
satokf9f01002011-05-19 21:31:50 +09001689 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1691 long ident = Binder.clearCallingIdentity();
1692 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001694 if (!calledWithValidToken(token)) {
1695 final int uid = Binder.getCallingUid();
1696 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1697 + " token:" + token);
1698 return;
1699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001701 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001702 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001703 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001706 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001707 CharSequence contentDescription = null;
1708 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001709 // Use PackageManager to load label
1710 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001711 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001712 mIPackageManager.getApplicationInfo(packageName, 0,
1713 mSettings.getCurrentUserId()));
1714 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001715 /* ignore */
1716 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001717 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001718 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001719 contentDescription != null
1720 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001721 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 }
1725 } finally {
1726 Binder.restoreCallingIdentity(ident);
1727 }
1728 }
1729
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001730 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001731 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001732 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001733 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001734 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001735 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001736 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1737 // When physical keyboard is attached, we show the ime switcher (or notification if
1738 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1739 // exists in the IME switcher dialog. Might be OK to remove this condition once
1740 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1741 return true;
1742 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001743 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1744 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001745 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001746
1747 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1748 final int N = imis.size();
1749 if (N > 2) return true;
1750 if (N < 1) return false;
1751 int nonAuxCount = 0;
1752 int auxCount = 0;
1753 InputMethodSubtype nonAuxSubtype = null;
1754 InputMethodSubtype auxSubtype = null;
1755 for(int i = 0; i < N; ++i) {
1756 final InputMethodInfo imi = imis.get(i);
1757 final List<InputMethodSubtype> subtypes =
1758 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1759 final int subtypeCount = subtypes.size();
1760 if (subtypeCount == 0) {
1761 ++nonAuxCount;
1762 } else {
1763 for (int j = 0; j < subtypeCount; ++j) {
1764 final InputMethodSubtype subtype = subtypes.get(j);
1765 if (!subtype.isAuxiliary()) {
1766 ++nonAuxCount;
1767 nonAuxSubtype = subtype;
1768 } else {
1769 ++auxCount;
1770 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001771 }
1772 }
satok7cfc0ed2011-06-20 21:29:36 +09001773 }
1774 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001775 if (nonAuxCount > 1 || auxCount > 1) {
1776 return true;
1777 } else if (nonAuxCount == 1 && auxCount == 1) {
1778 if (nonAuxSubtype != null && auxSubtype != null
1779 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1780 || auxSubtype.overridesImplicitlyEnabledSubtype()
1781 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1782 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1783 return false;
1784 }
1785 return true;
1786 }
1787 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001788 }
1789
John Spurlocke0980502013-10-25 11:59:29 -04001790 private boolean isKeyguardLocked() {
1791 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1792 }
1793
satokdbf29502011-08-25 15:28:23 +09001794 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001795 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001796 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001797 if (!calledWithValidToken(token)) {
1798 final int uid = Binder.getCallingUid();
1799 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1800 + " token:" + token);
1801 return;
1802 }
1803
1804 synchronized (mMethodMap) {
1805 mImeWindowVis = vis;
1806 mBackDisposition = backDisposition;
1807 updateSystemUiLocked(token, vis, backDisposition);
1808 }
1809 }
1810
1811 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1812 synchronized (mMethodMap) {
1813 updateSystemUiLocked(token, vis, backDisposition);
1814 }
1815 }
1816
1817 // Caution! This method is called in this class. Handle multi-user carefully
1818 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1819 if (!calledWithValidToken(token)) {
1820 final int uid = Binder.getCallingUid();
1821 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1822 + " token:" + token);
1823 return;
1824 }
1825
1826 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1827 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001828 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001829 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001830 // apply policy for binder calls
1831 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1832 vis = 0;
satok06487a52010-10-29 11:37:18 +09001833 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001834 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1835 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1836 if (mStatusBar != null) {
1837 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1838 needsToShowImeSwitcher);
1839 }
1840 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1841 if (imi != null && needsToShowImeSwitcher) {
1842 // Used to load label
1843 final CharSequence title = mRes.getText(
1844 com.android.internal.R.string.select_input_method);
1845 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1846 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001847 mImeSwitcherNotification.setContentTitle(title)
1848 .setContentText(summary)
1849 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001850 try {
1851 if ((mNotificationManager != null)
1852 && !mIWindowManager.hasNavigationBar()) {
1853 if (DEBUG) {
1854 Slog.d(TAG, "--- show notification: label = " + summary);
1855 }
1856 mNotificationManager.notifyAsUser(null,
1857 com.android.internal.R.string.select_input_method,
1858 mImeSwitcherNotification.build(), UserHandle.ALL);
1859 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001860 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001861 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001862 }
1863 } else {
1864 if (mNotificationShown && mNotificationManager != null) {
1865 if (DEBUG) {
1866 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001867 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001868 mNotificationManager.cancelAsUser(null,
1869 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1870 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001871 }
satok06487a52010-10-29 11:37:18 +09001872 }
1873 } finally {
1874 Binder.restoreCallingIdentity(ident);
1875 }
1876 }
1877
satoke7c6998e2011-06-03 17:57:59 +09001878 @Override
satokf9f01002011-05-19 21:31:50 +09001879 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001880 if (!calledFromValidUser()) {
1881 return;
1882 }
satokf9f01002011-05-19 21:31:50 +09001883 synchronized (mMethodMap) {
1884 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1885 for (int i = 0; i < spans.length; ++i) {
1886 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001887 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001888 mSecureSuggestionSpans.put(ss, currentImi);
1889 }
1890 }
1891 }
1892 }
1893
satoke7c6998e2011-06-03 17:57:59 +09001894 @Override
satokf9f01002011-05-19 21:31:50 +09001895 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001896 if (!calledFromValidUser()) {
1897 return false;
1898 }
satokf9f01002011-05-19 21:31:50 +09001899 synchronized (mMethodMap) {
1900 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1901 // TODO: Do not send the intent if the process of the targetImi is already dead.
1902 if (targetImi != null) {
1903 final String[] suggestions = span.getSuggestions();
1904 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001905 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001906 final Intent intent = new Intent();
1907 // Ensures that only a class in the original IME package will receive the
1908 // notification.
satok42c5a162011-05-26 16:46:14 +09001909 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001910 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1911 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1912 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1913 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001914 final long ident = Binder.clearCallingIdentity();
1915 try {
1916 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1917 } finally {
1918 Binder.restoreCallingIdentity(ident);
1919 }
satokf9f01002011-05-19 21:31:50 +09001920 return true;
1921 }
1922 }
1923 return false;
1924 }
1925
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001926 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001927 updateInputMethodsFromSettingsLocked(enabledMayChange);
1928 updateKeyboardFromSettingsLocked();
1929 }
1930
1931 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001932 if (enabledMayChange) {
1933 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1934 for (int i=0; i<enabled.size(); i++) {
1935 // We allow the user to select "disabled until used" apps, so if they
1936 // are enabling one of those here we now need to make it enabled.
1937 InputMethodInfo imm = enabled.get(i);
1938 try {
1939 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1940 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1941 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001942 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001943 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001944 if (DEBUG) {
1945 Slog.d(TAG, "Update state(" + imm.getId()
1946 + "): DISABLED_UNTIL_USED -> DEFAULT");
1947 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001948 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1949 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001950 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1951 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001952 }
1953 } catch (RemoteException e) {
1954 }
1955 }
1956 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001957 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1958 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1959 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1960 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001961 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001962 // There is no input method selected, try to choose new applicable input method.
1963 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001964 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001965 }
1966 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001968 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001970 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001971 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
satokf3db1af2010-11-23 13:34:33 +09001973 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001974 } else {
1975 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001976 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001978 // Here is not the perfect place to reset the switching controller. Ideally
1979 // mSwitchingController and mSettings should be able to share the same state.
1980 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1981 // the same enabled IMEs list.
1982 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001983
1984 }
1985
1986 public void updateKeyboardFromSettingsLocked() {
1987 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1988 if (mSwitchingDialog != null
1989 && mSwitchingDialogTitleView != null
1990 && mSwitchingDialog.isShowing()) {
1991 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1992 com.android.internal.R.id.hard_keyboard_switch);
1993 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001996
Yohei Yukawab097b822015-12-01 10:43:08 -08001997 private void notifyInputMethodSubtypeChanged(final int userId,
1998 @Nullable final InputMethodInfo inputMethodInfo,
1999 @Nullable final InputMethodSubtype subtype) {
2000 final InputManagerInternal inputManagerInternal =
2001 LocalServices.getService(InputManagerInternal.class);
2002 if (inputManagerInternal != null) {
2003 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2004 }
2005 }
2006
satokab751aa2010-09-14 19:17:36 +09002007 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 InputMethodInfo info = mMethodMap.get(id);
2009 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002010 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002012
satokd81e9502012-05-21 12:58:45 +09002013 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002015 final int subtypeCount = info.getSubtypeCount();
2016 if (subtypeCount <= 0) {
2017 return;
satokcd7cd292010-11-20 15:46:23 +09002018 }
satokd81e9502012-05-21 12:58:45 +09002019 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2020 final InputMethodSubtype newSubtype;
2021 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2022 newSubtype = info.getSubtypeAt(subtypeId);
2023 } else {
2024 // If subtype is null, try to find the most applicable one from
2025 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002026 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002027 }
2028 if (newSubtype == null || oldSubtype == null) {
2029 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2030 + ", new subtype = " + newSubtype);
2031 return;
2032 }
2033 if (newSubtype != oldSubtype) {
2034 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2035 if (mCurMethod != null) {
2036 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002037 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002038 mCurMethod.changeInputMethodSubtype(newSubtype);
2039 } catch (RemoteException e) {
2040 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002041 return;
satokab751aa2010-09-14 19:17:36 +09002042 }
2043 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002044 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 return;
2047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002048
satokd81e9502012-05-21 12:58:45 +09002049 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 final long ident = Binder.clearCallingIdentity();
2051 try {
satokab751aa2010-09-14 19:17:36 +09002052 // Set a subtype to this input method.
2053 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002054 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2055 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2056 // because mCurMethodId is stored as a history in
2057 // setSelectedInputMethodAndSubtypeLocked().
2058 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002060 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002062 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002064 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002066 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 } finally {
2068 Binder.restoreCallingIdentity(ident);
2069 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002070
2071 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2072 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002074
satok42c5a162011-05-26 16:46:14 +09002075 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002076 public boolean showSoftInput(IInputMethodClient client, int flags,
2077 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002078 if (!calledFromValidUser()) {
2079 return false;
2080 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002081 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 long ident = Binder.clearCallingIdentity();
2083 try {
2084 synchronized (mMethodMap) {
2085 if (mCurClient == null || client == null
2086 || mCurClient.client.asBinder() != client.asBinder()) {
2087 try {
2088 // We need to check if this is the current client with
2089 // focus in the window manager, to allow this call to
2090 // be made before input is started in it.
2091 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002092 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002093 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002096 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
2098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002099
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002100 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002101 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
2103 } finally {
2104 Binder.restoreCallingIdentity(ident);
2105 }
2106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002107
The Android Open Source Project4df24232009-03-05 14:34:35 -08002108 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002110 if (mAccessibilityRequestingNoSoftKeyboard) {
2111 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
Anna Galusza9b278112016-01-04 11:37:37 -08002113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2115 mShowExplicitlyRequested = true;
2116 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002117 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2118 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002120
Dianne Hackborncc278702009-09-02 23:07:23 -07002121 if (!mSystemReady) {
2122 return false;
2123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002124
The Android Open Source Project4df24232009-03-05 14:34:35 -08002125 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002127 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002128 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2129 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2130 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002132 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002133 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002134 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002135 | Context.BIND_TREAT_LIKE_ACTIVITY
2136 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002137 mVisibleBound = true;
2138 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002139 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002141 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 // The client has asked to have the input method shown, but
2143 // we have been sitting here too long with a connection to the
2144 // service and no interface received, so let's disconnect/connect
2145 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002146 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002148 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002150 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002151 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002152 } else {
2153 if (DEBUG) {
2154 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2155 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002158
The Android Open Source Project4df24232009-03-05 14:34:35 -08002159 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002161
satok42c5a162011-05-26 16:46:14 +09002162 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002163 public boolean hideSoftInput(IInputMethodClient client, int flags,
2164 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002165 if (!calledFromValidUser()) {
2166 return false;
2167 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002168 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 long ident = Binder.clearCallingIdentity();
2170 try {
2171 synchronized (mMethodMap) {
2172 if (mCurClient == null || client == null
2173 || mCurClient.client.asBinder() != client.asBinder()) {
2174 try {
2175 // We need to check if this is the current client with
2176 // focus in the window manager, to allow this call to
2177 // be made before input is started in it.
2178 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002179 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2180 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002181 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
2183 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002184 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
2186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002187
Joe Onorato8a9b2202010-02-26 18:56:32 -08002188 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002189 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
2191 } finally {
2192 Binder.restoreCallingIdentity(ident);
2193 }
2194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002195
The Android Open Source Project4df24232009-03-05 14:34:35 -08002196 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2198 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002199 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 -08002200 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 }
2202 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002203 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 -08002204 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002206
2207 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2208 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2209 // to be updated with the new value sent from IME process. Even in such a transient state
2210 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2211 // application process as a valid request, and have even promised such a behavior with CTS
2212 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2213 // IMMS#InputShown indicates that the software keyboard is shown.
2214 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2215 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2216 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002217 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002218 if (shouldHideSoftInput) {
2219 // The IME will report its visible state again after the following message finally
2220 // delivered to the IME process as an IPC. Hence the inconsistency between
2221 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2222 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002223 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2224 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2225 res = true;
2226 } else {
2227 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002229 if (mHaveConnection && mVisibleBound) {
2230 mContext.unbindService(mVisibleConnection);
2231 mVisibleBound = false;
2232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 mInputShown = false;
2234 mShowRequested = false;
2235 mShowExplicitlyRequested = false;
2236 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002237 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002239
satok42c5a162011-05-26 16:46:14 +09002240 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002241 public InputBindResult startInputOrWindowGainedFocus(
2242 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2243 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002244 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002245 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002246 if (windowToken != null) {
2247 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002248 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002249 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002250 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2251 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002252 }
2253 }
2254
2255 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002256 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2257 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002258 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2259 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002260 // Needs to check the validity before clearing calling identity
2261 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002262 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 long ident = Binder.clearCallingIdentity();
2264 try {
2265 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002266 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2267 + InputMethodClient.getStartInputReason(startInputReason)
2268 + " client=" + client.asBinder()
2269 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002270 + " missingMethods="
2271 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002272 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002273 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002275 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002276
Dianne Hackborn7663d802012-02-24 13:08:49 -08002277 ClientState cs = mClients.get(client.asBinder());
2278 if (cs == null) {
2279 throw new IllegalArgumentException("unknown client "
2280 + client.asBinder());
2281 }
2282
2283 try {
2284 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2285 // Check with the window manager to make sure this client actually
2286 // has a window with focus. If not, reject. This is thread safe
2287 // because if the focus changes some time before or after, the
2288 // next client receiving focus that has any interest in input will
2289 // be calling through here after that change happens.
2290 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2291 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2292 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002294 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002296
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002297 if (!calledFromValidUser) {
2298 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2299 Slog.w(TAG, "If you want to interect with IME, you need "
2300 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2301 hideCurrentInputLocked(0, null);
2302 return null;
2303 }
2304
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002305 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002306 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002307 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002308 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002309 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2310 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002311 }
2312 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002313 }
2314 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002315 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002316
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002317 // Should we auto-show the IME even if the caller has not
2318 // specified what should be done with it?
2319 // We only do this automatically if the window can resize
2320 // to accommodate the IME (so what the user sees will give
2321 // them good context without input information being obscured
2322 // by the IME) or if running on a large screen where there
2323 // is more room for the target window + IME.
2324 final boolean doAutoShow =
2325 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2326 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2327 || mRes.getConfiguration().isLayoutSizeAtLeast(
2328 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002329 final boolean isTextEditor =
2330 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2331
2332 // We want to start input before showing the IME, but after closing
2333 // it. We want to do this after closing it to help the IME disappear
2334 // more quickly (not get stuck behind it initializing itself for the
2335 // new focused input, even if its window wants to hide the IME).
2336 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2339 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002340 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2342 // There is no focus view, and this window will
2343 // be behind any soft input window, so hide the
2344 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002345 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002346 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002348 } else if (isTextEditor && doAutoShow && (softInputMode &
2349 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 // There is a focus view, and we are navigating forward
2351 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002352 // We only do this automatically if the window can resize
2353 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002354 // them good context without input information being obscured
2355 // by the IME) or if running on a large screen where there
2356 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002357 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002358 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002359 res = startInputUncheckedLocked(cs, inputContext,
2360 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002361 didStart = true;
2362 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002363 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
2365 break;
2366 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2367 // Do nothing.
2368 break;
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2370 if ((softInputMode &
2371 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002372 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002373 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
2375 break;
2376 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002377 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002378 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 break;
2380 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2381 if ((softInputMode &
2382 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002383 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002384 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002385 res = startInputUncheckedLocked(cs, inputContext,
2386 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002387 didStart = true;
2388 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002389 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
2391 break;
2392 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002393 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002394 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002395 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2396 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002397 didStart = true;
2398 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002399 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 break;
2401 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002402
2403 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002404 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002405 controlFlags);
2406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 }
2408 } finally {
2409 Binder.restoreCallingIdentity(ident);
2410 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002411
2412 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002414
satok42c5a162011-05-26 16:46:14 +09002415 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002416 public void showInputMethodPickerFromClient(
2417 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002418 if (!calledFromValidUser()) {
2419 return;
2420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 synchronized (mMethodMap) {
2422 if (mCurClient == null || client == null
2423 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002424 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002425 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
2427
satok440aab52010-11-25 09:43:11 +09002428 // Always call subtype picker, because subtype picker is a superset of input method
2429 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002430 mHandler.sendMessage(mCaller.obtainMessageI(
2431 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002432 }
2433 }
2434
satok42c5a162011-05-26 16:46:14 +09002435 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002437 if (!calledFromValidUser()) {
2438 return;
2439 }
satok28203512010-11-24 11:06:49 +09002440 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2441 }
2442
satok42c5a162011-05-26 16:46:14 +09002443 @Override
satok28203512010-11-24 11:06:49 +09002444 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002445 if (!calledFromValidUser()) {
2446 return;
2447 }
satok28203512010-11-24 11:06:49 +09002448 synchronized (mMethodMap) {
2449 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002450 setInputMethodWithSubtypeIdLocked(token, id,
2451 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2452 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002453 } else {
2454 setInputMethod(token, id);
2455 }
2456 }
satokab751aa2010-09-14 19:17:36 +09002457 }
2458
satok42c5a162011-05-26 16:46:14 +09002459 @Override
satokb416a712010-11-25 20:42:14 +09002460 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002461 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002462 if (!calledFromValidUser()) {
2463 return;
2464 }
satokb416a712010-11-25 20:42:14 +09002465 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002466 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2467 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002468 }
2469 }
2470
satok4fc87d62011-05-20 16:13:43 +09002471 @Override
satok735cf382010-11-11 20:40:09 +09002472 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002473 if (!calledFromValidUser()) {
2474 return false;
2475 }
satok735cf382010-11-11 20:40:09 +09002476 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002477 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002478 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002479 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002480 lastImi = mMethodMap.get(lastIme.first);
2481 } else {
2482 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002483 }
satok4fc87d62011-05-20 16:13:43 +09002484 String targetLastImiId = null;
2485 int subtypeId = NOT_A_SUBTYPE_ID;
2486 if (lastIme != null && lastImi != null) {
2487 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002488 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002489 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2490 : mCurrentSubtype.hashCode();
2491 // If the last IME is the same as the current IME and the last subtype is not
2492 // defined, there is no need to switch to the last IME.
2493 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2494 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002495 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002496 }
2497 }
2498
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002499 if (TextUtils.isEmpty(targetLastImiId)
2500 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002501 // This is a safety net. If the currentSubtype can't be added to the history
2502 // and the framework couldn't find the last ime, we will make the last ime be
2503 // the most applicable enabled keyboard subtype of the system imes.
2504 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2505 if (enabled != null) {
2506 final int N = enabled.size();
2507 final String locale = mCurrentSubtype == null
2508 ? mRes.getConfiguration().locale.toString()
2509 : mCurrentSubtype.getLocale();
2510 for (int i = 0; i < N; ++i) {
2511 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002512 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002513 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002514 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2515 InputMethodUtils.getSubtypes(imi),
2516 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002517 if (keyboardSubtype != null) {
2518 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002519 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002520 imi, keyboardSubtype.hashCode());
2521 if(keyboardSubtype.getLocale().equals(locale)) {
2522 break;
2523 }
2524 }
2525 }
2526 }
2527 }
2528 }
2529
2530 if (!TextUtils.isEmpty(targetLastImiId)) {
2531 if (DEBUG) {
2532 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2533 + ", from: " + mCurMethodId + ", " + subtypeId);
2534 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002535 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002536 return true;
2537 } else {
2538 return false;
2539 }
satok735cf382010-11-11 20:40:09 +09002540 }
2541 }
2542
satoke7c6998e2011-06-03 17:57:59 +09002543 @Override
satok688bd472012-02-09 20:09:17 +09002544 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002545 if (!calledFromValidUser()) {
2546 return false;
2547 }
satok688bd472012-02-09 20:09:17 +09002548 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002549 if (!calledWithValidToken(token)) {
2550 final int uid = Binder.getCallingUid();
2551 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2552 + " token:" + token);
2553 return false;
2554 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002555 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002556 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2557 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002558 if (nextSubtype == null) {
2559 return false;
2560 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002561 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2562 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002563 return true;
2564 }
2565 }
2566
2567 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002568 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2569 if (!calledFromValidUser()) {
2570 return false;
2571 }
2572 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002573 if (!calledWithValidToken(token)) {
2574 final int uid = Binder.getCallingUid();
2575 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2576 + "token. uid:" + uid + " token:" + token);
2577 return false;
2578 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002579 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002580 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2581 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002582 if (nextSubtype == null) {
2583 return false;
2584 }
2585 return true;
2586 }
2587 }
2588
2589 @Override
satok68f1b782011-04-11 14:26:04 +09002590 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002591 if (!calledFromValidUser()) {
2592 return null;
2593 }
satok68f1b782011-04-11 14:26:04 +09002594 synchronized (mMethodMap) {
2595 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2596 // TODO: Handle the case of the last IME with no subtypes
2597 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2598 || TextUtils.isEmpty(lastIme.second)) return null;
2599 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2600 if (lastImi == null) return null;
2601 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002602 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002603 final int lastSubtypeId =
2604 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002605 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2606 return null;
2607 }
2608 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002609 } catch (NumberFormatException e) {
2610 return null;
2611 }
2612 }
2613 }
2614
satoke7c6998e2011-06-03 17:57:59 +09002615 @Override
satokee5e77c2011-09-02 18:50:15 +09002616 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002617 if (!calledFromValidUser()) {
2618 return;
2619 }
satok91e88122011-07-18 11:11:42 +09002620 // By this IPC call, only a process which shares the same uid with the IME can add
2621 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002622 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002623 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002624 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002625 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002626 final String[] packageInfos;
2627 try {
2628 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2629 } catch (RemoteException e) {
2630 Slog.e(TAG, "Failed to get package infos");
2631 return;
2632 }
satok91e88122011-07-18 11:11:42 +09002633 if (packageInfos != null) {
2634 final int packageNum = packageInfos.length;
2635 for (int i = 0; i < packageNum; ++i) {
2636 if (packageInfos[i].equals(imi.getPackageName())) {
2637 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002638 final long ident = Binder.clearCallingIdentity();
2639 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002640 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002641 } finally {
2642 Binder.restoreCallingIdentity(ident);
2643 }
satokee5e77c2011-09-02 18:50:15 +09002644 return;
satok91e88122011-07-18 11:11:42 +09002645 }
2646 }
2647 }
satoke7c6998e2011-06-03 17:57:59 +09002648 }
satokee5e77c2011-09-02 18:50:15 +09002649 return;
satoke7c6998e2011-06-03 17:57:59 +09002650 }
2651
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002652 @Override
2653 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002654 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002655 }
2656
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002657 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002658 public void clearLastInputMethodWindowForTransition(IBinder token) {
2659 if (!calledFromValidUser()) {
2660 return;
2661 }
2662 final long ident = Binder.clearCallingIdentity();
2663 try {
2664 synchronized (mMethodMap) {
2665 if (!calledWithValidToken(token)) {
2666 final int uid = Binder.getCallingUid();
2667 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2668 + "invalid token. uid:" + uid + " token:" + token);
2669 return;
2670 }
2671 }
2672 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2673 } finally {
2674 Binder.restoreCallingIdentity(ident);
2675 }
2676 }
2677
2678 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002679 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002680 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002681 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002682 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002683 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002684 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2685 if (DEBUG) {
2686 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2687 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2688 + " actual: " + sequenceNumber);
2689 }
2690 return;
2691 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002692 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2693 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002694 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002695 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002696 }
2697 }
2698
satok28203512010-11-24 11:06:49 +09002699 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002701 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2702 }
2703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002705 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2706 if (token == null) {
2707 if (mContext.checkCallingOrSelfPermission(
2708 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2709 != PackageManager.PERMISSION_GRANTED) {
2710 throw new SecurityException(
2711 "Using null token requires permission "
2712 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002714 } else if (mCurToken != token) {
2715 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2716 + " token: " + token);
2717 return;
2718 }
2719
2720 final long ident = Binder.clearCallingIdentity();
2721 try {
2722 setInputMethodLocked(id, subtypeId);
2723 } finally {
2724 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 }
2726 }
2727
satok42c5a162011-05-26 16:46:14 +09002728 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002730 if (!calledFromValidUser()) {
2731 return;
2732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002734 if (!calledWithValidToken(token)) {
2735 final int uid = Binder.getCallingUid();
2736 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2737 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 return;
2739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 long ident = Binder.clearCallingIdentity();
2741 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002742 hideCurrentInputLocked(flags, null);
2743 } finally {
2744 Binder.restoreCallingIdentity(ident);
2745 }
2746 }
2747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002748
satok42c5a162011-05-26 16:46:14 +09002749 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002750 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002751 if (!calledFromValidUser()) {
2752 return;
2753 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002754 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002755 if (!calledWithValidToken(token)) {
2756 final int uid = Binder.getCallingUid();
2757 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2758 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002759 return;
2760 }
2761 long ident = Binder.clearCallingIdentity();
2762 try {
2763 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 } finally {
2765 Binder.restoreCallingIdentity(ident);
2766 }
2767 }
2768 }
2769
2770 void setEnabledSessionInMainThread(SessionState session) {
2771 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002772 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002774 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002775 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 } catch (RemoteException e) {
2777 }
2778 }
2779 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002780 if (mEnabledSession != null && mEnabledSession.session != null) {
2781 try {
2782 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2783 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2784 } catch (RemoteException e) {
2785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 }
2787 }
2788 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002789
satok42c5a162011-05-26 16:46:14 +09002790 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002792 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002794 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002795 final boolean showAuxSubtypes;
2796 switch (msg.arg1) {
2797 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2798 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2799 // implemented so that auxiliary subtypes will be excluded when the soft
2800 // keyboard is invisible.
2801 showAuxSubtypes = mInputShown;
2802 break;
2803 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2804 showAuxSubtypes = true;
2805 break;
2806 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2807 showAuxSubtypes = false;
2808 break;
2809 default:
2810 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2811 return false;
2812 }
2813 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002814 return true;
2815
satok47a44912010-10-06 16:03:58 +09002816 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002817 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002818 return true;
2819
2820 case MSG_SHOW_IM_CONFIG:
2821 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002822 return true;
2823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 case MSG_UNBIND_INPUT:
2827 try {
2828 ((IInputMethod)msg.obj).unbindInput();
2829 } catch (RemoteException e) {
2830 // There is nothing interesting about the method dying.
2831 }
2832 return true;
2833 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002834 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 try {
2836 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2837 } catch (RemoteException e) {
2838 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002839 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 return true;
2841 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002842 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002844 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002845 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002846 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 } catch (RemoteException e) {
2848 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002849 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 return true;
2851 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002852 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002854 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002855 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002856 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 } catch (RemoteException e) {
2858 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002859 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002861 case MSG_HIDE_CURRENT_INPUT_METHOD:
2862 synchronized (mMethodMap) {
2863 hideCurrentInputLocked(0, null);
2864 }
2865 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002867 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002869 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2871 } catch (RemoteException e) {
2872 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002873 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002875 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002876 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002877 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002878 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002880 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002882 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002883 // Dispose the channel if the input method is not local to this process
2884 // because the remote proxy will get its own copy when unparceled.
2885 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002886 channel.dispose();
2887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002889 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002893
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002894 case MSG_START_INPUT: {
2895 int missingMethods = msg.arg1;
2896 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002898 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002900 session.method.startInput((IInputContext) args.arg2, missingMethods,
2901 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 } catch (RemoteException e) {
2903 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002904 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002906 }
2907 case MSG_RESTART_INPUT: {
2908 int missingMethods = msg.arg1;
2909 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002911 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002913 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2914 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 } catch (RemoteException e) {
2916 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002917 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002922
Yohei Yukawa33e81792015-11-17 21:14:42 -08002923 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002925 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 } catch (RemoteException e) {
2927 // There is nothing interesting about the last client dying.
2928 }
2929 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002930 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002931 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002932 IInputMethodClient client = (IInputMethodClient)args.arg1;
2933 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002935 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002937 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002938 } finally {
2939 // Dispose the channel if the input method is not local to this process
2940 // because the remote proxy will get its own copy when unparceled.
2941 if (res.channel != null && Binder.isProxy(client)) {
2942 res.channel.dispose();
2943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002945 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002947 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002948 case MSG_SET_ACTIVE:
2949 try {
2950 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2951 } catch (RemoteException e) {
2952 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2953 + ((ClientState)msg.obj).pid + " uid "
2954 + ((ClientState)msg.obj).uid);
2955 }
2956 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002957 case MSG_SET_INTERACTIVE:
2958 handleSetInteractive(msg.arg1 != 0);
2959 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002960 case MSG_SWITCH_IME:
2961 handleSwitchInputMethod(msg.arg1 != 0);
2962 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002963 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2964 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002965 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002966 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002967 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002968 } catch (RemoteException e) {
2969 Slog.w(TAG, "Got RemoteException sending "
2970 + "setUserActionNotificationSequenceNumber("
2971 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002972 + clientState.pid + " uid "
2973 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002974 }
2975 return true;
2976 }
satok01038492012-04-09 21:08:27 +09002977
2978 // --------------------------------------------------------------
2979 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002980 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002981 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002982 case MSG_SYSTEM_UNLOCK_USER:
2983 final int userId = msg.arg1;
2984 onUnlockUser(userId);
2985 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
2987 return false;
2988 }
2989
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002990 private void handleSetInteractive(final boolean interactive) {
2991 synchronized (mMethodMap) {
2992 mIsInteractive = interactive;
2993 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2994
2995 // Inform the current client of the change in active status
2996 if (mCurClient != null && mCurClient.client != null) {
2997 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2998 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2999 }
3000 }
3001 }
3002
Yohei Yukawaae61f712015-12-09 13:00:10 -08003003 private void handleSwitchInputMethod(final boolean forwardDirection) {
3004 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003005 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003006 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003007 if (nextSubtype == null) {
3008 return;
3009 }
3010 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003011 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3012 if (newInputMethodInfo == null) {
3013 return;
3014 }
3015 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3016 newInputMethodInfo, mCurrentSubtype);
3017 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003018 if (mSubtypeSwitchedByShortCutToast == null) {
3019 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3020 Toast.LENGTH_SHORT);
3021 } else {
3022 mSubtypeSwitchedByShortCutToast.setText(toastText);
3023 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003024 mSubtypeSwitchedByShortCutToast.show();
3025 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003026 }
3027 }
3028
satokdc9ddae2011-10-06 12:22:36 +09003029 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003030 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3031 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003032 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003033 if (DEBUG) {
3034 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3035 }
satok723a27e2010-11-11 14:58:11 +09003036 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003037 return true;
3038 }
3039
3040 return false;
3041 }
3042
Yohei Yukawa94e33302016-02-12 19:37:03 -08003043 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003044 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003045 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003046 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003047 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003048 mMethodList.clear();
3049 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003050
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003051 // Use for queryIntentServicesAsUser
3052 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003054 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3055 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3056 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003057 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003059 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3060 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003061
satoke7c6998e2011-06-03 17:57:59 +09003062 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3063 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 for (int i = 0; i < services.size(); ++i) {
3065 ResolveInfo ri = services.get(i);
3066 ServiceInfo si = ri.serviceInfo;
3067 ComponentName compName = new ComponentName(si.packageName, si.name);
3068 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3069 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003070 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 + ": it does not require the permission "
3072 + android.Manifest.permission.BIND_INPUT_METHOD);
3073 continue;
3074 }
3075
Joe Onorato8a9b2202010-02-26 18:56:32 -08003076 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077
3078 try {
satoke7c6998e2011-06-03 17:57:59 +09003079 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003080 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003081 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003082 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083
3084 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003085 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003087 } catch (Exception e) {
3088 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
3090 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003091
Yohei Yukawa859df052016-02-17 07:56:46 -08003092 // TODO: The following code should find better place to live.
3093 if (!resetDefaultEnabledIme) {
3094 boolean enabledImeFound = false;
3095 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3096 final int N = enabledImes.size();
3097 for (int i = 0; i < N; ++i) {
3098 final InputMethodInfo imi = enabledImes.get(i);
3099 if (mMethodList.contains(imi)) {
3100 enabledImeFound = true;
3101 break;
3102 }
3103 }
3104 if (!enabledImeFound) {
3105 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3106 resetDefaultEnabledIme = true;
3107 resetSelectedInputMethodAndSubtypeLocked("");
3108 }
3109 }
3110
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003111 if (resetDefaultEnabledIme) {
3112 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003113 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003114 final int N = defaultEnabledIme.size();
3115 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003116 final InputMethodInfo imi = defaultEnabledIme.get(i);
3117 if (DEBUG) {
3118 Slog.d(TAG, "--- enable ime = " + imi);
3119 }
3120 setInputMethodEnabledLocked(imi.getId(), true);
3121 }
3122 }
3123
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003124 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003125 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003126 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003127 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3128 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003129 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003130 }
3131 } else {
3132 // Double check that the default IME is certainly enabled.
3133 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003134 }
3135 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003136 // Here is not the perfect place to reset the switching controller. Ideally
3137 // mSwitchingController and mSettings should be able to share the same state.
3138 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3139 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003140 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003144
satok217f5482010-12-15 05:19:19 +09003145 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003146 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003147 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003148 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3149 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003150 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003151 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003152 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003153 final int userId;
3154 synchronized (mMethodMap) {
3155 userId = mSettings.getCurrentUserId();
3156 }
3157 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003158 }
3159
3160 private void showConfigureInputMethods() {
3161 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3162 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3163 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3164 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003165 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003166 }
3167
satok2c93efc2012-04-02 19:33:47 +09003168 private boolean isScreenLocked() {
3169 return mKeyguardManager != null
3170 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3171 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003172
Seigo Nonaka14e13912015-05-06 21:04:13 -07003173 private void showInputMethodMenu(boolean showAuxSubtypes) {
3174 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003177 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003178
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003179 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003180 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003181 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003182
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003183 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003184 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003185 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3186 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003187 if (immis == null || immis.size() == 0) {
3188 return;
3189 }
3190
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003191 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192
satok688bd472012-02-09 20:09:17 +09003193 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003194 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003195 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003196
satokc3690562012-01-10 20:14:43 +09003197 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003198 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003199 if (currentSubtype != null) {
3200 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003201 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3202 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003203 }
3204 }
3205
Ken Wakasa761eb372011-03-04 19:06:18 +09003206 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003207 mIms = new InputMethodInfo[N];
3208 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003209 int checkedItem = 0;
3210 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003211 final ImeSubtypeListItem item = imList.get(i);
3212 mIms[i] = item.mImi;
3213 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003214 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003215 int subtypeId = mSubtypeIds[i];
3216 if ((subtypeId == NOT_A_SUBTYPE_ID)
3217 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3218 || (subtypeId == lastInputMethodSubtypeId)) {
3219 checkedItem = i;
3220 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003223
3224 final Context settingsContext = new ContextThemeWrapper(context,
3225 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3226
3227 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003228 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3229 @Override
3230 public void onCancel(DialogInterface dialog) {
3231 hideInputMethodMenu();
3232 }
3233 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003234
3235 final Context dialogContext = mDialogBuilder.getContext();
3236 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3237 com.android.internal.R.styleable.DialogPreference,
3238 com.android.internal.R.attr.alertDialogStyle, 0);
3239 final Drawable dialogIcon = a.getDrawable(
3240 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3241 a.recycle();
3242
3243 mDialogBuilder.setIcon(dialogIcon);
3244
Yohei Yukawad34e1482016-02-11 08:03:52 -08003245 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003246 final View tv = inflater.inflate(
3247 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3248 mDialogBuilder.setCustomTitle(tv);
3249
3250 // Setup layout for a toggle switch of the hardware keyboard
3251 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003252 mSwitchingDialogTitleView
3253 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003254 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003255 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003256 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003257 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003258 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003259 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3260 @Override
3261 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003262 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003263 // Ensure that the input method dialog is dismissed when changing
3264 // the hardware keyboard state.
3265 hideInputMethodMenu();
3266 }
3267 });
3268
Alan Viverette505e3ab2014-11-24 15:22:11 -08003269 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003270 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3271 final OnClickListener choiceListener = new OnClickListener() {
3272 @Override
3273 public void onClick(final DialogInterface dialog, final int which) {
3274 synchronized (mMethodMap) {
3275 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3276 || mSubtypeIds.length <= which) {
3277 return;
satok01038492012-04-09 21:08:27 +09003278 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003279 final InputMethodInfo im = mIms[which];
3280 int subtypeId = mSubtypeIds[which];
3281 adapter.mCheckedItem = which;
3282 adapter.notifyDataSetChanged();
3283 hideInputMethodMenu();
3284 if (im != null) {
3285 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3286 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003287 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003288 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003290 }
3291 }
3292 };
3293 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003296 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003297 final Window w = mSwitchingDialog.getWindow();
3298 final WindowManager.LayoutParams attrs = w.getAttributes();
3299 w.setType(TYPE_INPUT_METHOD_DIALOG);
3300 // Use an alternate token for the dialog for that window manager can group the token
3301 // with other IME windows based on type vs. grouping based on whichever token happens
3302 // to get selected by the system later on.
3303 attrs.token = mSwitchingDialogToken;
3304 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3305 attrs.setTitle("Select input method");
3306 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003307 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 mSwitchingDialog.show();
3309 }
3310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003311
Ken Wakasa05dbb652011-08-22 15:22:43 +09003312 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3313 private final LayoutInflater mInflater;
3314 private final int mTextViewResourceId;
3315 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003316 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003317 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3318 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3319 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003320
Ken Wakasa05dbb652011-08-22 15:22:43 +09003321 mTextViewResourceId = textViewResourceId;
3322 mItemsList = itemsList;
3323 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003324 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003325 }
3326
3327 @Override
3328 public View getView(int position, View convertView, ViewGroup parent) {
3329 final View view = convertView != null ? convertView
3330 : mInflater.inflate(mTextViewResourceId, null);
3331 if (position < 0 || position >= mItemsList.size()) return view;
3332 final ImeSubtypeListItem item = mItemsList.get(position);
3333 final CharSequence imeName = item.mImeName;
3334 final CharSequence subtypeName = item.mSubtypeName;
3335 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3336 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3337 if (TextUtils.isEmpty(subtypeName)) {
3338 firstTextView.setText(imeName);
3339 secondTextView.setVisibility(View.GONE);
3340 } else {
3341 firstTextView.setText(subtypeName);
3342 secondTextView.setText(imeName);
3343 secondTextView.setVisibility(View.VISIBLE);
3344 }
3345 final RadioButton radioButton =
3346 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3347 radioButton.setChecked(position == mCheckedItem);
3348 return view;
3349 }
3350 }
3351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003353 synchronized (mMethodMap) {
3354 hideInputMethodMenuLocked();
3355 }
3356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003357
The Android Open Source Project10592532009-03-18 17:39:46 -07003358 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003359 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360
The Android Open Source Project10592532009-03-18 17:39:46 -07003361 if (mSwitchingDialog != null) {
3362 mSwitchingDialog.dismiss();
3363 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003365
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003366 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003367 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003368 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003372
satok42c5a162011-05-26 16:46:14 +09003373 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003375 // TODO: Make this work even for non-current users?
3376 if (!calledFromValidUser()) {
3377 return false;
3378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 synchronized (mMethodMap) {
3380 if (mContext.checkCallingOrSelfPermission(
3381 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3382 != PackageManager.PERMISSION_GRANTED) {
3383 throw new SecurityException(
3384 "Requires permission "
3385 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3386 }
Anna Galusza9b278112016-01-04 11:37:37 -08003387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 long ident = Binder.clearCallingIdentity();
3389 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003390 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 } finally {
3392 Binder.restoreCallingIdentity(ident);
3393 }
3394 }
3395 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003396
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003397 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3398 // Make sure this is a valid input method.
3399 InputMethodInfo imm = mMethodMap.get(id);
3400 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003401 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003402 }
3403
satokd87c2592010-09-29 11:52:06 +09003404 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3405 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003406
satokd87c2592010-09-29 11:52:06 +09003407 if (enabled) {
3408 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3409 if (pair.first.equals(id)) {
3410 // We are enabling this input method, but it is already enabled.
3411 // Nothing to do. The previous state was enabled.
3412 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003413 }
3414 }
satokd87c2592010-09-29 11:52:06 +09003415 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3416 // Previous state was disabled.
3417 return false;
3418 } else {
3419 StringBuilder builder = new StringBuilder();
3420 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3421 builder, enabledInputMethodsList, id)) {
3422 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003423 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003424 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3425 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3426 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003427 }
3428 // Previous state was enabled.
3429 return true;
3430 } else {
3431 // We are disabling the input method but it is already disabled.
3432 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003433 return false;
3434 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003435 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003436 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003437
satok723a27e2010-11-11 14:58:11 +09003438 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3439 boolean setSubtypeOnly) {
3440 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003441 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003442
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003443 mCurUserActionNotificationSequenceNumber =
3444 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3445 if (DEBUG) {
3446 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3447 + mCurUserActionNotificationSequenceNumber);
3448 }
3449
3450 if (mCurClient != null && mCurClient.client != null) {
3451 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3452 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003453 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003454 }
3455
satok723a27e2010-11-11 14:58:11 +09003456 // Set Subtype here
3457 if (imi == null || subtypeId < 0) {
3458 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003459 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003460 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003461 if (subtypeId < imi.getSubtypeCount()) {
3462 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3463 mSettings.putSelectedSubtype(subtype.hashCode());
3464 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003465 } else {
3466 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003467 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003468 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003469 }
satokab751aa2010-09-14 19:17:36 +09003470 }
satok723a27e2010-11-11 14:58:11 +09003471
Yohei Yukawa68645a62016-02-17 07:54:20 -08003472 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003473 // Set InputMethod here
3474 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3475 }
3476 }
3477
3478 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3479 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3480 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3481 // newDefaultIme is empty when there is no candidate for the selected IME.
3482 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3483 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3484 if (subtypeHashCode != null) {
3485 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003486 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003487 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003488 } catch (NumberFormatException e) {
3489 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3490 }
3491 }
3492 }
3493 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003494 }
3495
satok4e4569d2010-11-19 18:45:53 +09003496 // If there are no selected shortcuts, tries finding the most applicable ones.
3497 private Pair<InputMethodInfo, InputMethodSubtype>
3498 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3499 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3500 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003501 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003502 boolean foundInSystemIME = false;
3503
3504 // Search applicable subtype for each InputMethodInfo
3505 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003506 final String imiId = imi.getId();
3507 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3508 continue;
3509 }
satokcd7cd292010-11-20 15:46:23 +09003510 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003511 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003512 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003513 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003514 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003515 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003516 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003517 }
satokdf31ae62011-01-15 06:19:44 +09003518 // 2. Search by the system locale from enabledSubtypes.
3519 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003520 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003521 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003522 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003523 }
satoka86f5e42011-09-02 17:12:42 +09003524 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003525 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003526 final ArrayList<InputMethodSubtype> subtypesForSearch =
3527 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003528 ? InputMethodUtils.getSubtypes(imi)
3529 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003530 // 4. Search by the current subtype's locale from all subtypes.
3531 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003532 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003533 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003534 }
3535 // 5. Search by the system locale from all subtypes.
3536 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003537 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003538 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003539 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003540 }
satokcd7cd292010-11-20 15:46:23 +09003541 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003542 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003543 // The current input method is the most applicable IME.
3544 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003545 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003546 break;
satok7599a7f2010-12-22 13:45:23 +09003547 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003548 // The system input method is 2nd applicable IME.
3549 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003550 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003551 if ((imi.getServiceInfo().applicationInfo.flags
3552 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3553 foundInSystemIME = true;
3554 }
satok4e4569d2010-11-19 18:45:53 +09003555 }
3556 }
3557 }
3558 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003559 if (mostApplicableIMI != null) {
3560 Slog.w(TAG, "Most applicable shortcut input method was:"
3561 + mostApplicableIMI.getId());
3562 if (mostApplicableSubtype != null) {
3563 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3564 + "," + mostApplicableSubtype.getMode() + ","
3565 + mostApplicableSubtype.getLocale());
3566 }
3567 }
satok4e4569d2010-11-19 18:45:53 +09003568 }
satokcd7cd292010-11-20 15:46:23 +09003569 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003570 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003571 } else {
3572 return null;
3573 }
3574 }
3575
satokab751aa2010-09-14 19:17:36 +09003576 /**
3577 * @return Return the current subtype of this input method.
3578 */
satok42c5a162011-05-26 16:46:14 +09003579 @Override
satokab751aa2010-09-14 19:17:36 +09003580 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003581 // TODO: Make this work even for non-current users?
3582 if (!calledFromValidUser()) {
3583 return null;
3584 }
3585 synchronized (mMethodMap) {
3586 return getCurrentInputMethodSubtypeLocked();
3587 }
3588 }
3589
3590 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003591 if (mCurMethodId == null) {
3592 return null;
3593 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003594 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003595 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3596 if (imi == null || imi.getSubtypeCount() == 0) {
3597 return null;
satok4e4569d2010-11-19 18:45:53 +09003598 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003599 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003600 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3601 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003602 if (subtypeId == NOT_A_SUBTYPE_ID) {
3603 // If there are no selected subtypes, the framework will try to find
3604 // the most applicable subtype from explicitly or implicitly enabled
3605 // subtypes.
3606 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003607 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003608 // If there is only one explicitly or implicitly enabled subtype,
3609 // just returns it.
3610 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3611 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3612 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003613 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003614 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003615 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003616 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003617 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003618 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3619 true);
satok4e4569d2010-11-19 18:45:53 +09003620 }
satok3ef8b292010-11-23 06:06:29 +09003621 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003622 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003623 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003624 }
3625 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003626 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003627 }
3628
satok4e4569d2010-11-19 18:45:53 +09003629 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003630 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003631 @Override
satok4e4569d2010-11-19 18:45:53 +09003632 public List getShortcutInputMethodsAndSubtypes() {
3633 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003634 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003635 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003636 // If there are no selected shortcut subtypes, the framework will try to find
3637 // the most applicable subtype from all subtypes whose mode is
3638 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003639 Pair<InputMethodInfo, InputMethodSubtype> info =
3640 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003641 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003642 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003643 ret.add(info.first);
3644 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003645 }
satok3da92232011-01-11 22:46:30 +09003646 return ret;
satokf3db1af2010-11-23 13:34:33 +09003647 }
satokf3db1af2010-11-23 13:34:33 +09003648 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3649 ret.add(imi);
3650 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3651 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003652 }
3653 }
satokf3db1af2010-11-23 13:34:33 +09003654 return ret;
satok4e4569d2010-11-19 18:45:53 +09003655 }
3656 }
3657
satok42c5a162011-05-26 16:46:14 +09003658 @Override
satokb66d2872010-11-10 01:04:04 +09003659 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003660 // TODO: Make this work even for non-current users?
3661 if (!calledFromValidUser()) {
3662 return false;
3663 }
satokb66d2872010-11-10 01:04:04 +09003664 synchronized (mMethodMap) {
3665 if (subtype != null && mCurMethodId != null) {
3666 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003667 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003668 if (subtypeId != NOT_A_SUBTYPE_ID) {
3669 setInputMethodLocked(mCurMethodId, subtypeId);
3670 return true;
3671 }
3672 }
3673 return false;
3674 }
3675 }
3676
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003677 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003678 private static class InputMethodFileManager {
3679 private static final String SYSTEM_PATH = "system";
3680 private static final String INPUT_METHOD_PATH = "inputmethod";
3681 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3682 private static final String NODE_SUBTYPES = "subtypes";
3683 private static final String NODE_SUBTYPE = "subtype";
3684 private static final String NODE_IMI = "imi";
3685 private static final String ATTR_ID = "id";
3686 private static final String ATTR_LABEL = "label";
3687 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003688 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003689 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003690 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003691 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3692 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3693 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003694 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003695 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3696 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003697 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003698 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003699 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003700 if (methodMap == null) {
3701 throw new NullPointerException("methodMap is null");
3702 }
3703 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003704 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003705 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3706 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003707 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003708 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003709 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3710 }
3711 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3712 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3713 if (!subtypeFile.exists()) {
3714 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003715 writeAdditionalInputMethodSubtypes(
3716 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003717 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003718 readAdditionalInputMethodSubtypes(
3719 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003720 }
3721 }
3722
3723 private void deleteAllInputMethodSubtypes(String imiId) {
3724 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003725 mAdditionalSubtypesMap.remove(imiId);
3726 writeAdditionalInputMethodSubtypes(
3727 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003728 }
3729 }
3730
3731 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003732 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003733 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003734 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003735 final int N = additionalSubtypes.length;
3736 for (int i = 0; i < N; ++i) {
3737 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003738 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003739 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003740 } else {
3741 Slog.w(TAG, "Duplicated subtype definition found: "
3742 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003743 }
3744 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003745 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3746 writeAdditionalInputMethodSubtypes(
3747 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003748 }
3749 }
3750
3751 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3752 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003753 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003754 }
3755 }
3756
3757 private static void writeAdditionalInputMethodSubtypes(
3758 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3759 HashMap<String, InputMethodInfo> methodMap) {
3760 // Safety net for the case that this function is called before methodMap is set.
3761 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3762 FileOutputStream fos = null;
3763 try {
3764 fos = subtypesFile.startWrite();
3765 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003766 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003767 out.startDocument(null, true);
3768 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3769 out.startTag(null, NODE_SUBTYPES);
3770 for (String imiId : allSubtypes.keySet()) {
3771 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3772 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3773 continue;
3774 }
3775 out.startTag(null, NODE_IMI);
3776 out.attribute(null, ATTR_ID, imiId);
3777 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3778 final int N = subtypesList.size();
3779 for (int i = 0; i < N; ++i) {
3780 final InputMethodSubtype subtype = subtypesList.get(i);
3781 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003782 if (subtype.hasSubtypeId()) {
3783 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3784 String.valueOf(subtype.getSubtypeId()));
3785 }
satoke7c6998e2011-06-03 17:57:59 +09003786 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3787 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3788 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003789 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3790 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003791 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3792 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3793 out.attribute(null, ATTR_IS_AUXILIARY,
3794 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003795 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3796 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003797 out.endTag(null, NODE_SUBTYPE);
3798 }
3799 out.endTag(null, NODE_IMI);
3800 }
3801 out.endTag(null, NODE_SUBTYPES);
3802 out.endDocument();
3803 subtypesFile.finishWrite(fos);
3804 } catch (java.io.IOException e) {
3805 Slog.w(TAG, "Error writing subtypes", e);
3806 if (fos != null) {
3807 subtypesFile.failWrite(fos);
3808 }
3809 }
3810 }
3811
3812 private static void readAdditionalInputMethodSubtypes(
3813 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3814 if (allSubtypes == null || subtypesFile == null) return;
3815 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003816 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003817 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003818 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003819 int type = parser.getEventType();
3820 // Skip parsing until START_TAG
3821 while ((type = parser.next()) != XmlPullParser.START_TAG
3822 && type != XmlPullParser.END_DOCUMENT) {}
3823 String firstNodeName = parser.getName();
3824 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3825 throw new XmlPullParserException("Xml doesn't start with subtypes");
3826 }
3827 final int depth =parser.getDepth();
3828 String currentImiId = null;
3829 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3830 while (((type = parser.next()) != XmlPullParser.END_TAG
3831 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3832 if (type != XmlPullParser.START_TAG)
3833 continue;
3834 final String nodeName = parser.getName();
3835 if (NODE_IMI.equals(nodeName)) {
3836 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3837 if (TextUtils.isEmpty(currentImiId)) {
3838 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3839 continue;
3840 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003841 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003842 allSubtypes.put(currentImiId, tempSubtypesArray);
3843 } else if (NODE_SUBTYPE.equals(nodeName)) {
3844 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3845 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3846 continue;
3847 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003848 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003849 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003850 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003851 parser.getAttributeValue(null, ATTR_LABEL));
3852 final String imeSubtypeLocale =
3853 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003854 final String languageTag =
3855 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003856 final String imeSubtypeMode =
3857 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3858 final String imeSubtypeExtraValue =
3859 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003860 final boolean isAuxiliary = "1".equals(String.valueOf(
3861 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003862 final boolean isAsciiCapable = "1".equals(String.valueOf(
3863 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003864 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003865 .setSubtypeNameResId(label)
3866 .setSubtypeIconResId(icon)
3867 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003868 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003869 .setSubtypeMode(imeSubtypeMode)
3870 .setSubtypeExtraValue(imeSubtypeExtraValue)
3871 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003872 .setIsAsciiCapable(isAsciiCapable);
3873 final String subtypeIdString =
3874 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3875 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003876 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003877 }
3878 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003879 }
3880 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003881 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3882 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003883 return;
satoke7c6998e2011-06-03 17:57:59 +09003884 }
3885 }
3886 }
3887
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003888 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3889 @NonNull
3890 private final Handler mHandler;
3891
3892 LocalServiceImpl(@NonNull final Handler handler) {
3893 mHandler = handler;
3894 }
3895
3896 @Override
3897 public void setInteractive(boolean interactive) {
3898 // Do everything in handler so as not to block the caller.
3899 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3900 interactive ? 1 : 0, 0));
3901 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003902
3903 @Override
3904 public void switchInputMethod(boolean forwardDirection) {
3905 // Do everything in handler so as not to block the caller.
3906 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3907 forwardDirection ? 1 : 0, 0));
3908 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003909
3910 @Override
3911 public void hideCurrentInputMethod() {
3912 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3913 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3914 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003915 }
3916
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003917 private static String imeWindowStatusToString(final int imeWindowVis) {
3918 final StringBuilder sb = new StringBuilder();
3919 boolean first = true;
3920 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3921 sb.append("Active");
3922 first = false;
3923 }
3924 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3925 if (!first) {
3926 sb.append("|");
3927 }
3928 sb.append("Visible");
3929 }
3930 return sb.toString();
3931 }
3932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003934 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3935 @Nullable Uri contentUri, @Nullable String packageName) {
3936 if (!calledFromValidUser()) {
3937 return null;
3938 }
3939
3940 if (token == null) {
3941 throw new NullPointerException("token");
3942 }
3943 if (packageName == null) {
3944 throw new NullPointerException("packageName");
3945 }
3946 if (contentUri == null) {
3947 throw new NullPointerException("contentUri");
3948 }
3949 final String contentUriScheme = contentUri.getScheme();
3950 if (!"content".equals(contentUriScheme)) {
3951 throw new InvalidParameterException("contentUri must have content scheme");
3952 }
3953
3954 synchronized (mMethodMap) {
3955 final int uid = Binder.getCallingUid();
3956 if (mCurMethodId == null) {
3957 return null;
3958 }
3959 if (mCurToken != token) {
3960 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3961 + " token=" + token);
3962 return null;
3963 }
3964 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3965 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3966 // nature of our system. Let's compare it with our internal record.
3967 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3968 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3969 + mCurAttribute.packageName + " packageName=" + packageName);
3970 return null;
3971 }
3972 final int imeUserId = UserHandle.getUserId(uid);
3973 final int appUserId = UserHandle.getUserId(mCurClient.uid);
3974 return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
3975 appUserId);
3976 }
3977 }
3978
3979 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3981 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3982 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3985 + Binder.getCallingPid()
3986 + ", uid=" + Binder.getCallingUid());
3987 return;
3988 }
3989
3990 IInputMethod method;
3991 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003992 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 synchronized (mMethodMap) {
3997 p.println("Current Input Method Manager state:");
3998 int N = mMethodList.size();
3999 p.println(" Input Methods:");
4000 for (int i=0; i<N; i++) {
4001 InputMethodInfo info = mMethodList.get(i);
4002 p.println(" InputMethod #" + i + ":");
4003 info.dump(p, " ");
4004 }
4005 p.println(" Clients:");
4006 for (ClientState ci : mClients.values()) {
4007 p.println(" Client " + ci + ":");
4008 p.println(" client=" + ci.client);
4009 p.println(" inputContext=" + ci.inputContext);
4010 p.println(" sessionRequested=" + ci.sessionRequested);
4011 p.println(" curSession=" + ci.curSession);
4012 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004013 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004015 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4016 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004017 focusedWindowClient = mCurFocusedWindowClient;
4018 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4020 + " mBoundToMethod=" + mBoundToMethod);
4021 p.println(" mCurToken=" + mCurToken);
4022 p.println(" mCurIntent=" + mCurIntent);
4023 method = mCurMethod;
4024 p.println(" mCurMethod=" + mCurMethod);
4025 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004026 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 p.println(" mShowRequested=" + mShowRequested
4028 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4029 + " mShowForced=" + mShowForced
4030 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004031 p.println(" mCurUserActionNotificationSequenceNumber="
4032 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004033 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004034 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004035 p.println(" mSwitchingController:");
4036 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004037 p.println(" mSettings:");
4038 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004040
Jeff Brownb88102f2010-09-08 11:49:43 -07004041 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 pw.flush();
4044 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004045 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4046 } catch (IOException | RemoteException e) {
4047 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004049 } else {
4050 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004052
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004053 if (focusedWindowClient != null && client != focusedWindowClient) {
4054 p.println(" ");
4055 p.println("Warning: Current input method client doesn't match the last focused. "
4056 + "window.");
4057 p.println("Dumping input method client in the last focused window just in case.");
4058 p.println(" ");
4059 pw.flush();
4060 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004061 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4062 } catch (IOException | RemoteException e) {
4063 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004064 }
4065 }
4066
Jeff Brownb88102f2010-09-08 11:49:43 -07004067 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 pw.flush();
4070 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004071 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4072 } catch (IOException | RemoteException e) {
4073 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004075 } else {
4076 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 }
4078 }
4079}