blob: 3ad264d38bb5b332bf946e6d43b351b992a3fc20 [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
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070018import static java.lang.annotation.RetentionPolicy.SOURCE;
19
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070021import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090023import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090024import com.android.internal.inputmethod.InputMethodUtils;
25import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070027import com.android.internal.os.SomeArgs;
Jeff Sharkeyc06f1842016-11-09 12:25:44 -070028import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090029import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.view.IInputContext;
31import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.view.IInputMethodClient;
33import com.android.internal.view.IInputMethodManager;
34import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070035import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080037import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080038import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
satoke7c6998e2011-06-03 17:57:59 +090040import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090042import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070044import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070045import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070046import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080047import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.app.ActivityManagerNative;
49import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070050import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090051import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090052import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090053import android.app.Notification;
54import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070055import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090056import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.ComponentName;
58import android.content.ContentResolver;
59import android.content.Context;
60import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090062import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090064import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070066import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090067import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.pm.PackageManager;
69import android.content.pm.ResolveInfo;
70import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070071import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.res.Resources;
73import android.content.res.TypedArray;
74import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080075import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080076import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080077import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070078import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040080import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070081import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090082import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.Handler;
84import android.os.IBinder;
85import android.os.IInterface;
86import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070087import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070089import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080091import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.os.ServiceManager;
93import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090094import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080095import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.provider.Settings;
97import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090098import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070099import android.util.ArrayMap;
100import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700101import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900103import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900104import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.util.PrintWriterPrinter;
106import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900107import android.util.Slog;
108import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900109import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700111import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900112import android.view.LayoutInflater;
113import android.view.View;
114import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700116import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900117import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700119import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.inputmethod.InputMethod;
121import android.view.inputmethod.InputMethodInfo;
122import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700123import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900124import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900125import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900126import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900127import android.widget.CompoundButton;
128import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900129import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900130import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900131import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700132import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
satoke7c6998e2011-06-03 17:57:59 +0900134import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900136import java.io.FileInputStream;
137import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import java.io.IOException;
139import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700140import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100141import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700142import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900144import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.util.HashMap;
146import java.util.List;
147
148/**
149 * This class provides a system service that manages input methods.
150 */
151public class InputMethodManagerService extends IInputMethodManager.Stub
152 implements ServiceConnection, Handler.Callback {
153 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700154 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700155 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700157 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
158 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
159 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 static final int MSG_UNBIND_INPUT = 1000;
162 static final int MSG_BIND_INPUT = 1010;
163 static final int MSG_SHOW_SOFT_INPUT = 1020;
164 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700165 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 static final int MSG_ATTACH_TOKEN = 1040;
167 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 static final int MSG_START_INPUT = 2000;
170 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
Yohei Yukawa33e81792015-11-17 21:14:42 -0800172 static final int MSG_UNBIND_CLIENT = 3000;
173 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700174 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700175 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900176 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800177 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
satok01038492012-04-09 21:08:27 +0900179 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
180
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700181 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
182
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700183 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
satokf9f01002011-05-19 21:31:50 +0900185 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
186
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900187 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900188 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900189
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700190 @Retention(SOURCE)
191 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
192 private @interface HardKeyboardBehavior {
193 int WIRELESS_AFFORDANCE = 0;
194 int WIRED_AFFORDANCE = 1;
195 }
satok4e4569d2010-11-19 18:45:53 +0900196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800198 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900200 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700203 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700205 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900206 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900207 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900208 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800209 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900211 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 // All known input methods. mMethodMap also serves as the global
214 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700215 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
216 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900217 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700218 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900219 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700221 // Used to bring IME service up to visible adjustment while it is being shown.
222 final ServiceConnection mVisibleConnection = new ServiceConnection() {
223 @Override public void onServiceConnected(ComponentName name, IBinder service) {
224 }
225
226 @Override public void onServiceDisconnected(ComponentName name) {
227 }
228 };
229 boolean mVisibleBound = false;
230
satok7cfc0ed2011-06-20 21:29:36 +0900231 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700232 private NotificationManager mNotificationManager;
233 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700234 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400235 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700236 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900237 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900238 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900239 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900240
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900241 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 final ClientState client;
243 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700244
245 IInputMethodSession session;
246 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 @Override
249 public String toString() {
250 return "SessionState{uid " + client.uid + " pid " + client.pid
251 + " method " + Integer.toHexString(
252 System.identityHashCode(method))
253 + " session " + Integer.toHexString(
254 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700255 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 + "}";
257 }
258
259 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700260 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 client = _client;
262 method = _method;
263 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700264 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
Jeff Brownc28867a2013-03-26 15:42:39 -0700268 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 final IInputMethodClient client;
270 final IInputContext inputContext;
271 final int uid;
272 final int pid;
273 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 boolean sessionRequested;
276 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 @Override
279 public String toString() {
280 return "ClientState{" + Integer.toHexString(
281 System.identityHashCode(this)) + " uid " + uid
282 + " pid " + pid + "}";
283 }
284
285 ClientState(IInputMethodClient _client, IInputContext _inputContext,
286 int _uid, int _pid) {
287 client = _client;
288 inputContext = _inputContext;
289 uid = _uid;
290 pid = _pid;
291 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
292 }
293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700295 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700298 * Set once the system is ready to run third party code.
299 */
300 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700302 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700303 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
304 * method. This is to be synchronized with the secure settings keyed with
305 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
306 *
307 * <p>This can be transiently {@code null} when the system is re-initializing input method
308 * settings, e.g., the system locale is just changed.</p>
309 *
310 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
311 * {@link InputMethodManagerService}.</p>
312 *
313 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700315 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
319 * The current binding sequence number, incremented every time there is
320 * a new bind performed.
321 */
322 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * The client that is currently bound to an input method.
326 */
327 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800330 * The last window token that we confirmed to be focused. This is always updated upon reports
331 * from the input method client. If the window state is already changed before the report is
332 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700333 */
334 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700336 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800337 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
338 */
339 ClientState mCurFocusedWindowClient;
340
341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 * The input context last provided by the current client.
343 */
344 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700347 * The missing method flags for the input context last provided by the current client.
348 *
349 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
350 */
351 int mCurInputContextMissingMethods;
352
353 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 * The attributes last provided by the current client.
355 */
356 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700359 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700361 *
362 * <p>This can be {@code null} when no input method is connected.</p>
363 *
364 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700366 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 /**
satokab751aa2010-09-14 19:17:36 +0900370 * The current subtype of the current input method.
371 */
372 private InputMethodSubtype mCurrentSubtype;
373
satok4e4569d2010-11-19 18:45:53 +0900374 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900375 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700376 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900377
John Spurlocke0980502013-10-25 11:59:29 -0400378 // Was the keyguard locked when this client became current?
379 private boolean mCurClientInKeyguard;
380
satokab751aa2010-09-14 19:17:36 +0900381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * Set to true if our ServiceConnection is currently actively bound to
383 * a service (whether or not we have gotten its IBinder back yet).
384 */
385 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Set if the client has asked for the input method to be shown.
389 */
390 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * Set if we were explicitly told to show the input method.
394 */
395 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 /**
398 * Set if we were forced to be shown.
399 */
400 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * Set if we last told the input method to show itself.
404 */
405 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 /**
408 * The Intent used to connect to the current input method.
409 */
410 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
413 * The token we have made for the currently active input method, to
414 * identify it in the future.
415 */
416 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
419 * If non-null, this is the input method service we are currently connected
420 * to.
421 */
422 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * Time that we last initiated a bind to the input method, to determine
426 * if we should try to disconnect and reconnect to it.
427 */
428 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * Have we called mCurMethod.bindInput()?
432 */
433 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
436 * Currently enabled session. Only touched by service thread, not
437 * protected by a lock.
438 */
439 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700442 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700444 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900446 int mCurUserActionNotificationSequenceNumber = 0;
447
Joe Onorato857fd9b2011-01-27 15:08:35 -0800448 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900449
450 /**
451 * A set of status bits regarding the active IME.
452 *
453 * <p>This value is a combination of following two bits:</p>
454 * <dl>
455 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
456 * <dd>
457 * If this bit is ON, connected IME is ready to accept touch/key events.
458 * </dd>
459 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
460 * <dd>
461 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
462 * </dd>
463 * </dl>
464 * <em>Do not update this value outside of setImeWindowStatus.</em>
465 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800466 int mImeWindowVis;
467
Ken Wakasa05dbb652011-08-22 15:22:43 +0900468 private AlertDialog.Builder mDialogBuilder;
469 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900470 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700471 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900472 private InputMethodInfo[] mIms;
473 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800474 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700475 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800476 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900477 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
478 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500479 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700480 @HardKeyboardBehavior
481 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700484 int mUserId;
485 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700486 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800487 String mLastEnabled = "";
488
Yohei Yukawa81482972015-06-04 00:58:59 -0700489 /**
490 * <em>This constructor must be called within the lock.</em>
491 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 SettingsObserver(Handler handler) {
493 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700494 }
495
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800496 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700497 if (mRegistered && mUserId == userId) {
498 return;
499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700501 if (mRegistered) {
502 mContext.getContentResolver().unregisterContentObserver(this);
503 mRegistered = false;
504 }
505 if (mUserId != userId) {
506 mLastEnabled = "";
507 mUserId = userId;
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700510 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900511 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700512 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900513 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700514 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700515 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700516 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800517 resolver.registerContentObserver(Settings.Secure.getUriFor(
518 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700519 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800521
Michael Wright7b5a96b2014-08-09 19:28:42 -0700522 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800523 final Uri showImeUri = Settings.Secure.getUriFor(
524 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
525 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
526 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700528 if (showImeUri.equals(uri)) {
529 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800530 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
531 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
532 mContext.getContentResolver(),
533 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
534 0, mUserId) == 1;
535 if (mAccessibilityRequestingNoSoftKeyboard) {
536 final boolean showRequested = mShowRequested;
537 hideCurrentInputLocked(0, null);
538 mShowRequested = showRequested;
539 } else if (mShowRequested) {
540 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
541 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700542 } else {
543 boolean enabledChanged = false;
544 String newEnabled = mSettings.getEnabledInputMethodsStr();
545 if (!mLastEnabled.equals(newEnabled)) {
546 mLastEnabled = newEnabled;
547 enabledChanged = true;
548 }
549 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700553
554 @Override
555 public String toString() {
556 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
557 + " mLastEnabled=" + mLastEnabled + "}";
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900561 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900562 @Override
563 public void onReceive(Context context, Intent intent) {
564 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700565 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900566 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700567 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900568 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800569 } else if (Intent.ACTION_USER_ADDED.equals(action)
570 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100571 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800572 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700573 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
574 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
575 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
576 final String prevValue = intent.getStringExtra(
577 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
578 final String newValue = intent.getStringExtra(
579 Intent.EXTRA_SETTING_NEW_VALUE);
580 restoreEnabledInputMethods(mContext, prevValue, newValue);
581 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900582 } else {
583 Slog.w(TAG, "Unexpected intent " + intent);
584 }
585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800587
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700588 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
589 // does not attempt to validate on the fly with any installed device policy, so must only
590 // be run in the context of initial device setup.
591 //
592 // TODO: Move this method to InputMethodUtils with adding unit tests.
593 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
594 if (DEBUG_RESTORE) {
595 Slog.i(TAG, "Restoring enabled input methods:");
596 Slog.i(TAG, "prev=" + prevValue);
597 Slog.i(TAG, " new=" + newValue);
598 }
599 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900600 ArrayMap<String, ArraySet<String>> prevMap =
601 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
602 ArrayMap<String, ArraySet<String>> newMap =
603 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700604
605 // Merge the restored ime+subtype enabled states into the live state
606 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
607 final String imeId = entry.getKey();
608 ArraySet<String> prevSubtypes = prevMap.get(imeId);
609 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700610 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700611 prevMap.put(imeId, prevSubtypes);
612 }
613 prevSubtypes.addAll(entry.getValue());
614 }
615
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700616 final String mergedImesAndSubtypesString =
617 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700618 if (DEBUG_RESTORE) {
619 Slog.i(TAG, "Merged IME string:");
620 Slog.i(TAG, " " + mergedImesAndSubtypesString);
621 }
622 Settings.Secure.putString(context.getContentResolver(),
623 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
624 }
625
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800626 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900627 private boolean isChangingPackagesOfCurrentUser() {
628 final int userId = getChangingUserId();
629 final boolean retval = userId == mSettings.getCurrentUserId();
630 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900631 if (!retval) {
632 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
633 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900634 }
635 return retval;
636 }
637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800639 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900640 if (!isChangingPackagesOfCurrentUser()) {
641 return false;
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900644 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 final int N = mMethodList.size();
646 if (curInputMethodId != null) {
647 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800648 InputMethodInfo imi = mMethodList.get(i);
649 if (imi.getId().equals(curInputMethodId)) {
650 for (String pkg : packages) {
651 if (imi.getPackageName().equals(pkg)) {
652 if (!doit) {
653 return true;
654 }
satok723a27e2010-11-11 14:58:11 +0900655 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 chooseNewDefaultIMELocked();
657 return true;
658 }
659 }
660 }
661 }
662 }
663 }
664 return false;
665 }
666
667 @Override
668 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900669 if (!isChangingPackagesOfCurrentUser()) {
670 return;
671 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 synchronized (mMethodMap) {
673 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900674 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800675 final int N = mMethodList.size();
676 if (curInputMethodId != null) {
677 for (int i=0; i<N; i++) {
678 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900679 final String imiId = imi.getId();
680 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 curIm = imi;
682 }
satoke7c6998e2011-06-03 17:57:59 +0900683
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800684 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900685 if (isPackageModified(imi.getPackageName())) {
686 mFileManager.deleteAllInputMethodSubtypes(imiId);
687 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800688 if (change == PACKAGE_TEMPORARY_CHANGE
689 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800690 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800691 + imi.getComponent());
692 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694 }
695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800696
Yohei Yukawa94e33302016-02-12 19:37:03 -0800697 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800701 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800702 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800703 if (change == PACKAGE_TEMPORARY_CHANGE
704 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800705 ServiceInfo si = null;
706 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900707 si = mIPackageManager.getServiceInfo(
708 curIm.getComponent(), 0, mSettings.getCurrentUserId());
709 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800710 }
711 if (si == null) {
712 // Uh oh, current input method is no longer around!
713 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800714 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900715 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800716 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800717 changed = true;
718 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800719 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900720 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800721 }
722 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800723 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800724 }
satokab751aa2010-09-14 19:17:36 +0900725
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800726 if (curIm == null) {
727 // We currently don't have a default input method... is
728 // one now available?
729 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700730 } else if (!changed && isPackageModified(curIm.getPackageName())) {
731 // Even if the current input method is still available, mCurrentSubtype could
732 // be obsolete when the package is modified in practice.
733 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800736 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800737 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739 }
740 }
741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800742
Jeff Brownc28867a2013-03-26 15:42:39 -0700743 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900744 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700745 private final IInputMethod mMethod;
746 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800747
Jeff Brownc28867a2013-03-26 15:42:39 -0700748 MethodCallback(InputMethodManagerService imms, IInputMethod method,
749 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900750 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700751 mMethod = method;
752 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754
satoke7c6998e2011-06-03 17:57:59 +0900755 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700756 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700757 long ident = Binder.clearCallingIdentity();
758 try {
759 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
760 } finally {
761 Binder.restoreCallingIdentity(ident);
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800765
satok01038492012-04-09 21:08:27 +0900766 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700767 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900768 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700769 public void onHardKeyboardStatusChange(boolean available) {
770 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
771 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900772 }
773
Michael Wright7b5a96b2014-08-09 19:28:42 -0700774 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900775 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700776 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900777 }
778 synchronized(mMethodMap) {
779 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
780 && mSwitchingDialog.isShowing()) {
781 mSwitchingDialogTitleView.findViewById(
782 com.android.internal.R.id.hard_keyboard_section).setVisibility(
783 available ? View.VISIBLE : View.GONE);
784 }
785 }
786 }
787 }
788
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800789 public static final class Lifecycle extends SystemService {
790 private InputMethodManagerService mService;
791
792 public Lifecycle(Context context) {
793 super(context);
794 mService = new InputMethodManagerService(context);
795 }
796
797 @Override
798 public void onStart() {
799 LocalServices.addService(InputMethodManagerInternal.class,
800 new LocalServiceImpl(mService.mHandler));
801 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
802 }
803
804 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800805 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700806 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800807 // TODO: Dispatch this to a worker thread as needed.
808 mService.onSwitchUser(userHandle);
809 }
810
811 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800812 public void onBootPhase(int phase) {
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.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800815 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
816 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
817 .getService(Context.STATUS_BAR_SERVICE);
818 mService.systemRunning(statusBarService);
819 }
820 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800821
822 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700823 public void onUnlockUser(final @UserIdInt int userHandle) {
824 // Called on ActivityManager thread.
825 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700826 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800827 }
828 }
829
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800830 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800831 synchronized(mMethodMap) {
832 final int currentUserId = mSettings.getCurrentUserId();
833 if (DEBUG) {
834 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
835 }
836 if (userId != currentUserId) {
837 return;
838 }
839 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
840 // We need to rebuild IMEs.
841 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
842 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
843 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800844 }
845
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800846 void onSwitchUser(@UserIdInt int userId) {
847 synchronized (mMethodMap) {
848 switchUserLocked(userId);
849 }
850 }
851
Seigo Nonaka7309b122015-08-17 18:34:13 -0700852 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900853 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800855 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700857 // Note: SettingsObserver doesn't register observers in its constructor.
858 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 mIWindowManager = IWindowManager.Stub.asInterface(
860 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700861 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800862 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900863 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 public void executeMessage(Message msg) {
865 handleMessage(msg);
866 }
Mita Yuned218c72012-12-06 17:18:25 -0800867 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800868 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800869 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900870 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700871 mHasFeature = context.getPackageManager().hasSystemFeature(
872 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500873 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700874 mHardKeyboardBehavior = mContext.getResources().getInteger(
875 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900876
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400877 Bundle extras = new Bundle();
878 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
879 mImeSwitcherNotification = new Notification.Builder(mContext)
880 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
881 .setWhen(0)
882 .setOngoing(true)
883 .addExtras(extras)
884 .setCategory(Notification.CATEGORY_SYSTEM)
885 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400886
satok7cfc0ed2011-06-20 21:29:36 +0900887 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900888 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900889
890 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900891
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900893 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800894 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
895 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700896 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
satok7cfc0ed2011-06-20 21:29:36 +0900899 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900900 int userId = 0;
901 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900902 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
903 } catch (RemoteException e) {
904 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
905 }
satok81f8b7c2012-12-04 20:42:56 +0900906 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900907
satokd87c2592010-09-29 11:52:06 +0900908 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900909 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800910 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700911
Kenny Guy2a764942014-04-02 13:29:20 +0100912 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900913 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900914 synchronized (mMethodMap) {
915 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
916 mSettings, context);
917 }
satok0a1bcf42012-05-16 19:26:31 +0900918
919 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900920 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900921 if (DEBUG) {
922 Slog.d(TAG, "Initial default ime = " + defaultImiId);
923 }
satok0a1bcf42012-05-16 19:26:31 +0900924 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
925
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900926 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800927 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900928 }
satokd87c2592010-09-29 11:52:06 +0900929 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930
satok0a1bcf42012-05-16 19:26:31 +0900931 if (!mImeSelectedOnBoot) {
932 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900933 synchronized (mMethodMap) {
934 resetDefaultImeLocked(context);
935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800937
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900938 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700939 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900940 updateFromSettingsLocked(true);
941 }
satok5b927c432012-05-01 20:09:34 +0900942
943 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
944 // according to the new system locale.
945 final IntentFilter filter = new IntentFilter();
946 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
947 mContext.registerReceiver(
948 new BroadcastReceiver() {
949 @Override
950 public void onReceive(Context context, Intent intent) {
951 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900952 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900953 }
954 }
955 }, filter);
956 }
957
satok5b927c432012-05-01 20:09:34 +0900958 private void resetDefaultImeLocked(Context context) {
959 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800960 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900961 return;
962 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800963 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
964 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
965 if (suitableImes.isEmpty()) {
966 Slog.i(TAG, "No default found");
967 return;
satok5b927c432012-05-01 20:09:34 +0900968 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800969 final InputMethodInfo defIm = suitableImes.get(0);
970 Slog.i(TAG, "Default found, using " + defIm.getId());
971 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900972 }
973
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900974 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
975 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900976 if (!mSystemReady) {
977 // not system ready
978 return;
979 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800980 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900981 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800982 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 if (!updateOnlyWhenLocaleChanged) {
984 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800985 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 }
987 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800988 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800990 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 if (!updateOnlyWhenLocaleChanged) {
992 final String selectedImiId = mSettings.getSelectedInputMethod();
993 if (TextUtils.isEmpty(selectedImiId)) {
994 // This is the first time of the user switch and
995 // set the current ime to the proper one.
996 resetDefaultImeLocked(mContext);
997 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900998 } else {
999 // If the locale is changed, needs to reset the default ime
1000 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001001 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001002 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001003 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 if (!updateOnlyWhenLocaleChanged) {
1005 try {
1006 startInputInnerLocked();
1007 } catch (RuntimeException e) {
1008 Slog.w(TAG, "Unexpected exception", e);
1009 }
1010 }
1011 }
1012 }
1013
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001014 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001015 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1016 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001017 }
1018
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001019 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001020 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1021 + " currentUserId=" + mSettings.getCurrentUserId());
1022
Yohei Yukawa81482972015-06-04 00:58:59 -07001023 // ContentObserver should be registered again when the user is changed
1024 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001025
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001026 // If the system is not ready or the device is not yed unlocked by the user, then we use
1027 // copy-on-write settings.
1028 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001029 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001030 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001031 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001032 // InputMethodFileManager should be reset when the user is changed
1033 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001034 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001035
1036 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1037 + " defaultImiId=" + defaultImiId);
1038
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001039 // For secondary users, the list of enabled IMEs may not have been updated since the
1040 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1041 // not be empty even if the IME has been uninstalled by the primary user.
1042 // Even in such cases, IMMS works fine because it will find the most applicable
1043 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001044 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001045 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001046 initialUserSwitch /* needsToResetDefaultIme */);
1047 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001048 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1049 mSettings.getEnabledInputMethodListLocked(), newUserId,
1050 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001051 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001052
1053 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1054 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001055 }
1056
Kenny Guy2a764942014-04-02 13:29:20 +01001057 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001058 mSettings.setCurrentProfileIds(
1059 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001060 }
1061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 @Override
1063 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1064 throws RemoteException {
1065 try {
1066 return super.onTransact(code, data, reply, flags);
1067 } catch (RuntimeException e) {
1068 // The input method manager only throws security exceptions, so let's
1069 // log all others.
1070 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001071 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 throw e;
1074 }
1075 }
1076
Svetoslav Ganova0027152013-06-25 14:59:53 -07001077 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001078 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001079 if (DEBUG) {
1080 Slog.d(TAG, "--- systemReady");
1081 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001082 if (!mSystemReady) {
1083 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001084 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001085 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001086 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001087 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1088 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001089 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001090 if (mStatusBar != null) {
1091 mStatusBar.setIconVisibility(mSlotIme, false);
1092 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001093 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001094 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1095 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001096 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001097 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001098 mHardKeyboardListener);
1099 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001100 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001101 if (!mImeSelectedOnBoot) {
1102 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001103 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001104 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1105 mSettings.getEnabledInputMethodListLocked(),
1106 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001107 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001108 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001109 try {
1110 startInputInnerLocked();
1111 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001112 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001113 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001114 }
1115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001117
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001118 // ---------------------------------------------------------------------------------------
1119 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1120 // 1) it comes from the system process
1121 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1122 private boolean calledFromValidUser() {
1123 final int uid = Binder.getCallingUid();
1124 final int userId = UserHandle.getUserId(uid);
1125 if (DEBUG) {
1126 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1127 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1128 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001129 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1130 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001131 }
Kenny Guy2a764942014-04-02 13:29:20 +01001132 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001133 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001134 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001135
1136 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1137 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1138 // must not manage background users' states in any functions.
1139 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1140 // by a token.
1141 if (mContext.checkCallingOrSelfPermission(
1142 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1143 == PackageManager.PERMISSION_GRANTED) {
1144 if (DEBUG) {
1145 Slog.d(TAG, "--- Access granted because the calling process has "
1146 + "the INTERACT_ACROSS_USERS_FULL permission");
1147 }
1148 return true;
1149 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001150 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1151 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001152 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001153 }
1154
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001155
1156 /**
1157 * Returns true iff the caller is identified to be the current input method with the token.
1158 * @param token The window token given to the input method when it was started.
1159 * @return true if and only if non-null valid token is specified.
1160 */
1161 private boolean calledWithValidToken(IBinder token) {
1162 if (token == null || mCurToken != token) {
1163 return false;
1164 }
1165 return true;
1166 }
1167
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001168 private boolean bindCurrentInputMethodService(
1169 Intent service, ServiceConnection conn, int flags) {
1170 if (service == null || conn == null) {
1171 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1172 return false;
1173 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001174 return mContext.bindServiceAsUser(service, conn, flags,
1175 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001176 }
1177
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 // TODO: Make this work even for non-current users?
1181 if (!calledFromValidUser()) {
1182 return Collections.emptyList();
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001185 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 }
1188
satoke7c6998e2011-06-03 17:57:59 +09001189 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 // TODO: Make this work even for non-current users?
1192 if (!calledFromValidUser()) {
1193 return Collections.emptyList();
1194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001196 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198 }
1199
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001200 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001201 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001202 * @return enabled subtypes of the specified imi
1203 */
satoke7c6998e2011-06-03 17:57:59 +09001204 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001205 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001206 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 // TODO: Make this work even for non-current users?
1208 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001209 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001210 }
satok67ddf9c2010-11-17 09:45:54 +09001211 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001212 final InputMethodInfo imi;
1213 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001214 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001215 } else {
1216 imi = mMethodMap.get(imiId);
1217 }
1218 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001219 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001220 }
1221 return mSettings.getEnabledInputMethodSubtypeListLocked(
1222 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001223 }
1224 }
1225
satoke7c6998e2011-06-03 17:57:59 +09001226 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 public void addClient(IInputMethodClient client,
1228 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001229 if (!calledFromValidUser()) {
1230 return;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 synchronized (mMethodMap) {
1233 mClients.put(client.asBinder(), new ClientState(client,
1234 inputContext, uid, pid));
1235 }
1236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001237
satoke7c6998e2011-06-03 17:57:59 +09001238 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001240 if (!calledFromValidUser()) {
1241 return;
1242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001244 ClientState cs = mClients.remove(client.asBinder());
1245 if (cs != null) {
1246 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001247 if (mCurClient == cs) {
1248 mCurClient = null;
1249 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001250 if (mCurFocusedWindowClient == cs) {
1251 mCurFocusedWindowClient = null;
1252 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 void executeOrSendMessage(IInterface target, Message msg) {
1258 if (target.asBinder() instanceof Binder) {
1259 mCaller.sendMessage(msg);
1260 } else {
1261 handleMessage(msg);
1262 msg.recycle();
1263 }
1264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
Yohei Yukawa33e81792015-11-17 21:14:42 -08001266 void unbindCurrentClientLocked(
1267 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001269 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 + mCurClient.client.asBinder());
1271 if (mBoundToMethod) {
1272 mBoundToMethod = false;
1273 if (mCurMethod != null) {
1274 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1275 MSG_UNBIND_INPUT, mCurMethod));
1276 }
1277 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001278
1279 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1280 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001281 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1282 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
The Android Open Source Project10592532009-03-18 17:39:46 -07001286 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
1288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 private int getImeShowFlags() {
1291 int flags = 0;
1292 if (mShowForced) {
1293 flags |= InputMethod.SHOW_FORCED
1294 | InputMethod.SHOW_EXPLICIT;
1295 } else if (mShowExplicitlyRequested) {
1296 flags |= InputMethod.SHOW_EXPLICIT;
1297 }
1298 return flags;
1299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 private int getAppShowFlags() {
1302 int flags = 0;
1303 if (mShowForced) {
1304 flags |= InputMethodManager.SHOW_FORCED;
1305 } else if (!mShowExplicitlyRequested) {
1306 flags |= InputMethodManager.SHOW_IMPLICIT;
1307 }
1308 return flags;
1309 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001310
Dianne Hackborn7663d802012-02-24 13:08:49 -08001311 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 if (!mBoundToMethod) {
1313 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1314 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1315 mBoundToMethod = true;
1316 }
1317 final SessionState session = mCurClient.curSession;
1318 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001319 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1320 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1321 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001323 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1324 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1325 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001329 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001331 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001332 (session.channel != null ? session.channel.dup() : null),
1333 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001336 InputBindResult startInputLocked(
1337 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001338 IInputMethodClient client, IInputContext inputContext,
1339 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001340 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 // If no method is currently selected, do nothing.
1342 if (mCurMethodId == null) {
1343 return mNoBinding;
1344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 ClientState cs = mClients.get(client.asBinder());
1347 if (cs == null) {
1348 throw new IllegalArgumentException("unknown client "
1349 + client.asBinder());
1350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001351
Yohei Yukawa74750f22016-03-22 12:54:22 -07001352 if (attribute == null) {
1353 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1354 + " uid=" + cs.uid + " pid=" + cs.pid);
1355 return null;
1356 }
1357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 try {
1359 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1360 // Check with the window manager to make sure this client actually
1361 // has a window with focus. If not, reject. This is thread safe
1362 // because if the focus changes some time before or after, the
1363 // next client receiving focus that has any interest in input will
1364 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001365 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1367 return null;
1368 }
1369 } catch (RemoteException e) {
1370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001372 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1373 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001374 }
1375
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001376 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001377 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001378 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001379 // If no method is currently selected, do nothing.
1380 if (mCurMethodId == null) {
1381 return mNoBinding;
1382 }
1383
Yohei Yukawad57ba672015-06-08 16:39:46 -07001384 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1385 attribute.packageName)) {
1386 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1387 + " uid=" + cs.uid + " package=" + attribute.packageName);
1388 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001389 }
1390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001392 // Was the keyguard locked when switching over to the new client?
1393 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 // If the client is changing, we need to switch over to the new
1395 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001396 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001397 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001398 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399
1400 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001401 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001402 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001403 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 // Bump up the sequence for this client and attach it.
1408 mCurSeq++;
1409 if (mCurSeq <= 0) mCurSeq = 1;
1410 mCurClient = cs;
1411 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001412 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 // Check if the input method is changing.
1416 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1417 if (cs.curSession != null) {
1418 // Fast case: if we are already connected to the input method,
1419 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001420 return attachNewInputLocked(
1421 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423 if (mHaveConnection) {
1424 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 // Return to client, and we will get back with it when
1426 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001427 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001428 return new InputBindResult(null, null, mCurId, mCurSeq,
1429 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 } else if (SystemClock.uptimeMillis()
1431 < (mLastBindTime+TIME_TO_RECONNECT)) {
1432 // In this case we have connected to the service, but
1433 // don't yet have its interface. If it hasn't been too
1434 // long since we did the connection, we'll return to
1435 // the client and wait to get the service interface so
1436 // we can report back. If it has been too long, we want
1437 // to fall through so we can try a disconnect/reconnect
1438 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001439 return new InputBindResult(null, null, mCurId, mCurSeq,
1440 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1443 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
1445 }
1446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001447
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001448 return startInputInnerLocked();
1449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001451 InputBindResult startInputInnerLocked() {
1452 if (mCurMethodId == null) {
1453 return mNoBinding;
1454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001456 if (!mSystemReady) {
1457 // If the system is not yet ready, we shouldn't be running third
1458 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001459 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1460 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1464 if (info == null) {
1465 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1466 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001467
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001468 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1471 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001472 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1473 com.android.internal.R.string.input_method_binding_label);
1474 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1475 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001476 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001477 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1478 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 mLastBindTime = SystemClock.uptimeMillis();
1480 mHaveConnection = true;
1481 mCurId = info.getId();
1482 mCurToken = new Binder();
1483 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001484 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 mIWindowManager.addWindowToken(mCurToken,
1486 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1487 } catch (RemoteException e) {
1488 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001489 return new InputBindResult(null, null, mCurId, mCurSeq,
1490 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 } else {
1492 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001493 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 + mCurIntent);
1495 }
1496 return null;
1497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001499 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001500 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001501 IInputMethodClient client, IInputContext inputContext,
1502 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001503 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001504 if (!calledFromValidUser()) {
1505 return null;
1506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001508 if (DEBUG) {
1509 Slog.v(TAG, "startInput: reason="
1510 + InputMethodClient.getStartInputReason(startInputReason)
1511 + " client = " + client.asBinder()
1512 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001513 + " missingMethods="
1514 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001515 + " attribute=" + attribute
1516 + " controlFlags=#" + Integer.toHexString(controlFlags));
1517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 final long ident = Binder.clearCallingIdentity();
1519 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001520 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1521 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 } finally {
1523 Binder.restoreCallingIdentity(ident);
1524 }
1525 }
1526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001527
satoke7c6998e2011-06-03 17:57:59 +09001528 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 public void finishInput(IInputMethodClient client) {
1530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
satoke7c6998e2011-06-03 17:57:59 +09001532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 public void onServiceConnected(ComponentName name, IBinder service) {
1534 synchronized (mMethodMap) {
1535 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1536 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001537 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001538 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001539 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001540 return;
1541 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001542 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001543 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1544 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001546 clearClientSessionLocked(mCurClient);
1547 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
1549 }
1550 }
1551 }
1552
Jeff Brownc28867a2013-03-26 15:42:39 -07001553 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1554 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 synchronized (mMethodMap) {
1556 if (mCurMethod != null && method != null
1557 && mCurMethod.asBinder() == method.asBinder()) {
1558 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001559 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001561 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001562 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 if (res.method != null) {
1564 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001565 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001567 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569 }
1570 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001571
1572 // Session abandoned. Close its associated input channel.
1573 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001576 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001577 if (mVisibleBound) {
1578 mContext.unbindService(mVisibleConnection);
1579 mVisibleBound = false;
1580 }
1581
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001582 if (mHaveConnection) {
1583 mContext.unbindService(this);
1584 mHaveConnection = false;
1585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001586
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001587 if (mCurToken != null) {
1588 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001589 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001590 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001591 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001592 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001593 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001594 mIWindowManager.removeWindowToken(mCurToken);
1595 } catch (RemoteException e) {
1596 }
1597 mCurToken = null;
1598 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001599
The Android Open Source Project10592532009-03-18 17:39:46 -07001600 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001601 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001603
Yohei Yukawa33e81792015-11-17 21:14:42 -08001604 void resetCurrentMethodAndClient(
1605 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001606 mCurMethodId = null;
1607 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001608 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001609 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001610
1611 void requestClientSessionLocked(ClientState cs) {
1612 if (!cs.sessionRequested) {
1613 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1614 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1615 cs.sessionRequested = true;
1616 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1617 MSG_CREATE_SESSION, mCurMethod, channels[1],
1618 new MethodCallback(this, mCurMethod, channels[0])));
1619 }
1620 }
1621
1622 void clearClientSessionLocked(ClientState cs) {
1623 finishSessionLocked(cs.curSession);
1624 cs.curSession = null;
1625 cs.sessionRequested = false;
1626 }
1627
1628 private void finishSessionLocked(SessionState sessionState) {
1629 if (sessionState != null) {
1630 if (sessionState.session != null) {
1631 try {
1632 sessionState.session.finishSession();
1633 } catch (RemoteException e) {
1634 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001635 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001636 }
1637 sessionState.session = null;
1638 }
1639 if (sessionState.channel != null) {
1640 sessionState.channel.dispose();
1641 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001642 }
1643 }
1644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001646 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 if (mCurMethod != null) {
1648 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001649 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001651
Jeff Brownc28867a2013-03-26 15:42:39 -07001652 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001653 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 mCurMethod = null;
1655 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001656 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001657 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001660
satoke7c6998e2011-06-03 17:57:59 +09001661 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 public void onServiceDisconnected(ComponentName name) {
1663 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001664 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 + " mCurIntent=" + mCurIntent);
1666 if (mCurMethod != null && mCurIntent != null
1667 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001668 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 // We consider this to be a new bind attempt, since the system
1670 // should now try to restart the service for us.
1671 mLastBindTime = SystemClock.uptimeMillis();
1672 mShowRequested = mInputShown;
1673 mInputShown = false;
1674 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001675 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1676 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1677 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
1679 }
1680 }
1681 }
1682
satokf9f01002011-05-19 21:31:50 +09001683 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1685 long ident = Binder.clearCallingIdentity();
1686 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001688 if (!calledWithValidToken(token)) {
1689 final int uid = Binder.getCallingUid();
1690 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1691 + " token:" + token);
1692 return;
1693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001695 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001696 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001697 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001701 CharSequence contentDescription = null;
1702 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001703 // Use PackageManager to load label
1704 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001705 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001706 mIPackageManager.getApplicationInfo(packageName, 0,
1707 mSettings.getCurrentUserId()));
1708 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001709 /* ignore */
1710 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001711 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001712 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001713 contentDescription != null
1714 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001715 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
1718 }
1719 } finally {
1720 Binder.restoreCallingIdentity(ident);
1721 }
1722 }
1723
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001724 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001725 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001726 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001727 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001728 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001729 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001730 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1731 // When physical keyboard is attached, we show the ime switcher (or notification if
1732 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1733 // exists in the IME switcher dialog. Might be OK to remove this condition once
1734 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1735 return true;
1736 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001737 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1738 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001739 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001740
1741 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1742 final int N = imis.size();
1743 if (N > 2) return true;
1744 if (N < 1) return false;
1745 int nonAuxCount = 0;
1746 int auxCount = 0;
1747 InputMethodSubtype nonAuxSubtype = null;
1748 InputMethodSubtype auxSubtype = null;
1749 for(int i = 0; i < N; ++i) {
1750 final InputMethodInfo imi = imis.get(i);
1751 final List<InputMethodSubtype> subtypes =
1752 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1753 final int subtypeCount = subtypes.size();
1754 if (subtypeCount == 0) {
1755 ++nonAuxCount;
1756 } else {
1757 for (int j = 0; j < subtypeCount; ++j) {
1758 final InputMethodSubtype subtype = subtypes.get(j);
1759 if (!subtype.isAuxiliary()) {
1760 ++nonAuxCount;
1761 nonAuxSubtype = subtype;
1762 } else {
1763 ++auxCount;
1764 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001765 }
1766 }
satok7cfc0ed2011-06-20 21:29:36 +09001767 }
1768 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001769 if (nonAuxCount > 1 || auxCount > 1) {
1770 return true;
1771 } else if (nonAuxCount == 1 && auxCount == 1) {
1772 if (nonAuxSubtype != null && auxSubtype != null
1773 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1774 || auxSubtype.overridesImplicitlyEnabledSubtype()
1775 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1776 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1777 return false;
1778 }
1779 return true;
1780 }
1781 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001782 }
1783
John Spurlocke0980502013-10-25 11:59:29 -04001784 private boolean isKeyguardLocked() {
1785 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1786 }
1787
satokdbf29502011-08-25 15:28:23 +09001788 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001789 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001790 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001791 if (!calledWithValidToken(token)) {
1792 final int uid = Binder.getCallingUid();
1793 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1794 + " token:" + token);
1795 return;
1796 }
1797
1798 synchronized (mMethodMap) {
1799 mImeWindowVis = vis;
1800 mBackDisposition = backDisposition;
1801 updateSystemUiLocked(token, vis, backDisposition);
1802 }
1803 }
1804
1805 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1806 synchronized (mMethodMap) {
1807 updateSystemUiLocked(token, vis, backDisposition);
1808 }
1809 }
1810
1811 // Caution! This method is called in this class. Handle multi-user carefully
1812 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1813 if (!calledWithValidToken(token)) {
1814 final int uid = Binder.getCallingUid();
1815 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1816 + " token:" + token);
1817 return;
1818 }
1819
1820 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1821 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001822 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001823 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001824 // apply policy for binder calls
1825 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1826 vis = 0;
satok06487a52010-10-29 11:37:18 +09001827 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001828 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1829 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1830 if (mStatusBar != null) {
1831 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1832 needsToShowImeSwitcher);
1833 }
1834 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1835 if (imi != null && needsToShowImeSwitcher) {
1836 // Used to load label
1837 final CharSequence title = mRes.getText(
1838 com.android.internal.R.string.select_input_method);
1839 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1840 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001841 mImeSwitcherNotification.setContentTitle(title)
1842 .setContentText(summary)
1843 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001844 try {
1845 if ((mNotificationManager != null)
1846 && !mIWindowManager.hasNavigationBar()) {
1847 if (DEBUG) {
1848 Slog.d(TAG, "--- show notification: label = " + summary);
1849 }
1850 mNotificationManager.notifyAsUser(null,
1851 com.android.internal.R.string.select_input_method,
1852 mImeSwitcherNotification.build(), UserHandle.ALL);
1853 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001854 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001855 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001856 }
1857 } else {
1858 if (mNotificationShown && mNotificationManager != null) {
1859 if (DEBUG) {
1860 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001861 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001862 mNotificationManager.cancelAsUser(null,
1863 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1864 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001865 }
satok06487a52010-10-29 11:37:18 +09001866 }
1867 } finally {
1868 Binder.restoreCallingIdentity(ident);
1869 }
1870 }
1871
satoke7c6998e2011-06-03 17:57:59 +09001872 @Override
satokf9f01002011-05-19 21:31:50 +09001873 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001874 if (!calledFromValidUser()) {
1875 return;
1876 }
satokf9f01002011-05-19 21:31:50 +09001877 synchronized (mMethodMap) {
1878 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1879 for (int i = 0; i < spans.length; ++i) {
1880 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001881 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001882 mSecureSuggestionSpans.put(ss, currentImi);
1883 }
1884 }
1885 }
1886 }
1887
satoke7c6998e2011-06-03 17:57:59 +09001888 @Override
satokf9f01002011-05-19 21:31:50 +09001889 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001890 if (!calledFromValidUser()) {
1891 return false;
1892 }
satokf9f01002011-05-19 21:31:50 +09001893 synchronized (mMethodMap) {
1894 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1895 // TODO: Do not send the intent if the process of the targetImi is already dead.
1896 if (targetImi != null) {
1897 final String[] suggestions = span.getSuggestions();
1898 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001899 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001900 final Intent intent = new Intent();
1901 // Ensures that only a class in the original IME package will receive the
1902 // notification.
satok42c5a162011-05-26 16:46:14 +09001903 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001904 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1905 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1906 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1907 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001908 final long ident = Binder.clearCallingIdentity();
1909 try {
1910 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1911 } finally {
1912 Binder.restoreCallingIdentity(ident);
1913 }
satokf9f01002011-05-19 21:31:50 +09001914 return true;
1915 }
1916 }
1917 return false;
1918 }
1919
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001920 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001921 updateInputMethodsFromSettingsLocked(enabledMayChange);
1922 updateKeyboardFromSettingsLocked();
1923 }
1924
1925 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001926 if (enabledMayChange) {
1927 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1928 for (int i=0; i<enabled.size(); i++) {
1929 // We allow the user to select "disabled until used" apps, so if they
1930 // are enabling one of those here we now need to make it enabled.
1931 InputMethodInfo imm = enabled.get(i);
1932 try {
1933 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1934 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1935 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001936 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001937 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001938 if (DEBUG) {
1939 Slog.d(TAG, "Update state(" + imm.getId()
1940 + "): DISABLED_UNTIL_USED -> DEFAULT");
1941 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001942 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1943 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001944 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1945 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001946 }
1947 } catch (RemoteException e) {
1948 }
1949 }
1950 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001951 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1952 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1953 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1954 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001955 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001956 // There is no input method selected, try to choose new applicable input method.
1957 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001958 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001959 }
1960 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001962 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001964 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001965 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
satokf3db1af2010-11-23 13:34:33 +09001967 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001968 } else {
1969 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001970 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001972 // Here is not the perfect place to reset the switching controller. Ideally
1973 // mSwitchingController and mSettings should be able to share the same state.
1974 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1975 // the same enabled IMEs list.
1976 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001977
1978 }
1979
1980 public void updateKeyboardFromSettingsLocked() {
1981 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1982 if (mSwitchingDialog != null
1983 && mSwitchingDialogTitleView != null
1984 && mSwitchingDialog.isShowing()) {
1985 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1986 com.android.internal.R.id.hard_keyboard_switch);
1987 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001990
Yohei Yukawab097b822015-12-01 10:43:08 -08001991 private void notifyInputMethodSubtypeChanged(final int userId,
1992 @Nullable final InputMethodInfo inputMethodInfo,
1993 @Nullable final InputMethodSubtype subtype) {
1994 final InputManagerInternal inputManagerInternal =
1995 LocalServices.getService(InputManagerInternal.class);
1996 if (inputManagerInternal != null) {
1997 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1998 }
1999 }
2000
satokab751aa2010-09-14 19:17:36 +09002001 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 InputMethodInfo info = mMethodMap.get(id);
2003 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002004 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
satokd81e9502012-05-21 12:58:45 +09002007 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002009 final int subtypeCount = info.getSubtypeCount();
2010 if (subtypeCount <= 0) {
2011 return;
satokcd7cd292010-11-20 15:46:23 +09002012 }
satokd81e9502012-05-21 12:58:45 +09002013 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2014 final InputMethodSubtype newSubtype;
2015 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2016 newSubtype = info.getSubtypeAt(subtypeId);
2017 } else {
2018 // If subtype is null, try to find the most applicable one from
2019 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002021 }
2022 if (newSubtype == null || oldSubtype == null) {
2023 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2024 + ", new subtype = " + newSubtype);
2025 return;
2026 }
2027 if (newSubtype != oldSubtype) {
2028 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2029 if (mCurMethod != null) {
2030 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002031 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002032 mCurMethod.changeInputMethodSubtype(newSubtype);
2033 } catch (RemoteException e) {
2034 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002035 return;
satokab751aa2010-09-14 19:17:36 +09002036 }
2037 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002038 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 return;
2041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002042
satokd81e9502012-05-21 12:58:45 +09002043 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 final long ident = Binder.clearCallingIdentity();
2045 try {
satokab751aa2010-09-14 19:17:36 +09002046 // Set a subtype to this input method.
2047 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002048 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2049 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2050 // because mCurMethodId is stored as a history in
2051 // setSelectedInputMethodAndSubtypeLocked().
2052 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053
2054 if (ActivityManagerNative.isSystemReady()) {
2055 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002056 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002058 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002060 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 } finally {
2062 Binder.restoreCallingIdentity(ident);
2063 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002064
2065 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2066 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002068
satok42c5a162011-05-26 16:46:14 +09002069 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002070 public boolean showSoftInput(IInputMethodClient client, int flags,
2071 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002072 if (!calledFromValidUser()) {
2073 return false;
2074 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002075 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 long ident = Binder.clearCallingIdentity();
2077 try {
2078 synchronized (mMethodMap) {
2079 if (mCurClient == null || client == null
2080 || mCurClient.client.asBinder() != client.asBinder()) {
2081 try {
2082 // We need to check if this is the current client with
2083 // focus in the window manager, to allow this call to
2084 // be made before input is started in it.
2085 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002086 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002090 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002093
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002094 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002095 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097 } finally {
2098 Binder.restoreCallingIdentity(ident);
2099 }
2100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002101
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002104 if (mAccessibilityRequestingNoSoftKeyboard) {
2105 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
Anna Galusza9b278112016-01-04 11:37:37 -08002107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2109 mShowExplicitlyRequested = true;
2110 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002111 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2112 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002114
Dianne Hackborncc278702009-09-02 23:07:23 -07002115 if (!mSystemReady) {
2116 return false;
2117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118
The Android Open Source Project4df24232009-03-05 14:34:35 -08002119 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002121 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002122 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2123 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2124 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002126 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002127 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002128 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002129 | Context.BIND_TREAT_LIKE_ACTIVITY
2130 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002131 mVisibleBound = true;
2132 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002133 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002135 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 // The client has asked to have the input method shown, but
2137 // we have been sitting here too long with a connection to the
2138 // service and no interface received, so let's disconnect/connect
2139 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002140 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002142 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002144 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002145 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002146 } else {
2147 if (DEBUG) {
2148 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2149 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002152
The Android Open Source Project4df24232009-03-05 14:34:35 -08002153 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002155
satok42c5a162011-05-26 16:46:14 +09002156 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002157 public boolean hideSoftInput(IInputMethodClient client, int flags,
2158 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002159 if (!calledFromValidUser()) {
2160 return false;
2161 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002162 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 long ident = Binder.clearCallingIdentity();
2164 try {
2165 synchronized (mMethodMap) {
2166 if (mCurClient == null || client == null
2167 || mCurClient.client.asBinder() != client.asBinder()) {
2168 try {
2169 // We need to check if this is the current client with
2170 // focus in the window manager, to allow this call to
2171 // be made before input is started in it.
2172 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002173 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2174 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002175 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002178 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
2180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002181
Joe Onorato8a9b2202010-02-26 18:56:32 -08002182 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185 } finally {
2186 Binder.restoreCallingIdentity(ident);
2187 }
2188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002189
The Android Open Source Project4df24232009-03-05 14:34:35 -08002190 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2192 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002193 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 -08002194 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
2196 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002197 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 -08002198 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002200
2201 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2202 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2203 // to be updated with the new value sent from IME process. Even in such a transient state
2204 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2205 // application process as a valid request, and have even promised such a behavior with CTS
2206 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2207 // IMMS#InputShown indicates that the software keyboard is shown.
2208 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2209 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2210 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002211 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002212 if (shouldHideSoftInput) {
2213 // The IME will report its visible state again after the following message finally
2214 // delivered to the IME process as an IPC. Hence the inconsistency between
2215 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2216 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002217 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2218 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2219 res = true;
2220 } else {
2221 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002223 if (mHaveConnection && mVisibleBound) {
2224 mContext.unbindService(mVisibleConnection);
2225 mVisibleBound = false;
2226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 mInputShown = false;
2228 mShowRequested = false;
2229 mShowExplicitlyRequested = false;
2230 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002231 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002233
satok42c5a162011-05-26 16:46:14 +09002234 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002235 public InputBindResult startInputOrWindowGainedFocus(
2236 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2237 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002238 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002239 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002240 if (windowToken != null) {
2241 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002242 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002243 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002244 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2245 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002246 }
2247 }
2248
2249 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002250 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2251 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002252 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2253 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002254 // Needs to check the validity before clearing calling identity
2255 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002256 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 long ident = Binder.clearCallingIdentity();
2258 try {
2259 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002260 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2261 + InputMethodClient.getStartInputReason(startInputReason)
2262 + " client=" + client.asBinder()
2263 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002264 + " missingMethods="
2265 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002266 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002267 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002269 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002270
Dianne Hackborn7663d802012-02-24 13:08:49 -08002271 ClientState cs = mClients.get(client.asBinder());
2272 if (cs == null) {
2273 throw new IllegalArgumentException("unknown client "
2274 + client.asBinder());
2275 }
2276
2277 try {
2278 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2279 // Check with the window manager to make sure this client actually
2280 // has a window with focus. If not, reject. This is thread safe
2281 // because if the focus changes some time before or after, the
2282 // next client receiving focus that has any interest in input will
2283 // be calling through here after that change happens.
2284 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2285 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2286 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002288 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002290
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002291 if (!calledFromValidUser) {
2292 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2293 Slog.w(TAG, "If you want to interect with IME, you need "
2294 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2295 hideCurrentInputLocked(0, null);
2296 return null;
2297 }
2298
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002299 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002300 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002301 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002302 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002303 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2304 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002305 }
2306 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002307 }
2308 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002309 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002310
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002311 // Should we auto-show the IME even if the caller has not
2312 // specified what should be done with it?
2313 // We only do this automatically if the window can resize
2314 // to accommodate the IME (so what the user sees will give
2315 // them good context without input information being obscured
2316 // by the IME) or if running on a large screen where there
2317 // is more room for the target window + IME.
2318 final boolean doAutoShow =
2319 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2320 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2321 || mRes.getConfiguration().isLayoutSizeAtLeast(
2322 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002323 final boolean isTextEditor =
2324 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2325
2326 // We want to start input before showing the IME, but after closing
2327 // it. We want to do this after closing it to help the IME disappear
2328 // more quickly (not get stuck behind it initializing itself for the
2329 // new focused input, even if its window wants to hide the IME).
2330 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2333 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002334 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2336 // There is no focus view, and this window will
2337 // be behind any soft input window, so hide the
2338 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002339 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002340 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002342 } else if (isTextEditor && doAutoShow && (softInputMode &
2343 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 // There is a focus view, and we are navigating forward
2345 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002346 // We only do this automatically if the window can resize
2347 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002348 // them good context without input information being obscured
2349 // by the IME) or if running on a large screen where there
2350 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002351 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002352 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002353 res = startInputUncheckedLocked(cs, inputContext,
2354 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002355 didStart = true;
2356 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002357 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359 break;
2360 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2361 // Do nothing.
2362 break;
2363 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2364 if ((softInputMode &
2365 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002366 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002367 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 }
2369 break;
2370 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002371 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002372 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 break;
2374 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2375 if ((softInputMode &
2376 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002377 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002378 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002379 res = startInputUncheckedLocked(cs, inputContext,
2380 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002381 didStart = true;
2382 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002383 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385 break;
2386 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002387 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002388 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002389 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2390 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002391 didStart = true;
2392 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002393 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 break;
2395 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002396
2397 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002398 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002399 controlFlags);
2400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
2402 } finally {
2403 Binder.restoreCallingIdentity(ident);
2404 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002405
2406 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002408
satok42c5a162011-05-26 16:46:14 +09002409 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002410 public void showInputMethodPickerFromClient(
2411 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002412 if (!calledFromValidUser()) {
2413 return;
2414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 synchronized (mMethodMap) {
2416 if (mCurClient == null || client == null
2417 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002418 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002419 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
2421
satok440aab52010-11-25 09:43:11 +09002422 // Always call subtype picker, because subtype picker is a superset of input method
2423 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002424 mHandler.sendMessage(mCaller.obtainMessageI(
2425 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002426 }
2427 }
2428
satok42c5a162011-05-26 16:46:14 +09002429 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002431 if (!calledFromValidUser()) {
2432 return;
2433 }
satok28203512010-11-24 11:06:49 +09002434 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2435 }
2436
satok42c5a162011-05-26 16:46:14 +09002437 @Override
satok28203512010-11-24 11:06:49 +09002438 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002439 if (!calledFromValidUser()) {
2440 return;
2441 }
satok28203512010-11-24 11:06:49 +09002442 synchronized (mMethodMap) {
2443 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002444 setInputMethodWithSubtypeIdLocked(token, id,
2445 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2446 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002447 } else {
2448 setInputMethod(token, id);
2449 }
2450 }
satokab751aa2010-09-14 19:17:36 +09002451 }
2452
satok42c5a162011-05-26 16:46:14 +09002453 @Override
satokb416a712010-11-25 20:42:14 +09002454 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002455 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002456 if (!calledFromValidUser()) {
2457 return;
2458 }
satokb416a712010-11-25 20:42:14 +09002459 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002460 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2461 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002462 }
2463 }
2464
satok4fc87d62011-05-20 16:13:43 +09002465 @Override
satok735cf382010-11-11 20:40:09 +09002466 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002467 if (!calledFromValidUser()) {
2468 return false;
2469 }
satok735cf382010-11-11 20:40:09 +09002470 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002471 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002472 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002473 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002474 lastImi = mMethodMap.get(lastIme.first);
2475 } else {
2476 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002477 }
satok4fc87d62011-05-20 16:13:43 +09002478 String targetLastImiId = null;
2479 int subtypeId = NOT_A_SUBTYPE_ID;
2480 if (lastIme != null && lastImi != null) {
2481 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002482 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002483 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2484 : mCurrentSubtype.hashCode();
2485 // If the last IME is the same as the current IME and the last subtype is not
2486 // defined, there is no need to switch to the last IME.
2487 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2488 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002489 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002490 }
2491 }
2492
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002493 if (TextUtils.isEmpty(targetLastImiId)
2494 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002495 // This is a safety net. If the currentSubtype can't be added to the history
2496 // and the framework couldn't find the last ime, we will make the last ime be
2497 // the most applicable enabled keyboard subtype of the system imes.
2498 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2499 if (enabled != null) {
2500 final int N = enabled.size();
2501 final String locale = mCurrentSubtype == null
2502 ? mRes.getConfiguration().locale.toString()
2503 : mCurrentSubtype.getLocale();
2504 for (int i = 0; i < N; ++i) {
2505 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002506 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002507 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002508 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2509 InputMethodUtils.getSubtypes(imi),
2510 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002511 if (keyboardSubtype != null) {
2512 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002513 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002514 imi, keyboardSubtype.hashCode());
2515 if(keyboardSubtype.getLocale().equals(locale)) {
2516 break;
2517 }
2518 }
2519 }
2520 }
2521 }
2522 }
2523
2524 if (!TextUtils.isEmpty(targetLastImiId)) {
2525 if (DEBUG) {
2526 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2527 + ", from: " + mCurMethodId + ", " + subtypeId);
2528 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002529 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002530 return true;
2531 } else {
2532 return false;
2533 }
satok735cf382010-11-11 20:40:09 +09002534 }
2535 }
2536
satoke7c6998e2011-06-03 17:57:59 +09002537 @Override
satok688bd472012-02-09 20:09:17 +09002538 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002539 if (!calledFromValidUser()) {
2540 return false;
2541 }
satok688bd472012-02-09 20:09:17 +09002542 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002543 if (!calledWithValidToken(token)) {
2544 final int uid = Binder.getCallingUid();
2545 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2546 + " token:" + token);
2547 return false;
2548 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002549 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002550 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2551 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002552 if (nextSubtype == null) {
2553 return false;
2554 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002555 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2556 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002557 return true;
2558 }
2559 }
2560
2561 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002562 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2563 if (!calledFromValidUser()) {
2564 return false;
2565 }
2566 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002567 if (!calledWithValidToken(token)) {
2568 final int uid = Binder.getCallingUid();
2569 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2570 + "token. uid:" + uid + " token:" + token);
2571 return false;
2572 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002573 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002574 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2575 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002576 if (nextSubtype == null) {
2577 return false;
2578 }
2579 return true;
2580 }
2581 }
2582
2583 @Override
satok68f1b782011-04-11 14:26:04 +09002584 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002585 if (!calledFromValidUser()) {
2586 return null;
2587 }
satok68f1b782011-04-11 14:26:04 +09002588 synchronized (mMethodMap) {
2589 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2590 // TODO: Handle the case of the last IME with no subtypes
2591 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2592 || TextUtils.isEmpty(lastIme.second)) return null;
2593 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2594 if (lastImi == null) return null;
2595 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002596 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002597 final int lastSubtypeId =
2598 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002599 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2600 return null;
2601 }
2602 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002603 } catch (NumberFormatException e) {
2604 return null;
2605 }
2606 }
2607 }
2608
satoke7c6998e2011-06-03 17:57:59 +09002609 @Override
satokee5e77c2011-09-02 18:50:15 +09002610 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002611 if (!calledFromValidUser()) {
2612 return;
2613 }
satok91e88122011-07-18 11:11:42 +09002614 // By this IPC call, only a process which shares the same uid with the IME can add
2615 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002616 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002617 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002618 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002619 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002620 final String[] packageInfos;
2621 try {
2622 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2623 } catch (RemoteException e) {
2624 Slog.e(TAG, "Failed to get package infos");
2625 return;
2626 }
satok91e88122011-07-18 11:11:42 +09002627 if (packageInfos != null) {
2628 final int packageNum = packageInfos.length;
2629 for (int i = 0; i < packageNum; ++i) {
2630 if (packageInfos[i].equals(imi.getPackageName())) {
2631 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002632 final long ident = Binder.clearCallingIdentity();
2633 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002634 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002635 } finally {
2636 Binder.restoreCallingIdentity(ident);
2637 }
satokee5e77c2011-09-02 18:50:15 +09002638 return;
satok91e88122011-07-18 11:11:42 +09002639 }
2640 }
2641 }
satoke7c6998e2011-06-03 17:57:59 +09002642 }
satokee5e77c2011-09-02 18:50:15 +09002643 return;
satoke7c6998e2011-06-03 17:57:59 +09002644 }
2645
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002646 @Override
2647 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002648 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002649 }
2650
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002651 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002652 public void clearLastInputMethodWindowForTransition(IBinder token) {
2653 if (!calledFromValidUser()) {
2654 return;
2655 }
2656 final long ident = Binder.clearCallingIdentity();
2657 try {
2658 synchronized (mMethodMap) {
2659 if (!calledWithValidToken(token)) {
2660 final int uid = Binder.getCallingUid();
2661 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2662 + "invalid token. uid:" + uid + " token:" + token);
2663 return;
2664 }
2665 }
2666 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2667 } finally {
2668 Binder.restoreCallingIdentity(ident);
2669 }
2670 }
2671
2672 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002673 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002674 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002675 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002676 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002677 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002678 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2679 if (DEBUG) {
2680 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2681 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2682 + " actual: " + sequenceNumber);
2683 }
2684 return;
2685 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002686 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2687 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002688 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002689 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002690 }
2691 }
2692
satok28203512010-11-24 11:06:49 +09002693 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002695 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2696 }
2697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002699 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2700 if (token == null) {
2701 if (mContext.checkCallingOrSelfPermission(
2702 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2703 != PackageManager.PERMISSION_GRANTED) {
2704 throw new SecurityException(
2705 "Using null token requires permission "
2706 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002708 } else if (mCurToken != token) {
2709 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2710 + " token: " + token);
2711 return;
2712 }
2713
2714 final long ident = Binder.clearCallingIdentity();
2715 try {
2716 setInputMethodLocked(id, subtypeId);
2717 } finally {
2718 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 }
2720 }
2721
satok42c5a162011-05-26 16:46:14 +09002722 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002724 if (!calledFromValidUser()) {
2725 return;
2726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002728 if (!calledWithValidToken(token)) {
2729 final int uid = Binder.getCallingUid();
2730 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2731 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 return;
2733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 long ident = Binder.clearCallingIdentity();
2735 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002736 hideCurrentInputLocked(flags, null);
2737 } finally {
2738 Binder.restoreCallingIdentity(ident);
2739 }
2740 }
2741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002742
satok42c5a162011-05-26 16:46:14 +09002743 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002744 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002745 if (!calledFromValidUser()) {
2746 return;
2747 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002748 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002749 if (!calledWithValidToken(token)) {
2750 final int uid = Binder.getCallingUid();
2751 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2752 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002753 return;
2754 }
2755 long ident = Binder.clearCallingIdentity();
2756 try {
2757 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 } finally {
2759 Binder.restoreCallingIdentity(ident);
2760 }
2761 }
2762 }
2763
2764 void setEnabledSessionInMainThread(SessionState session) {
2765 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002766 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002768 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002769 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 } catch (RemoteException e) {
2771 }
2772 }
2773 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002774 if (mEnabledSession != null && mEnabledSession.session != null) {
2775 try {
2776 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2777 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2778 } catch (RemoteException e) {
2779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781 }
2782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002783
satok42c5a162011-05-26 16:46:14 +09002784 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002786 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002788 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002789 final boolean showAuxSubtypes;
2790 switch (msg.arg1) {
2791 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2792 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2793 // implemented so that auxiliary subtypes will be excluded when the soft
2794 // keyboard is invisible.
2795 showAuxSubtypes = mInputShown;
2796 break;
2797 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2798 showAuxSubtypes = true;
2799 break;
2800 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2801 showAuxSubtypes = false;
2802 break;
2803 default:
2804 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2805 return false;
2806 }
2807 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002808 return true;
2809
satok47a44912010-10-06 16:03:58 +09002810 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002811 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002812 return true;
2813
2814 case MSG_SHOW_IM_CONFIG:
2815 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002816 return true;
2817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 case MSG_UNBIND_INPUT:
2821 try {
2822 ((IInputMethod)msg.obj).unbindInput();
2823 } catch (RemoteException e) {
2824 // There is nothing interesting about the method dying.
2825 }
2826 return true;
2827 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002828 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 try {
2830 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2831 } catch (RemoteException e) {
2832 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002833 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 return true;
2835 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002836 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002838 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002839 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002840 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 } catch (RemoteException e) {
2842 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002843 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 return true;
2845 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002846 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002848 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002849 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002850 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 } catch (RemoteException e) {
2852 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002853 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002855 case MSG_HIDE_CURRENT_INPUT_METHOD:
2856 synchronized (mMethodMap) {
2857 hideCurrentInputLocked(0, null);
2858 }
2859 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002861 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002863 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2865 } catch (RemoteException e) {
2866 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002867 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002869 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002870 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002871 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002872 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002874 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002876 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002877 // Dispose the channel if the input method is not local to this process
2878 // because the remote proxy will get its own copy when unparceled.
2879 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002880 channel.dispose();
2881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002883 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002887
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002888 case MSG_START_INPUT: {
2889 int missingMethods = msg.arg1;
2890 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002892 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002894 session.method.startInput((IInputContext) args.arg2, missingMethods,
2895 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } catch (RemoteException e) {
2897 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002898 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002900 }
2901 case MSG_RESTART_INPUT: {
2902 int missingMethods = msg.arg1;
2903 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002905 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002907 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2908 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 } catch (RemoteException e) {
2910 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002911 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002916
Yohei Yukawa33e81792015-11-17 21:14:42 -08002917 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002919 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 } catch (RemoteException e) {
2921 // There is nothing interesting about the last client dying.
2922 }
2923 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002924 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002925 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002926 IInputMethodClient client = (IInputMethodClient)args.arg1;
2927 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002929 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002931 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002932 } finally {
2933 // Dispose the channel if the input method is not local to this process
2934 // because the remote proxy will get its own copy when unparceled.
2935 if (res.channel != null && Binder.isProxy(client)) {
2936 res.channel.dispose();
2937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002939 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002941 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002942 case MSG_SET_ACTIVE:
2943 try {
2944 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2945 } catch (RemoteException e) {
2946 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2947 + ((ClientState)msg.obj).pid + " uid "
2948 + ((ClientState)msg.obj).uid);
2949 }
2950 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002951 case MSG_SET_INTERACTIVE:
2952 handleSetInteractive(msg.arg1 != 0);
2953 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002954 case MSG_SWITCH_IME:
2955 handleSwitchInputMethod(msg.arg1 != 0);
2956 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002957 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2958 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002959 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002960 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002961 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002962 } catch (RemoteException e) {
2963 Slog.w(TAG, "Got RemoteException sending "
2964 + "setUserActionNotificationSequenceNumber("
2965 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002966 + clientState.pid + " uid "
2967 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002968 }
2969 return true;
2970 }
satok01038492012-04-09 21:08:27 +09002971
2972 // --------------------------------------------------------------
2973 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002974 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002975 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002976 case MSG_SYSTEM_UNLOCK_USER:
2977 final int userId = msg.arg1;
2978 onUnlockUser(userId);
2979 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 }
2981 return false;
2982 }
2983
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002984 private void handleSetInteractive(final boolean interactive) {
2985 synchronized (mMethodMap) {
2986 mIsInteractive = interactive;
2987 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2988
2989 // Inform the current client of the change in active status
2990 if (mCurClient != null && mCurClient.client != null) {
2991 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2992 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2993 }
2994 }
2995 }
2996
Yohei Yukawaae61f712015-12-09 13:00:10 -08002997 private void handleSwitchInputMethod(final boolean forwardDirection) {
2998 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002999 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003000 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003001 if (nextSubtype == null) {
3002 return;
3003 }
3004 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003005 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3006 if (newInputMethodInfo == null) {
3007 return;
3008 }
3009 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3010 newInputMethodInfo, mCurrentSubtype);
3011 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003012 if (mSubtypeSwitchedByShortCutToast == null) {
3013 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3014 Toast.LENGTH_SHORT);
3015 } else {
3016 mSubtypeSwitchedByShortCutToast.setText(toastText);
3017 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003018 mSubtypeSwitchedByShortCutToast.show();
3019 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003020 }
3021 }
3022
satokdc9ddae2011-10-06 12:22:36 +09003023 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003024 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3025 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003026 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003027 if (DEBUG) {
3028 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3029 }
satok723a27e2010-11-11 14:58:11 +09003030 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003031 return true;
3032 }
3033
3034 return false;
3035 }
3036
Yohei Yukawa94e33302016-02-12 19:37:03 -08003037 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003038 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003039 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003040 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003041 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003042 mMethodList.clear();
3043 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003044
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003045 // Use for queryIntentServicesAsUser
3046 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003048 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3049 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3050 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003051 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003053 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3054 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003055
satoke7c6998e2011-06-03 17:57:59 +09003056 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3057 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 for (int i = 0; i < services.size(); ++i) {
3059 ResolveInfo ri = services.get(i);
3060 ServiceInfo si = ri.serviceInfo;
3061 ComponentName compName = new ComponentName(si.packageName, si.name);
3062 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3063 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003064 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 + ": it does not require the permission "
3066 + android.Manifest.permission.BIND_INPUT_METHOD);
3067 continue;
3068 }
3069
Joe Onorato8a9b2202010-02-26 18:56:32 -08003070 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071
3072 try {
satoke7c6998e2011-06-03 17:57:59 +09003073 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003074 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003075 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003076 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077
3078 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003079 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 }
Tadashi G. Takaokadddc4382016-09-16 11:41:07 +09003081 } catch (Exception e) {
3082 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
3084 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003085
Yohei Yukawa859df052016-02-17 07:56:46 -08003086 // TODO: The following code should find better place to live.
3087 if (!resetDefaultEnabledIme) {
3088 boolean enabledImeFound = false;
3089 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3090 final int N = enabledImes.size();
3091 for (int i = 0; i < N; ++i) {
3092 final InputMethodInfo imi = enabledImes.get(i);
3093 if (mMethodList.contains(imi)) {
3094 enabledImeFound = true;
3095 break;
3096 }
3097 }
3098 if (!enabledImeFound) {
3099 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3100 resetDefaultEnabledIme = true;
3101 resetSelectedInputMethodAndSubtypeLocked("");
3102 }
3103 }
3104
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003105 if (resetDefaultEnabledIme) {
3106 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003107 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003108 final int N = defaultEnabledIme.size();
3109 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003110 final InputMethodInfo imi = defaultEnabledIme.get(i);
3111 if (DEBUG) {
3112 Slog.d(TAG, "--- enable ime = " + imi);
3113 }
3114 setInputMethodEnabledLocked(imi.getId(), true);
3115 }
3116 }
3117
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003118 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003119 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003120 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003121 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3122 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003123 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003124 }
3125 } else {
3126 // Double check that the default IME is certainly enabled.
3127 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003128 }
3129 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003130 // Here is not the perfect place to reset the switching controller. Ideally
3131 // mSwitchingController and mSettings should be able to share the same state.
3132 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3133 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003134 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003138
satok217f5482010-12-15 05:19:19 +09003139 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003140 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003141 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003142 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3143 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003144 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003145 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003146 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003147 final int userId;
3148 synchronized (mMethodMap) {
3149 userId = mSettings.getCurrentUserId();
3150 }
3151 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003152 }
3153
3154 private void showConfigureInputMethods() {
3155 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3156 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3157 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3158 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003159 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003160 }
3161
satok2c93efc2012-04-02 19:33:47 +09003162 private boolean isScreenLocked() {
3163 return mKeyguardManager != null
3164 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3165 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003166
Seigo Nonaka14e13912015-05-06 21:04:13 -07003167 private void showInputMethodMenu(boolean showAuxSubtypes) {
3168 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003171 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003172
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003173 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003174 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003175 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003176
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003177 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003178 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003179 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3180 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003181 if (immis == null || immis.size() == 0) {
3182 return;
3183 }
3184
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003185 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186
satok688bd472012-02-09 20:09:17 +09003187 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003188 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003189 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003190
satokc3690562012-01-10 20:14:43 +09003191 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003192 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003193 if (currentSubtype != null) {
3194 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003195 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3196 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003197 }
3198 }
3199
Ken Wakasa761eb372011-03-04 19:06:18 +09003200 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003201 mIms = new InputMethodInfo[N];
3202 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003203 int checkedItem = 0;
3204 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003205 final ImeSubtypeListItem item = imList.get(i);
3206 mIms[i] = item.mImi;
3207 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003208 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003209 int subtypeId = mSubtypeIds[i];
3210 if ((subtypeId == NOT_A_SUBTYPE_ID)
3211 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3212 || (subtypeId == lastInputMethodSubtypeId)) {
3213 checkedItem = i;
3214 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003217
3218 final Context settingsContext = new ContextThemeWrapper(context,
3219 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3220
3221 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003222 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3223 @Override
3224 public void onCancel(DialogInterface dialog) {
3225 hideInputMethodMenu();
3226 }
3227 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003228
3229 final Context dialogContext = mDialogBuilder.getContext();
3230 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3231 com.android.internal.R.styleable.DialogPreference,
3232 com.android.internal.R.attr.alertDialogStyle, 0);
3233 final Drawable dialogIcon = a.getDrawable(
3234 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3235 a.recycle();
3236
3237 mDialogBuilder.setIcon(dialogIcon);
3238
Yohei Yukawad34e1482016-02-11 08:03:52 -08003239 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003240 final View tv = inflater.inflate(
3241 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3242 mDialogBuilder.setCustomTitle(tv);
3243
3244 // Setup layout for a toggle switch of the hardware keyboard
3245 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003246 mSwitchingDialogTitleView
3247 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003248 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003249 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003250 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003251 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003252 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003253 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3254 @Override
3255 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003256 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003257 // Ensure that the input method dialog is dismissed when changing
3258 // the hardware keyboard state.
3259 hideInputMethodMenu();
3260 }
3261 });
3262
Alan Viverette505e3ab2014-11-24 15:22:11 -08003263 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003264 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3265 final OnClickListener choiceListener = new OnClickListener() {
3266 @Override
3267 public void onClick(final DialogInterface dialog, final int which) {
3268 synchronized (mMethodMap) {
3269 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3270 || mSubtypeIds.length <= which) {
3271 return;
satok01038492012-04-09 21:08:27 +09003272 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003273 final InputMethodInfo im = mIms[which];
3274 int subtypeId = mSubtypeIds[which];
3275 adapter.mCheckedItem = which;
3276 adapter.notifyDataSetChanged();
3277 hideInputMethodMenu();
3278 if (im != null) {
3279 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3280 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003281 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003282 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003284 }
3285 }
3286 };
3287 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003290 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 mSwitchingDialog.getWindow().setType(
3292 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003293 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3294 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003295 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003296 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 mSwitchingDialog.show();
3298 }
3299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003300
Ken Wakasa05dbb652011-08-22 15:22:43 +09003301 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3302 private final LayoutInflater mInflater;
3303 private final int mTextViewResourceId;
3304 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003305 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003306 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3307 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3308 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003309
Ken Wakasa05dbb652011-08-22 15:22:43 +09003310 mTextViewResourceId = textViewResourceId;
3311 mItemsList = itemsList;
3312 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003313 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003314 }
3315
3316 @Override
3317 public View getView(int position, View convertView, ViewGroup parent) {
3318 final View view = convertView != null ? convertView
3319 : mInflater.inflate(mTextViewResourceId, null);
3320 if (position < 0 || position >= mItemsList.size()) return view;
3321 final ImeSubtypeListItem item = mItemsList.get(position);
3322 final CharSequence imeName = item.mImeName;
3323 final CharSequence subtypeName = item.mSubtypeName;
3324 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3325 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3326 if (TextUtils.isEmpty(subtypeName)) {
3327 firstTextView.setText(imeName);
3328 secondTextView.setVisibility(View.GONE);
3329 } else {
3330 firstTextView.setText(subtypeName);
3331 secondTextView.setText(imeName);
3332 secondTextView.setVisibility(View.VISIBLE);
3333 }
3334 final RadioButton radioButton =
3335 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3336 radioButton.setChecked(position == mCheckedItem);
3337 return view;
3338 }
3339 }
3340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003342 synchronized (mMethodMap) {
3343 hideInputMethodMenuLocked();
3344 }
3345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003346
The Android Open Source Project10592532009-03-18 17:39:46 -07003347 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003348 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349
The Android Open Source Project10592532009-03-18 17:39:46 -07003350 if (mSwitchingDialog != null) {
3351 mSwitchingDialog.dismiss();
3352 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003354
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003355 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003356 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003357 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003361
satok42c5a162011-05-26 16:46:14 +09003362 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003364 // TODO: Make this work even for non-current users?
3365 if (!calledFromValidUser()) {
3366 return false;
3367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 synchronized (mMethodMap) {
3369 if (mContext.checkCallingOrSelfPermission(
3370 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3371 != PackageManager.PERMISSION_GRANTED) {
3372 throw new SecurityException(
3373 "Requires permission "
3374 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3375 }
Anna Galusza9b278112016-01-04 11:37:37 -08003376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 long ident = Binder.clearCallingIdentity();
3378 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003379 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 } finally {
3381 Binder.restoreCallingIdentity(ident);
3382 }
3383 }
3384 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003385
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003386 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3387 // Make sure this is a valid input method.
3388 InputMethodInfo imm = mMethodMap.get(id);
3389 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003390 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003391 }
3392
satokd87c2592010-09-29 11:52:06 +09003393 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3394 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003395
satokd87c2592010-09-29 11:52:06 +09003396 if (enabled) {
3397 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3398 if (pair.first.equals(id)) {
3399 // We are enabling this input method, but it is already enabled.
3400 // Nothing to do. The previous state was enabled.
3401 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003402 }
3403 }
satokd87c2592010-09-29 11:52:06 +09003404 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3405 // Previous state was disabled.
3406 return false;
3407 } else {
3408 StringBuilder builder = new StringBuilder();
3409 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3410 builder, enabledInputMethodsList, id)) {
3411 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003412 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003413 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3414 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3415 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003416 }
3417 // Previous state was enabled.
3418 return true;
3419 } else {
3420 // We are disabling the input method but it is already disabled.
3421 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003422 return false;
3423 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003424 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003425 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003426
satok723a27e2010-11-11 14:58:11 +09003427 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3428 boolean setSubtypeOnly) {
3429 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003430 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003431
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003432 mCurUserActionNotificationSequenceNumber =
3433 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3434 if (DEBUG) {
3435 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3436 + mCurUserActionNotificationSequenceNumber);
3437 }
3438
3439 if (mCurClient != null && mCurClient.client != null) {
3440 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3441 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003442 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003443 }
3444
satok723a27e2010-11-11 14:58:11 +09003445 // Set Subtype here
3446 if (imi == null || subtypeId < 0) {
3447 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003448 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003449 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003450 if (subtypeId < imi.getSubtypeCount()) {
3451 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3452 mSettings.putSelectedSubtype(subtype.hashCode());
3453 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003454 } else {
3455 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003456 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003457 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003458 }
satokab751aa2010-09-14 19:17:36 +09003459 }
satok723a27e2010-11-11 14:58:11 +09003460
Yohei Yukawa68645a62016-02-17 07:54:20 -08003461 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003462 // Set InputMethod here
3463 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3464 }
3465 }
3466
3467 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3468 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3469 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3470 // newDefaultIme is empty when there is no candidate for the selected IME.
3471 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3472 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3473 if (subtypeHashCode != null) {
3474 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003475 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003476 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003477 } catch (NumberFormatException e) {
3478 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3479 }
3480 }
3481 }
3482 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003483 }
3484
satok4e4569d2010-11-19 18:45:53 +09003485 // If there are no selected shortcuts, tries finding the most applicable ones.
3486 private Pair<InputMethodInfo, InputMethodSubtype>
3487 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3488 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3489 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003490 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003491 boolean foundInSystemIME = false;
3492
3493 // Search applicable subtype for each InputMethodInfo
3494 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003495 final String imiId = imi.getId();
3496 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3497 continue;
3498 }
satokcd7cd292010-11-20 15:46:23 +09003499 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003500 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003501 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003502 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003503 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003504 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003505 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003506 }
satokdf31ae62011-01-15 06:19:44 +09003507 // 2. Search by the system locale from enabledSubtypes.
3508 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003509 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003510 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003511 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003512 }
satoka86f5e42011-09-02 17:12:42 +09003513 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003514 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003515 final ArrayList<InputMethodSubtype> subtypesForSearch =
3516 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003517 ? InputMethodUtils.getSubtypes(imi)
3518 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003519 // 4. Search by the current subtype's locale from all subtypes.
3520 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003521 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003522 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003523 }
3524 // 5. Search by the system locale from all subtypes.
3525 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003526 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003527 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003528 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003529 }
satokcd7cd292010-11-20 15:46:23 +09003530 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003531 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003532 // The current input method is the most applicable IME.
3533 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003534 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003535 break;
satok7599a7f2010-12-22 13:45:23 +09003536 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003537 // The system input method is 2nd applicable IME.
3538 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003539 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003540 if ((imi.getServiceInfo().applicationInfo.flags
3541 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3542 foundInSystemIME = true;
3543 }
satok4e4569d2010-11-19 18:45:53 +09003544 }
3545 }
3546 }
3547 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003548 if (mostApplicableIMI != null) {
3549 Slog.w(TAG, "Most applicable shortcut input method was:"
3550 + mostApplicableIMI.getId());
3551 if (mostApplicableSubtype != null) {
3552 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3553 + "," + mostApplicableSubtype.getMode() + ","
3554 + mostApplicableSubtype.getLocale());
3555 }
3556 }
satok4e4569d2010-11-19 18:45:53 +09003557 }
satokcd7cd292010-11-20 15:46:23 +09003558 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003559 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003560 } else {
3561 return null;
3562 }
3563 }
3564
satokab751aa2010-09-14 19:17:36 +09003565 /**
3566 * @return Return the current subtype of this input method.
3567 */
satok42c5a162011-05-26 16:46:14 +09003568 @Override
satokab751aa2010-09-14 19:17:36 +09003569 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003570 // TODO: Make this work even for non-current users?
3571 if (!calledFromValidUser()) {
3572 return null;
3573 }
3574 synchronized (mMethodMap) {
3575 return getCurrentInputMethodSubtypeLocked();
3576 }
3577 }
3578
3579 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003580 if (mCurMethodId == null) {
3581 return null;
3582 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003583 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003584 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3585 if (imi == null || imi.getSubtypeCount() == 0) {
3586 return null;
satok4e4569d2010-11-19 18:45:53 +09003587 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003588 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003589 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3590 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003591 if (subtypeId == NOT_A_SUBTYPE_ID) {
3592 // If there are no selected subtypes, the framework will try to find
3593 // the most applicable subtype from explicitly or implicitly enabled
3594 // subtypes.
3595 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003596 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003597 // If there is only one explicitly or implicitly enabled subtype,
3598 // just returns it.
3599 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3600 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3601 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003602 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003603 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003604 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003605 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003606 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003607 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3608 true);
satok4e4569d2010-11-19 18:45:53 +09003609 }
satok3ef8b292010-11-23 06:06:29 +09003610 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003611 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003612 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003613 }
3614 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003615 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003616 }
3617
satok4e4569d2010-11-19 18:45:53 +09003618 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003619 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003620 @Override
satok4e4569d2010-11-19 18:45:53 +09003621 public List getShortcutInputMethodsAndSubtypes() {
3622 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003623 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003624 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003625 // If there are no selected shortcut subtypes, the framework will try to find
3626 // the most applicable subtype from all subtypes whose mode is
3627 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003628 Pair<InputMethodInfo, InputMethodSubtype> info =
3629 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003630 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003631 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003632 ret.add(info.first);
3633 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003634 }
satok3da92232011-01-11 22:46:30 +09003635 return ret;
satokf3db1af2010-11-23 13:34:33 +09003636 }
satokf3db1af2010-11-23 13:34:33 +09003637 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3638 ret.add(imi);
3639 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3640 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003641 }
3642 }
satokf3db1af2010-11-23 13:34:33 +09003643 return ret;
satok4e4569d2010-11-19 18:45:53 +09003644 }
3645 }
3646
satok42c5a162011-05-26 16:46:14 +09003647 @Override
satokb66d2872010-11-10 01:04:04 +09003648 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003649 // TODO: Make this work even for non-current users?
3650 if (!calledFromValidUser()) {
3651 return false;
3652 }
satokb66d2872010-11-10 01:04:04 +09003653 synchronized (mMethodMap) {
3654 if (subtype != null && mCurMethodId != null) {
3655 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003656 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003657 if (subtypeId != NOT_A_SUBTYPE_ID) {
3658 setInputMethodLocked(mCurMethodId, subtypeId);
3659 return true;
3660 }
3661 }
3662 return false;
3663 }
3664 }
3665
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003666 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003667 private static class InputMethodFileManager {
3668 private static final String SYSTEM_PATH = "system";
3669 private static final String INPUT_METHOD_PATH = "inputmethod";
3670 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3671 private static final String NODE_SUBTYPES = "subtypes";
3672 private static final String NODE_SUBTYPE = "subtype";
3673 private static final String NODE_IMI = "imi";
3674 private static final String ATTR_ID = "id";
3675 private static final String ATTR_LABEL = "label";
3676 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003677 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003678 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003679 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003680 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3681 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3682 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003683 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003684 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3685 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003686 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003687 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003688 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003689 if (methodMap == null) {
3690 throw new NullPointerException("methodMap is null");
3691 }
3692 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003693 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003694 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3695 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003696 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003697 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003698 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3699 }
3700 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3701 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3702 if (!subtypeFile.exists()) {
3703 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003704 writeAdditionalInputMethodSubtypes(
3705 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003706 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003707 readAdditionalInputMethodSubtypes(
3708 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003709 }
3710 }
3711
3712 private void deleteAllInputMethodSubtypes(String imiId) {
3713 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003714 mAdditionalSubtypesMap.remove(imiId);
3715 writeAdditionalInputMethodSubtypes(
3716 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003717 }
3718 }
3719
3720 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003721 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003722 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003723 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003724 final int N = additionalSubtypes.length;
3725 for (int i = 0; i < N; ++i) {
3726 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003727 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003728 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003729 } else {
3730 Slog.w(TAG, "Duplicated subtype definition found: "
3731 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003732 }
3733 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003734 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3735 writeAdditionalInputMethodSubtypes(
3736 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003737 }
3738 }
3739
3740 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3741 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003742 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003743 }
3744 }
3745
3746 private static void writeAdditionalInputMethodSubtypes(
3747 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3748 HashMap<String, InputMethodInfo> methodMap) {
3749 // Safety net for the case that this function is called before methodMap is set.
3750 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3751 FileOutputStream fos = null;
3752 try {
3753 fos = subtypesFile.startWrite();
3754 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003755 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003756 out.startDocument(null, true);
3757 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3758 out.startTag(null, NODE_SUBTYPES);
3759 for (String imiId : allSubtypes.keySet()) {
3760 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3761 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3762 continue;
3763 }
3764 out.startTag(null, NODE_IMI);
3765 out.attribute(null, ATTR_ID, imiId);
3766 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3767 final int N = subtypesList.size();
3768 for (int i = 0; i < N; ++i) {
3769 final InputMethodSubtype subtype = subtypesList.get(i);
3770 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003771 if (subtype.hasSubtypeId()) {
3772 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3773 String.valueOf(subtype.getSubtypeId()));
3774 }
satoke7c6998e2011-06-03 17:57:59 +09003775 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3776 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3777 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003778 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3779 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003780 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3781 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3782 out.attribute(null, ATTR_IS_AUXILIARY,
3783 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003784 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3785 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003786 out.endTag(null, NODE_SUBTYPE);
3787 }
3788 out.endTag(null, NODE_IMI);
3789 }
3790 out.endTag(null, NODE_SUBTYPES);
3791 out.endDocument();
3792 subtypesFile.finishWrite(fos);
3793 } catch (java.io.IOException e) {
3794 Slog.w(TAG, "Error writing subtypes", e);
3795 if (fos != null) {
3796 subtypesFile.failWrite(fos);
3797 }
3798 }
3799 }
3800
3801 private static void readAdditionalInputMethodSubtypes(
3802 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3803 if (allSubtypes == null || subtypesFile == null) return;
3804 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003805 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003806 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003807 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003808 int type = parser.getEventType();
3809 // Skip parsing until START_TAG
3810 while ((type = parser.next()) != XmlPullParser.START_TAG
3811 && type != XmlPullParser.END_DOCUMENT) {}
3812 String firstNodeName = parser.getName();
3813 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3814 throw new XmlPullParserException("Xml doesn't start with subtypes");
3815 }
3816 final int depth =parser.getDepth();
3817 String currentImiId = null;
3818 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3819 while (((type = parser.next()) != XmlPullParser.END_TAG
3820 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3821 if (type != XmlPullParser.START_TAG)
3822 continue;
3823 final String nodeName = parser.getName();
3824 if (NODE_IMI.equals(nodeName)) {
3825 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3826 if (TextUtils.isEmpty(currentImiId)) {
3827 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3828 continue;
3829 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003830 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003831 allSubtypes.put(currentImiId, tempSubtypesArray);
3832 } else if (NODE_SUBTYPE.equals(nodeName)) {
3833 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3834 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3835 continue;
3836 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003837 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003838 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003839 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003840 parser.getAttributeValue(null, ATTR_LABEL));
3841 final String imeSubtypeLocale =
3842 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003843 final String languageTag =
3844 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003845 final String imeSubtypeMode =
3846 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3847 final String imeSubtypeExtraValue =
3848 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003849 final boolean isAuxiliary = "1".equals(String.valueOf(
3850 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003851 final boolean isAsciiCapable = "1".equals(String.valueOf(
3852 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003853 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003854 .setSubtypeNameResId(label)
3855 .setSubtypeIconResId(icon)
3856 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003857 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003858 .setSubtypeMode(imeSubtypeMode)
3859 .setSubtypeExtraValue(imeSubtypeExtraValue)
3860 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003861 .setIsAsciiCapable(isAsciiCapable);
3862 final String subtypeIdString =
3863 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3864 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003865 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003866 }
3867 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003868 }
3869 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003870 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3871 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003872 return;
satoke7c6998e2011-06-03 17:57:59 +09003873 }
3874 }
3875 }
3876
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003877 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3878 @NonNull
3879 private final Handler mHandler;
3880
3881 LocalServiceImpl(@NonNull final Handler handler) {
3882 mHandler = handler;
3883 }
3884
3885 @Override
3886 public void setInteractive(boolean interactive) {
3887 // Do everything in handler so as not to block the caller.
3888 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3889 interactive ? 1 : 0, 0));
3890 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003891
3892 @Override
3893 public void switchInputMethod(boolean forwardDirection) {
3894 // Do everything in handler so as not to block the caller.
3895 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3896 forwardDirection ? 1 : 0, 0));
3897 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003898
3899 @Override
3900 public void hideCurrentInputMethod() {
3901 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3902 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3903 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003904 }
3905
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003906 private static String imeWindowStatusToString(final int imeWindowVis) {
3907 final StringBuilder sb = new StringBuilder();
3908 boolean first = true;
3909 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3910 sb.append("Active");
3911 first = false;
3912 }
3913 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3914 if (!first) {
3915 sb.append("|");
3916 }
3917 sb.append("Visible");
3918 }
3919 return sb.toString();
3920 }
3921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003923 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3924 @Nullable Uri contentUri, @Nullable String packageName) {
3925 if (!calledFromValidUser()) {
3926 return null;
3927 }
3928
3929 if (token == null) {
3930 throw new NullPointerException("token");
3931 }
3932 if (packageName == null) {
3933 throw new NullPointerException("packageName");
3934 }
3935 if (contentUri == null) {
3936 throw new NullPointerException("contentUri");
3937 }
3938 final String contentUriScheme = contentUri.getScheme();
3939 if (!"content".equals(contentUriScheme)) {
3940 throw new InvalidParameterException("contentUri must have content scheme");
3941 }
3942
3943 synchronized (mMethodMap) {
3944 final int uid = Binder.getCallingUid();
3945 if (mCurMethodId == null) {
3946 return null;
3947 }
3948 if (mCurToken != token) {
3949 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3950 + " token=" + token);
3951 return null;
3952 }
3953 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3954 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3955 // nature of our system. Let's compare it with our internal record.
3956 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3957 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3958 + mCurAttribute.packageName + " packageName=" + packageName);
3959 return null;
3960 }
3961 final int imeUserId = UserHandle.getUserId(uid);
3962 final int appUserId = UserHandle.getUserId(mCurClient.uid);
3963 return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
3964 appUserId);
3965 }
3966 }
3967
3968 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3970 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3971 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3974 + Binder.getCallingPid()
3975 + ", uid=" + Binder.getCallingUid());
3976 return;
3977 }
3978
3979 IInputMethod method;
3980 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003981 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 synchronized (mMethodMap) {
3986 p.println("Current Input Method Manager state:");
3987 int N = mMethodList.size();
3988 p.println(" Input Methods:");
3989 for (int i=0; i<N; i++) {
3990 InputMethodInfo info = mMethodList.get(i);
3991 p.println(" InputMethod #" + i + ":");
3992 info.dump(p, " ");
3993 }
3994 p.println(" Clients:");
3995 for (ClientState ci : mClients.values()) {
3996 p.println(" Client " + ci + ":");
3997 p.println(" client=" + ci.client);
3998 p.println(" inputContext=" + ci.inputContext);
3999 p.println(" sessionRequested=" + ci.sessionRequested);
4000 p.println(" curSession=" + ci.curSession);
4001 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004002 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004004 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4005 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004006 focusedWindowClient = mCurFocusedWindowClient;
4007 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4009 + " mBoundToMethod=" + mBoundToMethod);
4010 p.println(" mCurToken=" + mCurToken);
4011 p.println(" mCurIntent=" + mCurIntent);
4012 method = mCurMethod;
4013 p.println(" mCurMethod=" + mCurMethod);
4014 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004015 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 p.println(" mShowRequested=" + mShowRequested
4017 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4018 + " mShowForced=" + mShowForced
4019 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004020 p.println(" mCurUserActionNotificationSequenceNumber="
4021 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004022 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004023 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004024 p.println(" mSwitchingController:");
4025 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004026 p.println(" mSettings:");
4027 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004029
Jeff Brownb88102f2010-09-08 11:49:43 -07004030 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 pw.flush();
4033 try {
Jeff Sharkeyc06f1842016-11-09 12:25:44 -07004034 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4035 } catch (IOException | RemoteException e) {
4036 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004038 } else {
4039 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004041
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004042 if (focusedWindowClient != null && client != focusedWindowClient) {
4043 p.println(" ");
4044 p.println("Warning: Current input method client doesn't match the last focused. "
4045 + "window.");
4046 p.println("Dumping input method client in the last focused window just in case.");
4047 p.println(" ");
4048 pw.flush();
4049 try {
Jeff Sharkeyc06f1842016-11-09 12:25:44 -07004050 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4051 } catch (IOException | RemoteException e) {
4052 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004053 }
4054 }
4055
Jeff Brownb88102f2010-09-08 11:49:43 -07004056 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 pw.flush();
4059 try {
Jeff Sharkeyc06f1842016-11-09 12:25:44 -07004060 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4061 } catch (IOException | RemoteException e) {
4062 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004064 } else {
4065 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
4067 }
4068}