blob: 3dfbdc392b13b890a3b0d282f0f895a585b7ea0f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080024import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070025import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090026import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090028import com.android.internal.inputmethod.InputMethodUtils;
29import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070031import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070032import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090033import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.view.IInputContext;
35import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import com.android.internal.view.IInputMethodClient;
37import com.android.internal.view.IInputMethodManager;
38import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070039import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080041import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080042import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
satoke7c6998e2011-06-03 17:57:59 +090044import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090046import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070048import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070049import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070050import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080051import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080052import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070053import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070055import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090056import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090057import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090058import android.app.Notification;
59import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070060import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090061import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080063import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ContentResolver;
65import android.content.Context;
66import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090068import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090070import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070072import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090073import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.pm.PackageManager;
75import android.content.pm.ResolveInfo;
76import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070077import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.res.Resources;
79import android.content.res.TypedArray;
80import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080081import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080082import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080083import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070084import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040086import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070087import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090088import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Handler;
90import android.os.IBinder;
91import android.os.IInterface;
92import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070093import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070095import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080097import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.ServiceManager;
99import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900100import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800101import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.provider.Settings;
103import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900104import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700105import android.util.ArrayMap;
106import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700107import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900109import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900110import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.util.PrintWriterPrinter;
112import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900113import android.util.Slog;
114import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900115import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700117import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900118import android.view.LayoutInflater;
119import android.view.View;
120import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700121import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700123import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900124import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700126import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import android.view.inputmethod.InputMethod;
128import android.view.inputmethod.InputMethodInfo;
129import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700130import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900131import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900132import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900133import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900134import android.widget.CompoundButton;
135import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900136import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900137import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900138import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700139import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
satoke7c6998e2011-06-03 17:57:59 +0900141import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900143import java.io.FileInputStream;
144import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.io.IOException;
146import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700147import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100148import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700149import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900151import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.util.HashMap;
153import java.util.List;
154
155/**
156 * This class provides a system service that manages input methods.
157 */
158public class InputMethodManagerService extends IInputMethodManager.Stub
159 implements ServiceConnection, Handler.Callback {
160 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700161 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700162 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700164 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
165 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
166 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 static final int MSG_UNBIND_INPUT = 1000;
169 static final int MSG_BIND_INPUT = 1010;
170 static final int MSG_SHOW_SOFT_INPUT = 1020;
171 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700172 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 static final int MSG_ATTACH_TOKEN = 1040;
174 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_START_INPUT = 2000;
177 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
Yohei Yukawa33e81792015-11-17 21:14:42 -0800179 static final int MSG_UNBIND_CLIENT = 3000;
180 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700181 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700182 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900183 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800184 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
satok01038492012-04-09 21:08:27 +0900186 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
187
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700188 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
189
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700190 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800191
satokf9f01002011-05-19 21:31:50 +0900192 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
193
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900194 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900195 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900196
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700197 @Retention(SOURCE)
198 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
199 private @interface HardKeyboardBehavior {
200 int WIRELESS_AFFORDANCE = 0;
201 int WIRED_AFFORDANCE = 1;
202 }
satok4e4569d2010-11-19 18:45:53 +0900203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800205 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900207 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700210 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700212 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900213 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900214 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900215 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800216 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900218 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 // All known input methods. mMethodMap also serves as the global
221 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700222 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
223 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900224 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700225 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900226 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700228 // Used to bring IME service up to visible adjustment while it is being shown.
229 final ServiceConnection mVisibleConnection = new ServiceConnection() {
230 @Override public void onServiceConnected(ComponentName name, IBinder service) {
231 }
232
233 @Override public void onServiceDisconnected(ComponentName name) {
234 }
235 };
236 boolean mVisibleBound = false;
237
satok7cfc0ed2011-06-20 21:29:36 +0900238 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700239 private NotificationManager mNotificationManager;
240 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700241 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400242 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700243 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900244 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900245 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900246 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900247
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 /**
415 * The Intent used to connect to the current input method.
416 */
417 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * The token we have made for the currently active input method, to
421 * identify it in the future.
422 */
423 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * If non-null, this is the input method service we are currently connected
427 * to.
428 */
429 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 /**
432 * Time that we last initiated a bind to the input method, to determine
433 * if we should try to disconnect and reconnect to it.
434 */
435 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 /**
438 * Have we called mCurMethod.bindInput()?
439 */
440 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /**
443 * Currently enabled session. Only touched by service thread, not
444 * protected by a lock.
445 */
446 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700449 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700451 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900453 int mCurUserActionNotificationSequenceNumber = 0;
454
Joe Onorato857fd9b2011-01-27 15:08:35 -0800455 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900456
457 /**
458 * A set of status bits regarding the active IME.
459 *
460 * <p>This value is a combination of following two bits:</p>
461 * <dl>
462 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
463 * <dd>
464 * If this bit is ON, connected IME is ready to accept touch/key events.
465 * </dd>
466 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
467 * <dd>
468 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
469 * </dd>
470 * </dl>
471 * <em>Do not update this value outside of setImeWindowStatus.</em>
472 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800473 int mImeWindowVis;
474
Ken Wakasa05dbb652011-08-22 15:22:43 +0900475 private AlertDialog.Builder mDialogBuilder;
476 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700477 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900478 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700479 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900480 private InputMethodInfo[] mIms;
481 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800482 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700483 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800484 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900485 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
486 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500487 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700488 @HardKeyboardBehavior
489 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700492 int mUserId;
493 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700494 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800495 String mLastEnabled = "";
496
Yohei Yukawa81482972015-06-04 00:58:59 -0700497 /**
498 * <em>This constructor must be called within the lock.</em>
499 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 SettingsObserver(Handler handler) {
501 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700502 }
503
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800504 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700505 if (mRegistered && mUserId == userId) {
506 return;
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700509 if (mRegistered) {
510 mContext.getContentResolver().unregisterContentObserver(this);
511 mRegistered = false;
512 }
513 if (mUserId != userId) {
514 mLastEnabled = "";
515 mUserId = userId;
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700518 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900519 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700520 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900521 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700522 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700523 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700524 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800525 resolver.registerContentObserver(Settings.Secure.getUriFor(
526 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700527 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800529
Michael Wright7b5a96b2014-08-09 19:28:42 -0700530 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800531 final Uri showImeUri = Settings.Secure.getUriFor(
532 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
533 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
534 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700536 if (showImeUri.equals(uri)) {
537 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800538 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
539 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
540 mContext.getContentResolver(),
541 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
542 0, mUserId) == 1;
543 if (mAccessibilityRequestingNoSoftKeyboard) {
544 final boolean showRequested = mShowRequested;
545 hideCurrentInputLocked(0, null);
546 mShowRequested = showRequested;
547 } else if (mShowRequested) {
548 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
549 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700550 } else {
551 boolean enabledChanged = false;
552 String newEnabled = mSettings.getEnabledInputMethodsStr();
553 if (!mLastEnabled.equals(newEnabled)) {
554 mLastEnabled = newEnabled;
555 enabledChanged = true;
556 }
557 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700561
562 @Override
563 public String toString() {
564 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
565 + " mLastEnabled=" + mLastEnabled + "}";
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900569 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900570 @Override
571 public void onReceive(Context context, Intent intent) {
572 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700573 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900574 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700575 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900576 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800577 } else if (Intent.ACTION_USER_ADDED.equals(action)
578 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100579 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800580 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700581 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
582 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
583 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
584 final String prevValue = intent.getStringExtra(
585 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
586 final String newValue = intent.getStringExtra(
587 Intent.EXTRA_SETTING_NEW_VALUE);
588 restoreEnabledInputMethods(mContext, prevValue, newValue);
589 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900590 } else {
591 Slog.w(TAG, "Unexpected intent " + intent);
592 }
593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800595
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700596 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
597 // does not attempt to validate on the fly with any installed device policy, so must only
598 // be run in the context of initial device setup.
599 //
600 // TODO: Move this method to InputMethodUtils with adding unit tests.
601 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
602 if (DEBUG_RESTORE) {
603 Slog.i(TAG, "Restoring enabled input methods:");
604 Slog.i(TAG, "prev=" + prevValue);
605 Slog.i(TAG, " new=" + newValue);
606 }
607 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900608 ArrayMap<String, ArraySet<String>> prevMap =
609 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
610 ArrayMap<String, ArraySet<String>> newMap =
611 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700612
613 // Merge the restored ime+subtype enabled states into the live state
614 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
615 final String imeId = entry.getKey();
616 ArraySet<String> prevSubtypes = prevMap.get(imeId);
617 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700618 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700619 prevMap.put(imeId, prevSubtypes);
620 }
621 prevSubtypes.addAll(entry.getValue());
622 }
623
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700624 final String mergedImesAndSubtypesString =
625 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700626 if (DEBUG_RESTORE) {
627 Slog.i(TAG, "Merged IME string:");
628 Slog.i(TAG, " " + mergedImesAndSubtypesString);
629 }
630 Settings.Secure.putString(context.getContentResolver(),
631 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
632 }
633
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800634 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 private boolean isChangingPackagesOfCurrentUser() {
636 final int userId = getChangingUserId();
637 final boolean retval = userId == mSettings.getCurrentUserId();
638 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900639 if (!retval) {
640 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
641 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900642 }
643 return retval;
644 }
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800647 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900648 if (!isChangingPackagesOfCurrentUser()) {
649 return false;
650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900652 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 final int N = mMethodList.size();
654 if (curInputMethodId != null) {
655 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 InputMethodInfo imi = mMethodList.get(i);
657 if (imi.getId().equals(curInputMethodId)) {
658 for (String pkg : packages) {
659 if (imi.getPackageName().equals(pkg)) {
660 if (!doit) {
661 return true;
662 }
satok723a27e2010-11-11 14:58:11 +0900663 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800664 chooseNewDefaultIMELocked();
665 return true;
666 }
667 }
668 }
669 }
670 }
671 }
672 return false;
673 }
674
675 @Override
676 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900677 if (!isChangingPackagesOfCurrentUser()) {
678 return;
679 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 synchronized (mMethodMap) {
681 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900682 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800683 final int N = mMethodList.size();
684 if (curInputMethodId != null) {
685 for (int i=0; i<N; i++) {
686 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900687 final String imiId = imi.getId();
688 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800689 curIm = imi;
690 }
satoke7c6998e2011-06-03 17:57:59 +0900691
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900693 if (isPackageModified(imi.getPackageName())) {
694 mFileManager.deleteAllInputMethodSubtypes(imiId);
695 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800696 if (change == PACKAGE_TEMPORARY_CHANGE
697 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800698 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800699 + imi.getComponent());
700 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702 }
703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800704
Yohei Yukawa94e33302016-02-12 19:37:03 -0800705 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800708
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800709 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800710 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800711 if (change == PACKAGE_TEMPORARY_CHANGE
712 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800713 ServiceInfo si = null;
714 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900715 si = mIPackageManager.getServiceInfo(
716 curIm.getComponent(), 0, mSettings.getCurrentUserId());
717 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800718 }
719 if (si == null) {
720 // Uh oh, current input method is no longer around!
721 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800722 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900723 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800724 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800725 changed = true;
726 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800727 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900728 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800729 }
730 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800731 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800732 }
satokab751aa2010-09-14 19:17:36 +0900733
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800734 if (curIm == null) {
735 // We currently don't have a default input method... is
736 // one now available?
737 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700738 } else if (!changed && isPackageModified(curIm.getPackageName())) {
739 // Even if the current input method is still available, mCurrentSubtype could
740 // be obsolete when the package is modified in practice.
741 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800744 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800745 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
748 }
749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750
Jeff Brownc28867a2013-03-26 15:42:39 -0700751 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900752 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700753 private final IInputMethod mMethod;
754 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
Jeff Brownc28867a2013-03-26 15:42:39 -0700756 MethodCallback(InputMethodManagerService imms, IInputMethod method,
757 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900758 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700759 mMethod = method;
760 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
satoke7c6998e2011-06-03 17:57:59 +0900763 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700764 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700765 long ident = Binder.clearCallingIdentity();
766 try {
767 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
768 } finally {
769 Binder.restoreCallingIdentity(ident);
770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
satok01038492012-04-09 21:08:27 +0900774 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700775 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900776 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700777 public void onHardKeyboardStatusChange(boolean available) {
778 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
779 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900780 }
781
Michael Wright7b5a96b2014-08-09 19:28:42 -0700782 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900783 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700784 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900785 }
786 synchronized(mMethodMap) {
787 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
788 && mSwitchingDialog.isShowing()) {
789 mSwitchingDialogTitleView.findViewById(
790 com.android.internal.R.id.hard_keyboard_section).setVisibility(
791 available ? View.VISIBLE : View.GONE);
792 }
793 }
794 }
795 }
796
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800797 public static final class Lifecycle extends SystemService {
798 private InputMethodManagerService mService;
799
800 public Lifecycle(Context context) {
801 super(context);
802 mService = new InputMethodManagerService(context);
803 }
804
805 @Override
806 public void onStart() {
807 LocalServices.addService(InputMethodManagerInternal.class,
808 new LocalServiceImpl(mService.mHandler));
809 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
810 }
811
812 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800813 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700814 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800815 // TODO: Dispatch this to a worker thread as needed.
816 mService.onSwitchUser(userHandle);
817 }
818
819 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800820 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700821 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800822 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800823 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
824 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
825 .getService(Context.STATUS_BAR_SERVICE);
826 mService.systemRunning(statusBarService);
827 }
828 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800829
830 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700831 public void onUnlockUser(final @UserIdInt int userHandle) {
832 // Called on ActivityManager thread.
833 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700834 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800835 }
836 }
837
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800838 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800839 synchronized(mMethodMap) {
840 final int currentUserId = mSettings.getCurrentUserId();
841 if (DEBUG) {
842 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
843 }
844 if (userId != currentUserId) {
845 return;
846 }
847 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
848 // We need to rebuild IMEs.
849 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
850 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
851 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800852 }
853
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800854 void onSwitchUser(@UserIdInt int userId) {
855 synchronized (mMethodMap) {
856 switchUserLocked(userId);
857 }
858 }
859
Seigo Nonaka7309b122015-08-17 18:34:13 -0700860 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900861 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800863 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700865 // Note: SettingsObserver doesn't register observers in its constructor.
866 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 mIWindowManager = IWindowManager.Stub.asInterface(
868 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700869 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800870 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900871 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 public void executeMessage(Message msg) {
873 handleMessage(msg);
874 }
Mita Yuned218c72012-12-06 17:18:25 -0800875 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800876 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800877 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900878 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700879 mHasFeature = context.getPackageManager().hasSystemFeature(
880 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500881 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700882 mHardKeyboardBehavior = mContext.getResources().getInteger(
883 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900884
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400885 Bundle extras = new Bundle();
886 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
887 mImeSwitcherNotification = new Notification.Builder(mContext)
888 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
889 .setWhen(0)
890 .setOngoing(true)
891 .addExtras(extras)
892 .setCategory(Notification.CATEGORY_SYSTEM)
893 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400894
satok7cfc0ed2011-06-20 21:29:36 +0900895 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900896 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900897
898 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900899
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900900 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800902 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
903 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700904 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900905 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906
satok7cfc0ed2011-06-20 21:29:36 +0900907 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 int userId = 0;
909 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800910 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900911 } catch (RemoteException e) {
912 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
913 }
satok81f8b7c2012-12-04 20:42:56 +0900914 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900915
satokd87c2592010-09-29 11:52:06 +0900916 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900917 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800918 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700919
Kenny Guy2a764942014-04-02 13:29:20 +0100920 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900921 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900922 synchronized (mMethodMap) {
923 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
924 mSettings, context);
925 }
satok0a1bcf42012-05-16 19:26:31 +0900926
927 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900928 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900929 if (DEBUG) {
930 Slog.d(TAG, "Initial default ime = " + defaultImiId);
931 }
satok0a1bcf42012-05-16 19:26:31 +0900932 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
933
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900934 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800935 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900936 }
satokd87c2592010-09-29 11:52:06 +0900937 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938
satok0a1bcf42012-05-16 19:26:31 +0900939 if (!mImeSelectedOnBoot) {
940 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900941 synchronized (mMethodMap) {
942 resetDefaultImeLocked(context);
943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800945
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900946 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700947 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900948 updateFromSettingsLocked(true);
949 }
satok5b927c432012-05-01 20:09:34 +0900950
951 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
952 // according to the new system locale.
953 final IntentFilter filter = new IntentFilter();
954 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
955 mContext.registerReceiver(
956 new BroadcastReceiver() {
957 @Override
958 public void onReceive(Context context, Intent intent) {
959 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900960 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900961 }
962 }
963 }, filter);
964 }
965
satok5b927c432012-05-01 20:09:34 +0900966 private void resetDefaultImeLocked(Context context) {
967 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800968 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900969 return;
970 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800971 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
972 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
973 if (suitableImes.isEmpty()) {
974 Slog.i(TAG, "No default found");
975 return;
satok5b927c432012-05-01 20:09:34 +0900976 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800977 final InputMethodInfo defIm = suitableImes.get(0);
978 Slog.i(TAG, "Default found, using " + defIm.getId());
979 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900980 }
981
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900982 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
983 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 if (!mSystemReady) {
985 // not system ready
986 return;
987 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800988 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800990 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 if (!updateOnlyWhenLocaleChanged) {
992 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800993 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900994 }
995 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800996 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900997 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800998 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 if (!updateOnlyWhenLocaleChanged) {
1000 final String selectedImiId = mSettings.getSelectedInputMethod();
1001 if (TextUtils.isEmpty(selectedImiId)) {
1002 // This is the first time of the user switch and
1003 // set the current ime to the proper one.
1004 resetDefaultImeLocked(mContext);
1005 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001006 } else {
1007 // If the locale is changed, needs to reset the default ime
1008 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001009 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001010 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001011 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001012 if (!updateOnlyWhenLocaleChanged) {
1013 try {
1014 startInputInnerLocked();
1015 } catch (RuntimeException e) {
1016 Slog.w(TAG, "Unexpected exception", e);
1017 }
1018 }
1019 }
1020 }
1021
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001022 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001023 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1024 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001025 }
1026
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001027 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001028 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1029 + " currentUserId=" + mSettings.getCurrentUserId());
1030
Yohei Yukawa81482972015-06-04 00:58:59 -07001031 // ContentObserver should be registered again when the user is changed
1032 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001033
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001034 // If the system is not ready or the device is not yed unlocked by the user, then we use
1035 // copy-on-write settings.
1036 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001037 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001038 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001039 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001040 // InputMethodFileManager should be reset when the user is changed
1041 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001042 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001043
1044 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1045 + " defaultImiId=" + defaultImiId);
1046
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001047 // For secondary users, the list of enabled IMEs may not have been updated since the
1048 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1049 // not be empty even if the IME has been uninstalled by the primary user.
1050 // Even in such cases, IMMS works fine because it will find the most applicable
1051 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001052 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001053 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001054 initialUserSwitch /* needsToResetDefaultIme */);
1055 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001056 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1057 mSettings.getEnabledInputMethodListLocked(), newUserId,
1058 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001059 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001060
1061 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1062 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001063 }
1064
Kenny Guy2a764942014-04-02 13:29:20 +01001065 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001066 mSettings.setCurrentProfileIds(
1067 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001068 }
1069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 @Override
1071 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1072 throws RemoteException {
1073 try {
1074 return super.onTransact(code, data, reply, flags);
1075 } catch (RuntimeException e) {
1076 // The input method manager only throws security exceptions, so let's
1077 // log all others.
1078 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001079 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 throw e;
1082 }
1083 }
1084
Svetoslav Ganova0027152013-06-25 14:59:53 -07001085 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001086 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001087 if (DEBUG) {
1088 Slog.d(TAG, "--- systemReady");
1089 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001090 if (!mSystemReady) {
1091 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001092 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001093 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001094 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001095 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1096 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001097 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001098 if (mStatusBar != null) {
1099 mStatusBar.setIconVisibility(mSlotIme, false);
1100 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001101 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001102 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1103 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001104 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001105 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001106 mHardKeyboardListener);
1107 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001108 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001109 if (!mImeSelectedOnBoot) {
1110 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001111 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001112 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1113 mSettings.getEnabledInputMethodListLocked(),
1114 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001115 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001116 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001117 try {
1118 startInputInnerLocked();
1119 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001120 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001121 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001122 }
1123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001126 // ---------------------------------------------------------------------------------------
1127 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1128 // 1) it comes from the system process
1129 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1130 private boolean calledFromValidUser() {
1131 final int uid = Binder.getCallingUid();
1132 final int userId = UserHandle.getUserId(uid);
1133 if (DEBUG) {
1134 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1135 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1136 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001137 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1138 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001139 }
Kenny Guy2a764942014-04-02 13:29:20 +01001140 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001141 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001142 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001143
1144 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1145 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1146 // must not manage background users' states in any functions.
1147 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1148 // by a token.
1149 if (mContext.checkCallingOrSelfPermission(
1150 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1151 == PackageManager.PERMISSION_GRANTED) {
1152 if (DEBUG) {
1153 Slog.d(TAG, "--- Access granted because the calling process has "
1154 + "the INTERACT_ACROSS_USERS_FULL permission");
1155 }
1156 return true;
1157 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001158 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1159 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001160 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001161 }
1162
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001163
1164 /**
1165 * Returns true iff the caller is identified to be the current input method with the token.
1166 * @param token The window token given to the input method when it was started.
1167 * @return true if and only if non-null valid token is specified.
1168 */
1169 private boolean calledWithValidToken(IBinder token) {
1170 if (token == null || mCurToken != token) {
1171 return false;
1172 }
1173 return true;
1174 }
1175
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001176 private boolean bindCurrentInputMethodService(
1177 Intent service, ServiceConnection conn, int flags) {
1178 if (service == null || conn == null) {
1179 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1180 return false;
1181 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001182 return mContext.bindServiceAsUser(service, conn, flags,
1183 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001184 }
1185
satoke7c6998e2011-06-03 17:57:59 +09001186 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001188 // TODO: Make this work even for non-current users?
1189 if (!calledFromValidUser()) {
1190 return Collections.emptyList();
1191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001193 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 }
1196
satoke7c6998e2011-06-03 17:57:59 +09001197 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001199 // TODO: Make this work even for non-current users?
1200 if (!calledFromValidUser()) {
1201 return Collections.emptyList();
1202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001204 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206 }
1207
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001208 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001209 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001210 * @return enabled subtypes of the specified imi
1211 */
satoke7c6998e2011-06-03 17:57:59 +09001212 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001213 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001214 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001215 // TODO: Make this work even for non-current users?
1216 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001217 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001218 }
satok67ddf9c2010-11-17 09:45:54 +09001219 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001220 final InputMethodInfo imi;
1221 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001222 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001223 } else {
1224 imi = mMethodMap.get(imiId);
1225 }
1226 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001227 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001228 }
1229 return mSettings.getEnabledInputMethodSubtypeListLocked(
1230 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001231 }
1232 }
1233
satoke7c6998e2011-06-03 17:57:59 +09001234 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 public void addClient(IInputMethodClient client,
1236 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001237 if (!calledFromValidUser()) {
1238 return;
1239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 synchronized (mMethodMap) {
1241 mClients.put(client.asBinder(), new ClientState(client,
1242 inputContext, uid, pid));
1243 }
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
satoke7c6998e2011-06-03 17:57:59 +09001246 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001248 if (!calledFromValidUser()) {
1249 return;
1250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001252 ClientState cs = mClients.remove(client.asBinder());
1253 if (cs != null) {
1254 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001255 if (mCurClient == cs) {
1256 mCurClient = null;
1257 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001258 if (mCurFocusedWindowClient == cs) {
1259 mCurFocusedWindowClient = null;
1260 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 void executeOrSendMessage(IInterface target, Message msg) {
1266 if (target.asBinder() instanceof Binder) {
1267 mCaller.sendMessage(msg);
1268 } else {
1269 handleMessage(msg);
1270 msg.recycle();
1271 }
1272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
Yohei Yukawa33e81792015-11-17 21:14:42 -08001274 void unbindCurrentClientLocked(
1275 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001277 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 + mCurClient.client.asBinder());
1279 if (mBoundToMethod) {
1280 mBoundToMethod = false;
1281 if (mCurMethod != null) {
1282 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1283 MSG_UNBIND_INPUT, mCurMethod));
1284 }
1285 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001286
1287 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1288 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001289 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1290 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
The Android Open Source Project10592532009-03-18 17:39:46 -07001294 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 }
1296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 private int getImeShowFlags() {
1299 int flags = 0;
1300 if (mShowForced) {
1301 flags |= InputMethod.SHOW_FORCED
1302 | InputMethod.SHOW_EXPLICIT;
1303 } else if (mShowExplicitlyRequested) {
1304 flags |= InputMethod.SHOW_EXPLICIT;
1305 }
1306 return flags;
1307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 private int getAppShowFlags() {
1310 int flags = 0;
1311 if (mShowForced) {
1312 flags |= InputMethodManager.SHOW_FORCED;
1313 } else if (!mShowExplicitlyRequested) {
1314 flags |= InputMethodManager.SHOW_IMPLICIT;
1315 }
1316 return flags;
1317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
Dianne Hackborn7663d802012-02-24 13:08:49 -08001319 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if (!mBoundToMethod) {
1321 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1322 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1323 mBoundToMethod = true;
1324 }
1325 final SessionState session = mCurClient.curSession;
1326 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001327 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1328 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1329 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001331 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1332 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1333 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
1335 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001336 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001337 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001339 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001340 (session.channel != null ? session.channel.dup() : null),
1341 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001344 InputBindResult startInputLocked(
1345 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001346 IInputMethodClient client, IInputContext inputContext,
1347 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001348 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 // If no method is currently selected, do nothing.
1350 if (mCurMethodId == null) {
1351 return mNoBinding;
1352 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 ClientState cs = mClients.get(client.asBinder());
1355 if (cs == null) {
1356 throw new IllegalArgumentException("unknown client "
1357 + client.asBinder());
1358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001359
Yohei Yukawa74750f22016-03-22 12:54:22 -07001360 if (attribute == null) {
1361 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1362 + " uid=" + cs.uid + " pid=" + cs.pid);
1363 return null;
1364 }
1365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 try {
1367 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1368 // Check with the window manager to make sure this client actually
1369 // has a window with focus. If not, reject. This is thread safe
1370 // because if the focus changes some time before or after, the
1371 // next client receiving focus that has any interest in input will
1372 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001373 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1375 return null;
1376 }
1377 } catch (RemoteException e) {
1378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001380 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1381 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001382 }
1383
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001384 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001385 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001386 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001387 // If no method is currently selected, do nothing.
1388 if (mCurMethodId == null) {
1389 return mNoBinding;
1390 }
1391
Yohei Yukawad57ba672015-06-08 16:39:46 -07001392 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1393 attribute.packageName)) {
1394 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1395 + " uid=" + cs.uid + " package=" + attribute.packageName);
1396 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001397 }
1398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001400 // Was the keyguard locked when switching over to the new client?
1401 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 // If the client is changing, we need to switch over to the new
1403 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001404 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001405 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001406 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407
1408 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001409 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001410 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001411 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
1413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 // Bump up the sequence for this client and attach it.
1416 mCurSeq++;
1417 if (mCurSeq <= 0) mCurSeq = 1;
1418 mCurClient = cs;
1419 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001420 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 // Check if the input method is changing.
1424 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1425 if (cs.curSession != null) {
1426 // Fast case: if we are already connected to the input method,
1427 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001428 return attachNewInputLocked(
1429 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431 if (mHaveConnection) {
1432 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 // Return to client, and we will get back with it when
1434 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001435 requestClientSessionLocked(cs);
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 if (SystemClock.uptimeMillis()
1439 < (mLastBindTime+TIME_TO_RECONNECT)) {
1440 // In this case we have connected to the service, but
1441 // don't yet have its interface. If it hasn't been too
1442 // long since we did the connection, we'll return to
1443 // the client and wait to get the service interface so
1444 // we can report back. If it has been too long, we want
1445 // to fall through so we can try a disconnect/reconnect
1446 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001447 return new InputBindResult(null, null, mCurId, mCurSeq,
1448 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1451 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 }
1453 }
1454 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001456 return startInputInnerLocked();
1457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001458
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001459 InputBindResult startInputInnerLocked() {
1460 if (mCurMethodId == null) {
1461 return mNoBinding;
1462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001463
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001464 if (!mSystemReady) {
1465 // If the system is not yet ready, we shouldn't be running third
1466 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001467 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1468 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001469 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1472 if (info == null) {
1473 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1474 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001475
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001476 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1479 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001480 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1481 com.android.internal.R.string.input_method_binding_label);
1482 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1483 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001484 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001485 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1486 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 mLastBindTime = SystemClock.uptimeMillis();
1488 mHaveConnection = true;
1489 mCurId = info.getId();
1490 mCurToken = new Binder();
1491 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001492 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001493 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 } catch (RemoteException e) {
1495 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001496 return new InputBindResult(null, null, mCurId, mCurSeq,
1497 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 } else {
1499 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 + mCurIntent);
1502 }
1503 return null;
1504 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001505
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001506 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001507 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001508 IInputMethodClient client, IInputContext inputContext,
1509 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001510 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001511 if (!calledFromValidUser()) {
1512 return null;
1513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001515 if (DEBUG) {
1516 Slog.v(TAG, "startInput: reason="
1517 + InputMethodClient.getStartInputReason(startInputReason)
1518 + " client = " + client.asBinder()
1519 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001520 + " missingMethods="
1521 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001522 + " attribute=" + attribute
1523 + " controlFlags=#" + Integer.toHexString(controlFlags));
1524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 final long ident = Binder.clearCallingIdentity();
1526 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001527 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1528 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 } finally {
1530 Binder.restoreCallingIdentity(ident);
1531 }
1532 }
1533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001534
satoke7c6998e2011-06-03 17:57:59 +09001535 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public void finishInput(IInputMethodClient client) {
1537 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001538
satoke7c6998e2011-06-03 17:57:59 +09001539 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 public void onServiceConnected(ComponentName name, IBinder service) {
1541 synchronized (mMethodMap) {
1542 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1543 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001544 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001545 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001546 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001547 return;
1548 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001549 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001550 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1551 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001553 clearClientSessionLocked(mCurClient);
1554 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556 }
1557 }
1558 }
1559
Jeff Brownc28867a2013-03-26 15:42:39 -07001560 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1561 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 synchronized (mMethodMap) {
1563 if (mCurMethod != null && method != null
1564 && mCurMethod.asBinder() == method.asBinder()) {
1565 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001566 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001568 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001569 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 if (res.method != null) {
1571 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001572 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001574 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576 }
1577 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001578
1579 // Session abandoned. Close its associated input channel.
1580 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001583 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001584 if (mVisibleBound) {
1585 mContext.unbindService(mVisibleConnection);
1586 mVisibleBound = false;
1587 }
1588
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001589 if (mHaveConnection) {
1590 mContext.unbindService(this);
1591 mHaveConnection = false;
1592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001594 if (mCurToken != null) {
1595 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001596 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001597 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001598 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001599 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001600 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001601 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001602 } catch (RemoteException e) {
1603 }
1604 mCurToken = null;
1605 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001606
The Android Open Source Project10592532009-03-18 17:39:46 -07001607 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001608 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
Yohei Yukawa33e81792015-11-17 21:14:42 -08001611 void resetCurrentMethodAndClient(
1612 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001613 mCurMethodId = null;
1614 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001615 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001616 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001617
1618 void requestClientSessionLocked(ClientState cs) {
1619 if (!cs.sessionRequested) {
1620 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1621 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1622 cs.sessionRequested = true;
1623 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1624 MSG_CREATE_SESSION, mCurMethod, channels[1],
1625 new MethodCallback(this, mCurMethod, channels[0])));
1626 }
1627 }
1628
1629 void clearClientSessionLocked(ClientState cs) {
1630 finishSessionLocked(cs.curSession);
1631 cs.curSession = null;
1632 cs.sessionRequested = false;
1633 }
1634
1635 private void finishSessionLocked(SessionState sessionState) {
1636 if (sessionState != null) {
1637 if (sessionState.session != null) {
1638 try {
1639 sessionState.session.finishSession();
1640 } catch (RemoteException e) {
1641 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001642 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001643 }
1644 sessionState.session = null;
1645 }
1646 if (sessionState.channel != null) {
1647 sessionState.channel.dispose();
1648 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001649 }
1650 }
1651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001653 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 if (mCurMethod != null) {
1655 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001656 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001658
Jeff Brownc28867a2013-03-26 15:42:39 -07001659 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001660 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 mCurMethod = null;
1662 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001663 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001664 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001667
satoke7c6998e2011-06-03 17:57:59 +09001668 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 public void onServiceDisconnected(ComponentName name) {
1670 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001671 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 + " mCurIntent=" + mCurIntent);
1673 if (mCurMethod != null && mCurIntent != null
1674 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001675 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 // We consider this to be a new bind attempt, since the system
1677 // should now try to restart the service for us.
1678 mLastBindTime = SystemClock.uptimeMillis();
1679 mShowRequested = mInputShown;
1680 mInputShown = false;
1681 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001682 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1683 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1684 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
1686 }
1687 }
1688 }
1689
satokf9f01002011-05-19 21:31:50 +09001690 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1692 long ident = Binder.clearCallingIdentity();
1693 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001695 if (!calledWithValidToken(token)) {
1696 final int uid = Binder.getCallingUid();
1697 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1698 + " token:" + token);
1699 return;
1700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001702 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001703 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001704 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001707 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001708 CharSequence contentDescription = null;
1709 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001710 // Use PackageManager to load label
1711 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001712 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001713 mIPackageManager.getApplicationInfo(packageName, 0,
1714 mSettings.getCurrentUserId()));
1715 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001716 /* ignore */
1717 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001718 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001719 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001720 contentDescription != null
1721 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001722 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 }
1725 }
1726 } finally {
1727 Binder.restoreCallingIdentity(ident);
1728 }
1729 }
1730
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001731 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001732 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001733 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001734 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001735 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001736 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001737 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1738 // When physical keyboard is attached, we show the ime switcher (or notification if
1739 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1740 // exists in the IME switcher dialog. Might be OK to remove this condition once
1741 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1742 return true;
1743 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001744 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1745 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001746 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001747
1748 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1749 final int N = imis.size();
1750 if (N > 2) return true;
1751 if (N < 1) return false;
1752 int nonAuxCount = 0;
1753 int auxCount = 0;
1754 InputMethodSubtype nonAuxSubtype = null;
1755 InputMethodSubtype auxSubtype = null;
1756 for(int i = 0; i < N; ++i) {
1757 final InputMethodInfo imi = imis.get(i);
1758 final List<InputMethodSubtype> subtypes =
1759 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1760 final int subtypeCount = subtypes.size();
1761 if (subtypeCount == 0) {
1762 ++nonAuxCount;
1763 } else {
1764 for (int j = 0; j < subtypeCount; ++j) {
1765 final InputMethodSubtype subtype = subtypes.get(j);
1766 if (!subtype.isAuxiliary()) {
1767 ++nonAuxCount;
1768 nonAuxSubtype = subtype;
1769 } else {
1770 ++auxCount;
1771 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001772 }
1773 }
satok7cfc0ed2011-06-20 21:29:36 +09001774 }
1775 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001776 if (nonAuxCount > 1 || auxCount > 1) {
1777 return true;
1778 } else if (nonAuxCount == 1 && auxCount == 1) {
1779 if (nonAuxSubtype != null && auxSubtype != null
1780 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1781 || auxSubtype.overridesImplicitlyEnabledSubtype()
1782 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1783 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1784 return false;
1785 }
1786 return true;
1787 }
1788 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001789 }
1790
John Spurlocke0980502013-10-25 11:59:29 -04001791 private boolean isKeyguardLocked() {
1792 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1793 }
1794
satokdbf29502011-08-25 15:28:23 +09001795 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001796 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001797 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001798 if (!calledWithValidToken(token)) {
1799 final int uid = Binder.getCallingUid();
1800 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1801 + " token:" + token);
1802 return;
1803 }
1804
1805 synchronized (mMethodMap) {
1806 mImeWindowVis = vis;
1807 mBackDisposition = backDisposition;
1808 updateSystemUiLocked(token, vis, backDisposition);
1809 }
1810 }
1811
1812 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1813 synchronized (mMethodMap) {
1814 updateSystemUiLocked(token, vis, backDisposition);
1815 }
1816 }
1817
1818 // Caution! This method is called in this class. Handle multi-user carefully
1819 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1820 if (!calledWithValidToken(token)) {
1821 final int uid = Binder.getCallingUid();
1822 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1823 + " token:" + token);
1824 return;
1825 }
1826
1827 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1828 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001829 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001830 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001831 // apply policy for binder calls
1832 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1833 vis = 0;
satok06487a52010-10-29 11:37:18 +09001834 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001835 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1836 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1837 if (mStatusBar != null) {
1838 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1839 needsToShowImeSwitcher);
1840 }
1841 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1842 if (imi != null && needsToShowImeSwitcher) {
1843 // Used to load label
1844 final CharSequence title = mRes.getText(
1845 com.android.internal.R.string.select_input_method);
1846 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1847 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001848 mImeSwitcherNotification.setContentTitle(title)
1849 .setContentText(summary)
1850 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001851 try {
1852 if ((mNotificationManager != null)
1853 && !mIWindowManager.hasNavigationBar()) {
1854 if (DEBUG) {
1855 Slog.d(TAG, "--- show notification: label = " + summary);
1856 }
1857 mNotificationManager.notifyAsUser(null,
1858 com.android.internal.R.string.select_input_method,
1859 mImeSwitcherNotification.build(), UserHandle.ALL);
1860 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001861 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001862 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001863 }
1864 } else {
1865 if (mNotificationShown && mNotificationManager != null) {
1866 if (DEBUG) {
1867 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001868 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001869 mNotificationManager.cancelAsUser(null,
1870 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1871 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001872 }
satok06487a52010-10-29 11:37:18 +09001873 }
1874 } finally {
1875 Binder.restoreCallingIdentity(ident);
1876 }
1877 }
1878
satoke7c6998e2011-06-03 17:57:59 +09001879 @Override
satokf9f01002011-05-19 21:31:50 +09001880 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001881 if (!calledFromValidUser()) {
1882 return;
1883 }
satokf9f01002011-05-19 21:31:50 +09001884 synchronized (mMethodMap) {
1885 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1886 for (int i = 0; i < spans.length; ++i) {
1887 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001888 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001889 mSecureSuggestionSpans.put(ss, currentImi);
1890 }
1891 }
1892 }
1893 }
1894
satoke7c6998e2011-06-03 17:57:59 +09001895 @Override
satokf9f01002011-05-19 21:31:50 +09001896 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001897 if (!calledFromValidUser()) {
1898 return false;
1899 }
satokf9f01002011-05-19 21:31:50 +09001900 synchronized (mMethodMap) {
1901 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1902 // TODO: Do not send the intent if the process of the targetImi is already dead.
1903 if (targetImi != null) {
1904 final String[] suggestions = span.getSuggestions();
1905 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001906 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001907 final Intent intent = new Intent();
1908 // Ensures that only a class in the original IME package will receive the
1909 // notification.
satok42c5a162011-05-26 16:46:14 +09001910 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001911 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1912 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1913 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1914 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001915 final long ident = Binder.clearCallingIdentity();
1916 try {
1917 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1918 } finally {
1919 Binder.restoreCallingIdentity(ident);
1920 }
satokf9f01002011-05-19 21:31:50 +09001921 return true;
1922 }
1923 }
1924 return false;
1925 }
1926
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001927 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001928 updateInputMethodsFromSettingsLocked(enabledMayChange);
1929 updateKeyboardFromSettingsLocked();
1930 }
1931
1932 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001933 if (enabledMayChange) {
1934 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1935 for (int i=0; i<enabled.size(); i++) {
1936 // We allow the user to select "disabled until used" apps, so if they
1937 // are enabling one of those here we now need to make it enabled.
1938 InputMethodInfo imm = enabled.get(i);
1939 try {
1940 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1941 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1942 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001943 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001944 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001945 if (DEBUG) {
1946 Slog.d(TAG, "Update state(" + imm.getId()
1947 + "): DISABLED_UNTIL_USED -> DEFAULT");
1948 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001949 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1950 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001951 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1952 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001953 }
1954 } catch (RemoteException e) {
1955 }
1956 }
1957 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001958 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1959 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1960 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1961 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001962 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001963 // There is no input method selected, try to choose new applicable input method.
1964 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001965 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001966 }
1967 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001969 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001971 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001972 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 }
satokf3db1af2010-11-23 13:34:33 +09001974 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001975 } else {
1976 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001977 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001979 // Here is not the perfect place to reset the switching controller. Ideally
1980 // mSwitchingController and mSettings should be able to share the same state.
1981 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1982 // the same enabled IMEs list.
1983 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001984
1985 }
1986
1987 public void updateKeyboardFromSettingsLocked() {
1988 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1989 if (mSwitchingDialog != null
1990 && mSwitchingDialogTitleView != null
1991 && mSwitchingDialog.isShowing()) {
1992 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1993 com.android.internal.R.id.hard_keyboard_switch);
1994 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
Yohei Yukawab097b822015-12-01 10:43:08 -08001998 private void notifyInputMethodSubtypeChanged(final int userId,
1999 @Nullable final InputMethodInfo inputMethodInfo,
2000 @Nullable final InputMethodSubtype subtype) {
2001 final InputManagerInternal inputManagerInternal =
2002 LocalServices.getService(InputManagerInternal.class);
2003 if (inputManagerInternal != null) {
2004 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2005 }
2006 }
2007
satokab751aa2010-09-14 19:17:36 +09002008 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 InputMethodInfo info = mMethodMap.get(id);
2010 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002011 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002013
satokd81e9502012-05-21 12:58:45 +09002014 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002016 final int subtypeCount = info.getSubtypeCount();
2017 if (subtypeCount <= 0) {
2018 return;
satokcd7cd292010-11-20 15:46:23 +09002019 }
satokd81e9502012-05-21 12:58:45 +09002020 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2021 final InputMethodSubtype newSubtype;
2022 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2023 newSubtype = info.getSubtypeAt(subtypeId);
2024 } else {
2025 // If subtype is null, try to find the most applicable one from
2026 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002027 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002028 }
2029 if (newSubtype == null || oldSubtype == null) {
2030 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2031 + ", new subtype = " + newSubtype);
2032 return;
2033 }
2034 if (newSubtype != oldSubtype) {
2035 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2036 if (mCurMethod != null) {
2037 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002038 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002039 mCurMethod.changeInputMethodSubtype(newSubtype);
2040 } catch (RemoteException e) {
2041 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002042 return;
satokab751aa2010-09-14 19:17:36 +09002043 }
2044 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002045 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 return;
2048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002049
satokd81e9502012-05-21 12:58:45 +09002050 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 final long ident = Binder.clearCallingIdentity();
2052 try {
satokab751aa2010-09-14 19:17:36 +09002053 // Set a subtype to this input method.
2054 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002055 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2056 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2057 // because mCurMethodId is stored as a history in
2058 // setSelectedInputMethodAndSubtypeLocked().
2059 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002061 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002063 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002065 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002067 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 } finally {
2069 Binder.restoreCallingIdentity(ident);
2070 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002071
2072 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2073 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002075
satok42c5a162011-05-26 16:46:14 +09002076 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002077 public boolean showSoftInput(IInputMethodClient client, int flags,
2078 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002079 if (!calledFromValidUser()) {
2080 return false;
2081 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002082 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 long ident = Binder.clearCallingIdentity();
2084 try {
2085 synchronized (mMethodMap) {
2086 if (mCurClient == null || client == null
2087 || mCurClient.client.asBinder() != client.asBinder()) {
2088 try {
2089 // We need to check if this is the current client with
2090 // focus in the window manager, to allow this call to
2091 // be made before input is started in it.
2092 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002093 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002094 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002097 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 }
2099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002100
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002101 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 }
2104 } finally {
2105 Binder.restoreCallingIdentity(ident);
2106 }
2107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002108
The Android Open Source Project4df24232009-03-05 14:34:35 -08002109 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002111 if (mAccessibilityRequestingNoSoftKeyboard) {
2112 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 }
Anna Galusza9b278112016-01-04 11:37:37 -08002114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2116 mShowExplicitlyRequested = true;
2117 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002118 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2119 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002121
Dianne Hackborncc278702009-09-02 23:07:23 -07002122 if (!mSystemReady) {
2123 return false;
2124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002125
The Android Open Source Project4df24232009-03-05 14:34:35 -08002126 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002128 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002129 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2130 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2131 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002133 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002134 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002135 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002136 | Context.BIND_TREAT_LIKE_ACTIVITY
2137 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002138 mVisibleBound = true;
2139 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002140 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002142 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 // The client has asked to have the input method shown, but
2144 // we have been sitting here too long with a connection to the
2145 // service and no interface received, so let's disconnect/connect
2146 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002147 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002149 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002151 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002152 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002153 } else {
2154 if (DEBUG) {
2155 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2156 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002159
The Android Open Source Project4df24232009-03-05 14:34:35 -08002160 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002162
satok42c5a162011-05-26 16:46:14 +09002163 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002164 public boolean hideSoftInput(IInputMethodClient client, int flags,
2165 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002166 if (!calledFromValidUser()) {
2167 return false;
2168 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002169 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 long ident = Binder.clearCallingIdentity();
2171 try {
2172 synchronized (mMethodMap) {
2173 if (mCurClient == null || client == null
2174 || mCurClient.client.asBinder() != client.asBinder()) {
2175 try {
2176 // We need to check if this is the current client with
2177 // focus in the window manager, to allow this call to
2178 // be made before input is started in it.
2179 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002180 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2181 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002182 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184 } catch (RemoteException e) {
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002188
Joe Onorato8a9b2202010-02-26 18:56:32 -08002189 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002190 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 }
2192 } finally {
2193 Binder.restoreCallingIdentity(ident);
2194 }
2195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002196
The Android Open Source Project4df24232009-03-05 14:34:35 -08002197 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2199 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002200 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 -08002201 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
2203 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002204 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 -08002205 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002207
2208 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2209 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2210 // to be updated with the new value sent from IME process. Even in such a transient state
2211 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2212 // application process as a valid request, and have even promised such a behavior with CTS
2213 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2214 // IMMS#InputShown indicates that the software keyboard is shown.
2215 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2216 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2217 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002218 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002219 if (shouldHideSoftInput) {
2220 // The IME will report its visible state again after the following message finally
2221 // delivered to the IME process as an IPC. Hence the inconsistency between
2222 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2223 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002224 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2225 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2226 res = true;
2227 } else {
2228 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002230 if (mHaveConnection && mVisibleBound) {
2231 mContext.unbindService(mVisibleConnection);
2232 mVisibleBound = false;
2233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 mInputShown = false;
2235 mShowRequested = false;
2236 mShowExplicitlyRequested = false;
2237 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002238 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002240
satok42c5a162011-05-26 16:46:14 +09002241 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002242 public InputBindResult startInputOrWindowGainedFocus(
2243 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2244 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002245 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002246 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002247 if (windowToken != null) {
2248 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002249 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002250 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002251 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2252 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002253 }
2254 }
2255
2256 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002257 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2258 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002259 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2260 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002261 // Needs to check the validity before clearing calling identity
2262 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002263 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 long ident = Binder.clearCallingIdentity();
2265 try {
2266 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002267 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2268 + InputMethodClient.getStartInputReason(startInputReason)
2269 + " client=" + client.asBinder()
2270 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002271 + " missingMethods="
2272 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002273 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002274 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002276 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002277
Dianne Hackborn7663d802012-02-24 13:08:49 -08002278 ClientState cs = mClients.get(client.asBinder());
2279 if (cs == null) {
2280 throw new IllegalArgumentException("unknown client "
2281 + client.asBinder());
2282 }
2283
2284 try {
2285 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2286 // Check with the window manager to make sure this client actually
2287 // has a window with focus. If not, reject. This is thread safe
2288 // because if the focus changes some time before or after, the
2289 // next client receiving focus that has any interest in input will
2290 // be calling through here after that change happens.
2291 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2292 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2293 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002295 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002297
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002298 if (!calledFromValidUser) {
2299 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2300 Slog.w(TAG, "If you want to interect with IME, you need "
2301 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2302 hideCurrentInputLocked(0, null);
2303 return null;
2304 }
2305
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002306 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002307 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002308 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002309 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002310 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2311 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002312 }
2313 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002314 }
2315 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002316 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002317
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002318 // Should we auto-show the IME even if the caller has not
2319 // specified what should be done with it?
2320 // We only do this automatically if the window can resize
2321 // to accommodate the IME (so what the user sees will give
2322 // them good context without input information being obscured
2323 // by the IME) or if running on a large screen where there
2324 // is more room for the target window + IME.
2325 final boolean doAutoShow =
2326 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2327 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2328 || mRes.getConfiguration().isLayoutSizeAtLeast(
2329 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002330 final boolean isTextEditor =
2331 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2332
2333 // We want to start input before showing the IME, but after closing
2334 // it. We want to do this after closing it to help the IME disappear
2335 // more quickly (not get stuck behind it initializing itself for the
2336 // new focused input, even if its window wants to hide the IME).
2337 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2340 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002341 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2343 // There is no focus view, and this window will
2344 // be behind any soft input window, so hide the
2345 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002346 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002347 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002349 } else if (isTextEditor && doAutoShow && (softInputMode &
2350 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 // There is a focus view, and we are navigating forward
2352 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002353 // We only do this automatically if the window can resize
2354 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002355 // them good context without input information being obscured
2356 // by the IME) or if running on a large screen where there
2357 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002358 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002359 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002360 res = startInputUncheckedLocked(cs, inputContext,
2361 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002362 didStart = true;
2363 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002364 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
2366 break;
2367 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2368 // Do nothing.
2369 break;
2370 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2371 if ((softInputMode &
2372 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002373 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002374 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
2376 break;
2377 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002378 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002379 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 break;
2381 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2382 if ((softInputMode &
2383 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002384 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002385 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002386 res = startInputUncheckedLocked(cs, inputContext,
2387 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002388 didStart = true;
2389 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002390 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 }
2392 break;
2393 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002394 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002395 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002396 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2397 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002398 didStart = true;
2399 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002400 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 break;
2402 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002403
2404 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002405 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002406 controlFlags);
2407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
2409 } finally {
2410 Binder.restoreCallingIdentity(ident);
2411 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002412
2413 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002415
satok42c5a162011-05-26 16:46:14 +09002416 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002417 public void showInputMethodPickerFromClient(
2418 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002419 if (!calledFromValidUser()) {
2420 return;
2421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 synchronized (mMethodMap) {
2423 if (mCurClient == null || client == null
2424 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002425 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002426 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428
satok440aab52010-11-25 09:43:11 +09002429 // Always call subtype picker, because subtype picker is a superset of input method
2430 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002431 mHandler.sendMessage(mCaller.obtainMessageI(
2432 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002433 }
2434 }
2435
satok42c5a162011-05-26 16:46:14 +09002436 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002438 if (!calledFromValidUser()) {
2439 return;
2440 }
satok28203512010-11-24 11:06:49 +09002441 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2442 }
2443
satok42c5a162011-05-26 16:46:14 +09002444 @Override
satok28203512010-11-24 11:06:49 +09002445 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002446 if (!calledFromValidUser()) {
2447 return;
2448 }
satok28203512010-11-24 11:06:49 +09002449 synchronized (mMethodMap) {
2450 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002451 setInputMethodWithSubtypeIdLocked(token, id,
2452 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2453 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002454 } else {
2455 setInputMethod(token, id);
2456 }
2457 }
satokab751aa2010-09-14 19:17:36 +09002458 }
2459
satok42c5a162011-05-26 16:46:14 +09002460 @Override
satokb416a712010-11-25 20:42:14 +09002461 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002462 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002463 if (!calledFromValidUser()) {
2464 return;
2465 }
satokb416a712010-11-25 20:42:14 +09002466 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002467 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2468 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002469 }
2470 }
2471
satok4fc87d62011-05-20 16:13:43 +09002472 @Override
satok735cf382010-11-11 20:40:09 +09002473 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002474 if (!calledFromValidUser()) {
2475 return false;
2476 }
satok735cf382010-11-11 20:40:09 +09002477 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002478 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002479 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002480 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002481 lastImi = mMethodMap.get(lastIme.first);
2482 } else {
2483 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002484 }
satok4fc87d62011-05-20 16:13:43 +09002485 String targetLastImiId = null;
2486 int subtypeId = NOT_A_SUBTYPE_ID;
2487 if (lastIme != null && lastImi != null) {
2488 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002489 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002490 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2491 : mCurrentSubtype.hashCode();
2492 // If the last IME is the same as the current IME and the last subtype is not
2493 // defined, there is no need to switch to the last IME.
2494 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2495 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002496 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002497 }
2498 }
2499
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002500 if (TextUtils.isEmpty(targetLastImiId)
2501 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002502 // This is a safety net. If the currentSubtype can't be added to the history
2503 // and the framework couldn't find the last ime, we will make the last ime be
2504 // the most applicable enabled keyboard subtype of the system imes.
2505 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2506 if (enabled != null) {
2507 final int N = enabled.size();
2508 final String locale = mCurrentSubtype == null
2509 ? mRes.getConfiguration().locale.toString()
2510 : mCurrentSubtype.getLocale();
2511 for (int i = 0; i < N; ++i) {
2512 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002513 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002514 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002515 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2516 InputMethodUtils.getSubtypes(imi),
2517 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002518 if (keyboardSubtype != null) {
2519 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002520 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002521 imi, keyboardSubtype.hashCode());
2522 if(keyboardSubtype.getLocale().equals(locale)) {
2523 break;
2524 }
2525 }
2526 }
2527 }
2528 }
2529 }
2530
2531 if (!TextUtils.isEmpty(targetLastImiId)) {
2532 if (DEBUG) {
2533 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2534 + ", from: " + mCurMethodId + ", " + subtypeId);
2535 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002536 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002537 return true;
2538 } else {
2539 return false;
2540 }
satok735cf382010-11-11 20:40:09 +09002541 }
2542 }
2543
satoke7c6998e2011-06-03 17:57:59 +09002544 @Override
satok688bd472012-02-09 20:09:17 +09002545 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002546 if (!calledFromValidUser()) {
2547 return false;
2548 }
satok688bd472012-02-09 20:09:17 +09002549 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002550 if (!calledWithValidToken(token)) {
2551 final int uid = Binder.getCallingUid();
2552 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2553 + " token:" + token);
2554 return false;
2555 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002556 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002557 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2558 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002559 if (nextSubtype == null) {
2560 return false;
2561 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002562 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2563 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002564 return true;
2565 }
2566 }
2567
2568 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002569 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2570 if (!calledFromValidUser()) {
2571 return false;
2572 }
2573 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002574 if (!calledWithValidToken(token)) {
2575 final int uid = Binder.getCallingUid();
2576 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2577 + "token. uid:" + uid + " token:" + token);
2578 return false;
2579 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002580 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002581 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2582 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002583 if (nextSubtype == null) {
2584 return false;
2585 }
2586 return true;
2587 }
2588 }
2589
2590 @Override
satok68f1b782011-04-11 14:26:04 +09002591 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002592 if (!calledFromValidUser()) {
2593 return null;
2594 }
satok68f1b782011-04-11 14:26:04 +09002595 synchronized (mMethodMap) {
2596 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2597 // TODO: Handle the case of the last IME with no subtypes
2598 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2599 || TextUtils.isEmpty(lastIme.second)) return null;
2600 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2601 if (lastImi == null) return null;
2602 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002603 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002604 final int lastSubtypeId =
2605 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002606 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2607 return null;
2608 }
2609 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002610 } catch (NumberFormatException e) {
2611 return null;
2612 }
2613 }
2614 }
2615
satoke7c6998e2011-06-03 17:57:59 +09002616 @Override
satokee5e77c2011-09-02 18:50:15 +09002617 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002618 if (!calledFromValidUser()) {
2619 return;
2620 }
satok91e88122011-07-18 11:11:42 +09002621 // By this IPC call, only a process which shares the same uid with the IME can add
2622 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002623 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002624 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002625 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002626 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002627 final String[] packageInfos;
2628 try {
2629 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2630 } catch (RemoteException e) {
2631 Slog.e(TAG, "Failed to get package infos");
2632 return;
2633 }
satok91e88122011-07-18 11:11:42 +09002634 if (packageInfos != null) {
2635 final int packageNum = packageInfos.length;
2636 for (int i = 0; i < packageNum; ++i) {
2637 if (packageInfos[i].equals(imi.getPackageName())) {
2638 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002639 final long ident = Binder.clearCallingIdentity();
2640 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002641 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002642 } finally {
2643 Binder.restoreCallingIdentity(ident);
2644 }
satokee5e77c2011-09-02 18:50:15 +09002645 return;
satok91e88122011-07-18 11:11:42 +09002646 }
2647 }
2648 }
satoke7c6998e2011-06-03 17:57:59 +09002649 }
satokee5e77c2011-09-02 18:50:15 +09002650 return;
satoke7c6998e2011-06-03 17:57:59 +09002651 }
2652
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002653 @Override
2654 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002655 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002656 }
2657
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002658 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002659 public void clearLastInputMethodWindowForTransition(IBinder token) {
2660 if (!calledFromValidUser()) {
2661 return;
2662 }
2663 final long ident = Binder.clearCallingIdentity();
2664 try {
2665 synchronized (mMethodMap) {
2666 if (!calledWithValidToken(token)) {
2667 final int uid = Binder.getCallingUid();
2668 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2669 + "invalid token. uid:" + uid + " token:" + token);
2670 return;
2671 }
2672 }
2673 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2674 } finally {
2675 Binder.restoreCallingIdentity(ident);
2676 }
2677 }
2678
2679 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002680 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002681 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002682 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002683 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002684 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002685 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2686 if (DEBUG) {
2687 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2688 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2689 + " actual: " + sequenceNumber);
2690 }
2691 return;
2692 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002693 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2694 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002695 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002696 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002697 }
2698 }
2699
satok28203512010-11-24 11:06:49 +09002700 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002702 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2703 }
2704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002706 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2707 if (token == null) {
2708 if (mContext.checkCallingOrSelfPermission(
2709 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2710 != PackageManager.PERMISSION_GRANTED) {
2711 throw new SecurityException(
2712 "Using null token requires permission "
2713 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002715 } else if (mCurToken != token) {
2716 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2717 + " token: " + token);
2718 return;
2719 }
2720
2721 final long ident = Binder.clearCallingIdentity();
2722 try {
2723 setInputMethodLocked(id, subtypeId);
2724 } finally {
2725 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 }
2727 }
2728
satok42c5a162011-05-26 16:46:14 +09002729 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002731 if (!calledFromValidUser()) {
2732 return;
2733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002735 if (!calledWithValidToken(token)) {
2736 final int uid = Binder.getCallingUid();
2737 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2738 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 return;
2740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 long ident = Binder.clearCallingIdentity();
2742 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002743 hideCurrentInputLocked(flags, null);
2744 } finally {
2745 Binder.restoreCallingIdentity(ident);
2746 }
2747 }
2748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002749
satok42c5a162011-05-26 16:46:14 +09002750 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002751 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002752 if (!calledFromValidUser()) {
2753 return;
2754 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002755 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002756 if (!calledWithValidToken(token)) {
2757 final int uid = Binder.getCallingUid();
2758 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2759 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002760 return;
2761 }
2762 long ident = Binder.clearCallingIdentity();
2763 try {
2764 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 } finally {
2766 Binder.restoreCallingIdentity(ident);
2767 }
2768 }
2769 }
2770
2771 void setEnabledSessionInMainThread(SessionState session) {
2772 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002773 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002775 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002776 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 } catch (RemoteException e) {
2778 }
2779 }
2780 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002781 if (mEnabledSession != null && mEnabledSession.session != null) {
2782 try {
2783 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2784 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2785 } catch (RemoteException e) {
2786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 }
2788 }
2789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002790
satok42c5a162011-05-26 16:46:14 +09002791 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002793 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002795 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002796 final boolean showAuxSubtypes;
2797 switch (msg.arg1) {
2798 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2799 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2800 // implemented so that auxiliary subtypes will be excluded when the soft
2801 // keyboard is invisible.
2802 showAuxSubtypes = mInputShown;
2803 break;
2804 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2805 showAuxSubtypes = true;
2806 break;
2807 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2808 showAuxSubtypes = false;
2809 break;
2810 default:
2811 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2812 return false;
2813 }
2814 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002815 return true;
2816
satok47a44912010-10-06 16:03:58 +09002817 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002818 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002819 return true;
2820
2821 case MSG_SHOW_IM_CONFIG:
2822 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002823 return true;
2824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 case MSG_UNBIND_INPUT:
2828 try {
2829 ((IInputMethod)msg.obj).unbindInput();
2830 } catch (RemoteException e) {
2831 // There is nothing interesting about the method dying.
2832 }
2833 return true;
2834 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002835 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 try {
2837 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2838 } catch (RemoteException e) {
2839 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002840 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 return true;
2842 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002843 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002845 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002846 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002847 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 } catch (RemoteException e) {
2849 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002850 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 return true;
2852 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002853 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002855 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002856 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002857 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 } catch (RemoteException e) {
2859 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002860 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002862 case MSG_HIDE_CURRENT_INPUT_METHOD:
2863 synchronized (mMethodMap) {
2864 hideCurrentInputLocked(0, null);
2865 }
2866 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002868 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002870 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2872 } catch (RemoteException e) {
2873 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002874 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002876 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002877 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002878 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002879 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002881 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002883 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002884 // Dispose the channel if the input method is not local to this process
2885 // because the remote proxy will get its own copy when unparceled.
2886 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002887 channel.dispose();
2888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002890 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002894
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002895 case MSG_START_INPUT: {
2896 int missingMethods = msg.arg1;
2897 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002899 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002901 session.method.startInput((IInputContext) args.arg2, missingMethods,
2902 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 } catch (RemoteException e) {
2904 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002905 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002907 }
2908 case MSG_RESTART_INPUT: {
2909 int missingMethods = msg.arg1;
2910 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002912 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002914 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2915 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 } catch (RemoteException e) {
2917 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002918 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002923
Yohei Yukawa33e81792015-11-17 21:14:42 -08002924 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002926 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 } catch (RemoteException e) {
2928 // There is nothing interesting about the last client dying.
2929 }
2930 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002931 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002932 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002933 IInputMethodClient client = (IInputMethodClient)args.arg1;
2934 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002936 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002938 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002939 } finally {
2940 // Dispose the channel if the input method is not local to this process
2941 // because the remote proxy will get its own copy when unparceled.
2942 if (res.channel != null && Binder.isProxy(client)) {
2943 res.channel.dispose();
2944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002946 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002948 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002949 case MSG_SET_ACTIVE:
2950 try {
2951 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2952 } catch (RemoteException e) {
2953 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2954 + ((ClientState)msg.obj).pid + " uid "
2955 + ((ClientState)msg.obj).uid);
2956 }
2957 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002958 case MSG_SET_INTERACTIVE:
2959 handleSetInteractive(msg.arg1 != 0);
2960 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002961 case MSG_SWITCH_IME:
2962 handleSwitchInputMethod(msg.arg1 != 0);
2963 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002964 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2965 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002966 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002967 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002968 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002969 } catch (RemoteException e) {
2970 Slog.w(TAG, "Got RemoteException sending "
2971 + "setUserActionNotificationSequenceNumber("
2972 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002973 + clientState.pid + " uid "
2974 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002975 }
2976 return true;
2977 }
satok01038492012-04-09 21:08:27 +09002978
2979 // --------------------------------------------------------------
2980 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002981 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002982 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002983 case MSG_SYSTEM_UNLOCK_USER:
2984 final int userId = msg.arg1;
2985 onUnlockUser(userId);
2986 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 }
2988 return false;
2989 }
2990
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002991 private void handleSetInteractive(final boolean interactive) {
2992 synchronized (mMethodMap) {
2993 mIsInteractive = interactive;
2994 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2995
2996 // Inform the current client of the change in active status
2997 if (mCurClient != null && mCurClient.client != null) {
2998 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2999 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
3000 }
3001 }
3002 }
3003
Yohei Yukawaae61f712015-12-09 13:00:10 -08003004 private void handleSwitchInputMethod(final boolean forwardDirection) {
3005 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003006 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003007 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003008 if (nextSubtype == null) {
3009 return;
3010 }
3011 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003012 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3013 if (newInputMethodInfo == null) {
3014 return;
3015 }
3016 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3017 newInputMethodInfo, mCurrentSubtype);
3018 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003019 if (mSubtypeSwitchedByShortCutToast == null) {
3020 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3021 Toast.LENGTH_SHORT);
3022 } else {
3023 mSubtypeSwitchedByShortCutToast.setText(toastText);
3024 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003025 mSubtypeSwitchedByShortCutToast.show();
3026 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003027 }
3028 }
3029
satokdc9ddae2011-10-06 12:22:36 +09003030 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003031 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3032 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003033 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003034 if (DEBUG) {
3035 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3036 }
satok723a27e2010-11-11 14:58:11 +09003037 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003038 return true;
3039 }
3040
3041 return false;
3042 }
3043
Yohei Yukawa94e33302016-02-12 19:37:03 -08003044 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003045 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003046 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003047 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003048 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003049 mMethodList.clear();
3050 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003051
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003052 // Use for queryIntentServicesAsUser
3053 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003055 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3056 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3057 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003058 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003060 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3061 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003062
satoke7c6998e2011-06-03 17:57:59 +09003063 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3064 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 for (int i = 0; i < services.size(); ++i) {
3066 ResolveInfo ri = services.get(i);
3067 ServiceInfo si = ri.serviceInfo;
3068 ComponentName compName = new ComponentName(si.packageName, si.name);
3069 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3070 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003071 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 + ": it does not require the permission "
3073 + android.Manifest.permission.BIND_INPUT_METHOD);
3074 continue;
3075 }
3076
Joe Onorato8a9b2202010-02-26 18:56:32 -08003077 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078
3079 try {
satoke7c6998e2011-06-03 17:57:59 +09003080 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003081 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003082 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003083 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084
3085 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003086 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003088 } catch (Exception e) {
3089 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 }
3091 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003092
Yohei Yukawa859df052016-02-17 07:56:46 -08003093 // TODO: The following code should find better place to live.
3094 if (!resetDefaultEnabledIme) {
3095 boolean enabledImeFound = false;
3096 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3097 final int N = enabledImes.size();
3098 for (int i = 0; i < N; ++i) {
3099 final InputMethodInfo imi = enabledImes.get(i);
3100 if (mMethodList.contains(imi)) {
3101 enabledImeFound = true;
3102 break;
3103 }
3104 }
3105 if (!enabledImeFound) {
3106 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3107 resetDefaultEnabledIme = true;
3108 resetSelectedInputMethodAndSubtypeLocked("");
3109 }
3110 }
3111
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003112 if (resetDefaultEnabledIme) {
3113 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003114 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003115 final int N = defaultEnabledIme.size();
3116 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003117 final InputMethodInfo imi = defaultEnabledIme.get(i);
3118 if (DEBUG) {
3119 Slog.d(TAG, "--- enable ime = " + imi);
3120 }
3121 setInputMethodEnabledLocked(imi.getId(), true);
3122 }
3123 }
3124
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003125 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003126 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003127 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003128 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3129 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003130 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003131 }
3132 } else {
3133 // Double check that the default IME is certainly enabled.
3134 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003135 }
3136 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003137 // Here is not the perfect place to reset the switching controller. Ideally
3138 // mSwitchingController and mSettings should be able to share the same state.
3139 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3140 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003141 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003145
satok217f5482010-12-15 05:19:19 +09003146 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003147 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003148 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003149 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3150 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003151 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003152 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003153 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003154 final int userId;
3155 synchronized (mMethodMap) {
3156 userId = mSettings.getCurrentUserId();
3157 }
3158 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003159 }
3160
3161 private void showConfigureInputMethods() {
3162 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3163 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3164 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3165 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003166 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003167 }
3168
satok2c93efc2012-04-02 19:33:47 +09003169 private boolean isScreenLocked() {
3170 return mKeyguardManager != null
3171 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3172 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003173
Seigo Nonaka14e13912015-05-06 21:04:13 -07003174 private void showInputMethodMenu(boolean showAuxSubtypes) {
3175 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003178 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003179
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003180 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003181 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003182 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003183
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003184 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003185 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003186 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3187 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003188 if (immis == null || immis.size() == 0) {
3189 return;
3190 }
3191
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003192 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193
satok688bd472012-02-09 20:09:17 +09003194 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003195 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003196 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003197
satokc3690562012-01-10 20:14:43 +09003198 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003199 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003200 if (currentSubtype != null) {
3201 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003202 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3203 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003204 }
3205 }
3206
Ken Wakasa761eb372011-03-04 19:06:18 +09003207 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003208 mIms = new InputMethodInfo[N];
3209 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003210 int checkedItem = 0;
3211 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003212 final ImeSubtypeListItem item = imList.get(i);
3213 mIms[i] = item.mImi;
3214 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003215 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003216 int subtypeId = mSubtypeIds[i];
3217 if ((subtypeId == NOT_A_SUBTYPE_ID)
3218 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3219 || (subtypeId == lastInputMethodSubtypeId)) {
3220 checkedItem = i;
3221 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003224
3225 final Context settingsContext = new ContextThemeWrapper(context,
3226 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3227
3228 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003229 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3230 @Override
3231 public void onCancel(DialogInterface dialog) {
3232 hideInputMethodMenu();
3233 }
3234 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003235
3236 final Context dialogContext = mDialogBuilder.getContext();
3237 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3238 com.android.internal.R.styleable.DialogPreference,
3239 com.android.internal.R.attr.alertDialogStyle, 0);
3240 final Drawable dialogIcon = a.getDrawable(
3241 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3242 a.recycle();
3243
3244 mDialogBuilder.setIcon(dialogIcon);
3245
Yohei Yukawad34e1482016-02-11 08:03:52 -08003246 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003247 final View tv = inflater.inflate(
3248 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3249 mDialogBuilder.setCustomTitle(tv);
3250
3251 // Setup layout for a toggle switch of the hardware keyboard
3252 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003253 mSwitchingDialogTitleView
3254 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003255 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003256 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003257 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003258 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003259 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003260 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3261 @Override
3262 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003263 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003264 // Ensure that the input method dialog is dismissed when changing
3265 // the hardware keyboard state.
3266 hideInputMethodMenu();
3267 }
3268 });
3269
Alan Viverette505e3ab2014-11-24 15:22:11 -08003270 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003271 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3272 final OnClickListener choiceListener = new OnClickListener() {
3273 @Override
3274 public void onClick(final DialogInterface dialog, final int which) {
3275 synchronized (mMethodMap) {
3276 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3277 || mSubtypeIds.length <= which) {
3278 return;
satok01038492012-04-09 21:08:27 +09003279 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003280 final InputMethodInfo im = mIms[which];
3281 int subtypeId = mSubtypeIds[which];
3282 adapter.mCheckedItem = which;
3283 adapter.notifyDataSetChanged();
3284 hideInputMethodMenu();
3285 if (im != null) {
3286 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3287 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003288 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003289 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003291 }
3292 }
3293 };
3294 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003297 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003298 final Window w = mSwitchingDialog.getWindow();
3299 final WindowManager.LayoutParams attrs = w.getAttributes();
3300 w.setType(TYPE_INPUT_METHOD_DIALOG);
3301 // Use an alternate token for the dialog for that window manager can group the token
3302 // with other IME windows based on type vs. grouping based on whichever token happens
3303 // to get selected by the system later on.
3304 attrs.token = mSwitchingDialogToken;
3305 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3306 attrs.setTitle("Select input method");
3307 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003308 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 mSwitchingDialog.show();
3310 }
3311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003312
Ken Wakasa05dbb652011-08-22 15:22:43 +09003313 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3314 private final LayoutInflater mInflater;
3315 private final int mTextViewResourceId;
3316 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003317 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003318 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3319 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3320 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003321
Ken Wakasa05dbb652011-08-22 15:22:43 +09003322 mTextViewResourceId = textViewResourceId;
3323 mItemsList = itemsList;
3324 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003325 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003326 }
3327
3328 @Override
3329 public View getView(int position, View convertView, ViewGroup parent) {
3330 final View view = convertView != null ? convertView
3331 : mInflater.inflate(mTextViewResourceId, null);
3332 if (position < 0 || position >= mItemsList.size()) return view;
3333 final ImeSubtypeListItem item = mItemsList.get(position);
3334 final CharSequence imeName = item.mImeName;
3335 final CharSequence subtypeName = item.mSubtypeName;
3336 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3337 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3338 if (TextUtils.isEmpty(subtypeName)) {
3339 firstTextView.setText(imeName);
3340 secondTextView.setVisibility(View.GONE);
3341 } else {
3342 firstTextView.setText(subtypeName);
3343 secondTextView.setText(imeName);
3344 secondTextView.setVisibility(View.VISIBLE);
3345 }
3346 final RadioButton radioButton =
3347 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3348 radioButton.setChecked(position == mCheckedItem);
3349 return view;
3350 }
3351 }
3352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003354 synchronized (mMethodMap) {
3355 hideInputMethodMenuLocked();
3356 }
3357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003358
The Android Open Source Project10592532009-03-18 17:39:46 -07003359 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003360 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361
The Android Open Source Project10592532009-03-18 17:39:46 -07003362 if (mSwitchingDialog != null) {
3363 mSwitchingDialog.dismiss();
3364 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003366
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003367 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003368 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003369 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003373
satok42c5a162011-05-26 16:46:14 +09003374 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003376 // TODO: Make this work even for non-current users?
3377 if (!calledFromValidUser()) {
3378 return false;
3379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 synchronized (mMethodMap) {
3381 if (mContext.checkCallingOrSelfPermission(
3382 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3383 != PackageManager.PERMISSION_GRANTED) {
3384 throw new SecurityException(
3385 "Requires permission "
3386 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3387 }
Anna Galusza9b278112016-01-04 11:37:37 -08003388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 long ident = Binder.clearCallingIdentity();
3390 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003391 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 } finally {
3393 Binder.restoreCallingIdentity(ident);
3394 }
3395 }
3396 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003397
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003398 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3399 // Make sure this is a valid input method.
3400 InputMethodInfo imm = mMethodMap.get(id);
3401 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003402 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003403 }
3404
satokd87c2592010-09-29 11:52:06 +09003405 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3406 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003407
satokd87c2592010-09-29 11:52:06 +09003408 if (enabled) {
3409 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3410 if (pair.first.equals(id)) {
3411 // We are enabling this input method, but it is already enabled.
3412 // Nothing to do. The previous state was enabled.
3413 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003414 }
3415 }
satokd87c2592010-09-29 11:52:06 +09003416 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3417 // Previous state was disabled.
3418 return false;
3419 } else {
3420 StringBuilder builder = new StringBuilder();
3421 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3422 builder, enabledInputMethodsList, id)) {
3423 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003424 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003425 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3426 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3427 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003428 }
3429 // Previous state was enabled.
3430 return true;
3431 } else {
3432 // We are disabling the input method but it is already disabled.
3433 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003434 return false;
3435 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003436 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003437 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003438
satok723a27e2010-11-11 14:58:11 +09003439 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3440 boolean setSubtypeOnly) {
3441 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003442 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003443
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003444 mCurUserActionNotificationSequenceNumber =
3445 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3446 if (DEBUG) {
3447 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3448 + mCurUserActionNotificationSequenceNumber);
3449 }
3450
3451 if (mCurClient != null && mCurClient.client != null) {
3452 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3453 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003454 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003455 }
3456
satok723a27e2010-11-11 14:58:11 +09003457 // Set Subtype here
3458 if (imi == null || subtypeId < 0) {
3459 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003460 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003461 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003462 if (subtypeId < imi.getSubtypeCount()) {
3463 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3464 mSettings.putSelectedSubtype(subtype.hashCode());
3465 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003466 } else {
3467 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003468 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003469 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003470 }
satokab751aa2010-09-14 19:17:36 +09003471 }
satok723a27e2010-11-11 14:58:11 +09003472
Yohei Yukawa68645a62016-02-17 07:54:20 -08003473 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003474 // Set InputMethod here
3475 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3476 }
3477 }
3478
3479 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3480 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3481 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3482 // newDefaultIme is empty when there is no candidate for the selected IME.
3483 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3484 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3485 if (subtypeHashCode != null) {
3486 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003487 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003488 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003489 } catch (NumberFormatException e) {
3490 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3491 }
3492 }
3493 }
3494 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003495 }
3496
satok4e4569d2010-11-19 18:45:53 +09003497 // If there are no selected shortcuts, tries finding the most applicable ones.
3498 private Pair<InputMethodInfo, InputMethodSubtype>
3499 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3500 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3501 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003502 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003503 boolean foundInSystemIME = false;
3504
3505 // Search applicable subtype for each InputMethodInfo
3506 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003507 final String imiId = imi.getId();
3508 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3509 continue;
3510 }
satokcd7cd292010-11-20 15:46:23 +09003511 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003512 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003513 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003514 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003515 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003516 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003517 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003518 }
satokdf31ae62011-01-15 06:19:44 +09003519 // 2. Search by the system locale from enabledSubtypes.
3520 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003521 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003522 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003523 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003524 }
satoka86f5e42011-09-02 17:12:42 +09003525 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003526 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003527 final ArrayList<InputMethodSubtype> subtypesForSearch =
3528 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003529 ? InputMethodUtils.getSubtypes(imi)
3530 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003531 // 4. Search by the current subtype's locale from all subtypes.
3532 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003533 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003534 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003535 }
3536 // 5. Search by the system locale from all subtypes.
3537 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003538 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003539 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003540 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003541 }
satokcd7cd292010-11-20 15:46:23 +09003542 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003543 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003544 // The current input method is the most applicable IME.
3545 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003546 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003547 break;
satok7599a7f2010-12-22 13:45:23 +09003548 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003549 // The system input method is 2nd applicable IME.
3550 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003551 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003552 if ((imi.getServiceInfo().applicationInfo.flags
3553 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3554 foundInSystemIME = true;
3555 }
satok4e4569d2010-11-19 18:45:53 +09003556 }
3557 }
3558 }
3559 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003560 if (mostApplicableIMI != null) {
3561 Slog.w(TAG, "Most applicable shortcut input method was:"
3562 + mostApplicableIMI.getId());
3563 if (mostApplicableSubtype != null) {
3564 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3565 + "," + mostApplicableSubtype.getMode() + ","
3566 + mostApplicableSubtype.getLocale());
3567 }
3568 }
satok4e4569d2010-11-19 18:45:53 +09003569 }
satokcd7cd292010-11-20 15:46:23 +09003570 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003571 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003572 } else {
3573 return null;
3574 }
3575 }
3576
satokab751aa2010-09-14 19:17:36 +09003577 /**
3578 * @return Return the current subtype of this input method.
3579 */
satok42c5a162011-05-26 16:46:14 +09003580 @Override
satokab751aa2010-09-14 19:17:36 +09003581 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003582 // TODO: Make this work even for non-current users?
3583 if (!calledFromValidUser()) {
3584 return null;
3585 }
3586 synchronized (mMethodMap) {
3587 return getCurrentInputMethodSubtypeLocked();
3588 }
3589 }
3590
3591 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003592 if (mCurMethodId == null) {
3593 return null;
3594 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003595 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003596 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3597 if (imi == null || imi.getSubtypeCount() == 0) {
3598 return null;
satok4e4569d2010-11-19 18:45:53 +09003599 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003600 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003601 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3602 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003603 if (subtypeId == NOT_A_SUBTYPE_ID) {
3604 // If there are no selected subtypes, the framework will try to find
3605 // the most applicable subtype from explicitly or implicitly enabled
3606 // subtypes.
3607 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003608 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003609 // If there is only one explicitly or implicitly enabled subtype,
3610 // just returns it.
3611 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3612 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3613 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003614 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003615 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003616 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003617 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003618 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003619 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3620 true);
satok4e4569d2010-11-19 18:45:53 +09003621 }
satok3ef8b292010-11-23 06:06:29 +09003622 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003623 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003624 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003625 }
3626 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003627 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003628 }
3629
satok4e4569d2010-11-19 18:45:53 +09003630 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003631 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003632 @Override
satok4e4569d2010-11-19 18:45:53 +09003633 public List getShortcutInputMethodsAndSubtypes() {
3634 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003635 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003636 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003637 // If there are no selected shortcut subtypes, the framework will try to find
3638 // the most applicable subtype from all subtypes whose mode is
3639 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003640 Pair<InputMethodInfo, InputMethodSubtype> info =
3641 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003642 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003643 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003644 ret.add(info.first);
3645 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003646 }
satok3da92232011-01-11 22:46:30 +09003647 return ret;
satokf3db1af2010-11-23 13:34:33 +09003648 }
satokf3db1af2010-11-23 13:34:33 +09003649 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3650 ret.add(imi);
3651 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3652 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003653 }
3654 }
satokf3db1af2010-11-23 13:34:33 +09003655 return ret;
satok4e4569d2010-11-19 18:45:53 +09003656 }
3657 }
3658
satok42c5a162011-05-26 16:46:14 +09003659 @Override
satokb66d2872010-11-10 01:04:04 +09003660 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003661 // TODO: Make this work even for non-current users?
3662 if (!calledFromValidUser()) {
3663 return false;
3664 }
satokb66d2872010-11-10 01:04:04 +09003665 synchronized (mMethodMap) {
3666 if (subtype != null && mCurMethodId != null) {
3667 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003668 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003669 if (subtypeId != NOT_A_SUBTYPE_ID) {
3670 setInputMethodLocked(mCurMethodId, subtypeId);
3671 return true;
3672 }
3673 }
3674 return false;
3675 }
3676 }
3677
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003678 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003679 private static class InputMethodFileManager {
3680 private static final String SYSTEM_PATH = "system";
3681 private static final String INPUT_METHOD_PATH = "inputmethod";
3682 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3683 private static final String NODE_SUBTYPES = "subtypes";
3684 private static final String NODE_SUBTYPE = "subtype";
3685 private static final String NODE_IMI = "imi";
3686 private static final String ATTR_ID = "id";
3687 private static final String ATTR_LABEL = "label";
3688 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003689 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003690 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003691 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003692 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3693 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3694 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003695 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003696 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3697 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003698 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003699 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003700 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003701 if (methodMap == null) {
3702 throw new NullPointerException("methodMap is null");
3703 }
3704 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003705 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003706 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3707 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003708 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003709 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003710 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3711 }
3712 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3713 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3714 if (!subtypeFile.exists()) {
3715 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003716 writeAdditionalInputMethodSubtypes(
3717 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003718 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003719 readAdditionalInputMethodSubtypes(
3720 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003721 }
3722 }
3723
3724 private void deleteAllInputMethodSubtypes(String imiId) {
3725 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003726 mAdditionalSubtypesMap.remove(imiId);
3727 writeAdditionalInputMethodSubtypes(
3728 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003729 }
3730 }
3731
3732 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003733 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003734 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003735 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003736 final int N = additionalSubtypes.length;
3737 for (int i = 0; i < N; ++i) {
3738 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003739 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003740 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003741 } else {
3742 Slog.w(TAG, "Duplicated subtype definition found: "
3743 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003744 }
3745 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003746 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3747 writeAdditionalInputMethodSubtypes(
3748 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003749 }
3750 }
3751
3752 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3753 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003754 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003755 }
3756 }
3757
3758 private static void writeAdditionalInputMethodSubtypes(
3759 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3760 HashMap<String, InputMethodInfo> methodMap) {
3761 // Safety net for the case that this function is called before methodMap is set.
3762 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3763 FileOutputStream fos = null;
3764 try {
3765 fos = subtypesFile.startWrite();
3766 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003767 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003768 out.startDocument(null, true);
3769 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3770 out.startTag(null, NODE_SUBTYPES);
3771 for (String imiId : allSubtypes.keySet()) {
3772 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3773 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3774 continue;
3775 }
3776 out.startTag(null, NODE_IMI);
3777 out.attribute(null, ATTR_ID, imiId);
3778 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3779 final int N = subtypesList.size();
3780 for (int i = 0; i < N; ++i) {
3781 final InputMethodSubtype subtype = subtypesList.get(i);
3782 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003783 if (subtype.hasSubtypeId()) {
3784 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3785 String.valueOf(subtype.getSubtypeId()));
3786 }
satoke7c6998e2011-06-03 17:57:59 +09003787 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3788 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3789 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003790 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3791 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003792 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3793 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3794 out.attribute(null, ATTR_IS_AUXILIARY,
3795 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003796 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3797 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003798 out.endTag(null, NODE_SUBTYPE);
3799 }
3800 out.endTag(null, NODE_IMI);
3801 }
3802 out.endTag(null, NODE_SUBTYPES);
3803 out.endDocument();
3804 subtypesFile.finishWrite(fos);
3805 } catch (java.io.IOException e) {
3806 Slog.w(TAG, "Error writing subtypes", e);
3807 if (fos != null) {
3808 subtypesFile.failWrite(fos);
3809 }
3810 }
3811 }
3812
3813 private static void readAdditionalInputMethodSubtypes(
3814 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3815 if (allSubtypes == null || subtypesFile == null) return;
3816 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003817 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003818 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003819 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003820 int type = parser.getEventType();
3821 // Skip parsing until START_TAG
3822 while ((type = parser.next()) != XmlPullParser.START_TAG
3823 && type != XmlPullParser.END_DOCUMENT) {}
3824 String firstNodeName = parser.getName();
3825 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3826 throw new XmlPullParserException("Xml doesn't start with subtypes");
3827 }
3828 final int depth =parser.getDepth();
3829 String currentImiId = null;
3830 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3831 while (((type = parser.next()) != XmlPullParser.END_TAG
3832 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3833 if (type != XmlPullParser.START_TAG)
3834 continue;
3835 final String nodeName = parser.getName();
3836 if (NODE_IMI.equals(nodeName)) {
3837 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3838 if (TextUtils.isEmpty(currentImiId)) {
3839 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3840 continue;
3841 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003842 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003843 allSubtypes.put(currentImiId, tempSubtypesArray);
3844 } else if (NODE_SUBTYPE.equals(nodeName)) {
3845 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3846 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3847 continue;
3848 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003849 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003850 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003851 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003852 parser.getAttributeValue(null, ATTR_LABEL));
3853 final String imeSubtypeLocale =
3854 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003855 final String languageTag =
3856 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003857 final String imeSubtypeMode =
3858 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3859 final String imeSubtypeExtraValue =
3860 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003861 final boolean isAuxiliary = "1".equals(String.valueOf(
3862 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003863 final boolean isAsciiCapable = "1".equals(String.valueOf(
3864 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003865 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003866 .setSubtypeNameResId(label)
3867 .setSubtypeIconResId(icon)
3868 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003869 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003870 .setSubtypeMode(imeSubtypeMode)
3871 .setSubtypeExtraValue(imeSubtypeExtraValue)
3872 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003873 .setIsAsciiCapable(isAsciiCapable);
3874 final String subtypeIdString =
3875 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3876 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003877 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003878 }
3879 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003880 }
3881 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003882 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3883 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003884 return;
satoke7c6998e2011-06-03 17:57:59 +09003885 }
3886 }
3887 }
3888
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003889 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3890 @NonNull
3891 private final Handler mHandler;
3892
3893 LocalServiceImpl(@NonNull final Handler handler) {
3894 mHandler = handler;
3895 }
3896
3897 @Override
3898 public void setInteractive(boolean interactive) {
3899 // Do everything in handler so as not to block the caller.
3900 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3901 interactive ? 1 : 0, 0));
3902 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003903
3904 @Override
3905 public void switchInputMethod(boolean forwardDirection) {
3906 // Do everything in handler so as not to block the caller.
3907 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3908 forwardDirection ? 1 : 0, 0));
3909 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003910
3911 @Override
3912 public void hideCurrentInputMethod() {
3913 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3914 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3915 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003916 }
3917
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003918 private static String imeWindowStatusToString(final int imeWindowVis) {
3919 final StringBuilder sb = new StringBuilder();
3920 boolean first = true;
3921 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3922 sb.append("Active");
3923 first = false;
3924 }
3925 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3926 if (!first) {
3927 sb.append("|");
3928 }
3929 sb.append("Visible");
3930 }
3931 return sb.toString();
3932 }
3933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003935 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3936 @Nullable Uri contentUri, @Nullable String packageName) {
3937 if (!calledFromValidUser()) {
3938 return null;
3939 }
3940
3941 if (token == null) {
3942 throw new NullPointerException("token");
3943 }
3944 if (packageName == null) {
3945 throw new NullPointerException("packageName");
3946 }
3947 if (contentUri == null) {
3948 throw new NullPointerException("contentUri");
3949 }
3950 final String contentUriScheme = contentUri.getScheme();
3951 if (!"content".equals(contentUriScheme)) {
3952 throw new InvalidParameterException("contentUri must have content scheme");
3953 }
3954
3955 synchronized (mMethodMap) {
3956 final int uid = Binder.getCallingUid();
3957 if (mCurMethodId == null) {
3958 return null;
3959 }
3960 if (mCurToken != token) {
3961 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3962 + " token=" + token);
3963 return null;
3964 }
3965 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3966 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3967 // nature of our system. Let's compare it with our internal record.
3968 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3969 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3970 + mCurAttribute.packageName + " packageName=" + packageName);
3971 return null;
3972 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003973 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003974 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003975 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07003976 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08003977 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
3978 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
3979 imeUserId);
3980 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
3981 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
3982 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
3983 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
3984 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
3985 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
3986 // actually allowed to "uid", which is guaranteed to be the IME's one.
3987 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
3988 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07003989 }
3990 }
3991
3992 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3994 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3995 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3998 + Binder.getCallingPid()
3999 + ", uid=" + Binder.getCallingUid());
4000 return;
4001 }
4002
4003 IInputMethod method;
4004 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004005 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 synchronized (mMethodMap) {
4010 p.println("Current Input Method Manager state:");
4011 int N = mMethodList.size();
4012 p.println(" Input Methods:");
4013 for (int i=0; i<N; i++) {
4014 InputMethodInfo info = mMethodList.get(i);
4015 p.println(" InputMethod #" + i + ":");
4016 info.dump(p, " ");
4017 }
4018 p.println(" Clients:");
4019 for (ClientState ci : mClients.values()) {
4020 p.println(" Client " + ci + ":");
4021 p.println(" client=" + ci.client);
4022 p.println(" inputContext=" + ci.inputContext);
4023 p.println(" sessionRequested=" + ci.sessionRequested);
4024 p.println(" curSession=" + ci.curSession);
4025 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004026 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004028 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4029 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004030 focusedWindowClient = mCurFocusedWindowClient;
4031 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4033 + " mBoundToMethod=" + mBoundToMethod);
4034 p.println(" mCurToken=" + mCurToken);
4035 p.println(" mCurIntent=" + mCurIntent);
4036 method = mCurMethod;
4037 p.println(" mCurMethod=" + mCurMethod);
4038 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004039 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 p.println(" mShowRequested=" + mShowRequested
4041 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4042 + " mShowForced=" + mShowForced
4043 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004044 p.println(" mCurUserActionNotificationSequenceNumber="
4045 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004046 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004047 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004048 p.println(" mSwitchingController:");
4049 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004050 p.println(" mSettings:");
4051 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004053
Jeff Brownb88102f2010-09-08 11:49:43 -07004054 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 pw.flush();
4057 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004058 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4059 } catch (IOException | RemoteException e) {
4060 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004062 } else {
4063 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004065
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004066 if (focusedWindowClient != null && client != focusedWindowClient) {
4067 p.println(" ");
4068 p.println("Warning: Current input method client doesn't match the last focused. "
4069 + "window.");
4070 p.println("Dumping input method client in the last focused window just in case.");
4071 p.println(" ");
4072 pw.flush();
4073 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004074 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4075 } catch (IOException | RemoteException e) {
4076 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004077 }
4078 }
4079
Jeff Brownb88102f2010-09-08 11:49:43 -07004080 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 pw.flush();
4083 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004084 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4085 } catch (IOException | RemoteException e) {
4086 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004088 } else {
4089 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 }
4091 }
4092}