blob: c5e91f5de80c8e05fce9708c4866325996e803b5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080024import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070025import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090026import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090028import com.android.internal.inputmethod.InputMethodUtils;
29import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070031import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070032import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090033import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.view.IInputContext;
35import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import com.android.internal.view.IInputMethodClient;
37import com.android.internal.view.IInputMethodManager;
38import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070039import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080041import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080042import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
satoke7c6998e2011-06-03 17:57:59 +090044import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090046import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070048import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070049import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070050import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080051import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080052import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070053import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070055import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090056import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090057import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090058import android.app.Notification;
59import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070060import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090061import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080063import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ContentResolver;
65import android.content.Context;
66import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090068import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090070import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070072import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.pm.PackageManager;
75import android.content.pm.ResolveInfo;
76import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070077import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.res.Resources;
79import android.content.res.TypedArray;
80import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080081import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080082import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080083import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070084import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040086import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070087import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090088import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Handler;
90import android.os.IBinder;
91import android.os.IInterface;
92import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070093import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070095import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080097import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.ServiceManager;
99import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900100import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800101import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.provider.Settings;
103import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900104import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700105import android.util.ArrayMap;
106import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700107import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900109import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900110import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.util.PrintWriterPrinter;
112import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900113import android.util.Slog;
114import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900115import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700117import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900118import android.view.LayoutInflater;
119import android.view.View;
120import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700121import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700123import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900124import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700126import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import android.view.inputmethod.InputMethod;
128import android.view.inputmethod.InputMethodInfo;
129import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700130import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900131import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900132import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900133import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900134import android.widget.CompoundButton;
135import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900136import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900137import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900138import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700139import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
satoke7c6998e2011-06-03 17:57:59 +0900141import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900143import java.io.FileInputStream;
144import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.io.IOException;
146import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700147import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100148import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700149import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900151import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.util.HashMap;
153import java.util.List;
154
155/**
156 * This class provides a system service that manages input methods.
157 */
158public class InputMethodManagerService extends IInputMethodManager.Stub
159 implements ServiceConnection, Handler.Callback {
160 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700161 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700162 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700164 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
165 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
166 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 static final int MSG_UNBIND_INPUT = 1000;
169 static final int MSG_BIND_INPUT = 1010;
170 static final int MSG_SHOW_SOFT_INPUT = 1020;
171 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700172 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 static final int MSG_ATTACH_TOKEN = 1040;
174 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_START_INPUT = 2000;
177 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
Yohei Yukawa33e81792015-11-17 21:14:42 -0800179 static final int MSG_UNBIND_CLIENT = 3000;
180 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700181 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700182 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900183 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800184 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800185 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
satok01038492012-04-09 21:08:27 +0900187 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
188
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700189 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
190
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700191 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
satokf9f01002011-05-19 21:31:50 +0900193 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
194
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900195 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900196 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900197
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700198 @Retention(SOURCE)
199 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
200 private @interface HardKeyboardBehavior {
201 int WIRELESS_AFFORDANCE = 0;
202 int WIRED_AFFORDANCE = 1;
203 }
satok4e4569d2010-11-19 18:45:53 +0900204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800206 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900208 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700211 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700213 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900214 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900215 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900216 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800217 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900219 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 // All known input methods. mMethodMap also serves as the global
222 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700223 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
224 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900225 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700226 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900227 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700229 // Used to bring IME service up to visible adjustment while it is being shown.
230 final ServiceConnection mVisibleConnection = new ServiceConnection() {
231 @Override public void onServiceConnected(ComponentName name, IBinder service) {
232 }
233
234 @Override public void onServiceDisconnected(ComponentName name) {
235 }
236 };
237 boolean mVisibleBound = false;
238
satok7cfc0ed2011-06-20 21:29:36 +0900239 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700240 private NotificationManager mNotificationManager;
241 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700242 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400243 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700244 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900245 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900246 private boolean mNotificationShown;
247
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900248 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final ClientState client;
250 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700251
252 IInputMethodSession session;
253 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 @Override
256 public String toString() {
257 return "SessionState{uid " + client.uid + " pid " + client.pid
258 + " method " + Integer.toHexString(
259 System.identityHashCode(method))
260 + " session " + Integer.toHexString(
261 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700262 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 + "}";
264 }
265
266 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700267 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 client = _client;
269 method = _method;
270 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700271 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 }
273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
Jeff Brownc28867a2013-03-26 15:42:39 -0700275 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 final IInputMethodClient client;
277 final IInputContext inputContext;
278 final int uid;
279 final int pid;
280 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 boolean sessionRequested;
283 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 @Override
286 public String toString() {
287 return "ClientState{" + Integer.toHexString(
288 System.identityHashCode(this)) + " uid " + uid
289 + " pid " + pid + "}";
290 }
291
292 ClientState(IInputMethodClient _client, IInputContext _inputContext,
293 int _uid, int _pid) {
294 client = _client;
295 inputContext = _inputContext;
296 uid = _uid;
297 pid = _pid;
298 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
299 }
300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700302 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700305 * Set once the system is ready to run third party code.
306 */
307 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700309 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700310 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
311 * method. This is to be synchronized with the secure settings keyed with
312 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
313 *
314 * <p>This can be transiently {@code null} when the system is re-initializing input method
315 * settings, e.g., the system locale is just changed.</p>
316 *
317 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
318 * {@link InputMethodManagerService}.</p>
319 *
320 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700322 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * The current binding sequence number, incremented every time there is
327 * a new bind performed.
328 */
329 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * The client that is currently bound to an input method.
333 */
334 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800337 * The last window token that we confirmed to be focused. This is always updated upon reports
338 * from the input method client. If the window state is already changed before the report is
339 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700340 */
341 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700343 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800344 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
345 */
346 ClientState mCurFocusedWindowClient;
347
348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * The input context last provided by the current client.
350 */
351 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700354 * The missing method flags for the input context last provided by the current client.
355 *
356 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
357 */
358 int mCurInputContextMissingMethods;
359
360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 * The attributes last provided by the current client.
362 */
363 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700366 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700368 *
369 * <p>This can be {@code null} when no input method is connected.</p>
370 *
371 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700373 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
satokab751aa2010-09-14 19:17:36 +0900377 * The current subtype of the current input method.
378 */
379 private InputMethodSubtype mCurrentSubtype;
380
satok4e4569d2010-11-19 18:45:53 +0900381 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900382 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700383 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900384
John Spurlocke0980502013-10-25 11:59:29 -0400385 // Was the keyguard locked when this client became current?
386 private boolean mCurClientInKeyguard;
387
satokab751aa2010-09-14 19:17:36 +0900388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * Set to true if our ServiceConnection is currently actively bound to
390 * a service (whether or not we have gotten its IBinder back yet).
391 */
392 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * Set if the client has asked for the input method to be shown.
396 */
397 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 /**
400 * Set if we were explicitly told to show the input method.
401 */
402 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
405 * Set if we were forced to be shown.
406 */
407 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 /**
410 * Set if we last told the input method to show itself.
411 */
412 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800415 * {@code true} if the current input method is in fullscreen mode.
416 */
417 boolean mInFullscreenMode;
418
419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 * The Intent used to connect to the current input method.
421 */
422 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * The token we have made for the currently active input method, to
426 * identify it in the future.
427 */
428 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * If non-null, this is the input method service we are currently connected
432 * to.
433 */
434 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Time that we last initiated a bind to the input method, to determine
438 * if we should try to disconnect and reconnect to it.
439 */
440 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /**
443 * Have we called mCurMethod.bindInput()?
444 */
445 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
448 * Currently enabled session. Only touched by service thread, not
449 * protected by a lock.
450 */
451 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700454 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700456 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800457
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900458 int mCurUserActionNotificationSequenceNumber = 0;
459
Joe Onorato857fd9b2011-01-27 15:08:35 -0800460 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900461
462 /**
463 * A set of status bits regarding the active IME.
464 *
465 * <p>This value is a combination of following two bits:</p>
466 * <dl>
467 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
468 * <dd>
469 * If this bit is ON, connected IME is ready to accept touch/key events.
470 * </dd>
471 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
472 * <dd>
473 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
474 * </dd>
475 * </dl>
476 * <em>Do not update this value outside of setImeWindowStatus.</em>
477 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800478 int mImeWindowVis;
479
Ken Wakasa05dbb652011-08-22 15:22:43 +0900480 private AlertDialog.Builder mDialogBuilder;
481 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700482 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900483 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700484 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900485 private InputMethodInfo[] mIms;
486 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800487 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700488 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800489 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900490 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
491 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500492 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700493 @HardKeyboardBehavior
494 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700497 int mUserId;
498 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700499 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800500 String mLastEnabled = "";
501
Yohei Yukawa81482972015-06-04 00:58:59 -0700502 /**
503 * <em>This constructor must be called within the lock.</em>
504 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 SettingsObserver(Handler handler) {
506 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700507 }
508
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800509 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700510 if (mRegistered && mUserId == userId) {
511 return;
512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700514 if (mRegistered) {
515 mContext.getContentResolver().unregisterContentObserver(this);
516 mRegistered = false;
517 }
518 if (mUserId != userId) {
519 mLastEnabled = "";
520 mUserId = userId;
521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700523 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900524 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700525 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900526 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700527 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700528 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700529 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800530 resolver.registerContentObserver(Settings.Secure.getUriFor(
531 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700532 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800534
Michael Wright7b5a96b2014-08-09 19:28:42 -0700535 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800536 final Uri showImeUri = Settings.Secure.getUriFor(
537 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
538 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
539 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700541 if (showImeUri.equals(uri)) {
542 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800543 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
544 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
545 mContext.getContentResolver(),
546 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
547 0, mUserId) == 1;
548 if (mAccessibilityRequestingNoSoftKeyboard) {
549 final boolean showRequested = mShowRequested;
550 hideCurrentInputLocked(0, null);
551 mShowRequested = showRequested;
552 } else if (mShowRequested) {
553 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
554 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700555 } else {
556 boolean enabledChanged = false;
557 String newEnabled = mSettings.getEnabledInputMethodsStr();
558 if (!mLastEnabled.equals(newEnabled)) {
559 mLastEnabled = newEnabled;
560 enabledChanged = true;
561 }
562 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700566
567 @Override
568 public String toString() {
569 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
570 + " mLastEnabled=" + mLastEnabled + "}";
571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800573
Yohei Yukawa79247822017-01-23 15:26:15 -0800574 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900575 @Override
576 public void onReceive(Context context, Intent intent) {
577 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700578 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900579 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700580 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900581 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800582 } else if (Intent.ACTION_USER_ADDED.equals(action)
583 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100584 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800585 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700586 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
587 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
588 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
589 final String prevValue = intent.getStringExtra(
590 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
591 final String newValue = intent.getStringExtra(
592 Intent.EXTRA_SETTING_NEW_VALUE);
593 restoreEnabledInputMethods(mContext, prevValue, newValue);
594 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800595 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
596 synchronized (mMethodMap) {
597 resetStateIfCurrentLocaleChangedLocked();
598 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900599 } else {
600 Slog.w(TAG, "Unexpected intent " + intent);
601 }
602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700605 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
606 // does not attempt to validate on the fly with any installed device policy, so must only
607 // be run in the context of initial device setup.
608 //
609 // TODO: Move this method to InputMethodUtils with adding unit tests.
610 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
611 if (DEBUG_RESTORE) {
612 Slog.i(TAG, "Restoring enabled input methods:");
613 Slog.i(TAG, "prev=" + prevValue);
614 Slog.i(TAG, " new=" + newValue);
615 }
616 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900617 ArrayMap<String, ArraySet<String>> prevMap =
618 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
619 ArrayMap<String, ArraySet<String>> newMap =
620 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700621
622 // Merge the restored ime+subtype enabled states into the live state
623 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
624 final String imeId = entry.getKey();
625 ArraySet<String> prevSubtypes = prevMap.get(imeId);
626 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700627 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700628 prevMap.put(imeId, prevSubtypes);
629 }
630 prevSubtypes.addAll(entry.getValue());
631 }
632
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700633 final String mergedImesAndSubtypesString =
634 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700635 if (DEBUG_RESTORE) {
636 Slog.i(TAG, "Merged IME string:");
637 Slog.i(TAG, " " + mergedImesAndSubtypesString);
638 }
639 Settings.Secure.putString(context.getContentResolver(),
640 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
641 }
642
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800643 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900644 private boolean isChangingPackagesOfCurrentUser() {
645 final int userId = getChangingUserId();
646 final boolean retval = userId == mSettings.getCurrentUserId();
647 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900648 if (!retval) {
649 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
650 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900651 }
652 return retval;
653 }
654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900657 if (!isChangingPackagesOfCurrentUser()) {
658 return false;
659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900661 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 final int N = mMethodList.size();
663 if (curInputMethodId != null) {
664 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800665 InputMethodInfo imi = mMethodList.get(i);
666 if (imi.getId().equals(curInputMethodId)) {
667 for (String pkg : packages) {
668 if (imi.getPackageName().equals(pkg)) {
669 if (!doit) {
670 return true;
671 }
satok723a27e2010-11-11 14:58:11 +0900672 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800673 chooseNewDefaultIMELocked();
674 return true;
675 }
676 }
677 }
678 }
679 }
680 }
681 return false;
682 }
683
684 @Override
685 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900686 if (!isChangingPackagesOfCurrentUser()) {
687 return;
688 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800689 synchronized (mMethodMap) {
690 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900691 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692 final int N = mMethodList.size();
693 if (curInputMethodId != null) {
694 for (int i=0; i<N; i++) {
695 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900696 final String imiId = imi.getId();
697 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698 curIm = imi;
699 }
satoke7c6998e2011-06-03 17:57:59 +0900700
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800701 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900702 if (isPackageModified(imi.getPackageName())) {
703 mFileManager.deleteAllInputMethodSubtypes(imiId);
704 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800705 if (change == PACKAGE_TEMPORARY_CHANGE
706 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800707 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800708 + imi.getComponent());
709 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800713
Yohei Yukawa94e33302016-02-12 19:37:03 -0800714 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800718 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800719 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800720 if (change == PACKAGE_TEMPORARY_CHANGE
721 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800722 ServiceInfo si = null;
723 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900724 si = mIPackageManager.getServiceInfo(
725 curIm.getComponent(), 0, mSettings.getCurrentUserId());
726 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800727 }
728 if (si == null) {
729 // Uh oh, current input method is no longer around!
730 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800731 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900732 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800733 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800734 changed = true;
735 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800736 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900737 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800738 }
739 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800740 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800741 }
satokab751aa2010-09-14 19:17:36 +0900742
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800743 if (curIm == null) {
744 // We currently don't have a default input method... is
745 // one now available?
746 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700747 } else if (!changed && isPackageModified(curIm.getPackageName())) {
748 // Even if the current input method is still available, mCurrentSubtype could
749 // be obsolete when the package is modified in practice.
750 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800752
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800753 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800754 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756 }
757 }
758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800759
Jeff Brownc28867a2013-03-26 15:42:39 -0700760 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900761 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700762 private final IInputMethod mMethod;
763 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800764
Jeff Brownc28867a2013-03-26 15:42:39 -0700765 MethodCallback(InputMethodManagerService imms, IInputMethod method,
766 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900767 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700768 mMethod = method;
769 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771
satoke7c6998e2011-06-03 17:57:59 +0900772 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700773 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700774 long ident = Binder.clearCallingIdentity();
775 try {
776 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
777 } finally {
778 Binder.restoreCallingIdentity(ident);
779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
satok01038492012-04-09 21:08:27 +0900783 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700784 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900785 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700786 public void onHardKeyboardStatusChange(boolean available) {
787 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
788 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900789 }
790
Michael Wright7b5a96b2014-08-09 19:28:42 -0700791 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900792 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700793 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900794 }
795 synchronized(mMethodMap) {
796 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
797 && mSwitchingDialog.isShowing()) {
798 mSwitchingDialogTitleView.findViewById(
799 com.android.internal.R.id.hard_keyboard_section).setVisibility(
800 available ? View.VISIBLE : View.GONE);
801 }
802 }
803 }
804 }
805
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800806 public static final class Lifecycle extends SystemService {
807 private InputMethodManagerService mService;
808
809 public Lifecycle(Context context) {
810 super(context);
811 mService = new InputMethodManagerService(context);
812 }
813
814 @Override
815 public void onStart() {
816 LocalServices.addService(InputMethodManagerInternal.class,
817 new LocalServiceImpl(mService.mHandler));
818 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
819 }
820
821 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800822 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700823 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800824 // TODO: Dispatch this to a worker thread as needed.
825 mService.onSwitchUser(userHandle);
826 }
827
828 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800829 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700830 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800831 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800832 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
833 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
834 .getService(Context.STATUS_BAR_SERVICE);
835 mService.systemRunning(statusBarService);
836 }
837 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800838
839 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700840 public void onUnlockUser(final @UserIdInt int userHandle) {
841 // Called on ActivityManager thread.
842 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700843 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800844 }
845 }
846
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800847 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800848 synchronized(mMethodMap) {
849 final int currentUserId = mSettings.getCurrentUserId();
850 if (DEBUG) {
851 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
852 }
853 if (userId != currentUserId) {
854 return;
855 }
856 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -0800857 if (mSystemReady) {
858 // We need to rebuild IMEs.
859 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
860 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
861 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800862 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800863 }
864
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800865 void onSwitchUser(@UserIdInt int userId) {
866 synchronized (mMethodMap) {
867 switchUserLocked(userId);
868 }
869 }
870
Seigo Nonaka7309b122015-08-17 18:34:13 -0700871 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900872 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800874 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700876 // Note: SettingsObserver doesn't register observers in its constructor.
877 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 mIWindowManager = IWindowManager.Stub.asInterface(
879 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700880 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800881 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900882 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 public void executeMessage(Message msg) {
884 handleMessage(msg);
885 }
Mita Yuned218c72012-12-06 17:18:25 -0800886 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800887 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800888 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900889 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700890 mHasFeature = context.getPackageManager().hasSystemFeature(
891 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500892 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700893 mHardKeyboardBehavior = mContext.getResources().getInteger(
894 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900895
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400896 Bundle extras = new Bundle();
897 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
898 mImeSwitcherNotification = new Notification.Builder(mContext)
899 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
900 .setWhen(0)
901 .setOngoing(true)
902 .addExtras(extras)
903 .setCategory(Notification.CATEGORY_SYSTEM)
904 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400905
satok7cfc0ed2011-06-20 21:29:36 +0900906 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900907 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900908
909 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900910
satok7cfc0ed2011-06-20 21:29:36 +0900911 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900912 int userId = 0;
913 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800914 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900915 } catch (RemoteException e) {
916 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
917 }
satok913a8922010-08-26 21:53:41 +0900918
satokd87c2592010-09-29 11:52:06 +0900919 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900920 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800921 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700922
Kenny Guy2a764942014-04-02 13:29:20 +0100923 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900924 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -0800925 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
926 mSettings, context);
satok5b927c432012-05-01 20:09:34 +0900927 }
928
satok5b927c432012-05-01 20:09:34 +0900929 private void resetDefaultImeLocked(Context context) {
930 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800931 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900932 return;
933 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800934 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -0800935 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800936 if (suitableImes.isEmpty()) {
937 Slog.i(TAG, "No default found");
938 return;
satok5b927c432012-05-01 20:09:34 +0900939 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800940 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -0800941 if (DEBUG) {
942 Slog.i(TAG, "Default found, using " + defIm.getId());
943 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800944 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900945 }
946
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900947 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
948 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900949 if (!mSystemReady) {
950 // not system ready
951 return;
952 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800953 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800955 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 if (!updateOnlyWhenLocaleChanged) {
957 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800958 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900959 }
960 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800961 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800963 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 if (!updateOnlyWhenLocaleChanged) {
965 final String selectedImiId = mSettings.getSelectedInputMethod();
966 if (TextUtils.isEmpty(selectedImiId)) {
967 // This is the first time of the user switch and
968 // set the current ime to the proper one.
969 resetDefaultImeLocked(mContext);
970 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900971 } else {
972 // If the locale is changed, needs to reset the default ime
973 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900974 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800975 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800976 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900977 if (!updateOnlyWhenLocaleChanged) {
978 try {
979 startInputInnerLocked();
980 } catch (RuntimeException e) {
981 Slog.w(TAG, "Unexpected exception", e);
982 }
983 }
984 }
985 }
986
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900987 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900988 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
989 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 }
991
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900992 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700993 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
994 + " currentUserId=" + mSettings.getCurrentUserId());
995
Yohei Yukawa81482972015-06-04 00:58:59 -0700996 // ContentObserver should be registered again when the user is changed
997 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800998
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800999 // If the system is not ready or the device is not yed unlocked by the user, then we use
1000 // copy-on-write settings.
1001 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001002 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001003 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001004 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001005 // InputMethodFileManager should be reset when the user is changed
1006 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001007 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001008
1009 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1010 + " defaultImiId=" + defaultImiId);
1011
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001012 // For secondary users, the list of enabled IMEs may not have been updated since the
1013 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1014 // not be empty even if the IME has been uninstalled by the primary user.
1015 // Even in such cases, IMMS works fine because it will find the most applicable
1016 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001017 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001018 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001019 initialUserSwitch /* needsToResetDefaultIme */);
1020 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001021 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1022 mSettings.getEnabledInputMethodListLocked(), newUserId,
1023 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001024 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001025
1026 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1027 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001028 }
1029
Kenny Guy2a764942014-04-02 13:29:20 +01001030 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001031 mSettings.setCurrentProfileIds(
1032 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001033 }
1034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 @Override
1036 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1037 throws RemoteException {
1038 try {
1039 return super.onTransact(code, data, reply, flags);
1040 } catch (RuntimeException e) {
1041 // The input method manager only throws security exceptions, so let's
1042 // log all others.
1043 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001044 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 throw e;
1047 }
1048 }
1049
Svetoslav Ganova0027152013-06-25 14:59:53 -07001050 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001051 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001052 if (DEBUG) {
1053 Slog.d(TAG, "--- systemReady");
1054 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001055 if (!mSystemReady) {
1056 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001057 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001058 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001059 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001060 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001061 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1062 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001063 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001064 if (mStatusBar != null) {
1065 mStatusBar.setIconVisibility(mSlotIme, false);
1066 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001067 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001068 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1069 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001070 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001071 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001072 mHardKeyboardListener);
1073 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001074
1075 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1076 mSettingsObserver.registerContentObserverLocked(currentUserId);
1077
1078 final IntentFilter broadcastFilter = new IntentFilter();
1079 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1080 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1081 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1082 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1083 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1084 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1085
1086 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1087 resetDefaultImeLocked(mContext);
1088 updateFromSettingsLocked(true);
1089 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1090 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1091 mContext.getBasePackageName());
1092
Dianne Hackborncc278702009-09-02 23:07:23 -07001093 try {
1094 startInputInnerLocked();
1095 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001096 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001097 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001098 }
1099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001102 // ---------------------------------------------------------------------------------------
1103 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1104 // 1) it comes from the system process
1105 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1106 private boolean calledFromValidUser() {
1107 final int uid = Binder.getCallingUid();
1108 final int userId = UserHandle.getUserId(uid);
1109 if (DEBUG) {
1110 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1111 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1112 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001113 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1114 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001115 }
Kenny Guy2a764942014-04-02 13:29:20 +01001116 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001118 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001119
1120 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1121 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1122 // must not manage background users' states in any functions.
1123 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1124 // by a token.
1125 if (mContext.checkCallingOrSelfPermission(
1126 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1127 == PackageManager.PERMISSION_GRANTED) {
1128 if (DEBUG) {
1129 Slog.d(TAG, "--- Access granted because the calling process has "
1130 + "the INTERACT_ACROSS_USERS_FULL permission");
1131 }
1132 return true;
1133 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001134 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001135 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1136 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001137 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001138 }
1139
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001140
1141 /**
1142 * Returns true iff the caller is identified to be the current input method with the token.
1143 * @param token The window token given to the input method when it was started.
1144 * @return true if and only if non-null valid token is specified.
1145 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001146 private boolean calledWithValidToken(@Nullable IBinder token) {
1147 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1148 if (DEBUG) {
1149 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1150 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1151 }
1152 return false;
1153 }
1154 if (token == null || token != mCurToken) {
1155 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1156 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1157 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001158 return false;
1159 }
1160 return true;
1161 }
1162
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001163 private boolean bindCurrentInputMethodService(
1164 Intent service, ServiceConnection conn, int flags) {
1165 if (service == null || conn == null) {
1166 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1167 return false;
1168 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001169 return mContext.bindServiceAsUser(service, conn, flags,
1170 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001171 }
1172
satoke7c6998e2011-06-03 17:57:59 +09001173 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 // TODO: Make this work even for non-current users?
1176 if (!calledFromValidUser()) {
1177 return Collections.emptyList();
1178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001180 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182 }
1183
satoke7c6998e2011-06-03 17:57:59 +09001184 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001186 // TODO: Make this work even for non-current users?
1187 if (!calledFromValidUser()) {
1188 return Collections.emptyList();
1189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001191 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
1194
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001195 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001196 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001197 * @return enabled subtypes of the specified imi
1198 */
satoke7c6998e2011-06-03 17:57:59 +09001199 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001200 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001201 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001202 // TODO: Make this work even for non-current users?
1203 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001204 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001205 }
satok67ddf9c2010-11-17 09:45:54 +09001206 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001207 final InputMethodInfo imi;
1208 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001209 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001210 } else {
1211 imi = mMethodMap.get(imiId);
1212 }
1213 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001214 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001215 }
1216 return mSettings.getEnabledInputMethodSubtypeListLocked(
1217 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001218 }
1219 }
1220
satoke7c6998e2011-06-03 17:57:59 +09001221 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public void addClient(IInputMethodClient client,
1223 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001224 if (!calledFromValidUser()) {
1225 return;
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 synchronized (mMethodMap) {
1228 mClients.put(client.asBinder(), new ClientState(client,
1229 inputContext, uid, pid));
1230 }
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001235 if (!calledFromValidUser()) {
1236 return;
1237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001239 ClientState cs = mClients.remove(client.asBinder());
1240 if (cs != null) {
1241 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001242 if (mCurClient == cs) {
1243 mCurClient = null;
1244 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001245 if (mCurFocusedWindowClient == cs) {
1246 mCurFocusedWindowClient = null;
1247 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 void executeOrSendMessage(IInterface target, Message msg) {
1253 if (target.asBinder() instanceof Binder) {
1254 mCaller.sendMessage(msg);
1255 } else {
1256 handleMessage(msg);
1257 msg.recycle();
1258 }
1259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
Yohei Yukawa33e81792015-11-17 21:14:42 -08001261 void unbindCurrentClientLocked(
1262 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001264 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 + mCurClient.client.asBinder());
1266 if (mBoundToMethod) {
1267 mBoundToMethod = false;
1268 if (mCurMethod != null) {
1269 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1270 MSG_UNBIND_INPUT, mCurMethod));
1271 }
1272 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001273
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001274 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1275 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001276 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1277 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001280
The Android Open Source Project10592532009-03-18 17:39:46 -07001281 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 private int getImeShowFlags() {
1286 int flags = 0;
1287 if (mShowForced) {
1288 flags |= InputMethod.SHOW_FORCED
1289 | InputMethod.SHOW_EXPLICIT;
1290 } else if (mShowExplicitlyRequested) {
1291 flags |= InputMethod.SHOW_EXPLICIT;
1292 }
1293 return flags;
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 private int getAppShowFlags() {
1297 int flags = 0;
1298 if (mShowForced) {
1299 flags |= InputMethodManager.SHOW_FORCED;
1300 } else if (!mShowExplicitlyRequested) {
1301 flags |= InputMethodManager.SHOW_IMPLICIT;
1302 }
1303 return flags;
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
Dianne Hackborn7663d802012-02-24 13:08:49 -08001306 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 if (!mBoundToMethod) {
1308 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1309 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1310 mBoundToMethod = true;
1311 }
1312 final SessionState session = mCurClient.curSession;
1313 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001314 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1315 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1316 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001318 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1319 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1320 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001323 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001326 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001327 (session.channel != null ? session.channel.dup() : null),
1328 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001330
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001331 InputBindResult startInputLocked(
1332 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001333 IInputMethodClient client, IInputContext inputContext,
1334 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001335 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 // If no method is currently selected, do nothing.
1337 if (mCurMethodId == null) {
1338 return mNoBinding;
1339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 ClientState cs = mClients.get(client.asBinder());
1342 if (cs == null) {
1343 throw new IllegalArgumentException("unknown client "
1344 + client.asBinder());
1345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
Yohei Yukawa74750f22016-03-22 12:54:22 -07001347 if (attribute == null) {
1348 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1349 + " uid=" + cs.uid + " pid=" + cs.pid);
1350 return null;
1351 }
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 try {
1354 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1355 // Check with the window manager to make sure this client actually
1356 // has a window with focus. If not, reject. This is thread safe
1357 // because if the focus changes some time before or after, the
1358 // next client receiving focus that has any interest in input will
1359 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001360 if (DEBUG) {
1361 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1362 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 return null;
1365 }
1366 } catch (RemoteException e) {
1367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001369 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1370 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001371 }
1372
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001373 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001374 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001375 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001376 // If no method is currently selected, do nothing.
1377 if (mCurMethodId == null) {
1378 return mNoBinding;
1379 }
1380
Yohei Yukawad57ba672015-06-08 16:39:46 -07001381 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1382 attribute.packageName)) {
1383 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1384 + " uid=" + cs.uid + " package=" + attribute.packageName);
1385 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001386 }
1387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001389 // Was the keyguard locked when switching over to the new client?
1390 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 // If the client is changing, we need to switch over to the new
1392 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001393 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001394 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001395 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396
1397 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001398 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001399 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001400 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 // Bump up the sequence for this client and attach it.
1405 mCurSeq++;
1406 if (mCurSeq <= 0) mCurSeq = 1;
1407 mCurClient = cs;
1408 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001409 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 // Check if the input method is changing.
1413 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1414 if (cs.curSession != null) {
1415 // Fast case: if we are already connected to the input method,
1416 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001417 return attachNewInputLocked(
1418 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
1420 if (mHaveConnection) {
1421 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 // Return to client, and we will get back with it when
1423 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001424 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001425 return new InputBindResult(null, null, mCurId, mCurSeq,
1426 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 } else if (SystemClock.uptimeMillis()
1428 < (mLastBindTime+TIME_TO_RECONNECT)) {
1429 // In this case we have connected to the service, but
1430 // don't yet have its interface. If it hasn't been too
1431 // long since we did the connection, we'll return to
1432 // the client and wait to get the service interface so
1433 // we can report back. If it has been too long, we want
1434 // to fall through so we can try a disconnect/reconnect
1435 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001436 return new InputBindResult(null, null, mCurId, mCurSeq,
1437 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001439 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1440 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
1443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001445 return startInputInnerLocked();
1446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001447
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001448 InputBindResult startInputInnerLocked() {
1449 if (mCurMethodId == null) {
1450 return mNoBinding;
1451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001453 if (!mSystemReady) {
1454 // If the system is not yet ready, we shouldn't be running third
1455 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001456 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1457 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1461 if (info == null) {
1462 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001464
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001465 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1468 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001469 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1470 com.android.internal.R.string.input_method_binding_label);
1471 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1472 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001473 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001474 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1475 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 mLastBindTime = SystemClock.uptimeMillis();
1477 mHaveConnection = true;
1478 mCurId = info.getId();
1479 mCurToken = new Binder();
1480 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001481 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001482 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 } catch (RemoteException e) {
1484 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001485 return new InputBindResult(null, null, mCurId, mCurSeq,
1486 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 } else {
1488 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001489 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 + mCurIntent);
1491 }
1492 return null;
1493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001495 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001496 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001497 IInputMethodClient client, IInputContext inputContext,
1498 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001499 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001500 if (!calledFromValidUser()) {
1501 return null;
1502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001504 if (DEBUG) {
1505 Slog.v(TAG, "startInput: reason="
1506 + InputMethodClient.getStartInputReason(startInputReason)
1507 + " client = " + client.asBinder()
1508 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001509 + " missingMethods="
1510 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001511 + " attribute=" + attribute
1512 + " controlFlags=#" + Integer.toHexString(controlFlags));
1513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 final long ident = Binder.clearCallingIdentity();
1515 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001516 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1517 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 } finally {
1519 Binder.restoreCallingIdentity(ident);
1520 }
1521 }
1522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001523
satoke7c6998e2011-06-03 17:57:59 +09001524 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 public void finishInput(IInputMethodClient client) {
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 onServiceConnected(ComponentName name, IBinder service) {
1530 synchronized (mMethodMap) {
1531 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1532 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001533 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001534 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001535 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001536 return;
1537 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001538 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001539 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1540 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001542 clearClientSessionLocked(mCurClient);
1543 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545 }
1546 }
1547 }
1548
Jeff Brownc28867a2013-03-26 15:42:39 -07001549 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1550 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 synchronized (mMethodMap) {
1552 if (mCurMethod != null && method != null
1553 && mCurMethod.asBinder() == method.asBinder()) {
1554 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001555 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001557 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001558 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 if (res.method != null) {
1560 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001561 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001563 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565 }
1566 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001567
1568 // Session abandoned. Close its associated input channel.
1569 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001571
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001572 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001573 if (mVisibleBound) {
1574 mContext.unbindService(mVisibleConnection);
1575 mVisibleBound = false;
1576 }
1577
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001578 if (mHaveConnection) {
1579 mContext.unbindService(this);
1580 mHaveConnection = false;
1581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001583 if (mCurToken != null) {
1584 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001585 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001586 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001587 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001588 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001589 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001590 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001591 } catch (RemoteException e) {
1592 }
1593 mCurToken = null;
1594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001595
The Android Open Source Project10592532009-03-18 17:39:46 -07001596 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001597 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001598 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001599
Yohei Yukawa33e81792015-11-17 21:14:42 -08001600 void resetCurrentMethodAndClient(
1601 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001602 mCurMethodId = null;
1603 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001604 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001605 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001606
1607 void requestClientSessionLocked(ClientState cs) {
1608 if (!cs.sessionRequested) {
1609 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1610 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1611 cs.sessionRequested = true;
1612 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1613 MSG_CREATE_SESSION, mCurMethod, channels[1],
1614 new MethodCallback(this, mCurMethod, channels[0])));
1615 }
1616 }
1617
1618 void clearClientSessionLocked(ClientState cs) {
1619 finishSessionLocked(cs.curSession);
1620 cs.curSession = null;
1621 cs.sessionRequested = false;
1622 }
1623
1624 private void finishSessionLocked(SessionState sessionState) {
1625 if (sessionState != null) {
1626 if (sessionState.session != null) {
1627 try {
1628 sessionState.session.finishSession();
1629 } catch (RemoteException e) {
1630 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001631 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001632 }
1633 sessionState.session = null;
1634 }
1635 if (sessionState.channel != null) {
1636 sessionState.channel.dispose();
1637 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001638 }
1639 }
1640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001642 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 if (mCurMethod != null) {
1644 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001645 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001647
Jeff Brownc28867a2013-03-26 15:42:39 -07001648 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001649 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 mCurMethod = null;
1651 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001652 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001653 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001654 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001655 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001657
satoke7c6998e2011-06-03 17:57:59 +09001658 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001660 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1661 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1662 // which is irregular but can eventually happen for everyone just by continuing using the
1663 // device. Thus it is important to make sure that all the internal states are properly
1664 // refreshed when this method is called back. Running
1665 // adb install -r <APK that implements the current IME>
1666 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001668 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 + " mCurIntent=" + mCurIntent);
1670 if (mCurMethod != null && mCurIntent != null
1671 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001672 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 // We consider this to be a new bind attempt, since the system
1674 // should now try to restart the service for us.
1675 mLastBindTime = SystemClock.uptimeMillis();
1676 mShowRequested = mInputShown;
1677 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001678 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
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) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001685 synchronized (mMethodMap) {
1686 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001687 return;
1688 }
1689 final long ident = Binder.clearCallingIdentity();
1690 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001692 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001693 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001694 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001697 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001698 CharSequence contentDescription = null;
1699 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001700 // Use PackageManager to load label
1701 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001702 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001703 mIPackageManager.getApplicationInfo(packageName, 0,
1704 mSettings.getCurrentUserId()));
1705 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001706 /* ignore */
1707 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001708 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001709 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001710 contentDescription != null
1711 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001712 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001715 } finally {
1716 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719 }
1720
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001721 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001722 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001723 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001724 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001725 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001726 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001727 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1728 // When physical keyboard is attached, we show the ime switcher (or notification if
1729 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1730 // exists in the IME switcher dialog. Might be OK to remove this condition once
1731 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1732 return true;
1733 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001734 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1735 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001736 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001737
1738 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1739 final int N = imis.size();
1740 if (N > 2) return true;
1741 if (N < 1) return false;
1742 int nonAuxCount = 0;
1743 int auxCount = 0;
1744 InputMethodSubtype nonAuxSubtype = null;
1745 InputMethodSubtype auxSubtype = null;
1746 for(int i = 0; i < N; ++i) {
1747 final InputMethodInfo imi = imis.get(i);
1748 final List<InputMethodSubtype> subtypes =
1749 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1750 final int subtypeCount = subtypes.size();
1751 if (subtypeCount == 0) {
1752 ++nonAuxCount;
1753 } else {
1754 for (int j = 0; j < subtypeCount; ++j) {
1755 final InputMethodSubtype subtype = subtypes.get(j);
1756 if (!subtype.isAuxiliary()) {
1757 ++nonAuxCount;
1758 nonAuxSubtype = subtype;
1759 } else {
1760 ++auxCount;
1761 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001762 }
1763 }
satok7cfc0ed2011-06-20 21:29:36 +09001764 }
1765 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001766 if (nonAuxCount > 1 || auxCount > 1) {
1767 return true;
1768 } else if (nonAuxCount == 1 && auxCount == 1) {
1769 if (nonAuxSubtype != null && auxSubtype != null
1770 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1771 || auxSubtype.overridesImplicitlyEnabledSubtype()
1772 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1773 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1774 return false;
1775 }
1776 return true;
1777 }
1778 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001779 }
1780
John Spurlocke0980502013-10-25 11:59:29 -04001781 private boolean isKeyguardLocked() {
1782 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1783 }
1784
satokdbf29502011-08-25 15:28:23 +09001785 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001786 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001787 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001788 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001789 return;
1790 }
1791
1792 synchronized (mMethodMap) {
1793 mImeWindowVis = vis;
1794 mBackDisposition = backDisposition;
1795 updateSystemUiLocked(token, vis, backDisposition);
1796 }
1797 }
1798
1799 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1800 synchronized (mMethodMap) {
1801 updateSystemUiLocked(token, vis, backDisposition);
1802 }
1803 }
1804
1805 // Caution! This method is called in this class. Handle multi-user carefully
1806 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1807 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001808 return;
1809 }
1810
1811 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1812 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001813 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001814 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001815 // apply policy for binder calls
1816 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1817 vis = 0;
satok06487a52010-10-29 11:37:18 +09001818 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001819 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1820 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1821 if (mStatusBar != null) {
1822 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1823 needsToShowImeSwitcher);
1824 }
1825 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1826 if (imi != null && needsToShowImeSwitcher) {
1827 // Used to load label
1828 final CharSequence title = mRes.getText(
1829 com.android.internal.R.string.select_input_method);
1830 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1831 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001832 mImeSwitcherNotification.setContentTitle(title)
1833 .setContentText(summary)
1834 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001835 try {
1836 if ((mNotificationManager != null)
1837 && !mIWindowManager.hasNavigationBar()) {
1838 if (DEBUG) {
1839 Slog.d(TAG, "--- show notification: label = " + summary);
1840 }
1841 mNotificationManager.notifyAsUser(null,
1842 com.android.internal.R.string.select_input_method,
1843 mImeSwitcherNotification.build(), UserHandle.ALL);
1844 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001845 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001846 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001847 }
1848 } else {
1849 if (mNotificationShown && mNotificationManager != null) {
1850 if (DEBUG) {
1851 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001852 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001853 mNotificationManager.cancelAsUser(null,
1854 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1855 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001856 }
satok06487a52010-10-29 11:37:18 +09001857 }
1858 } finally {
1859 Binder.restoreCallingIdentity(ident);
1860 }
1861 }
1862
satoke7c6998e2011-06-03 17:57:59 +09001863 @Override
satokf9f01002011-05-19 21:31:50 +09001864 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001865 if (!calledFromValidUser()) {
1866 return;
1867 }
satokf9f01002011-05-19 21:31:50 +09001868 synchronized (mMethodMap) {
1869 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1870 for (int i = 0; i < spans.length; ++i) {
1871 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001872 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001873 mSecureSuggestionSpans.put(ss, currentImi);
1874 }
1875 }
1876 }
1877 }
1878
satoke7c6998e2011-06-03 17:57:59 +09001879 @Override
satokf9f01002011-05-19 21:31:50 +09001880 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001881 if (!calledFromValidUser()) {
1882 return false;
1883 }
satokf9f01002011-05-19 21:31:50 +09001884 synchronized (mMethodMap) {
1885 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1886 // TODO: Do not send the intent if the process of the targetImi is already dead.
1887 if (targetImi != null) {
1888 final String[] suggestions = span.getSuggestions();
1889 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001890 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001891 final Intent intent = new Intent();
1892 // Ensures that only a class in the original IME package will receive the
1893 // notification.
satok42c5a162011-05-26 16:46:14 +09001894 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001895 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1896 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1897 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1898 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001899 final long ident = Binder.clearCallingIdentity();
1900 try {
1901 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1902 } finally {
1903 Binder.restoreCallingIdentity(ident);
1904 }
satokf9f01002011-05-19 21:31:50 +09001905 return true;
1906 }
1907 }
1908 return false;
1909 }
1910
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001911 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001912 updateInputMethodsFromSettingsLocked(enabledMayChange);
1913 updateKeyboardFromSettingsLocked();
1914 }
1915
1916 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001917 if (enabledMayChange) {
1918 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1919 for (int i=0; i<enabled.size(); i++) {
1920 // We allow the user to select "disabled until used" apps, so if they
1921 // are enabling one of those here we now need to make it enabled.
1922 InputMethodInfo imm = enabled.get(i);
1923 try {
1924 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1925 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1926 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001927 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001928 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001929 if (DEBUG) {
1930 Slog.d(TAG, "Update state(" + imm.getId()
1931 + "): DISABLED_UNTIL_USED -> DEFAULT");
1932 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001933 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1934 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001935 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1936 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001937 }
1938 } catch (RemoteException e) {
1939 }
1940 }
1941 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001942 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1943 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1944 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1945 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001946 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001947 // There is no input method selected, try to choose new applicable input method.
1948 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001949 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001950 }
1951 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001953 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001955 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001956 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
satokf3db1af2010-11-23 13:34:33 +09001958 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001959 } else {
1960 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001961 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001963 // Here is not the perfect place to reset the switching controller. Ideally
1964 // mSwitchingController and mSettings should be able to share the same state.
1965 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1966 // the same enabled IMEs list.
1967 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001968
1969 }
1970
1971 public void updateKeyboardFromSettingsLocked() {
1972 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1973 if (mSwitchingDialog != null
1974 && mSwitchingDialogTitleView != null
1975 && mSwitchingDialog.isShowing()) {
1976 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1977 com.android.internal.R.id.hard_keyboard_switch);
1978 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001981
Yohei Yukawab097b822015-12-01 10:43:08 -08001982 private void notifyInputMethodSubtypeChanged(final int userId,
1983 @Nullable final InputMethodInfo inputMethodInfo,
1984 @Nullable final InputMethodSubtype subtype) {
1985 final InputManagerInternal inputManagerInternal =
1986 LocalServices.getService(InputManagerInternal.class);
1987 if (inputManagerInternal != null) {
1988 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1989 }
1990 }
1991
satokab751aa2010-09-14 19:17:36 +09001992 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 InputMethodInfo info = mMethodMap.get(id);
1994 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001995 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
satokd81e9502012-05-21 12:58:45 +09001998 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002000 final int subtypeCount = info.getSubtypeCount();
2001 if (subtypeCount <= 0) {
2002 return;
satokcd7cd292010-11-20 15:46:23 +09002003 }
satokd81e9502012-05-21 12:58:45 +09002004 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2005 final InputMethodSubtype newSubtype;
2006 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2007 newSubtype = info.getSubtypeAt(subtypeId);
2008 } else {
2009 // If subtype is null, try to find the most applicable one from
2010 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002011 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002012 }
2013 if (newSubtype == null || oldSubtype == null) {
2014 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2015 + ", new subtype = " + newSubtype);
2016 return;
2017 }
2018 if (newSubtype != oldSubtype) {
2019 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2020 if (mCurMethod != null) {
2021 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002022 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002023 mCurMethod.changeInputMethodSubtype(newSubtype);
2024 } catch (RemoteException e) {
2025 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002026 return;
satokab751aa2010-09-14 19:17:36 +09002027 }
2028 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002029 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 return;
2032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002033
satokd81e9502012-05-21 12:58:45 +09002034 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 final long ident = Binder.clearCallingIdentity();
2036 try {
satokab751aa2010-09-14 19:17:36 +09002037 // Set a subtype to this input method.
2038 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002039 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2040 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2041 // because mCurMethodId is stored as a history in
2042 // setSelectedInputMethodAndSubtypeLocked().
2043 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002045 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002047 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002049 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002051 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 } finally {
2053 Binder.restoreCallingIdentity(ident);
2054 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002055
2056 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2057 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002059
satok42c5a162011-05-26 16:46:14 +09002060 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002061 public boolean showSoftInput(IInputMethodClient client, int flags,
2062 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002063 if (!calledFromValidUser()) {
2064 return false;
2065 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002066 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 long ident = Binder.clearCallingIdentity();
2068 try {
2069 synchronized (mMethodMap) {
2070 if (mCurClient == null || client == null
2071 || mCurClient.client.asBinder() != client.asBinder()) {
2072 try {
2073 // We need to check if this is the current client with
2074 // focus in the window manager, to allow this call to
2075 // be made before input is started in it.
2076 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002077 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002078 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
2080 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002081 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002084
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002085 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
2088 } finally {
2089 Binder.restoreCallingIdentity(ident);
2090 }
2091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
The Android Open Source Project4df24232009-03-05 14:34:35 -08002093 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002095 if (mAccessibilityRequestingNoSoftKeyboard) {
2096 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
Anna Galusza9b278112016-01-04 11:37:37 -08002098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2100 mShowExplicitlyRequested = true;
2101 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002102 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2103 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002105
Dianne Hackborncc278702009-09-02 23:07:23 -07002106 if (!mSystemReady) {
2107 return false;
2108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002109
The Android Open Source Project4df24232009-03-05 14:34:35 -08002110 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002112 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002113 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2114 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2115 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002117 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002118 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002119 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002120 | Context.BIND_TREAT_LIKE_ACTIVITY
2121 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002122 mVisibleBound = true;
2123 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002124 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002126 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 // The client has asked to have the input method shown, but
2128 // we have been sitting here too long with a connection to the
2129 // service and no interface received, so let's disconnect/connect
2130 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002131 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002133 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002135 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002136 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002137 } else {
2138 if (DEBUG) {
2139 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2140 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002143
The Android Open Source Project4df24232009-03-05 14:34:35 -08002144 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002146
satok42c5a162011-05-26 16:46:14 +09002147 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002148 public boolean hideSoftInput(IInputMethodClient client, int flags,
2149 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002150 if (!calledFromValidUser()) {
2151 return false;
2152 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002153 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 long ident = Binder.clearCallingIdentity();
2155 try {
2156 synchronized (mMethodMap) {
2157 if (mCurClient == null || client == null
2158 || mCurClient.client.asBinder() != client.asBinder()) {
2159 try {
2160 // We need to check if this is the current client with
2161 // focus in the window manager, to allow this call to
2162 // be made before input is started in it.
2163 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002164 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2165 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002166 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002169 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
2171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002172
Joe Onorato8a9b2202010-02-26 18:56:32 -08002173 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002174 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
2176 } finally {
2177 Binder.restoreCallingIdentity(ident);
2178 }
2179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002180
The Android Open Source Project4df24232009-03-05 14:34:35 -08002181 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2183 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002184 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 -08002185 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
2187 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002188 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 -08002189 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002191
2192 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2193 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2194 // to be updated with the new value sent from IME process. Even in such a transient state
2195 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2196 // application process as a valid request, and have even promised such a behavior with CTS
2197 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2198 // IMMS#InputShown indicates that the software keyboard is shown.
2199 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2200 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2201 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002202 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002203 if (shouldHideSoftInput) {
2204 // The IME will report its visible state again after the following message finally
2205 // delivered to the IME process as an IPC. Hence the inconsistency between
2206 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2207 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002208 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2209 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2210 res = true;
2211 } else {
2212 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002214 if (mHaveConnection && mVisibleBound) {
2215 mContext.unbindService(mVisibleConnection);
2216 mVisibleBound = false;
2217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 mInputShown = false;
2219 mShowRequested = false;
2220 mShowExplicitlyRequested = false;
2221 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002224
satok42c5a162011-05-26 16:46:14 +09002225 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002226 public InputBindResult startInputOrWindowGainedFocus(
2227 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2228 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002229 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002230 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002231 if (windowToken != null) {
2232 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002233 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002234 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002235 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2236 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002237 }
2238 }
2239
2240 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002241 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2242 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002243 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2244 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002245 // Needs to check the validity before clearing calling identity
2246 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002247 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 long ident = Binder.clearCallingIdentity();
2249 try {
2250 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002251 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2252 + InputMethodClient.getStartInputReason(startInputReason)
2253 + " client=" + client.asBinder()
2254 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002255 + " missingMethods="
2256 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002257 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002258 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002260 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002261
Dianne Hackborn7663d802012-02-24 13:08:49 -08002262 ClientState cs = mClients.get(client.asBinder());
2263 if (cs == null) {
2264 throw new IllegalArgumentException("unknown client "
2265 + client.asBinder());
2266 }
2267
2268 try {
2269 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2270 // Check with the window manager to make sure this client actually
2271 // has a window with focus. If not, reject. This is thread safe
2272 // because if the focus changes some time before or after, the
2273 // next client receiving focus that has any interest in input will
2274 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002275 if (DEBUG) {
2276 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2277 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2278 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002279 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002281 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002283
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002284 if (!calledFromValidUser) {
2285 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2286 Slog.w(TAG, "If you want to interect with IME, you need "
2287 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2288 hideCurrentInputLocked(0, null);
2289 return null;
2290 }
2291
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002292 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002293 if (DEBUG) {
2294 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2295 + " attribute=" + attribute + ", token = " + windowToken);
2296 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002297 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002298 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2299 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002300 }
2301 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002302 }
2303 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002304 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002305
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002306 // Should we auto-show the IME even if the caller has not
2307 // specified what should be done with it?
2308 // We only do this automatically if the window can resize
2309 // to accommodate the IME (so what the user sees will give
2310 // them good context without input information being obscured
2311 // by the IME) or if running on a large screen where there
2312 // is more room for the target window + IME.
2313 final boolean doAutoShow =
2314 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2315 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2316 || mRes.getConfiguration().isLayoutSizeAtLeast(
2317 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002318 final boolean isTextEditor =
2319 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2320
2321 // We want to start input before showing the IME, but after closing
2322 // it. We want to do this after closing it to help the IME disappear
2323 // more quickly (not get stuck behind it initializing itself for the
2324 // new focused input, even if its window wants to hide the IME).
2325 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2328 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002329 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2331 // There is no focus view, and this window will
2332 // be behind any soft input window, so hide the
2333 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002334 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002335 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002337 } else if (isTextEditor && doAutoShow && (softInputMode &
2338 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 // There is a focus view, and we are navigating forward
2340 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002341 // We only do this automatically if the window can resize
2342 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002343 // them good context without input information being obscured
2344 // by the IME) or if running on a large screen where there
2345 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002346 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002347 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002348 res = startInputUncheckedLocked(cs, inputContext,
2349 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002350 didStart = true;
2351 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002352 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354 break;
2355 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2356 // Do nothing.
2357 break;
2358 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2359 if ((softInputMode &
2360 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002361 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002362 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
2364 break;
2365 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002366 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
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 break;
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2370 if ((softInputMode &
2371 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002372 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002373 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002374 res = startInputUncheckedLocked(cs, inputContext,
2375 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002376 didStart = true;
2377 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002378 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380 break;
2381 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002382 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002383 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002384 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2385 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002386 didStart = true;
2387 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002388 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 break;
2390 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002391
2392 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002393 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002394 controlFlags);
2395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 }
2397 } finally {
2398 Binder.restoreCallingIdentity(ident);
2399 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002400
2401 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002403
satok42c5a162011-05-26 16:46:14 +09002404 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002405 public void showInputMethodPickerFromClient(
2406 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002407 if (!calledFromValidUser()) {
2408 return;
2409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 synchronized (mMethodMap) {
2411 if (mCurClient == null || client == null
2412 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002413 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002414 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 }
2416
satok440aab52010-11-25 09:43:11 +09002417 // Always call subtype picker, because subtype picker is a superset of input method
2418 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002419 mHandler.sendMessage(mCaller.obtainMessageI(
2420 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002421 }
2422 }
2423
satok42c5a162011-05-26 16:46:14 +09002424 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 if (!calledFromValidUser()) {
2427 return;
2428 }
satok28203512010-11-24 11:06:49 +09002429 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2430 }
2431
satok42c5a162011-05-26 16:46:14 +09002432 @Override
satok28203512010-11-24 11:06:49 +09002433 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002434 if (!calledFromValidUser()) {
2435 return;
2436 }
satok28203512010-11-24 11:06:49 +09002437 synchronized (mMethodMap) {
2438 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002439 setInputMethodWithSubtypeIdLocked(token, id,
2440 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2441 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002442 } else {
2443 setInputMethod(token, id);
2444 }
2445 }
satokab751aa2010-09-14 19:17:36 +09002446 }
2447
satok42c5a162011-05-26 16:46:14 +09002448 @Override
satokb416a712010-11-25 20:42:14 +09002449 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002450 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002451 if (!calledFromValidUser()) {
2452 return;
2453 }
satokb416a712010-11-25 20:42:14 +09002454 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002455 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2456 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002457 }
2458 }
2459
satok4fc87d62011-05-20 16:13:43 +09002460 @Override
satok735cf382010-11-11 20:40:09 +09002461 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002462 if (!calledFromValidUser()) {
2463 return false;
2464 }
satok735cf382010-11-11 20:40:09 +09002465 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002466 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002467 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002468 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002469 lastImi = mMethodMap.get(lastIme.first);
2470 } else {
2471 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002472 }
satok4fc87d62011-05-20 16:13:43 +09002473 String targetLastImiId = null;
2474 int subtypeId = NOT_A_SUBTYPE_ID;
2475 if (lastIme != null && lastImi != null) {
2476 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002477 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002478 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2479 : mCurrentSubtype.hashCode();
2480 // If the last IME is the same as the current IME and the last subtype is not
2481 // defined, there is no need to switch to the last IME.
2482 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2483 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002484 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002485 }
2486 }
2487
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002488 if (TextUtils.isEmpty(targetLastImiId)
2489 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002490 // This is a safety net. If the currentSubtype can't be added to the history
2491 // and the framework couldn't find the last ime, we will make the last ime be
2492 // the most applicable enabled keyboard subtype of the system imes.
2493 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2494 if (enabled != null) {
2495 final int N = enabled.size();
2496 final String locale = mCurrentSubtype == null
2497 ? mRes.getConfiguration().locale.toString()
2498 : mCurrentSubtype.getLocale();
2499 for (int i = 0; i < N; ++i) {
2500 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002501 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002502 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002503 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2504 InputMethodUtils.getSubtypes(imi),
2505 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002506 if (keyboardSubtype != null) {
2507 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002508 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002509 imi, keyboardSubtype.hashCode());
2510 if(keyboardSubtype.getLocale().equals(locale)) {
2511 break;
2512 }
2513 }
2514 }
2515 }
2516 }
2517 }
2518
2519 if (!TextUtils.isEmpty(targetLastImiId)) {
2520 if (DEBUG) {
2521 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2522 + ", from: " + mCurMethodId + ", " + subtypeId);
2523 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002524 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002525 return true;
2526 } else {
2527 return false;
2528 }
satok735cf382010-11-11 20:40:09 +09002529 }
2530 }
2531
satoke7c6998e2011-06-03 17:57:59 +09002532 @Override
satok688bd472012-02-09 20:09:17 +09002533 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002534 if (!calledFromValidUser()) {
2535 return false;
2536 }
satok688bd472012-02-09 20:09:17 +09002537 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002538 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002539 return false;
2540 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002541 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002542 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2543 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002544 if (nextSubtype == null) {
2545 return false;
2546 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002547 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2548 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002549 return true;
2550 }
2551 }
2552
2553 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002554 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2555 if (!calledFromValidUser()) {
2556 return false;
2557 }
2558 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002559 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002560 return false;
2561 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002562 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002563 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2564 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002565 if (nextSubtype == null) {
2566 return false;
2567 }
2568 return true;
2569 }
2570 }
2571
2572 @Override
satok68f1b782011-04-11 14:26:04 +09002573 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002574 if (!calledFromValidUser()) {
2575 return null;
2576 }
satok68f1b782011-04-11 14:26:04 +09002577 synchronized (mMethodMap) {
2578 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2579 // TODO: Handle the case of the last IME with no subtypes
2580 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2581 || TextUtils.isEmpty(lastIme.second)) return null;
2582 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2583 if (lastImi == null) return null;
2584 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002585 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002586 final int lastSubtypeId =
2587 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002588 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2589 return null;
2590 }
2591 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002592 } catch (NumberFormatException e) {
2593 return null;
2594 }
2595 }
2596 }
2597
satoke7c6998e2011-06-03 17:57:59 +09002598 @Override
satokee5e77c2011-09-02 18:50:15 +09002599 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002600 if (!calledFromValidUser()) {
2601 return;
2602 }
satok91e88122011-07-18 11:11:42 +09002603 // By this IPC call, only a process which shares the same uid with the IME can add
2604 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002605 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002606 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002607 if (!mSystemReady) {
2608 return;
2609 }
satok91e88122011-07-18 11:11:42 +09002610 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002611 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002612 final String[] packageInfos;
2613 try {
2614 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2615 } catch (RemoteException e) {
2616 Slog.e(TAG, "Failed to get package infos");
2617 return;
2618 }
satok91e88122011-07-18 11:11:42 +09002619 if (packageInfos != null) {
2620 final int packageNum = packageInfos.length;
2621 for (int i = 0; i < packageNum; ++i) {
2622 if (packageInfos[i].equals(imi.getPackageName())) {
2623 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002624 final long ident = Binder.clearCallingIdentity();
2625 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002626 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002627 } finally {
2628 Binder.restoreCallingIdentity(ident);
2629 }
satokee5e77c2011-09-02 18:50:15 +09002630 return;
satok91e88122011-07-18 11:11:42 +09002631 }
2632 }
2633 }
satoke7c6998e2011-06-03 17:57:59 +09002634 }
satokee5e77c2011-09-02 18:50:15 +09002635 return;
satoke7c6998e2011-06-03 17:57:59 +09002636 }
2637
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002638 @Override
2639 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002640 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002641 }
2642
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002643 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002644 public void clearLastInputMethodWindowForTransition(IBinder token) {
2645 if (!calledFromValidUser()) {
2646 return;
2647 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002648 synchronized (mMethodMap) {
2649 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002650 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002651 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002652 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002653 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002654 }
2655
2656 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002657 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002658 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002659 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002660 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002661 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002662 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2663 if (DEBUG) {
2664 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2665 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2666 + " actual: " + sequenceNumber);
2667 }
2668 return;
2669 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002670 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2671 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002672 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002673 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002674 }
2675 }
2676
satok28203512010-11-24 11:06:49 +09002677 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002679 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2680 }
2681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002683 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2684 if (token == null) {
2685 if (mContext.checkCallingOrSelfPermission(
2686 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2687 != PackageManager.PERMISSION_GRANTED) {
2688 throw new SecurityException(
2689 "Using null token requires permission "
2690 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002692 } else if (mCurToken != token) {
2693 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2694 + " token: " + token);
2695 return;
2696 }
2697
2698 final long ident = Binder.clearCallingIdentity();
2699 try {
2700 setInputMethodLocked(id, subtypeId);
2701 } finally {
2702 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 }
2704 }
2705
satok42c5a162011-05-26 16:46:14 +09002706 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002708 if (!calledFromValidUser()) {
2709 return;
2710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002712 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 return;
2714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 long ident = Binder.clearCallingIdentity();
2716 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002717 hideCurrentInputLocked(flags, null);
2718 } finally {
2719 Binder.restoreCallingIdentity(ident);
2720 }
2721 }
2722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002723
satok42c5a162011-05-26 16:46:14 +09002724 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002725 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002726 if (!calledFromValidUser()) {
2727 return;
2728 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002729 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002730 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002731 return;
2732 }
2733 long ident = Binder.clearCallingIdentity();
2734 try {
2735 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 } finally {
2737 Binder.restoreCallingIdentity(ident);
2738 }
2739 }
2740 }
2741
2742 void setEnabledSessionInMainThread(SessionState session) {
2743 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002744 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002746 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002747 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 } catch (RemoteException e) {
2749 }
2750 }
2751 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002752 if (mEnabledSession != null && mEnabledSession.session != null) {
2753 try {
2754 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2755 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2756 } catch (RemoteException e) {
2757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
2759 }
2760 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002761
satok42c5a162011-05-26 16:46:14 +09002762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002764 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002766 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002767 final boolean showAuxSubtypes;
2768 switch (msg.arg1) {
2769 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2770 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2771 // implemented so that auxiliary subtypes will be excluded when the soft
2772 // keyboard is invisible.
2773 showAuxSubtypes = mInputShown;
2774 break;
2775 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2776 showAuxSubtypes = true;
2777 break;
2778 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2779 showAuxSubtypes = false;
2780 break;
2781 default:
2782 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2783 return false;
2784 }
2785 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002786 return true;
2787
satok47a44912010-10-06 16:03:58 +09002788 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002789 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002790 return true;
2791
2792 case MSG_SHOW_IM_CONFIG:
2793 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002794 return true;
2795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 case MSG_UNBIND_INPUT:
2799 try {
2800 ((IInputMethod)msg.obj).unbindInput();
2801 } catch (RemoteException e) {
2802 // There is nothing interesting about the method dying.
2803 }
2804 return true;
2805 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002806 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 try {
2808 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2809 } catch (RemoteException e) {
2810 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002811 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 return true;
2813 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002814 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002816 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002817 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002818 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 } catch (RemoteException e) {
2820 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002821 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 return true;
2823 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002824 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002826 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002827 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002828 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 } catch (RemoteException e) {
2830 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002831 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002833 case MSG_HIDE_CURRENT_INPUT_METHOD:
2834 synchronized (mMethodMap) {
2835 hideCurrentInputLocked(0, null);
2836 }
2837 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002839 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002841 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2843 } catch (RemoteException e) {
2844 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002845 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002847 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002848 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002849 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002850 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002852 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002854 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002855 // Dispose the channel if the input method is not local to this process
2856 // because the remote proxy will get its own copy when unparceled.
2857 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002858 channel.dispose();
2859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002861 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002865
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002866 case MSG_START_INPUT: {
2867 int missingMethods = msg.arg1;
2868 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002870 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002872 session.method.startInput((IInputContext) args.arg2, missingMethods,
2873 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 } catch (RemoteException e) {
2875 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002876 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002878 }
2879 case MSG_RESTART_INPUT: {
2880 int missingMethods = msg.arg1;
2881 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002883 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002885 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2886 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 } catch (RemoteException e) {
2888 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002889 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002891 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002894
Yohei Yukawa33e81792015-11-17 21:14:42 -08002895 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002897 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 } catch (RemoteException e) {
2899 // There is nothing interesting about the last client dying.
2900 }
2901 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002902 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002903 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002904 IInputMethodClient client = (IInputMethodClient)args.arg1;
2905 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002907 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002909 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002910 } finally {
2911 // Dispose the channel if the input method is not local to this process
2912 // because the remote proxy will get its own copy when unparceled.
2913 if (res.channel != null && Binder.isProxy(client)) {
2914 res.channel.dispose();
2915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002917 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002919 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002920 case MSG_SET_ACTIVE:
2921 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002922 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07002923 } catch (RemoteException e) {
2924 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2925 + ((ClientState)msg.obj).pid + " uid "
2926 + ((ClientState)msg.obj).uid);
2927 }
2928 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002929 case MSG_SET_INTERACTIVE:
2930 handleSetInteractive(msg.arg1 != 0);
2931 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002932 case MSG_SWITCH_IME:
2933 handleSwitchInputMethod(msg.arg1 != 0);
2934 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002935 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2936 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002937 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002938 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002939 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002940 } catch (RemoteException e) {
2941 Slog.w(TAG, "Got RemoteException sending "
2942 + "setUserActionNotificationSequenceNumber("
2943 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002944 + clientState.pid + " uid "
2945 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002946 }
2947 return true;
2948 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002949 case MSG_REPORT_FULLSCREEN_MODE: {
2950 final boolean fullscreen = msg.arg1 != 0;
2951 final ClientState clientState = (ClientState)msg.obj;
2952 try {
2953 clientState.client.reportFullscreenMode(fullscreen);
2954 } catch (RemoteException e) {
2955 Slog.w(TAG, "Got RemoteException sending "
2956 + "reportFullscreen(" + fullscreen + ") notification to pid="
2957 + clientState.pid + " uid=" + clientState.uid);
2958 }
2959 return true;
2960 }
satok01038492012-04-09 21:08:27 +09002961
2962 // --------------------------------------------------------------
2963 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002964 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002965 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002966 case MSG_SYSTEM_UNLOCK_USER:
2967 final int userId = msg.arg1;
2968 onUnlockUser(userId);
2969 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 }
2971 return false;
2972 }
2973
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002974 private void handleSetInteractive(final boolean interactive) {
2975 synchronized (mMethodMap) {
2976 mIsInteractive = interactive;
2977 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2978
2979 // Inform the current client of the change in active status
2980 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002981 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
2982 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
2983 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002984 }
2985 }
2986 }
2987
Yohei Yukawaae61f712015-12-09 13:00:10 -08002988 private void handleSwitchInputMethod(final boolean forwardDirection) {
2989 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002990 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002991 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002992 if (nextSubtype == null) {
2993 return;
2994 }
2995 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002996 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2997 if (newInputMethodInfo == null) {
2998 return;
2999 }
3000 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3001 newInputMethodInfo, mCurrentSubtype);
3002 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003003 if (mSubtypeSwitchedByShortCutToast == null) {
3004 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3005 Toast.LENGTH_SHORT);
3006 } else {
3007 mSubtypeSwitchedByShortCutToast.setText(toastText);
3008 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003009 mSubtypeSwitchedByShortCutToast.show();
3010 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003011 }
3012 }
3013
satokdc9ddae2011-10-06 12:22:36 +09003014 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003015 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3016 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003017 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003018 if (DEBUG) {
3019 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3020 }
satok723a27e2010-11-11 14:58:11 +09003021 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003022 return true;
3023 }
3024
3025 return false;
3026 }
3027
Yohei Yukawa94e33302016-02-12 19:37:03 -08003028 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003029 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003030 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003031 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003032 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003033 if (!mSystemReady) {
3034 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3035 return;
3036 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003037 mMethodList.clear();
3038 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003039
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003040 // Use for queryIntentServicesAsUser
3041 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003043 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3044 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3045 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003046 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003048 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3049 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003050
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003051 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003052 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 for (int i = 0; i < services.size(); ++i) {
3054 ResolveInfo ri = services.get(i);
3055 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003056 final String imeId = InputMethodInfo.computeId(ri);
3057 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3058 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 + ": it does not require the permission "
3060 + android.Manifest.permission.BIND_INPUT_METHOD);
3061 continue;
3062 }
3063
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003064 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003066 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 try {
satoke7c6998e2011-06-03 17:57:59 +09003068 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003069 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003070 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003071 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072
3073 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003074 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003076 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003077 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003080
Yohei Yukawa859df052016-02-17 07:56:46 -08003081 // TODO: The following code should find better place to live.
3082 if (!resetDefaultEnabledIme) {
3083 boolean enabledImeFound = false;
3084 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3085 final int N = enabledImes.size();
3086 for (int i = 0; i < N; ++i) {
3087 final InputMethodInfo imi = enabledImes.get(i);
3088 if (mMethodList.contains(imi)) {
3089 enabledImeFound = true;
3090 break;
3091 }
3092 }
3093 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003094 if (DEBUG) {
3095 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3096 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003097 resetDefaultEnabledIme = true;
3098 resetSelectedInputMethodAndSubtypeLocked("");
3099 }
3100 }
3101
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003102 if (resetDefaultEnabledIme) {
3103 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003104 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003105 final int N = defaultEnabledIme.size();
3106 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003107 final InputMethodInfo imi = defaultEnabledIme.get(i);
3108 if (DEBUG) {
3109 Slog.d(TAG, "--- enable ime = " + imi);
3110 }
3111 setInputMethodEnabledLocked(imi.getId(), true);
3112 }
3113 }
3114
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003115 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003116 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003117 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003118 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3119 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003120 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003121 }
3122 } else {
3123 // Double check that the default IME is certainly enabled.
3124 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003125 }
3126 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003127 // Here is not the perfect place to reset the switching controller. Ideally
3128 // mSwitchingController and mSettings should be able to share the same state.
3129 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3130 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003131 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003135
satok217f5482010-12-15 05:19:19 +09003136 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003137 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003138 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003139 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3140 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003141 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003142 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003143 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003144 final int userId;
3145 synchronized (mMethodMap) {
3146 userId = mSettings.getCurrentUserId();
3147 }
3148 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003149 }
3150
3151 private void showConfigureInputMethods() {
3152 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3153 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3154 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3155 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003156 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003157 }
3158
satok2c93efc2012-04-02 19:33:47 +09003159 private boolean isScreenLocked() {
3160 return mKeyguardManager != null
3161 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3162 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003163
Seigo Nonaka14e13912015-05-06 21:04:13 -07003164 private void showInputMethodMenu(boolean showAuxSubtypes) {
3165 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003168 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003169
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003170 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003171 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003172 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003173
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003174 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003175 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003176 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3177 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003178 if (immis == null || immis.size() == 0) {
3179 return;
3180 }
3181
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003182 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183
satok688bd472012-02-09 20:09:17 +09003184 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003185 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003186 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003187
satokc3690562012-01-10 20:14:43 +09003188 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003189 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003190 if (currentSubtype != null) {
3191 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003192 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3193 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003194 }
3195 }
3196
Ken Wakasa761eb372011-03-04 19:06:18 +09003197 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003198 mIms = new InputMethodInfo[N];
3199 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003200 int checkedItem = 0;
3201 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003202 final ImeSubtypeListItem item = imList.get(i);
3203 mIms[i] = item.mImi;
3204 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003205 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003206 int subtypeId = mSubtypeIds[i];
3207 if ((subtypeId == NOT_A_SUBTYPE_ID)
3208 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3209 || (subtypeId == lastInputMethodSubtypeId)) {
3210 checkedItem = i;
3211 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003214
3215 final Context settingsContext = new ContextThemeWrapper(context,
3216 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3217
3218 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003219 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3220 @Override
3221 public void onCancel(DialogInterface dialog) {
3222 hideInputMethodMenu();
3223 }
3224 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003225
3226 final Context dialogContext = mDialogBuilder.getContext();
3227 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3228 com.android.internal.R.styleable.DialogPreference,
3229 com.android.internal.R.attr.alertDialogStyle, 0);
3230 final Drawable dialogIcon = a.getDrawable(
3231 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3232 a.recycle();
3233
3234 mDialogBuilder.setIcon(dialogIcon);
3235
Yohei Yukawad34e1482016-02-11 08:03:52 -08003236 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003237 final View tv = inflater.inflate(
3238 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3239 mDialogBuilder.setCustomTitle(tv);
3240
3241 // Setup layout for a toggle switch of the hardware keyboard
3242 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003243 mSwitchingDialogTitleView
3244 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003245 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003246 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003247 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003248 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003249 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003250 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3251 @Override
3252 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003253 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003254 // Ensure that the input method dialog is dismissed when changing
3255 // the hardware keyboard state.
3256 hideInputMethodMenu();
3257 }
3258 });
3259
Alan Viverette505e3ab2014-11-24 15:22:11 -08003260 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003261 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3262 final OnClickListener choiceListener = new OnClickListener() {
3263 @Override
3264 public void onClick(final DialogInterface dialog, final int which) {
3265 synchronized (mMethodMap) {
3266 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3267 || mSubtypeIds.length <= which) {
3268 return;
satok01038492012-04-09 21:08:27 +09003269 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003270 final InputMethodInfo im = mIms[which];
3271 int subtypeId = mSubtypeIds[which];
3272 adapter.mCheckedItem = which;
3273 adapter.notifyDataSetChanged();
3274 hideInputMethodMenu();
3275 if (im != null) {
3276 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3277 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003278 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003279 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003281 }
3282 }
3283 };
3284 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003287 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003288 final Window w = mSwitchingDialog.getWindow();
3289 final WindowManager.LayoutParams attrs = w.getAttributes();
3290 w.setType(TYPE_INPUT_METHOD_DIALOG);
3291 // Use an alternate token for the dialog for that window manager can group the token
3292 // with other IME windows based on type vs. grouping based on whichever token happens
3293 // to get selected by the system later on.
3294 attrs.token = mSwitchingDialogToken;
3295 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3296 attrs.setTitle("Select input method");
3297 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003298 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 mSwitchingDialog.show();
3300 }
3301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003302
Ken Wakasa05dbb652011-08-22 15:22:43 +09003303 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3304 private final LayoutInflater mInflater;
3305 private final int mTextViewResourceId;
3306 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003307 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003308 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3309 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3310 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003311
Ken Wakasa05dbb652011-08-22 15:22:43 +09003312 mTextViewResourceId = textViewResourceId;
3313 mItemsList = itemsList;
3314 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003315 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003316 }
3317
3318 @Override
3319 public View getView(int position, View convertView, ViewGroup parent) {
3320 final View view = convertView != null ? convertView
3321 : mInflater.inflate(mTextViewResourceId, null);
3322 if (position < 0 || position >= mItemsList.size()) return view;
3323 final ImeSubtypeListItem item = mItemsList.get(position);
3324 final CharSequence imeName = item.mImeName;
3325 final CharSequence subtypeName = item.mSubtypeName;
3326 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3327 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3328 if (TextUtils.isEmpty(subtypeName)) {
3329 firstTextView.setText(imeName);
3330 secondTextView.setVisibility(View.GONE);
3331 } else {
3332 firstTextView.setText(subtypeName);
3333 secondTextView.setText(imeName);
3334 secondTextView.setVisibility(View.VISIBLE);
3335 }
3336 final RadioButton radioButton =
3337 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3338 radioButton.setChecked(position == mCheckedItem);
3339 return view;
3340 }
3341 }
3342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003344 synchronized (mMethodMap) {
3345 hideInputMethodMenuLocked();
3346 }
3347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003348
The Android Open Source Project10592532009-03-18 17:39:46 -07003349 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003350 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351
The Android Open Source Project10592532009-03-18 17:39:46 -07003352 if (mSwitchingDialog != null) {
3353 mSwitchingDialog.dismiss();
3354 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003356
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003357 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003358 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003359 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003363
satok42c5a162011-05-26 16:46:14 +09003364 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003366 // TODO: Make this work even for non-current users?
3367 if (!calledFromValidUser()) {
3368 return false;
3369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 synchronized (mMethodMap) {
3371 if (mContext.checkCallingOrSelfPermission(
3372 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3373 != PackageManager.PERMISSION_GRANTED) {
3374 throw new SecurityException(
3375 "Requires permission "
3376 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3377 }
Anna Galusza9b278112016-01-04 11:37:37 -08003378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 long ident = Binder.clearCallingIdentity();
3380 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003381 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 } finally {
3383 Binder.restoreCallingIdentity(ident);
3384 }
3385 }
3386 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003387
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003388 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3389 // Make sure this is a valid input method.
3390 InputMethodInfo imm = mMethodMap.get(id);
3391 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003392 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003393 }
3394
satokd87c2592010-09-29 11:52:06 +09003395 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3396 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003397
satokd87c2592010-09-29 11:52:06 +09003398 if (enabled) {
3399 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3400 if (pair.first.equals(id)) {
3401 // We are enabling this input method, but it is already enabled.
3402 // Nothing to do. The previous state was enabled.
3403 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003404 }
3405 }
satokd87c2592010-09-29 11:52:06 +09003406 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3407 // Previous state was disabled.
3408 return false;
3409 } else {
3410 StringBuilder builder = new StringBuilder();
3411 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3412 builder, enabledInputMethodsList, id)) {
3413 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003414 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003415 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3416 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3417 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003418 }
3419 // Previous state was enabled.
3420 return true;
3421 } else {
3422 // We are disabling the input method but it is already disabled.
3423 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003424 return false;
3425 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003426 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003427 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003428
satok723a27e2010-11-11 14:58:11 +09003429 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3430 boolean setSubtypeOnly) {
3431 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003432 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003433
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003434 mCurUserActionNotificationSequenceNumber =
3435 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3436 if (DEBUG) {
3437 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3438 + mCurUserActionNotificationSequenceNumber);
3439 }
3440
3441 if (mCurClient != null && mCurClient.client != null) {
3442 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3443 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003444 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003445 }
3446
satok723a27e2010-11-11 14:58:11 +09003447 // Set Subtype here
3448 if (imi == null || subtypeId < 0) {
3449 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003450 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003451 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003452 if (subtypeId < imi.getSubtypeCount()) {
3453 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3454 mSettings.putSelectedSubtype(subtype.hashCode());
3455 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003456 } else {
3457 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003458 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003459 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003460 }
satokab751aa2010-09-14 19:17:36 +09003461 }
satok723a27e2010-11-11 14:58:11 +09003462
Yohei Yukawa68645a62016-02-17 07:54:20 -08003463 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003464 // Set InputMethod here
3465 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3466 }
3467 }
3468
3469 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3470 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3471 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3472 // newDefaultIme is empty when there is no candidate for the selected IME.
3473 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3474 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3475 if (subtypeHashCode != null) {
3476 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003477 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003478 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003479 } catch (NumberFormatException e) {
3480 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3481 }
3482 }
3483 }
3484 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003485 }
3486
satok4e4569d2010-11-19 18:45:53 +09003487 // If there are no selected shortcuts, tries finding the most applicable ones.
3488 private Pair<InputMethodInfo, InputMethodSubtype>
3489 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3490 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3491 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003492 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003493 boolean foundInSystemIME = false;
3494
3495 // Search applicable subtype for each InputMethodInfo
3496 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003497 final String imiId = imi.getId();
3498 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3499 continue;
3500 }
satokcd7cd292010-11-20 15:46:23 +09003501 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003502 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003503 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003504 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003505 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003506 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003507 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003508 }
satokdf31ae62011-01-15 06:19:44 +09003509 // 2. Search by the system locale from enabledSubtypes.
3510 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003511 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003512 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003513 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003514 }
satoka86f5e42011-09-02 17:12:42 +09003515 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003516 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003517 final ArrayList<InputMethodSubtype> subtypesForSearch =
3518 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003519 ? InputMethodUtils.getSubtypes(imi)
3520 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003521 // 4. Search by the current subtype's locale from all subtypes.
3522 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003523 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003524 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003525 }
3526 // 5. Search by the system locale from all subtypes.
3527 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003528 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003529 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003530 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003531 }
satokcd7cd292010-11-20 15:46:23 +09003532 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003533 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003534 // The current input method is the most applicable IME.
3535 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003536 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003537 break;
satok7599a7f2010-12-22 13:45:23 +09003538 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003539 // The system input method is 2nd applicable IME.
3540 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003541 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003542 if ((imi.getServiceInfo().applicationInfo.flags
3543 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3544 foundInSystemIME = true;
3545 }
satok4e4569d2010-11-19 18:45:53 +09003546 }
3547 }
3548 }
3549 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003550 if (mostApplicableIMI != null) {
3551 Slog.w(TAG, "Most applicable shortcut input method was:"
3552 + mostApplicableIMI.getId());
3553 if (mostApplicableSubtype != null) {
3554 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3555 + "," + mostApplicableSubtype.getMode() + ","
3556 + mostApplicableSubtype.getLocale());
3557 }
3558 }
satok4e4569d2010-11-19 18:45:53 +09003559 }
satokcd7cd292010-11-20 15:46:23 +09003560 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003561 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003562 } else {
3563 return null;
3564 }
3565 }
3566
satokab751aa2010-09-14 19:17:36 +09003567 /**
3568 * @return Return the current subtype of this input method.
3569 */
satok42c5a162011-05-26 16:46:14 +09003570 @Override
satokab751aa2010-09-14 19:17:36 +09003571 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003572 // TODO: Make this work even for non-current users?
3573 if (!calledFromValidUser()) {
3574 return null;
3575 }
3576 synchronized (mMethodMap) {
3577 return getCurrentInputMethodSubtypeLocked();
3578 }
3579 }
3580
3581 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003582 if (mCurMethodId == null) {
3583 return null;
3584 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003585 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003586 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3587 if (imi == null || imi.getSubtypeCount() == 0) {
3588 return null;
satok4e4569d2010-11-19 18:45:53 +09003589 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003590 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003591 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3592 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003593 if (subtypeId == NOT_A_SUBTYPE_ID) {
3594 // If there are no selected subtypes, the framework will try to find
3595 // the most applicable subtype from explicitly or implicitly enabled
3596 // subtypes.
3597 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003598 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003599 // If there is only one explicitly or implicitly enabled subtype,
3600 // just returns it.
3601 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3602 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3603 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003604 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003605 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003606 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003607 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003608 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003609 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3610 true);
satok4e4569d2010-11-19 18:45:53 +09003611 }
satok3ef8b292010-11-23 06:06:29 +09003612 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003613 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003614 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003615 }
3616 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003617 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003618 }
3619
satok4e4569d2010-11-19 18:45:53 +09003620 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003621 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003622 @Override
satok4e4569d2010-11-19 18:45:53 +09003623 public List getShortcutInputMethodsAndSubtypes() {
3624 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003625 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003626 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003627 // If there are no selected shortcut subtypes, the framework will try to find
3628 // the most applicable subtype from all subtypes whose mode is
3629 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003630 Pair<InputMethodInfo, InputMethodSubtype> info =
3631 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003632 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003633 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003634 ret.add(info.first);
3635 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003636 }
satok3da92232011-01-11 22:46:30 +09003637 return ret;
satokf3db1af2010-11-23 13:34:33 +09003638 }
satokf3db1af2010-11-23 13:34:33 +09003639 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3640 ret.add(imi);
3641 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3642 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003643 }
3644 }
satokf3db1af2010-11-23 13:34:33 +09003645 return ret;
satok4e4569d2010-11-19 18:45:53 +09003646 }
3647 }
3648
satok42c5a162011-05-26 16:46:14 +09003649 @Override
satokb66d2872010-11-10 01:04:04 +09003650 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003651 // TODO: Make this work even for non-current users?
3652 if (!calledFromValidUser()) {
3653 return false;
3654 }
satokb66d2872010-11-10 01:04:04 +09003655 synchronized (mMethodMap) {
3656 if (subtype != null && mCurMethodId != null) {
3657 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003658 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003659 if (subtypeId != NOT_A_SUBTYPE_ID) {
3660 setInputMethodLocked(mCurMethodId, subtypeId);
3661 return true;
3662 }
3663 }
3664 return false;
3665 }
3666 }
3667
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003668 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003669 private static class InputMethodFileManager {
3670 private static final String SYSTEM_PATH = "system";
3671 private static final String INPUT_METHOD_PATH = "inputmethod";
3672 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3673 private static final String NODE_SUBTYPES = "subtypes";
3674 private static final String NODE_SUBTYPE = "subtype";
3675 private static final String NODE_IMI = "imi";
3676 private static final String ATTR_ID = "id";
3677 private static final String ATTR_LABEL = "label";
3678 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003679 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003680 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003681 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003682 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3683 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3684 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003685 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003686 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3687 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003688 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003689 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003690 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003691 if (methodMap == null) {
3692 throw new NullPointerException("methodMap is null");
3693 }
3694 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003695 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003696 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3697 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003698 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003699 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003700 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3701 }
3702 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3703 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3704 if (!subtypeFile.exists()) {
3705 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003706 writeAdditionalInputMethodSubtypes(
3707 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003708 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003709 readAdditionalInputMethodSubtypes(
3710 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003711 }
3712 }
3713
3714 private void deleteAllInputMethodSubtypes(String imiId) {
3715 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003716 mAdditionalSubtypesMap.remove(imiId);
3717 writeAdditionalInputMethodSubtypes(
3718 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003719 }
3720 }
3721
3722 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003723 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003724 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003725 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003726 final int N = additionalSubtypes.length;
3727 for (int i = 0; i < N; ++i) {
3728 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003729 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003730 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003731 } else {
3732 Slog.w(TAG, "Duplicated subtype definition found: "
3733 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003734 }
3735 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003736 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3737 writeAdditionalInputMethodSubtypes(
3738 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003739 }
3740 }
3741
3742 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3743 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003744 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003745 }
3746 }
3747
3748 private static void writeAdditionalInputMethodSubtypes(
3749 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3750 HashMap<String, InputMethodInfo> methodMap) {
3751 // Safety net for the case that this function is called before methodMap is set.
3752 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3753 FileOutputStream fos = null;
3754 try {
3755 fos = subtypesFile.startWrite();
3756 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003757 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003758 out.startDocument(null, true);
3759 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3760 out.startTag(null, NODE_SUBTYPES);
3761 for (String imiId : allSubtypes.keySet()) {
3762 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3763 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3764 continue;
3765 }
3766 out.startTag(null, NODE_IMI);
3767 out.attribute(null, ATTR_ID, imiId);
3768 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3769 final int N = subtypesList.size();
3770 for (int i = 0; i < N; ++i) {
3771 final InputMethodSubtype subtype = subtypesList.get(i);
3772 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003773 if (subtype.hasSubtypeId()) {
3774 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3775 String.valueOf(subtype.getSubtypeId()));
3776 }
satoke7c6998e2011-06-03 17:57:59 +09003777 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3778 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3779 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003780 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3781 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003782 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3783 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3784 out.attribute(null, ATTR_IS_AUXILIARY,
3785 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003786 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3787 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003788 out.endTag(null, NODE_SUBTYPE);
3789 }
3790 out.endTag(null, NODE_IMI);
3791 }
3792 out.endTag(null, NODE_SUBTYPES);
3793 out.endDocument();
3794 subtypesFile.finishWrite(fos);
3795 } catch (java.io.IOException e) {
3796 Slog.w(TAG, "Error writing subtypes", e);
3797 if (fos != null) {
3798 subtypesFile.failWrite(fos);
3799 }
3800 }
3801 }
3802
3803 private static void readAdditionalInputMethodSubtypes(
3804 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3805 if (allSubtypes == null || subtypesFile == null) return;
3806 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003807 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003808 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003809 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003810 int type = parser.getEventType();
3811 // Skip parsing until START_TAG
3812 while ((type = parser.next()) != XmlPullParser.START_TAG
3813 && type != XmlPullParser.END_DOCUMENT) {}
3814 String firstNodeName = parser.getName();
3815 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3816 throw new XmlPullParserException("Xml doesn't start with subtypes");
3817 }
3818 final int depth =parser.getDepth();
3819 String currentImiId = null;
3820 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3821 while (((type = parser.next()) != XmlPullParser.END_TAG
3822 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3823 if (type != XmlPullParser.START_TAG)
3824 continue;
3825 final String nodeName = parser.getName();
3826 if (NODE_IMI.equals(nodeName)) {
3827 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3828 if (TextUtils.isEmpty(currentImiId)) {
3829 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3830 continue;
3831 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003832 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003833 allSubtypes.put(currentImiId, tempSubtypesArray);
3834 } else if (NODE_SUBTYPE.equals(nodeName)) {
3835 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3836 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3837 continue;
3838 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003839 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003840 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003841 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003842 parser.getAttributeValue(null, ATTR_LABEL));
3843 final String imeSubtypeLocale =
3844 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003845 final String languageTag =
3846 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003847 final String imeSubtypeMode =
3848 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3849 final String imeSubtypeExtraValue =
3850 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003851 final boolean isAuxiliary = "1".equals(String.valueOf(
3852 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003853 final boolean isAsciiCapable = "1".equals(String.valueOf(
3854 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003855 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003856 .setSubtypeNameResId(label)
3857 .setSubtypeIconResId(icon)
3858 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003859 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003860 .setSubtypeMode(imeSubtypeMode)
3861 .setSubtypeExtraValue(imeSubtypeExtraValue)
3862 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003863 .setIsAsciiCapable(isAsciiCapable);
3864 final String subtypeIdString =
3865 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3866 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003867 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003868 }
3869 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003870 }
3871 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003872 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3873 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003874 return;
satoke7c6998e2011-06-03 17:57:59 +09003875 }
3876 }
3877 }
3878
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003879 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3880 @NonNull
3881 private final Handler mHandler;
3882
3883 LocalServiceImpl(@NonNull final Handler handler) {
3884 mHandler = handler;
3885 }
3886
3887 @Override
3888 public void setInteractive(boolean interactive) {
3889 // Do everything in handler so as not to block the caller.
3890 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3891 interactive ? 1 : 0, 0));
3892 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003893
3894 @Override
3895 public void switchInputMethod(boolean forwardDirection) {
3896 // Do everything in handler so as not to block the caller.
3897 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3898 forwardDirection ? 1 : 0, 0));
3899 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003900
3901 @Override
3902 public void hideCurrentInputMethod() {
3903 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3904 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3905 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003906 }
3907
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003908 private static String imeWindowStatusToString(final int imeWindowVis) {
3909 final StringBuilder sb = new StringBuilder();
3910 boolean first = true;
3911 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3912 sb.append("Active");
3913 first = false;
3914 }
3915 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3916 if (!first) {
3917 sb.append("|");
3918 }
3919 sb.append("Visible");
3920 }
3921 return sb.toString();
3922 }
3923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003925 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3926 @Nullable Uri contentUri, @Nullable String packageName) {
3927 if (!calledFromValidUser()) {
3928 return null;
3929 }
3930
3931 if (token == null) {
3932 throw new NullPointerException("token");
3933 }
3934 if (packageName == null) {
3935 throw new NullPointerException("packageName");
3936 }
3937 if (contentUri == null) {
3938 throw new NullPointerException("contentUri");
3939 }
3940 final String contentUriScheme = contentUri.getScheme();
3941 if (!"content".equals(contentUriScheme)) {
3942 throw new InvalidParameterException("contentUri must have content scheme");
3943 }
3944
3945 synchronized (mMethodMap) {
3946 final int uid = Binder.getCallingUid();
3947 if (mCurMethodId == null) {
3948 return null;
3949 }
3950 if (mCurToken != token) {
3951 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3952 + " token=" + token);
3953 return null;
3954 }
3955 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3956 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3957 // nature of our system. Let's compare it with our internal record.
3958 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3959 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3960 + mCurAttribute.packageName + " packageName=" + packageName);
3961 return null;
3962 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003963 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003964 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003965 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003966 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003967 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
3968 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
3969 imeUserId);
3970 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
3971 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
3972 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
3973 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
3974 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
3975 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
3976 // actually allowed to "uid", which is guaranteed to be the IME's one.
3977 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
3978 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07003979 }
3980 }
3981
3982 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003983 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
3984 if (!calledFromValidUser()) {
3985 return;
3986 }
3987 synchronized (mMethodMap) {
3988 if (!calledWithValidToken(token)) {
3989 return;
3990 }
3991 if (mCurClient != null && mCurClient.client != null) {
3992 mInFullscreenMode = fullscreen;
3993 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3994 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
3995 }
3996 }
3997 }
3998
3999 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4001 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4002 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4005 + Binder.getCallingPid()
4006 + ", uid=" + Binder.getCallingUid());
4007 return;
4008 }
4009
4010 IInputMethod method;
4011 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004012 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 synchronized (mMethodMap) {
4017 p.println("Current Input Method Manager state:");
4018 int N = mMethodList.size();
4019 p.println(" Input Methods:");
4020 for (int i=0; i<N; i++) {
4021 InputMethodInfo info = mMethodList.get(i);
4022 p.println(" InputMethod #" + i + ":");
4023 info.dump(p, " ");
4024 }
4025 p.println(" Clients:");
4026 for (ClientState ci : mClients.values()) {
4027 p.println(" Client " + ci + ":");
4028 p.println(" client=" + ci.client);
4029 p.println(" inputContext=" + ci.inputContext);
4030 p.println(" sessionRequested=" + ci.sessionRequested);
4031 p.println(" curSession=" + ci.curSession);
4032 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004033 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004035 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4036 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004037 focusedWindowClient = mCurFocusedWindowClient;
4038 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4040 + " mBoundToMethod=" + mBoundToMethod);
4041 p.println(" mCurToken=" + mCurToken);
4042 p.println(" mCurIntent=" + mCurIntent);
4043 method = mCurMethod;
4044 p.println(" mCurMethod=" + mCurMethod);
4045 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004046 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 p.println(" mShowRequested=" + mShowRequested
4048 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4049 + " mShowForced=" + mShowForced
4050 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004051 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004052 p.println(" mCurUserActionNotificationSequenceNumber="
4053 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004054 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004055 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004056 p.println(" mSwitchingController:");
4057 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004058 p.println(" mSettings:");
4059 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004061
Jeff Brownb88102f2010-09-08 11:49:43 -07004062 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 pw.flush();
4065 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004066 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4067 } catch (IOException | RemoteException e) {
4068 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004070 } else {
4071 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004073
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004074 if (focusedWindowClient != null && client != focusedWindowClient) {
4075 p.println(" ");
4076 p.println("Warning: Current input method client doesn't match the last focused. "
4077 + "window.");
4078 p.println("Dumping input method client in the last focused window just in case.");
4079 p.println(" ");
4080 pw.flush();
4081 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004082 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4083 } catch (IOException | RemoteException e) {
4084 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004085 }
4086 }
4087
Jeff Brownb88102f2010-09-08 11:49:43 -07004088 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 pw.flush();
4091 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004092 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4093 } catch (IOException | RemoteException e) {
4094 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004096 } else {
4097 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 }
4099 }
4100}