blob: 2698f9546357dca313db1c30614bd771b3abc863 [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;
satoke7c6998e2011-06-03 17:57:59 +090032import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.view.IInputContext;
34import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import com.android.internal.view.IInputMethodClient;
36import com.android.internal.view.IInputMethodManager;
37import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070038import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080040import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080041import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
satoke7c6998e2011-06-03 17:57:59 +090043import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090045import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070047import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070048import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070049import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080050import android.annotation.UserIdInt;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070051import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.app.ActivityManagerNative;
53import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070054import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090055import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090056import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090057import android.app.Notification;
58import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070059import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090060import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.ComponentName;
62import android.content.ContentResolver;
63import android.content.Context;
64import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090066import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090068import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070070import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090071import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.pm.PackageManager;
73import android.content.pm.ResolveInfo;
74import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070075import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.content.res.Resources;
77import android.content.res.TypedArray;
78import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080079import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080080import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080081import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070082import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040084import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070085import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090086import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.Handler;
88import android.os.IBinder;
89import android.os.IInterface;
90import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070091import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070093import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080095import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.ServiceManager;
97import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090098import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080099import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.provider.Settings;
101import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900102import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700103import android.util.ArrayMap;
104import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700105import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900107import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900108import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.util.PrintWriterPrinter;
110import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900111import android.util.Slog;
112import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900113import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700115import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900116import android.view.LayoutInflater;
117import android.view.View;
118import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700119import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700121import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900122import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700124import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import android.view.inputmethod.InputMethod;
126import android.view.inputmethod.InputMethodInfo;
127import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700128import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900129import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900130import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900131import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900132import android.widget.CompoundButton;
133import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900134import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900135import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900136import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700137import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
satoke7c6998e2011-06-03 17:57:59 +0900139import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900141import java.io.FileInputStream;
142import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import java.io.IOException;
144import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700145import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100146import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700147import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900149import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.util.HashMap;
151import java.util.List;
152
153/**
154 * This class provides a system service that manages input methods.
155 */
156public class InputMethodManagerService extends IInputMethodManager.Stub
157 implements ServiceConnection, Handler.Callback {
158 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700159 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700160 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700162 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
163 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
164 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 static final int MSG_UNBIND_INPUT = 1000;
167 static final int MSG_BIND_INPUT = 1010;
168 static final int MSG_SHOW_SOFT_INPUT = 1020;
169 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700170 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 static final int MSG_ATTACH_TOKEN = 1040;
172 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 static final int MSG_START_INPUT = 2000;
175 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
Yohei Yukawa33e81792015-11-17 21:14:42 -0800177 static final int MSG_UNBIND_CLIENT = 3000;
178 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700179 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700180 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900181 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800182 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
satok01038492012-04-09 21:08:27 +0900184 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
185
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700186 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
187
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700188 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
satokf9f01002011-05-19 21:31:50 +0900190 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
191
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900192 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900193 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900194
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700195 @Retention(SOURCE)
196 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
197 private @interface HardKeyboardBehavior {
198 int WIRELESS_AFFORDANCE = 0;
199 int WIRED_AFFORDANCE = 1;
200 }
satok4e4569d2010-11-19 18:45:53 +0900201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800203 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900205 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700208 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700210 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900211 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900212 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900213 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800214 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900216 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 // All known input methods. mMethodMap also serves as the global
219 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700220 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
221 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900222 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700223 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900224 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700226 // Used to bring IME service up to visible adjustment while it is being shown.
227 final ServiceConnection mVisibleConnection = new ServiceConnection() {
228 @Override public void onServiceConnected(ComponentName name, IBinder service) {
229 }
230
231 @Override public void onServiceDisconnected(ComponentName name) {
232 }
233 };
234 boolean mVisibleBound = false;
235
satok7cfc0ed2011-06-20 21:29:36 +0900236 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700237 private NotificationManager mNotificationManager;
238 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700239 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400240 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700241 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900242 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900243 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900244 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900245
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900246 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 final ClientState client;
248 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700249
250 IInputMethodSession session;
251 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 @Override
254 public String toString() {
255 return "SessionState{uid " + client.uid + " pid " + client.pid
256 + " method " + Integer.toHexString(
257 System.identityHashCode(method))
258 + " session " + Integer.toHexString(
259 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700260 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 + "}";
262 }
263
264 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700265 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 client = _client;
267 method = _method;
268 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700269 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
Jeff Brownc28867a2013-03-26 15:42:39 -0700273 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 final IInputMethodClient client;
275 final IInputContext inputContext;
276 final int uid;
277 final int pid;
278 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 boolean sessionRequested;
281 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 @Override
284 public String toString() {
285 return "ClientState{" + Integer.toHexString(
286 System.identityHashCode(this)) + " uid " + uid
287 + " pid " + pid + "}";
288 }
289
290 ClientState(IInputMethodClient _client, IInputContext _inputContext,
291 int _uid, int _pid) {
292 client = _client;
293 inputContext = _inputContext;
294 uid = _uid;
295 pid = _pid;
296 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
297 }
298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800299
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700300 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700303 * Set once the system is ready to run third party code.
304 */
305 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700307 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700308 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
309 * method. This is to be synchronized with the secure settings keyed with
310 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
311 *
312 * <p>This can be transiently {@code null} when the system is re-initializing input method
313 * settings, e.g., the system locale is just changed.</p>
314 *
315 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
316 * {@link InputMethodManagerService}.</p>
317 *
318 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700320 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
324 * The current binding sequence number, incremented every time there is
325 * a new bind performed.
326 */
327 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * The client that is currently bound to an input method.
331 */
332 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800335 * The last window token that we confirmed to be focused. This is always updated upon reports
336 * from the input method client. If the window state is already changed before the report is
337 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700338 */
339 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700341 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800342 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
343 */
344 ClientState mCurFocusedWindowClient;
345
346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 * The input context last provided by the current client.
348 */
349 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700352 * The missing method flags for the input context last provided by the current client.
353 *
354 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
355 */
356 int mCurInputContextMissingMethods;
357
358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 * The attributes last provided by the current client.
360 */
361 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700364 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700366 *
367 * <p>This can be {@code null} when no input method is connected.</p>
368 *
369 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700371 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
satokab751aa2010-09-14 19:17:36 +0900375 * The current subtype of the current input method.
376 */
377 private InputMethodSubtype mCurrentSubtype;
378
satok4e4569d2010-11-19 18:45:53 +0900379 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900380 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700381 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900382
John Spurlocke0980502013-10-25 11:59:29 -0400383 // Was the keyguard locked when this client became current?
384 private boolean mCurClientInKeyguard;
385
satokab751aa2010-09-14 19:17:36 +0900386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * Set to true if our ServiceConnection is currently actively bound to
388 * a service (whether or not we have gotten its IBinder back yet).
389 */
390 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * Set if the client has asked for the input method to be shown.
394 */
395 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 /**
398 * Set if we were explicitly told to show the input method.
399 */
400 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * Set if we were forced to be shown.
404 */
405 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 /**
408 * Set if we last told the input method to show itself.
409 */
410 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 /**
413 * The Intent used to connect to the current input method.
414 */
415 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
418 * The token we have made for the currently active input method, to
419 * identify it in the future.
420 */
421 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 /**
424 * If non-null, this is the input method service we are currently connected
425 * to.
426 */
427 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
430 * Time that we last initiated a bind to the input method, to determine
431 * if we should try to disconnect and reconnect to it.
432 */
433 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
436 * Have we called mCurMethod.bindInput()?
437 */
438 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 /**
441 * Currently enabled session. Only touched by service thread, not
442 * protected by a lock.
443 */
444 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700447 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700449 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900451 int mCurUserActionNotificationSequenceNumber = 0;
452
Joe Onorato857fd9b2011-01-27 15:08:35 -0800453 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900454
455 /**
456 * A set of status bits regarding the active IME.
457 *
458 * <p>This value is a combination of following two bits:</p>
459 * <dl>
460 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
461 * <dd>
462 * If this bit is ON, connected IME is ready to accept touch/key events.
463 * </dd>
464 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
465 * <dd>
466 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
467 * </dd>
468 * </dl>
469 * <em>Do not update this value outside of setImeWindowStatus.</em>
470 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800471 int mImeWindowVis;
472
Ken Wakasa05dbb652011-08-22 15:22:43 +0900473 private AlertDialog.Builder mDialogBuilder;
474 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700475 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900476 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700477 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900478 private InputMethodInfo[] mIms;
479 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800480 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700481 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800482 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900483 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
484 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500485 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700486 @HardKeyboardBehavior
487 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700490 int mUserId;
491 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700492 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800493 String mLastEnabled = "";
494
Yohei Yukawa81482972015-06-04 00:58:59 -0700495 /**
496 * <em>This constructor must be called within the lock.</em>
497 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 SettingsObserver(Handler handler) {
499 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700500 }
501
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800502 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700503 if (mRegistered && mUserId == userId) {
504 return;
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700507 if (mRegistered) {
508 mContext.getContentResolver().unregisterContentObserver(this);
509 mRegistered = false;
510 }
511 if (mUserId != userId) {
512 mLastEnabled = "";
513 mUserId = userId;
514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700516 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900517 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700518 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900519 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700520 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700521 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700522 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800523 resolver.registerContentObserver(Settings.Secure.getUriFor(
524 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700525 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800527
Michael Wright7b5a96b2014-08-09 19:28:42 -0700528 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800529 final Uri showImeUri = Settings.Secure.getUriFor(
530 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
531 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
532 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700534 if (showImeUri.equals(uri)) {
535 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800536 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
537 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
538 mContext.getContentResolver(),
539 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
540 0, mUserId) == 1;
541 if (mAccessibilityRequestingNoSoftKeyboard) {
542 final boolean showRequested = mShowRequested;
543 hideCurrentInputLocked(0, null);
544 mShowRequested = showRequested;
545 } else if (mShowRequested) {
546 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
547 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700548 } else {
549 boolean enabledChanged = false;
550 String newEnabled = mSettings.getEnabledInputMethodsStr();
551 if (!mLastEnabled.equals(newEnabled)) {
552 mLastEnabled = newEnabled;
553 enabledChanged = true;
554 }
555 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700559
560 @Override
561 public String toString() {
562 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
563 + " mLastEnabled=" + mLastEnabled + "}";
564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800566
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900567 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900568 @Override
569 public void onReceive(Context context, Intent intent) {
570 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700571 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900572 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700573 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900574 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800575 } else if (Intent.ACTION_USER_ADDED.equals(action)
576 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100577 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800578 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700579 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
580 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
581 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
582 final String prevValue = intent.getStringExtra(
583 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
584 final String newValue = intent.getStringExtra(
585 Intent.EXTRA_SETTING_NEW_VALUE);
586 restoreEnabledInputMethods(mContext, prevValue, newValue);
587 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900588 } else {
589 Slog.w(TAG, "Unexpected intent " + intent);
590 }
591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800593
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700594 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
595 // does not attempt to validate on the fly with any installed device policy, so must only
596 // be run in the context of initial device setup.
597 //
598 // TODO: Move this method to InputMethodUtils with adding unit tests.
599 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
600 if (DEBUG_RESTORE) {
601 Slog.i(TAG, "Restoring enabled input methods:");
602 Slog.i(TAG, "prev=" + prevValue);
603 Slog.i(TAG, " new=" + newValue);
604 }
605 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900606 ArrayMap<String, ArraySet<String>> prevMap =
607 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
608 ArrayMap<String, ArraySet<String>> newMap =
609 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700610
611 // Merge the restored ime+subtype enabled states into the live state
612 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
613 final String imeId = entry.getKey();
614 ArraySet<String> prevSubtypes = prevMap.get(imeId);
615 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700616 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700617 prevMap.put(imeId, prevSubtypes);
618 }
619 prevSubtypes.addAll(entry.getValue());
620 }
621
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700622 final String mergedImesAndSubtypesString =
623 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700624 if (DEBUG_RESTORE) {
625 Slog.i(TAG, "Merged IME string:");
626 Slog.i(TAG, " " + mergedImesAndSubtypesString);
627 }
628 Settings.Secure.putString(context.getContentResolver(),
629 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
630 }
631
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800632 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900633 private boolean isChangingPackagesOfCurrentUser() {
634 final int userId = getChangingUserId();
635 final boolean retval = userId == mSettings.getCurrentUserId();
636 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900637 if (!retval) {
638 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
639 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900640 }
641 return retval;
642 }
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900646 if (!isChangingPackagesOfCurrentUser()) {
647 return false;
648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900650 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 final int N = mMethodList.size();
652 if (curInputMethodId != null) {
653 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800654 InputMethodInfo imi = mMethodList.get(i);
655 if (imi.getId().equals(curInputMethodId)) {
656 for (String pkg : packages) {
657 if (imi.getPackageName().equals(pkg)) {
658 if (!doit) {
659 return true;
660 }
satok723a27e2010-11-11 14:58:11 +0900661 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800662 chooseNewDefaultIMELocked();
663 return true;
664 }
665 }
666 }
667 }
668 }
669 }
670 return false;
671 }
672
673 @Override
674 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900675 if (!isChangingPackagesOfCurrentUser()) {
676 return;
677 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800678 synchronized (mMethodMap) {
679 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900680 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681 final int N = mMethodList.size();
682 if (curInputMethodId != null) {
683 for (int i=0; i<N; i++) {
684 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900685 final String imiId = imi.getId();
686 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800687 curIm = imi;
688 }
satoke7c6998e2011-06-03 17:57:59 +0900689
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800690 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900691 if (isPackageModified(imi.getPackageName())) {
692 mFileManager.deleteAllInputMethodSubtypes(imiId);
693 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800694 if (change == PACKAGE_TEMPORARY_CHANGE
695 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800696 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800697 + imi.getComponent());
698 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
700 }
701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800702
Yohei Yukawa94e33302016-02-12 19:37:03 -0800703 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800707 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800708 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800709 if (change == PACKAGE_TEMPORARY_CHANGE
710 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800711 ServiceInfo si = null;
712 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900713 si = mIPackageManager.getServiceInfo(
714 curIm.getComponent(), 0, mSettings.getCurrentUserId());
715 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800716 }
717 if (si == null) {
718 // Uh oh, current input method is no longer around!
719 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800720 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900721 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800722 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800723 changed = true;
724 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800725 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900726 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800727 }
728 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800729 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800730 }
satokab751aa2010-09-14 19:17:36 +0900731
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800732 if (curIm == null) {
733 // We currently don't have a default input method... is
734 // one now available?
735 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700736 } else if (!changed && isPackageModified(curIm.getPackageName())) {
737 // Even if the current input method is still available, mCurrentSubtype could
738 // be obsolete when the package is modified in practice.
739 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800742 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800743 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745 }
746 }
747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800748
Jeff Brownc28867a2013-03-26 15:42:39 -0700749 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900750 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700751 private final IInputMethod mMethod;
752 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
Jeff Brownc28867a2013-03-26 15:42:39 -0700754 MethodCallback(InputMethodManagerService imms, IInputMethod method,
755 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900756 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700757 mMethod = method;
758 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
satoke7c6998e2011-06-03 17:57:59 +0900761 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700762 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700763 long ident = Binder.clearCallingIdentity();
764 try {
765 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
766 } finally {
767 Binder.restoreCallingIdentity(ident);
768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771
satok01038492012-04-09 21:08:27 +0900772 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700773 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900774 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700775 public void onHardKeyboardStatusChange(boolean available) {
776 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
777 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900778 }
779
Michael Wright7b5a96b2014-08-09 19:28:42 -0700780 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900781 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700782 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900783 }
784 synchronized(mMethodMap) {
785 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
786 && mSwitchingDialog.isShowing()) {
787 mSwitchingDialogTitleView.findViewById(
788 com.android.internal.R.id.hard_keyboard_section).setVisibility(
789 available ? View.VISIBLE : View.GONE);
790 }
791 }
792 }
793 }
794
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800795 public static final class Lifecycle extends SystemService {
796 private InputMethodManagerService mService;
797
798 public Lifecycle(Context context) {
799 super(context);
800 mService = new InputMethodManagerService(context);
801 }
802
803 @Override
804 public void onStart() {
805 LocalServices.addService(InputMethodManagerInternal.class,
806 new LocalServiceImpl(mService.mHandler));
807 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
808 }
809
810 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800811 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700812 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800813 // TODO: Dispatch this to a worker thread as needed.
814 mService.onSwitchUser(userHandle);
815 }
816
817 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800818 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700819 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800820 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800821 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
822 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
823 .getService(Context.STATUS_BAR_SERVICE);
824 mService.systemRunning(statusBarService);
825 }
826 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800827
828 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700829 public void onUnlockUser(final @UserIdInt int userHandle) {
830 // Called on ActivityManager thread.
831 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -0700832 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800833 }
834 }
835
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800836 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800837 synchronized(mMethodMap) {
838 final int currentUserId = mSettings.getCurrentUserId();
839 if (DEBUG) {
840 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
841 }
842 if (userId != currentUserId) {
843 return;
844 }
845 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
846 // We need to rebuild IMEs.
847 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
848 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
849 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800850 }
851
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800852 void onSwitchUser(@UserIdInt int userId) {
853 synchronized (mMethodMap) {
854 switchUserLocked(userId);
855 }
856 }
857
Seigo Nonaka7309b122015-08-17 18:34:13 -0700858 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900859 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800861 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700863 // Note: SettingsObserver doesn't register observers in its constructor.
864 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 mIWindowManager = IWindowManager.Stub.asInterface(
866 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700867 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800868 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900869 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 public void executeMessage(Message msg) {
871 handleMessage(msg);
872 }
Mita Yuned218c72012-12-06 17:18:25 -0800873 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800874 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800875 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900876 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700877 mHasFeature = context.getPackageManager().hasSystemFeature(
878 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500879 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700880 mHardKeyboardBehavior = mContext.getResources().getInteger(
881 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900882
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400883 Bundle extras = new Bundle();
884 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
885 mImeSwitcherNotification = new Notification.Builder(mContext)
886 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
887 .setWhen(0)
888 .setOngoing(true)
889 .addExtras(extras)
890 .setCategory(Notification.CATEGORY_SYSTEM)
891 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400892
satok7cfc0ed2011-06-20 21:29:36 +0900893 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900894 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900895
896 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900897
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900898 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900899 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800900 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
901 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700902 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900903 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
satok7cfc0ed2011-06-20 21:29:36 +0900905 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900906 int userId = 0;
907 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900908 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
909 } catch (RemoteException e) {
910 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
911 }
satok81f8b7c2012-12-04 20:42:56 +0900912 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900913
satokd87c2592010-09-29 11:52:06 +0900914 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900915 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800916 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700917
Kenny Guy2a764942014-04-02 13:29:20 +0100918 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900919 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900920 synchronized (mMethodMap) {
921 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
922 mSettings, context);
923 }
satok0a1bcf42012-05-16 19:26:31 +0900924
925 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900926 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900927 if (DEBUG) {
928 Slog.d(TAG, "Initial default ime = " + defaultImiId);
929 }
satok0a1bcf42012-05-16 19:26:31 +0900930 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
931
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900932 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800933 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900934 }
satokd87c2592010-09-29 11:52:06 +0900935 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
satok0a1bcf42012-05-16 19:26:31 +0900937 if (!mImeSelectedOnBoot) {
938 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900939 synchronized (mMethodMap) {
940 resetDefaultImeLocked(context);
941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800943
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900944 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700945 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900946 updateFromSettingsLocked(true);
947 }
satok5b927c432012-05-01 20:09:34 +0900948
949 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
950 // according to the new system locale.
951 final IntentFilter filter = new IntentFilter();
952 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
953 mContext.registerReceiver(
954 new BroadcastReceiver() {
955 @Override
956 public void onReceive(Context context, Intent intent) {
957 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900958 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900959 }
960 }
961 }, filter);
962 }
963
satok5b927c432012-05-01 20:09:34 +0900964 private void resetDefaultImeLocked(Context context) {
965 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800966 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900967 return;
968 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800969 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
970 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
971 if (suitableImes.isEmpty()) {
972 Slog.i(TAG, "No default found");
973 return;
satok5b927c432012-05-01 20:09:34 +0900974 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800975 final InputMethodInfo defIm = suitableImes.get(0);
976 Slog.i(TAG, "Default found, using " + defIm.getId());
977 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900978 }
979
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900980 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
981 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900982 if (!mSystemReady) {
983 // not system ready
984 return;
985 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800986 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800988 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 if (!updateOnlyWhenLocaleChanged) {
990 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800991 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900992 }
993 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800994 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900995 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800996 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900997 if (!updateOnlyWhenLocaleChanged) {
998 final String selectedImiId = mSettings.getSelectedInputMethod();
999 if (TextUtils.isEmpty(selectedImiId)) {
1000 // This is the first time of the user switch and
1001 // set the current ime to the proper one.
1002 resetDefaultImeLocked(mContext);
1003 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +09001004 } else {
1005 // If the locale is changed, needs to reset the default ime
1006 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001007 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001008 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -08001009 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001010 if (!updateOnlyWhenLocaleChanged) {
1011 try {
1012 startInputInnerLocked();
1013 } catch (RuntimeException e) {
1014 Slog.w(TAG, "Unexpected exception", e);
1015 }
1016 }
1017 }
1018 }
1019
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001020 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001021 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1022 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001023 }
1024
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001025 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001026 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1027 + " currentUserId=" + mSettings.getCurrentUserId());
1028
Yohei Yukawa81482972015-06-04 00:58:59 -07001029 // ContentObserver should be registered again when the user is changed
1030 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001031
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001032 // If the system is not ready or the device is not yed unlocked by the user, then we use
1033 // copy-on-write settings.
1034 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001035 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001036 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001037 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001038 // InputMethodFileManager should be reset when the user is changed
1039 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001040 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001041
1042 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1043 + " defaultImiId=" + defaultImiId);
1044
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001045 // For secondary users, the list of enabled IMEs may not have been updated since the
1046 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1047 // not be empty even if the IME has been uninstalled by the primary user.
1048 // Even in such cases, IMMS works fine because it will find the most applicable
1049 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001050 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001051 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001052 initialUserSwitch /* needsToResetDefaultIme */);
1053 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001054 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1055 mSettings.getEnabledInputMethodListLocked(), newUserId,
1056 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001057 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001058
1059 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1060 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001061 }
1062
Kenny Guy2a764942014-04-02 13:29:20 +01001063 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001064 mSettings.setCurrentProfileIds(
1065 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001066 }
1067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 @Override
1069 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1070 throws RemoteException {
1071 try {
1072 return super.onTransact(code, data, reply, flags);
1073 } catch (RuntimeException e) {
1074 // The input method manager only throws security exceptions, so let's
1075 // log all others.
1076 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001077 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079 throw e;
1080 }
1081 }
1082
Svetoslav Ganova0027152013-06-25 14:59:53 -07001083 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001084 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001085 if (DEBUG) {
1086 Slog.d(TAG, "--- systemReady");
1087 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001088 if (!mSystemReady) {
1089 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001090 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001091 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001092 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001093 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1094 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001095 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001096 if (mStatusBar != null) {
1097 mStatusBar.setIconVisibility(mSlotIme, false);
1098 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001099 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001100 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1101 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001102 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001103 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001104 mHardKeyboardListener);
1105 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001106 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001107 if (!mImeSelectedOnBoot) {
1108 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001109 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001110 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1111 mSettings.getEnabledInputMethodListLocked(),
1112 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001113 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001114 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001115 try {
1116 startInputInnerLocked();
1117 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001118 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001119 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001120 }
1121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001123
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001124 // ---------------------------------------------------------------------------------------
1125 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1126 // 1) it comes from the system process
1127 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1128 private boolean calledFromValidUser() {
1129 final int uid = Binder.getCallingUid();
1130 final int userId = UserHandle.getUserId(uid);
1131 if (DEBUG) {
1132 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1133 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1134 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001135 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1136 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001137 }
Kenny Guy2a764942014-04-02 13:29:20 +01001138 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001139 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001140 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001141
1142 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1143 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1144 // must not manage background users' states in any functions.
1145 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1146 // by a token.
1147 if (mContext.checkCallingOrSelfPermission(
1148 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1149 == PackageManager.PERMISSION_GRANTED) {
1150 if (DEBUG) {
1151 Slog.d(TAG, "--- Access granted because the calling process has "
1152 + "the INTERACT_ACROSS_USERS_FULL permission");
1153 }
1154 return true;
1155 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001156 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1157 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001158 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001159 }
1160
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001161
1162 /**
1163 * Returns true iff the caller is identified to be the current input method with the token.
1164 * @param token The window token given to the input method when it was started.
1165 * @return true if and only if non-null valid token is specified.
1166 */
1167 private boolean calledWithValidToken(IBinder token) {
1168 if (token == null || mCurToken != token) {
1169 return false;
1170 }
1171 return true;
1172 }
1173
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001174 private boolean bindCurrentInputMethodService(
1175 Intent service, ServiceConnection conn, int flags) {
1176 if (service == null || conn == null) {
1177 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1178 return false;
1179 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001180 return mContext.bindServiceAsUser(service, conn, flags,
1181 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001182 }
1183
satoke7c6998e2011-06-03 17:57:59 +09001184 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001186 // TODO: Make this work even for non-current users?
1187 if (!calledFromValidUser()) {
1188 return Collections.emptyList();
1189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001191 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
1194
satoke7c6998e2011-06-03 17:57:59 +09001195 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001197 // TODO: Make this work even for non-current users?
1198 if (!calledFromValidUser()) {
1199 return Collections.emptyList();
1200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001202 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 }
1205
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001206 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001207 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001208 * @return enabled subtypes of the specified imi
1209 */
satoke7c6998e2011-06-03 17:57:59 +09001210 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001211 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001212 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001213 // TODO: Make this work even for non-current users?
1214 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001215 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001216 }
satok67ddf9c2010-11-17 09:45:54 +09001217 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001218 final InputMethodInfo imi;
1219 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001220 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001221 } else {
1222 imi = mMethodMap.get(imiId);
1223 }
1224 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001225 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001226 }
1227 return mSettings.getEnabledInputMethodSubtypeListLocked(
1228 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001229 }
1230 }
1231
satoke7c6998e2011-06-03 17:57:59 +09001232 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 public void addClient(IInputMethodClient client,
1234 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001235 if (!calledFromValidUser()) {
1236 return;
1237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 synchronized (mMethodMap) {
1239 mClients.put(client.asBinder(), new ClientState(client,
1240 inputContext, uid, pid));
1241 }
1242 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
satoke7c6998e2011-06-03 17:57:59 +09001244 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001246 if (!calledFromValidUser()) {
1247 return;
1248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001250 ClientState cs = mClients.remove(client.asBinder());
1251 if (cs != null) {
1252 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001253 if (mCurClient == cs) {
1254 mCurClient = null;
1255 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001256 if (mCurFocusedWindowClient == cs) {
1257 mCurFocusedWindowClient = null;
1258 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 }
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 void executeOrSendMessage(IInterface target, Message msg) {
1264 if (target.asBinder() instanceof Binder) {
1265 mCaller.sendMessage(msg);
1266 } else {
1267 handleMessage(msg);
1268 msg.recycle();
1269 }
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
Yohei Yukawa33e81792015-11-17 21:14:42 -08001272 void unbindCurrentClientLocked(
1273 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001275 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 + mCurClient.client.asBinder());
1277 if (mBoundToMethod) {
1278 mBoundToMethod = false;
1279 if (mCurMethod != null) {
1280 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1281 MSG_UNBIND_INPUT, mCurMethod));
1282 }
1283 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001284
1285 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1286 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001287 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1288 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project10592532009-03-18 17:39:46 -07001292 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 private int getImeShowFlags() {
1297 int flags = 0;
1298 if (mShowForced) {
1299 flags |= InputMethod.SHOW_FORCED
1300 | InputMethod.SHOW_EXPLICIT;
1301 } else if (mShowExplicitlyRequested) {
1302 flags |= InputMethod.SHOW_EXPLICIT;
1303 }
1304 return flags;
1305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 private int getAppShowFlags() {
1308 int flags = 0;
1309 if (mShowForced) {
1310 flags |= InputMethodManager.SHOW_FORCED;
1311 } else if (!mShowExplicitlyRequested) {
1312 flags |= InputMethodManager.SHOW_IMPLICIT;
1313 }
1314 return flags;
1315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
Dianne Hackborn7663d802012-02-24 13:08:49 -08001317 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 if (!mBoundToMethod) {
1319 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1320 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1321 mBoundToMethod = true;
1322 }
1323 final SessionState session = mCurClient.curSession;
1324 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001325 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1326 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1327 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001329 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1330 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1331 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001334 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001335 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001337 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001338 (session.channel != null ? session.channel.dup() : null),
1339 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001341
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001342 InputBindResult startInputLocked(
1343 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001344 IInputMethodClient client, IInputContext inputContext,
1345 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001346 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 // If no method is currently selected, do nothing.
1348 if (mCurMethodId == null) {
1349 return mNoBinding;
1350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 ClientState cs = mClients.get(client.asBinder());
1353 if (cs == null) {
1354 throw new IllegalArgumentException("unknown client "
1355 + client.asBinder());
1356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001357
Yohei Yukawa74750f22016-03-22 12:54:22 -07001358 if (attribute == null) {
1359 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1360 + " uid=" + cs.uid + " pid=" + cs.pid);
1361 return null;
1362 }
1363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 try {
1365 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1366 // Check with the window manager to make sure this client actually
1367 // has a window with focus. If not, reject. This is thread safe
1368 // because if the focus changes some time before or after, the
1369 // next client receiving focus that has any interest in input will
1370 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001371 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1373 return null;
1374 }
1375 } catch (RemoteException e) {
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001378 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1379 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001380 }
1381
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001382 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001383 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001384 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001385 // If no method is currently selected, do nothing.
1386 if (mCurMethodId == null) {
1387 return mNoBinding;
1388 }
1389
Yohei Yukawad57ba672015-06-08 16:39:46 -07001390 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1391 attribute.packageName)) {
1392 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1393 + " uid=" + cs.uid + " package=" + attribute.packageName);
1394 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001395 }
1396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001398 // Was the keyguard locked when switching over to the new client?
1399 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 // If the client is changing, we need to switch over to the new
1401 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001402 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001403 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001404 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405
1406 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001407 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001408 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001409 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 }
1411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 // Bump up the sequence for this client and attach it.
1414 mCurSeq++;
1415 if (mCurSeq <= 0) mCurSeq = 1;
1416 mCurClient = cs;
1417 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001418 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 // Check if the input method is changing.
1422 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1423 if (cs.curSession != null) {
1424 // Fast case: if we are already connected to the input method,
1425 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001426 return attachNewInputLocked(
1427 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
1429 if (mHaveConnection) {
1430 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 // Return to client, and we will get back with it when
1432 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001433 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001434 return new InputBindResult(null, null, mCurId, mCurSeq,
1435 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 } else if (SystemClock.uptimeMillis()
1437 < (mLastBindTime+TIME_TO_RECONNECT)) {
1438 // In this case we have connected to the service, but
1439 // don't yet have its interface. If it hasn't been too
1440 // long since we did the connection, we'll return to
1441 // the client and wait to get the service interface so
1442 // we can report back. If it has been too long, we want
1443 // to fall through so we can try a disconnect/reconnect
1444 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001445 return new InputBindResult(null, null, mCurId, mCurSeq,
1446 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1449 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 }
1452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001453
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001454 return startInputInnerLocked();
1455 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001456
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001457 InputBindResult startInputInnerLocked() {
1458 if (mCurMethodId == null) {
1459 return mNoBinding;
1460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001462 if (!mSystemReady) {
1463 // If the system is not yet ready, we shouldn't be running third
1464 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001465 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1466 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1470 if (info == null) {
1471 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001474 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1477 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001478 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1479 com.android.internal.R.string.input_method_binding_label);
1480 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1481 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001482 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001483 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1484 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 mLastBindTime = SystemClock.uptimeMillis();
1486 mHaveConnection = true;
1487 mCurId = info.getId();
1488 mCurToken = new Binder();
1489 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001490 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001491 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 } catch (RemoteException e) {
1493 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001494 return new InputBindResult(null, null, mCurId, mCurSeq,
1495 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 } else {
1497 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001498 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 + mCurIntent);
1500 }
1501 return null;
1502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001504 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001505 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001506 IInputMethodClient client, IInputContext inputContext,
1507 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001508 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001509 if (!calledFromValidUser()) {
1510 return null;
1511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001513 if (DEBUG) {
1514 Slog.v(TAG, "startInput: reason="
1515 + InputMethodClient.getStartInputReason(startInputReason)
1516 + " client = " + client.asBinder()
1517 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001518 + " missingMethods="
1519 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001520 + " attribute=" + attribute
1521 + " controlFlags=#" + Integer.toHexString(controlFlags));
1522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 final long ident = Binder.clearCallingIdentity();
1524 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001525 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1526 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 } finally {
1528 Binder.restoreCallingIdentity(ident);
1529 }
1530 }
1531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
satoke7c6998e2011-06-03 17:57:59 +09001533 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 public void finishInput(IInputMethodClient client) {
1535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001536
satoke7c6998e2011-06-03 17:57:59 +09001537 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 public void onServiceConnected(ComponentName name, IBinder service) {
1539 synchronized (mMethodMap) {
1540 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1541 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001542 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001543 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001544 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001545 return;
1546 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001547 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001548 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1549 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001551 clearClientSessionLocked(mCurClient);
1552 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554 }
1555 }
1556 }
1557
Jeff Brownc28867a2013-03-26 15:42:39 -07001558 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1559 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 synchronized (mMethodMap) {
1561 if (mCurMethod != null && method != null
1562 && mCurMethod.asBinder() == method.asBinder()) {
1563 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001564 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001566 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001567 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 if (res.method != null) {
1569 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001570 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001572 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
1574 }
1575 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001576
1577 // Session abandoned. Close its associated input channel.
1578 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001580
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001581 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001582 if (mVisibleBound) {
1583 mContext.unbindService(mVisibleConnection);
1584 mVisibleBound = false;
1585 }
1586
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001587 if (mHaveConnection) {
1588 mContext.unbindService(this);
1589 mHaveConnection = false;
1590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001591
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001592 if (mCurToken != null) {
1593 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001595 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001596 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001597 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001598 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001599 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001600 } catch (RemoteException e) {
1601 }
1602 mCurToken = null;
1603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
The Android Open Source Project10592532009-03-18 17:39:46 -07001605 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001606 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001607 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001608
Yohei Yukawa33e81792015-11-17 21:14:42 -08001609 void resetCurrentMethodAndClient(
1610 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001611 mCurMethodId = null;
1612 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001613 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001614 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001615
1616 void requestClientSessionLocked(ClientState cs) {
1617 if (!cs.sessionRequested) {
1618 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1619 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1620 cs.sessionRequested = true;
1621 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1622 MSG_CREATE_SESSION, mCurMethod, channels[1],
1623 new MethodCallback(this, mCurMethod, channels[0])));
1624 }
1625 }
1626
1627 void clearClientSessionLocked(ClientState cs) {
1628 finishSessionLocked(cs.curSession);
1629 cs.curSession = null;
1630 cs.sessionRequested = false;
1631 }
1632
1633 private void finishSessionLocked(SessionState sessionState) {
1634 if (sessionState != null) {
1635 if (sessionState.session != null) {
1636 try {
1637 sessionState.session.finishSession();
1638 } catch (RemoteException e) {
1639 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001640 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001641 }
1642 sessionState.session = null;
1643 }
1644 if (sessionState.channel != null) {
1645 sessionState.channel.dispose();
1646 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001647 }
1648 }
1649 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001650
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001651 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 if (mCurMethod != null) {
1653 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001654 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001656
Jeff Brownc28867a2013-03-26 15:42:39 -07001657 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001658 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 mCurMethod = null;
1660 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001661 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001662 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001665
satoke7c6998e2011-06-03 17:57:59 +09001666 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 public void onServiceDisconnected(ComponentName name) {
1668 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001669 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 + " mCurIntent=" + mCurIntent);
1671 if (mCurMethod != null && mCurIntent != null
1672 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001673 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 // We consider this to be a new bind attempt, since the system
1675 // should now try to restart the service for us.
1676 mLastBindTime = SystemClock.uptimeMillis();
1677 mShowRequested = mInputShown;
1678 mInputShown = false;
1679 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001680 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1681 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1682 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
1684 }
1685 }
1686 }
1687
satokf9f01002011-05-19 21:31:50 +09001688 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1690 long ident = Binder.clearCallingIdentity();
1691 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001693 if (!calledWithValidToken(token)) {
1694 final int uid = Binder.getCallingUid();
1695 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1696 + " token:" + token);
1697 return;
1698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001701 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001702 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001705 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001706 CharSequence contentDescription = null;
1707 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001708 // Use PackageManager to load label
1709 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001710 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001711 mIPackageManager.getApplicationInfo(packageName, 0,
1712 mSettings.getCurrentUserId()));
1713 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001714 /* ignore */
1715 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001716 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001717 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001718 contentDescription != null
1719 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001720 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 }
1723 }
1724 } finally {
1725 Binder.restoreCallingIdentity(ident);
1726 }
1727 }
1728
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001729 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001730 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001731 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001732 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001733 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001734 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001735 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1736 // When physical keyboard is attached, we show the ime switcher (or notification if
1737 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1738 // exists in the IME switcher dialog. Might be OK to remove this condition once
1739 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1740 return true;
1741 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001742 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1743 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001744 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001745
1746 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1747 final int N = imis.size();
1748 if (N > 2) return true;
1749 if (N < 1) return false;
1750 int nonAuxCount = 0;
1751 int auxCount = 0;
1752 InputMethodSubtype nonAuxSubtype = null;
1753 InputMethodSubtype auxSubtype = null;
1754 for(int i = 0; i < N; ++i) {
1755 final InputMethodInfo imi = imis.get(i);
1756 final List<InputMethodSubtype> subtypes =
1757 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1758 final int subtypeCount = subtypes.size();
1759 if (subtypeCount == 0) {
1760 ++nonAuxCount;
1761 } else {
1762 for (int j = 0; j < subtypeCount; ++j) {
1763 final InputMethodSubtype subtype = subtypes.get(j);
1764 if (!subtype.isAuxiliary()) {
1765 ++nonAuxCount;
1766 nonAuxSubtype = subtype;
1767 } else {
1768 ++auxCount;
1769 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001770 }
1771 }
satok7cfc0ed2011-06-20 21:29:36 +09001772 }
1773 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001774 if (nonAuxCount > 1 || auxCount > 1) {
1775 return true;
1776 } else if (nonAuxCount == 1 && auxCount == 1) {
1777 if (nonAuxSubtype != null && auxSubtype != null
1778 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1779 || auxSubtype.overridesImplicitlyEnabledSubtype()
1780 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1781 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1782 return false;
1783 }
1784 return true;
1785 }
1786 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001787 }
1788
John Spurlocke0980502013-10-25 11:59:29 -04001789 private boolean isKeyguardLocked() {
1790 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1791 }
1792
satokdbf29502011-08-25 15:28:23 +09001793 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001794 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001795 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001796 if (!calledWithValidToken(token)) {
1797 final int uid = Binder.getCallingUid();
1798 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1799 + " token:" + token);
1800 return;
1801 }
1802
1803 synchronized (mMethodMap) {
1804 mImeWindowVis = vis;
1805 mBackDisposition = backDisposition;
1806 updateSystemUiLocked(token, vis, backDisposition);
1807 }
1808 }
1809
1810 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1811 synchronized (mMethodMap) {
1812 updateSystemUiLocked(token, vis, backDisposition);
1813 }
1814 }
1815
1816 // Caution! This method is called in this class. Handle multi-user carefully
1817 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1818 if (!calledWithValidToken(token)) {
1819 final int uid = Binder.getCallingUid();
1820 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1821 + " token:" + token);
1822 return;
1823 }
1824
1825 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1826 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001827 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001828 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001829 // apply policy for binder calls
1830 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1831 vis = 0;
satok06487a52010-10-29 11:37:18 +09001832 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001833 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1834 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1835 if (mStatusBar != null) {
1836 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1837 needsToShowImeSwitcher);
1838 }
1839 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1840 if (imi != null && needsToShowImeSwitcher) {
1841 // Used to load label
1842 final CharSequence title = mRes.getText(
1843 com.android.internal.R.string.select_input_method);
1844 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1845 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001846 mImeSwitcherNotification.setContentTitle(title)
1847 .setContentText(summary)
1848 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001849 try {
1850 if ((mNotificationManager != null)
1851 && !mIWindowManager.hasNavigationBar()) {
1852 if (DEBUG) {
1853 Slog.d(TAG, "--- show notification: label = " + summary);
1854 }
1855 mNotificationManager.notifyAsUser(null,
1856 com.android.internal.R.string.select_input_method,
1857 mImeSwitcherNotification.build(), UserHandle.ALL);
1858 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001859 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001860 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001861 }
1862 } else {
1863 if (mNotificationShown && mNotificationManager != null) {
1864 if (DEBUG) {
1865 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001866 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001867 mNotificationManager.cancelAsUser(null,
1868 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1869 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001870 }
satok06487a52010-10-29 11:37:18 +09001871 }
1872 } finally {
1873 Binder.restoreCallingIdentity(ident);
1874 }
1875 }
1876
satoke7c6998e2011-06-03 17:57:59 +09001877 @Override
satokf9f01002011-05-19 21:31:50 +09001878 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001879 if (!calledFromValidUser()) {
1880 return;
1881 }
satokf9f01002011-05-19 21:31:50 +09001882 synchronized (mMethodMap) {
1883 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1884 for (int i = 0; i < spans.length; ++i) {
1885 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001886 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001887 mSecureSuggestionSpans.put(ss, currentImi);
1888 }
1889 }
1890 }
1891 }
1892
satoke7c6998e2011-06-03 17:57:59 +09001893 @Override
satokf9f01002011-05-19 21:31:50 +09001894 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001895 if (!calledFromValidUser()) {
1896 return false;
1897 }
satokf9f01002011-05-19 21:31:50 +09001898 synchronized (mMethodMap) {
1899 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1900 // TODO: Do not send the intent if the process of the targetImi is already dead.
1901 if (targetImi != null) {
1902 final String[] suggestions = span.getSuggestions();
1903 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001904 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001905 final Intent intent = new Intent();
1906 // Ensures that only a class in the original IME package will receive the
1907 // notification.
satok42c5a162011-05-26 16:46:14 +09001908 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001909 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1910 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1911 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1912 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001913 final long ident = Binder.clearCallingIdentity();
1914 try {
1915 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1916 } finally {
1917 Binder.restoreCallingIdentity(ident);
1918 }
satokf9f01002011-05-19 21:31:50 +09001919 return true;
1920 }
1921 }
1922 return false;
1923 }
1924
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001925 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001926 updateInputMethodsFromSettingsLocked(enabledMayChange);
1927 updateKeyboardFromSettingsLocked();
1928 }
1929
1930 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001931 if (enabledMayChange) {
1932 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1933 for (int i=0; i<enabled.size(); i++) {
1934 // We allow the user to select "disabled until used" apps, so if they
1935 // are enabling one of those here we now need to make it enabled.
1936 InputMethodInfo imm = enabled.get(i);
1937 try {
1938 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1939 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1940 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001941 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001942 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001943 if (DEBUG) {
1944 Slog.d(TAG, "Update state(" + imm.getId()
1945 + "): DISABLED_UNTIL_USED -> DEFAULT");
1946 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001947 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1948 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001949 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1950 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001951 }
1952 } catch (RemoteException e) {
1953 }
1954 }
1955 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001956 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1957 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1958 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1959 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001960 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001961 // There is no input method selected, try to choose new applicable input method.
1962 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001963 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001964 }
1965 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001967 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001969 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001970 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
satokf3db1af2010-11-23 13:34:33 +09001972 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001973 } else {
1974 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001975 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001977 // Here is not the perfect place to reset the switching controller. Ideally
1978 // mSwitchingController and mSettings should be able to share the same state.
1979 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1980 // the same enabled IMEs list.
1981 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001982
1983 }
1984
1985 public void updateKeyboardFromSettingsLocked() {
1986 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1987 if (mSwitchingDialog != null
1988 && mSwitchingDialogTitleView != null
1989 && mSwitchingDialog.isShowing()) {
1990 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1991 com.android.internal.R.id.hard_keyboard_switch);
1992 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001995
Yohei Yukawab097b822015-12-01 10:43:08 -08001996 private void notifyInputMethodSubtypeChanged(final int userId,
1997 @Nullable final InputMethodInfo inputMethodInfo,
1998 @Nullable final InputMethodSubtype subtype) {
1999 final InputManagerInternal inputManagerInternal =
2000 LocalServices.getService(InputManagerInternal.class);
2001 if (inputManagerInternal != null) {
2002 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2003 }
2004 }
2005
satokab751aa2010-09-14 19:17:36 +09002006 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 InputMethodInfo info = mMethodMap.get(id);
2008 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002009 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002011
satokd81e9502012-05-21 12:58:45 +09002012 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002014 final int subtypeCount = info.getSubtypeCount();
2015 if (subtypeCount <= 0) {
2016 return;
satokcd7cd292010-11-20 15:46:23 +09002017 }
satokd81e9502012-05-21 12:58:45 +09002018 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2019 final InputMethodSubtype newSubtype;
2020 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2021 newSubtype = info.getSubtypeAt(subtypeId);
2022 } else {
2023 // If subtype is null, try to find the most applicable one from
2024 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002025 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002026 }
2027 if (newSubtype == null || oldSubtype == null) {
2028 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2029 + ", new subtype = " + newSubtype);
2030 return;
2031 }
2032 if (newSubtype != oldSubtype) {
2033 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2034 if (mCurMethod != null) {
2035 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002036 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002037 mCurMethod.changeInputMethodSubtype(newSubtype);
2038 } catch (RemoteException e) {
2039 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002040 return;
satokab751aa2010-09-14 19:17:36 +09002041 }
2042 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002043 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 return;
2046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002047
satokd81e9502012-05-21 12:58:45 +09002048 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 final long ident = Binder.clearCallingIdentity();
2050 try {
satokab751aa2010-09-14 19:17:36 +09002051 // Set a subtype to this input method.
2052 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002053 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2054 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2055 // because mCurMethodId is stored as a history in
2056 // setSelectedInputMethodAndSubtypeLocked().
2057 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002059 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002061 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002063 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002065 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 } finally {
2067 Binder.restoreCallingIdentity(ident);
2068 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002069
2070 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2071 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002073
satok42c5a162011-05-26 16:46:14 +09002074 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002075 public boolean showSoftInput(IInputMethodClient client, int flags,
2076 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002077 if (!calledFromValidUser()) {
2078 return false;
2079 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002080 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 long ident = Binder.clearCallingIdentity();
2082 try {
2083 synchronized (mMethodMap) {
2084 if (mCurClient == null || client == null
2085 || mCurClient.client.asBinder() != client.asBinder()) {
2086 try {
2087 // We need to check if this is the current client with
2088 // focus in the window manager, to allow this call to
2089 // be made before input is started in it.
2090 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002091 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002092 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002095 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002099 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002100 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
2102 } finally {
2103 Binder.restoreCallingIdentity(ident);
2104 }
2105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002106
The Android Open Source Project4df24232009-03-05 14:34:35 -08002107 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002109 if (mAccessibilityRequestingNoSoftKeyboard) {
2110 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
Anna Galusza9b278112016-01-04 11:37:37 -08002112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2114 mShowExplicitlyRequested = true;
2115 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002116 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2117 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002119
Dianne Hackborncc278702009-09-02 23:07:23 -07002120 if (!mSystemReady) {
2121 return false;
2122 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002123
The Android Open Source Project4df24232009-03-05 14:34:35 -08002124 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002126 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002127 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2128 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2129 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002131 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002132 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002133 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002134 | Context.BIND_TREAT_LIKE_ACTIVITY
2135 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002136 mVisibleBound = true;
2137 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002138 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002140 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 // The client has asked to have the input method shown, but
2142 // we have been sitting here too long with a connection to the
2143 // service and no interface received, so let's disconnect/connect
2144 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002145 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002147 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002149 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002150 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002151 } else {
2152 if (DEBUG) {
2153 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2154 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002157
The Android Open Source Project4df24232009-03-05 14:34:35 -08002158 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002160
satok42c5a162011-05-26 16:46:14 +09002161 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002162 public boolean hideSoftInput(IInputMethodClient client, int flags,
2163 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002164 if (!calledFromValidUser()) {
2165 return false;
2166 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002167 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 long ident = Binder.clearCallingIdentity();
2169 try {
2170 synchronized (mMethodMap) {
2171 if (mCurClient == null || client == null
2172 || mCurClient.client.asBinder() != client.asBinder()) {
2173 try {
2174 // We need to check if this is the current client with
2175 // focus in the window manager, to allow this call to
2176 // be made before input is started in it.
2177 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002178 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2179 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002180 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
2182 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002183 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 }
2185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002186
Joe Onorato8a9b2202010-02-26 18:56:32 -08002187 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002188 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
2190 } finally {
2191 Binder.restoreCallingIdentity(ident);
2192 }
2193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002194
The Android Open Source Project4df24232009-03-05 14:34:35 -08002195 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2197 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002198 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 -08002199 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
2201 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002202 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 -08002203 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002205
2206 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2207 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2208 // to be updated with the new value sent from IME process. Even in such a transient state
2209 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2210 // application process as a valid request, and have even promised such a behavior with CTS
2211 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2212 // IMMS#InputShown indicates that the software keyboard is shown.
2213 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2214 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2215 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002216 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002217 if (shouldHideSoftInput) {
2218 // The IME will report its visible state again after the following message finally
2219 // delivered to the IME process as an IPC. Hence the inconsistency between
2220 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2221 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2223 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2224 res = true;
2225 } else {
2226 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002228 if (mHaveConnection && mVisibleBound) {
2229 mContext.unbindService(mVisibleConnection);
2230 mVisibleBound = false;
2231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 mInputShown = false;
2233 mShowRequested = false;
2234 mShowExplicitlyRequested = false;
2235 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002236 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002238
satok42c5a162011-05-26 16:46:14 +09002239 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002240 public InputBindResult startInputOrWindowGainedFocus(
2241 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2242 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002243 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002244 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002245 if (windowToken != null) {
2246 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002247 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002248 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002249 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2250 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002251 }
2252 }
2253
2254 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002255 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2256 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002257 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2258 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002259 // Needs to check the validity before clearing calling identity
2260 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002261 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 long ident = Binder.clearCallingIdentity();
2263 try {
2264 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002265 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2266 + InputMethodClient.getStartInputReason(startInputReason)
2267 + " client=" + client.asBinder()
2268 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002269 + " missingMethods="
2270 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002271 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002272 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002274 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002275
Dianne Hackborn7663d802012-02-24 13:08:49 -08002276 ClientState cs = mClients.get(client.asBinder());
2277 if (cs == null) {
2278 throw new IllegalArgumentException("unknown client "
2279 + client.asBinder());
2280 }
2281
2282 try {
2283 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2284 // Check with the window manager to make sure this client actually
2285 // has a window with focus. If not, reject. This is thread safe
2286 // because if the focus changes some time before or after, the
2287 // next client receiving focus that has any interest in input will
2288 // be calling through here after that change happens.
2289 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2290 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2291 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002293 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002295
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002296 if (!calledFromValidUser) {
2297 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2298 Slog.w(TAG, "If you want to interect with IME, you need "
2299 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2300 hideCurrentInputLocked(0, null);
2301 return null;
2302 }
2303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002304 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002305 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002306 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002307 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002308 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2309 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002310 }
2311 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002312 }
2313 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002314 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002315
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002316 // Should we auto-show the IME even if the caller has not
2317 // specified what should be done with it?
2318 // We only do this automatically if the window can resize
2319 // to accommodate the IME (so what the user sees will give
2320 // them good context without input information being obscured
2321 // by the IME) or if running on a large screen where there
2322 // is more room for the target window + IME.
2323 final boolean doAutoShow =
2324 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2325 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2326 || mRes.getConfiguration().isLayoutSizeAtLeast(
2327 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002328 final boolean isTextEditor =
2329 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2330
2331 // We want to start input before showing the IME, but after closing
2332 // it. We want to do this after closing it to help the IME disappear
2333 // more quickly (not get stuck behind it initializing itself for the
2334 // new focused input, even if its window wants to hide the IME).
2335 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2338 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002339 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2341 // There is no focus view, and this window will
2342 // be behind any soft input window, so hide the
2343 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002344 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002345 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002347 } else if (isTextEditor && doAutoShow && (softInputMode &
2348 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 // There is a focus view, and we are navigating forward
2350 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002351 // We only do this automatically if the window can resize
2352 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002353 // them good context without input information being obscured
2354 // by the IME) or if running on a large screen where there
2355 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002356 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002357 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002358 res = startInputUncheckedLocked(cs, inputContext,
2359 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002360 didStart = true;
2361 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002362 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
2364 break;
2365 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2366 // Do nothing.
2367 break;
2368 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2369 if ((softInputMode &
2370 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002371 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002372 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 }
2374 break;
2375 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002376 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002377 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 break;
2379 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2380 if ((softInputMode &
2381 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002382 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002383 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002384 res = startInputUncheckedLocked(cs, inputContext,
2385 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002386 didStart = true;
2387 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002388 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390 break;
2391 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002392 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002393 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002394 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2395 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002396 didStart = true;
2397 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002398 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 break;
2400 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002401
2402 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002403 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002404 controlFlags);
2405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
2407 } finally {
2408 Binder.restoreCallingIdentity(ident);
2409 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002410
2411 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002413
satok42c5a162011-05-26 16:46:14 +09002414 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002415 public void showInputMethodPickerFromClient(
2416 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002417 if (!calledFromValidUser()) {
2418 return;
2419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 synchronized (mMethodMap) {
2421 if (mCurClient == null || client == null
2422 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002423 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002424 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
2426
satok440aab52010-11-25 09:43:11 +09002427 // Always call subtype picker, because subtype picker is a superset of input method
2428 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002429 mHandler.sendMessage(mCaller.obtainMessageI(
2430 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002431 }
2432 }
2433
satok42c5a162011-05-26 16:46:14 +09002434 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002436 if (!calledFromValidUser()) {
2437 return;
2438 }
satok28203512010-11-24 11:06:49 +09002439 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2440 }
2441
satok42c5a162011-05-26 16:46:14 +09002442 @Override
satok28203512010-11-24 11:06:49 +09002443 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002444 if (!calledFromValidUser()) {
2445 return;
2446 }
satok28203512010-11-24 11:06:49 +09002447 synchronized (mMethodMap) {
2448 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002449 setInputMethodWithSubtypeIdLocked(token, id,
2450 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2451 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002452 } else {
2453 setInputMethod(token, id);
2454 }
2455 }
satokab751aa2010-09-14 19:17:36 +09002456 }
2457
satok42c5a162011-05-26 16:46:14 +09002458 @Override
satokb416a712010-11-25 20:42:14 +09002459 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002460 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002461 if (!calledFromValidUser()) {
2462 return;
2463 }
satokb416a712010-11-25 20:42:14 +09002464 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002465 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2466 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002467 }
2468 }
2469
satok4fc87d62011-05-20 16:13:43 +09002470 @Override
satok735cf382010-11-11 20:40:09 +09002471 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002472 if (!calledFromValidUser()) {
2473 return false;
2474 }
satok735cf382010-11-11 20:40:09 +09002475 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002476 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002477 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002478 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002479 lastImi = mMethodMap.get(lastIme.first);
2480 } else {
2481 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002482 }
satok4fc87d62011-05-20 16:13:43 +09002483 String targetLastImiId = null;
2484 int subtypeId = NOT_A_SUBTYPE_ID;
2485 if (lastIme != null && lastImi != null) {
2486 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002487 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002488 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2489 : mCurrentSubtype.hashCode();
2490 // If the last IME is the same as the current IME and the last subtype is not
2491 // defined, there is no need to switch to the last IME.
2492 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2493 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002494 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002495 }
2496 }
2497
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002498 if (TextUtils.isEmpty(targetLastImiId)
2499 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002500 // This is a safety net. If the currentSubtype can't be added to the history
2501 // and the framework couldn't find the last ime, we will make the last ime be
2502 // the most applicable enabled keyboard subtype of the system imes.
2503 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2504 if (enabled != null) {
2505 final int N = enabled.size();
2506 final String locale = mCurrentSubtype == null
2507 ? mRes.getConfiguration().locale.toString()
2508 : mCurrentSubtype.getLocale();
2509 for (int i = 0; i < N; ++i) {
2510 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002511 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002512 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002513 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2514 InputMethodUtils.getSubtypes(imi),
2515 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002516 if (keyboardSubtype != null) {
2517 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002518 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002519 imi, keyboardSubtype.hashCode());
2520 if(keyboardSubtype.getLocale().equals(locale)) {
2521 break;
2522 }
2523 }
2524 }
2525 }
2526 }
2527 }
2528
2529 if (!TextUtils.isEmpty(targetLastImiId)) {
2530 if (DEBUG) {
2531 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2532 + ", from: " + mCurMethodId + ", " + subtypeId);
2533 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002534 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002535 return true;
2536 } else {
2537 return false;
2538 }
satok735cf382010-11-11 20:40:09 +09002539 }
2540 }
2541
satoke7c6998e2011-06-03 17:57:59 +09002542 @Override
satok688bd472012-02-09 20:09:17 +09002543 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002544 if (!calledFromValidUser()) {
2545 return false;
2546 }
satok688bd472012-02-09 20:09:17 +09002547 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002548 if (!calledWithValidToken(token)) {
2549 final int uid = Binder.getCallingUid();
2550 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2551 + " token:" + token);
2552 return false;
2553 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002554 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002555 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2556 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002557 if (nextSubtype == null) {
2558 return false;
2559 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002560 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2561 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002562 return true;
2563 }
2564 }
2565
2566 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002567 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2568 if (!calledFromValidUser()) {
2569 return false;
2570 }
2571 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002572 if (!calledWithValidToken(token)) {
2573 final int uid = Binder.getCallingUid();
2574 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2575 + "token. uid:" + uid + " token:" + token);
2576 return false;
2577 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002578 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002579 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2580 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002581 if (nextSubtype == null) {
2582 return false;
2583 }
2584 return true;
2585 }
2586 }
2587
2588 @Override
satok68f1b782011-04-11 14:26:04 +09002589 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002590 if (!calledFromValidUser()) {
2591 return null;
2592 }
satok68f1b782011-04-11 14:26:04 +09002593 synchronized (mMethodMap) {
2594 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2595 // TODO: Handle the case of the last IME with no subtypes
2596 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2597 || TextUtils.isEmpty(lastIme.second)) return null;
2598 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2599 if (lastImi == null) return null;
2600 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002601 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002602 final int lastSubtypeId =
2603 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002604 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2605 return null;
2606 }
2607 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002608 } catch (NumberFormatException e) {
2609 return null;
2610 }
2611 }
2612 }
2613
satoke7c6998e2011-06-03 17:57:59 +09002614 @Override
satokee5e77c2011-09-02 18:50:15 +09002615 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002616 if (!calledFromValidUser()) {
2617 return;
2618 }
satok91e88122011-07-18 11:11:42 +09002619 // By this IPC call, only a process which shares the same uid with the IME can add
2620 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002621 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002622 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002623 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002624 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002625 final String[] packageInfos;
2626 try {
2627 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2628 } catch (RemoteException e) {
2629 Slog.e(TAG, "Failed to get package infos");
2630 return;
2631 }
satok91e88122011-07-18 11:11:42 +09002632 if (packageInfos != null) {
2633 final int packageNum = packageInfos.length;
2634 for (int i = 0; i < packageNum; ++i) {
2635 if (packageInfos[i].equals(imi.getPackageName())) {
2636 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002637 final long ident = Binder.clearCallingIdentity();
2638 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002639 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002640 } finally {
2641 Binder.restoreCallingIdentity(ident);
2642 }
satokee5e77c2011-09-02 18:50:15 +09002643 return;
satok91e88122011-07-18 11:11:42 +09002644 }
2645 }
2646 }
satoke7c6998e2011-06-03 17:57:59 +09002647 }
satokee5e77c2011-09-02 18:50:15 +09002648 return;
satoke7c6998e2011-06-03 17:57:59 +09002649 }
2650
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002651 @Override
2652 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002653 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002654 }
2655
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002656 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002657 public void clearLastInputMethodWindowForTransition(IBinder token) {
2658 if (!calledFromValidUser()) {
2659 return;
2660 }
2661 final long ident = Binder.clearCallingIdentity();
2662 try {
2663 synchronized (mMethodMap) {
2664 if (!calledWithValidToken(token)) {
2665 final int uid = Binder.getCallingUid();
2666 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2667 + "invalid token. uid:" + uid + " token:" + token);
2668 return;
2669 }
2670 }
2671 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2672 } finally {
2673 Binder.restoreCallingIdentity(ident);
2674 }
2675 }
2676
2677 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002678 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002679 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002680 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002681 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002682 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002683 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2684 if (DEBUG) {
2685 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2686 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2687 + " actual: " + sequenceNumber);
2688 }
2689 return;
2690 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002691 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2692 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002693 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002694 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002695 }
2696 }
2697
satok28203512010-11-24 11:06:49 +09002698 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002700 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2701 }
2702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002704 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2705 if (token == null) {
2706 if (mContext.checkCallingOrSelfPermission(
2707 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2708 != PackageManager.PERMISSION_GRANTED) {
2709 throw new SecurityException(
2710 "Using null token requires permission "
2711 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002713 } else if (mCurToken != token) {
2714 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2715 + " token: " + token);
2716 return;
2717 }
2718
2719 final long ident = Binder.clearCallingIdentity();
2720 try {
2721 setInputMethodLocked(id, subtypeId);
2722 } finally {
2723 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
2725 }
2726
satok42c5a162011-05-26 16:46:14 +09002727 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002729 if (!calledFromValidUser()) {
2730 return;
2731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002733 if (!calledWithValidToken(token)) {
2734 final int uid = Binder.getCallingUid();
2735 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2736 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 return;
2738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 long ident = Binder.clearCallingIdentity();
2740 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002741 hideCurrentInputLocked(flags, null);
2742 } finally {
2743 Binder.restoreCallingIdentity(ident);
2744 }
2745 }
2746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002747
satok42c5a162011-05-26 16:46:14 +09002748 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002749 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002750 if (!calledFromValidUser()) {
2751 return;
2752 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002753 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002754 if (!calledWithValidToken(token)) {
2755 final int uid = Binder.getCallingUid();
2756 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2757 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002758 return;
2759 }
2760 long ident = Binder.clearCallingIdentity();
2761 try {
2762 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 } finally {
2764 Binder.restoreCallingIdentity(ident);
2765 }
2766 }
2767 }
2768
2769 void setEnabledSessionInMainThread(SessionState session) {
2770 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002771 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002773 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002774 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 } catch (RemoteException e) {
2776 }
2777 }
2778 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002779 if (mEnabledSession != null && mEnabledSession.session != null) {
2780 try {
2781 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2782 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2783 } catch (RemoteException e) {
2784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 }
2786 }
2787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002788
satok42c5a162011-05-26 16:46:14 +09002789 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002791 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002793 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002794 final boolean showAuxSubtypes;
2795 switch (msg.arg1) {
2796 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2797 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2798 // implemented so that auxiliary subtypes will be excluded when the soft
2799 // keyboard is invisible.
2800 showAuxSubtypes = mInputShown;
2801 break;
2802 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2803 showAuxSubtypes = true;
2804 break;
2805 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2806 showAuxSubtypes = false;
2807 break;
2808 default:
2809 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2810 return false;
2811 }
2812 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002813 return true;
2814
satok47a44912010-10-06 16:03:58 +09002815 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002816 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002817 return true;
2818
2819 case MSG_SHOW_IM_CONFIG:
2820 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002821 return true;
2822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 case MSG_UNBIND_INPUT:
2826 try {
2827 ((IInputMethod)msg.obj).unbindInput();
2828 } catch (RemoteException e) {
2829 // There is nothing interesting about the method dying.
2830 }
2831 return true;
2832 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002833 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 try {
2835 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2836 } catch (RemoteException e) {
2837 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002838 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 return true;
2840 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002841 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002843 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002844 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002845 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 } catch (RemoteException e) {
2847 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002848 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 return true;
2850 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002851 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002853 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002854 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002855 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 } catch (RemoteException e) {
2857 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002858 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002860 case MSG_HIDE_CURRENT_INPUT_METHOD:
2861 synchronized (mMethodMap) {
2862 hideCurrentInputLocked(0, null);
2863 }
2864 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002866 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002868 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2870 } catch (RemoteException e) {
2871 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002872 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002874 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002875 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002876 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002877 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002879 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002881 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002882 // Dispose the channel if the input method is not local to this process
2883 // because the remote proxy will get its own copy when unparceled.
2884 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002885 channel.dispose();
2886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002888 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002892
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002893 case MSG_START_INPUT: {
2894 int missingMethods = msg.arg1;
2895 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002897 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002899 session.method.startInput((IInputContext) args.arg2, missingMethods,
2900 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 } catch (RemoteException e) {
2902 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002903 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002905 }
2906 case MSG_RESTART_INPUT: {
2907 int missingMethods = msg.arg1;
2908 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002910 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002912 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2913 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 } catch (RemoteException e) {
2915 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002916 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002921
Yohei Yukawa33e81792015-11-17 21:14:42 -08002922 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002924 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 } catch (RemoteException e) {
2926 // There is nothing interesting about the last client dying.
2927 }
2928 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002929 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002930 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002931 IInputMethodClient client = (IInputMethodClient)args.arg1;
2932 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002934 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002936 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002937 } finally {
2938 // Dispose the channel if the input method is not local to this process
2939 // because the remote proxy will get its own copy when unparceled.
2940 if (res.channel != null && Binder.isProxy(client)) {
2941 res.channel.dispose();
2942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002944 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002946 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002947 case MSG_SET_ACTIVE:
2948 try {
2949 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2950 } catch (RemoteException e) {
2951 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2952 + ((ClientState)msg.obj).pid + " uid "
2953 + ((ClientState)msg.obj).uid);
2954 }
2955 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002956 case MSG_SET_INTERACTIVE:
2957 handleSetInteractive(msg.arg1 != 0);
2958 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002959 case MSG_SWITCH_IME:
2960 handleSwitchInputMethod(msg.arg1 != 0);
2961 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002962 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2963 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002964 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002965 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002966 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002967 } catch (RemoteException e) {
2968 Slog.w(TAG, "Got RemoteException sending "
2969 + "setUserActionNotificationSequenceNumber("
2970 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002971 + clientState.pid + " uid "
2972 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002973 }
2974 return true;
2975 }
satok01038492012-04-09 21:08:27 +09002976
2977 // --------------------------------------------------------------
2978 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002979 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002980 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07002981 case MSG_SYSTEM_UNLOCK_USER:
2982 final int userId = msg.arg1;
2983 onUnlockUser(userId);
2984 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 }
2986 return false;
2987 }
2988
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002989 private void handleSetInteractive(final boolean interactive) {
2990 synchronized (mMethodMap) {
2991 mIsInteractive = interactive;
2992 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2993
2994 // Inform the current client of the change in active status
2995 if (mCurClient != null && mCurClient.client != null) {
2996 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2997 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2998 }
2999 }
3000 }
3001
Yohei Yukawaae61f712015-12-09 13:00:10 -08003002 private void handleSwitchInputMethod(final boolean forwardDirection) {
3003 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003004 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003005 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003006 if (nextSubtype == null) {
3007 return;
3008 }
3009 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003010 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3011 if (newInputMethodInfo == null) {
3012 return;
3013 }
3014 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3015 newInputMethodInfo, mCurrentSubtype);
3016 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003017 if (mSubtypeSwitchedByShortCutToast == null) {
3018 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3019 Toast.LENGTH_SHORT);
3020 } else {
3021 mSubtypeSwitchedByShortCutToast.setText(toastText);
3022 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003023 mSubtypeSwitchedByShortCutToast.show();
3024 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003025 }
3026 }
3027
satokdc9ddae2011-10-06 12:22:36 +09003028 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003029 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3030 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003031 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003032 if (DEBUG) {
3033 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3034 }
satok723a27e2010-11-11 14:58:11 +09003035 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003036 return true;
3037 }
3038
3039 return false;
3040 }
3041
Yohei Yukawa94e33302016-02-12 19:37:03 -08003042 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003043 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003044 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003045 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003046 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003047 mMethodList.clear();
3048 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003049
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003050 // Use for queryIntentServicesAsUser
3051 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003053 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3054 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3055 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003056 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003058 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3059 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003060
satoke7c6998e2011-06-03 17:57:59 +09003061 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3062 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 for (int i = 0; i < services.size(); ++i) {
3064 ResolveInfo ri = services.get(i);
3065 ServiceInfo si = ri.serviceInfo;
3066 ComponentName compName = new ComponentName(si.packageName, si.name);
3067 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3068 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003069 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 + ": it does not require the permission "
3071 + android.Manifest.permission.BIND_INPUT_METHOD);
3072 continue;
3073 }
3074
Joe Onorato8a9b2202010-02-26 18:56:32 -08003075 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076
3077 try {
satoke7c6998e2011-06-03 17:57:59 +09003078 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003079 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003080 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003081 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082
3083 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003084 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003086 } catch (Exception e) {
3087 Slog.wtf(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 }
3089 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003090
Yohei Yukawa859df052016-02-17 07:56:46 -08003091 // TODO: The following code should find better place to live.
3092 if (!resetDefaultEnabledIme) {
3093 boolean enabledImeFound = false;
3094 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3095 final int N = enabledImes.size();
3096 for (int i = 0; i < N; ++i) {
3097 final InputMethodInfo imi = enabledImes.get(i);
3098 if (mMethodList.contains(imi)) {
3099 enabledImeFound = true;
3100 break;
3101 }
3102 }
3103 if (!enabledImeFound) {
3104 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3105 resetDefaultEnabledIme = true;
3106 resetSelectedInputMethodAndSubtypeLocked("");
3107 }
3108 }
3109
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003110 if (resetDefaultEnabledIme) {
3111 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003112 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003113 final int N = defaultEnabledIme.size();
3114 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003115 final InputMethodInfo imi = defaultEnabledIme.get(i);
3116 if (DEBUG) {
3117 Slog.d(TAG, "--- enable ime = " + imi);
3118 }
3119 setInputMethodEnabledLocked(imi.getId(), true);
3120 }
3121 }
3122
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003123 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003124 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003125 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003126 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3127 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003128 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003129 }
3130 } else {
3131 // Double check that the default IME is certainly enabled.
3132 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003133 }
3134 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003135 // Here is not the perfect place to reset the switching controller. Ideally
3136 // mSwitchingController and mSettings should be able to share the same state.
3137 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3138 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003139 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003143
satok217f5482010-12-15 05:19:19 +09003144 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003145 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003146 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003147 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3148 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003149 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003150 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003151 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003152 final int userId;
3153 synchronized (mMethodMap) {
3154 userId = mSettings.getCurrentUserId();
3155 }
3156 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003157 }
3158
3159 private void showConfigureInputMethods() {
3160 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3161 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3162 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3163 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003164 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003165 }
3166
satok2c93efc2012-04-02 19:33:47 +09003167 private boolean isScreenLocked() {
3168 return mKeyguardManager != null
3169 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3170 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003171
Seigo Nonaka14e13912015-05-06 21:04:13 -07003172 private void showInputMethodMenu(boolean showAuxSubtypes) {
3173 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003176 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003177
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003178 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003179 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003180 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003181
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003182 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003183 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003184 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3185 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003186 if (immis == null || immis.size() == 0) {
3187 return;
3188 }
3189
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003190 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191
satok688bd472012-02-09 20:09:17 +09003192 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003193 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003194 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003195
satokc3690562012-01-10 20:14:43 +09003196 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003197 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003198 if (currentSubtype != null) {
3199 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003200 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3201 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003202 }
3203 }
3204
Ken Wakasa761eb372011-03-04 19:06:18 +09003205 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003206 mIms = new InputMethodInfo[N];
3207 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003208 int checkedItem = 0;
3209 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003210 final ImeSubtypeListItem item = imList.get(i);
3211 mIms[i] = item.mImi;
3212 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003213 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003214 int subtypeId = mSubtypeIds[i];
3215 if ((subtypeId == NOT_A_SUBTYPE_ID)
3216 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3217 || (subtypeId == lastInputMethodSubtypeId)) {
3218 checkedItem = i;
3219 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003222
3223 final Context settingsContext = new ContextThemeWrapper(context,
3224 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3225
3226 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003227 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3228 @Override
3229 public void onCancel(DialogInterface dialog) {
3230 hideInputMethodMenu();
3231 }
3232 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003233
3234 final Context dialogContext = mDialogBuilder.getContext();
3235 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3236 com.android.internal.R.styleable.DialogPreference,
3237 com.android.internal.R.attr.alertDialogStyle, 0);
3238 final Drawable dialogIcon = a.getDrawable(
3239 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3240 a.recycle();
3241
3242 mDialogBuilder.setIcon(dialogIcon);
3243
Yohei Yukawad34e1482016-02-11 08:03:52 -08003244 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003245 final View tv = inflater.inflate(
3246 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3247 mDialogBuilder.setCustomTitle(tv);
3248
3249 // Setup layout for a toggle switch of the hardware keyboard
3250 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003251 mSwitchingDialogTitleView
3252 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003253 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003254 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003255 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003256 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003257 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003258 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3259 @Override
3260 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003261 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003262 // Ensure that the input method dialog is dismissed when changing
3263 // the hardware keyboard state.
3264 hideInputMethodMenu();
3265 }
3266 });
3267
Alan Viverette505e3ab2014-11-24 15:22:11 -08003268 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003269 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3270 final OnClickListener choiceListener = new OnClickListener() {
3271 @Override
3272 public void onClick(final DialogInterface dialog, final int which) {
3273 synchronized (mMethodMap) {
3274 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3275 || mSubtypeIds.length <= which) {
3276 return;
satok01038492012-04-09 21:08:27 +09003277 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003278 final InputMethodInfo im = mIms[which];
3279 int subtypeId = mSubtypeIds[which];
3280 adapter.mCheckedItem = which;
3281 adapter.notifyDataSetChanged();
3282 hideInputMethodMenu();
3283 if (im != null) {
3284 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3285 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003286 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003287 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003289 }
3290 }
3291 };
3292 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003295 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003296 final Window w = mSwitchingDialog.getWindow();
3297 final WindowManager.LayoutParams attrs = w.getAttributes();
3298 w.setType(TYPE_INPUT_METHOD_DIALOG);
3299 // Use an alternate token for the dialog for that window manager can group the token
3300 // with other IME windows based on type vs. grouping based on whichever token happens
3301 // to get selected by the system later on.
3302 attrs.token = mSwitchingDialogToken;
3303 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3304 attrs.setTitle("Select input method");
3305 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003306 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 mSwitchingDialog.show();
3308 }
3309 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003310
Ken Wakasa05dbb652011-08-22 15:22:43 +09003311 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3312 private final LayoutInflater mInflater;
3313 private final int mTextViewResourceId;
3314 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003315 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003316 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3317 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3318 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003319
Ken Wakasa05dbb652011-08-22 15:22:43 +09003320 mTextViewResourceId = textViewResourceId;
3321 mItemsList = itemsList;
3322 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003323 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003324 }
3325
3326 @Override
3327 public View getView(int position, View convertView, ViewGroup parent) {
3328 final View view = convertView != null ? convertView
3329 : mInflater.inflate(mTextViewResourceId, null);
3330 if (position < 0 || position >= mItemsList.size()) return view;
3331 final ImeSubtypeListItem item = mItemsList.get(position);
3332 final CharSequence imeName = item.mImeName;
3333 final CharSequence subtypeName = item.mSubtypeName;
3334 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3335 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3336 if (TextUtils.isEmpty(subtypeName)) {
3337 firstTextView.setText(imeName);
3338 secondTextView.setVisibility(View.GONE);
3339 } else {
3340 firstTextView.setText(subtypeName);
3341 secondTextView.setText(imeName);
3342 secondTextView.setVisibility(View.VISIBLE);
3343 }
3344 final RadioButton radioButton =
3345 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3346 radioButton.setChecked(position == mCheckedItem);
3347 return view;
3348 }
3349 }
3350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003352 synchronized (mMethodMap) {
3353 hideInputMethodMenuLocked();
3354 }
3355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003356
The Android Open Source Project10592532009-03-18 17:39:46 -07003357 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003358 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359
The Android Open Source Project10592532009-03-18 17:39:46 -07003360 if (mSwitchingDialog != null) {
3361 mSwitchingDialog.dismiss();
3362 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003364
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003365 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003366 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003367 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003371
satok42c5a162011-05-26 16:46:14 +09003372 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003374 // TODO: Make this work even for non-current users?
3375 if (!calledFromValidUser()) {
3376 return false;
3377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 synchronized (mMethodMap) {
3379 if (mContext.checkCallingOrSelfPermission(
3380 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3381 != PackageManager.PERMISSION_GRANTED) {
3382 throw new SecurityException(
3383 "Requires permission "
3384 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3385 }
Anna Galusza9b278112016-01-04 11:37:37 -08003386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 long ident = Binder.clearCallingIdentity();
3388 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003389 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 } finally {
3391 Binder.restoreCallingIdentity(ident);
3392 }
3393 }
3394 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003395
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003396 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3397 // Make sure this is a valid input method.
3398 InputMethodInfo imm = mMethodMap.get(id);
3399 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003400 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003401 }
3402
satokd87c2592010-09-29 11:52:06 +09003403 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3404 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003405
satokd87c2592010-09-29 11:52:06 +09003406 if (enabled) {
3407 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3408 if (pair.first.equals(id)) {
3409 // We are enabling this input method, but it is already enabled.
3410 // Nothing to do. The previous state was enabled.
3411 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003412 }
3413 }
satokd87c2592010-09-29 11:52:06 +09003414 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3415 // Previous state was disabled.
3416 return false;
3417 } else {
3418 StringBuilder builder = new StringBuilder();
3419 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3420 builder, enabledInputMethodsList, id)) {
3421 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003422 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003423 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3424 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3425 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003426 }
3427 // Previous state was enabled.
3428 return true;
3429 } else {
3430 // We are disabling the input method but it is already disabled.
3431 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003432 return false;
3433 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003434 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003435 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003436
satok723a27e2010-11-11 14:58:11 +09003437 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3438 boolean setSubtypeOnly) {
3439 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003440 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003441
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003442 mCurUserActionNotificationSequenceNumber =
3443 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3444 if (DEBUG) {
3445 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3446 + mCurUserActionNotificationSequenceNumber);
3447 }
3448
3449 if (mCurClient != null && mCurClient.client != null) {
3450 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3451 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003452 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003453 }
3454
satok723a27e2010-11-11 14:58:11 +09003455 // Set Subtype here
3456 if (imi == null || subtypeId < 0) {
3457 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003458 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003459 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003460 if (subtypeId < imi.getSubtypeCount()) {
3461 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3462 mSettings.putSelectedSubtype(subtype.hashCode());
3463 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003464 } else {
3465 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003466 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003467 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003468 }
satokab751aa2010-09-14 19:17:36 +09003469 }
satok723a27e2010-11-11 14:58:11 +09003470
Yohei Yukawa68645a62016-02-17 07:54:20 -08003471 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003472 // Set InputMethod here
3473 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3474 }
3475 }
3476
3477 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3478 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3479 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3480 // newDefaultIme is empty when there is no candidate for the selected IME.
3481 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3482 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3483 if (subtypeHashCode != null) {
3484 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003485 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003486 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003487 } catch (NumberFormatException e) {
3488 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3489 }
3490 }
3491 }
3492 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003493 }
3494
satok4e4569d2010-11-19 18:45:53 +09003495 // If there are no selected shortcuts, tries finding the most applicable ones.
3496 private Pair<InputMethodInfo, InputMethodSubtype>
3497 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3498 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3499 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003500 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003501 boolean foundInSystemIME = false;
3502
3503 // Search applicable subtype for each InputMethodInfo
3504 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003505 final String imiId = imi.getId();
3506 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3507 continue;
3508 }
satokcd7cd292010-11-20 15:46:23 +09003509 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003510 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003511 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003512 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003513 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003514 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003515 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003516 }
satokdf31ae62011-01-15 06:19:44 +09003517 // 2. Search by the system locale from enabledSubtypes.
3518 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003519 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003520 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003521 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003522 }
satoka86f5e42011-09-02 17:12:42 +09003523 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003524 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003525 final ArrayList<InputMethodSubtype> subtypesForSearch =
3526 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003527 ? InputMethodUtils.getSubtypes(imi)
3528 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003529 // 4. Search by the current subtype's locale from all subtypes.
3530 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003531 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003532 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003533 }
3534 // 5. Search by the system locale from all subtypes.
3535 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003536 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003537 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003538 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003539 }
satokcd7cd292010-11-20 15:46:23 +09003540 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003541 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003542 // The current input method is the most applicable IME.
3543 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003544 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003545 break;
satok7599a7f2010-12-22 13:45:23 +09003546 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003547 // The system input method is 2nd applicable IME.
3548 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003549 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003550 if ((imi.getServiceInfo().applicationInfo.flags
3551 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3552 foundInSystemIME = true;
3553 }
satok4e4569d2010-11-19 18:45:53 +09003554 }
3555 }
3556 }
3557 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003558 if (mostApplicableIMI != null) {
3559 Slog.w(TAG, "Most applicable shortcut input method was:"
3560 + mostApplicableIMI.getId());
3561 if (mostApplicableSubtype != null) {
3562 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3563 + "," + mostApplicableSubtype.getMode() + ","
3564 + mostApplicableSubtype.getLocale());
3565 }
3566 }
satok4e4569d2010-11-19 18:45:53 +09003567 }
satokcd7cd292010-11-20 15:46:23 +09003568 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003569 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003570 } else {
3571 return null;
3572 }
3573 }
3574
satokab751aa2010-09-14 19:17:36 +09003575 /**
3576 * @return Return the current subtype of this input method.
3577 */
satok42c5a162011-05-26 16:46:14 +09003578 @Override
satokab751aa2010-09-14 19:17:36 +09003579 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003580 // TODO: Make this work even for non-current users?
3581 if (!calledFromValidUser()) {
3582 return null;
3583 }
3584 synchronized (mMethodMap) {
3585 return getCurrentInputMethodSubtypeLocked();
3586 }
3587 }
3588
3589 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003590 if (mCurMethodId == null) {
3591 return null;
3592 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003593 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003594 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3595 if (imi == null || imi.getSubtypeCount() == 0) {
3596 return null;
satok4e4569d2010-11-19 18:45:53 +09003597 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003598 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003599 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3600 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003601 if (subtypeId == NOT_A_SUBTYPE_ID) {
3602 // If there are no selected subtypes, the framework will try to find
3603 // the most applicable subtype from explicitly or implicitly enabled
3604 // subtypes.
3605 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003606 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003607 // If there is only one explicitly or implicitly enabled subtype,
3608 // just returns it.
3609 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3610 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3611 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003612 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003613 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003614 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003615 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003616 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003617 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3618 true);
satok4e4569d2010-11-19 18:45:53 +09003619 }
satok3ef8b292010-11-23 06:06:29 +09003620 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003621 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003622 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003623 }
3624 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003625 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003626 }
3627
satok4e4569d2010-11-19 18:45:53 +09003628 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003629 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003630 @Override
satok4e4569d2010-11-19 18:45:53 +09003631 public List getShortcutInputMethodsAndSubtypes() {
3632 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003633 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003634 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003635 // If there are no selected shortcut subtypes, the framework will try to find
3636 // the most applicable subtype from all subtypes whose mode is
3637 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003638 Pair<InputMethodInfo, InputMethodSubtype> info =
3639 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003640 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003641 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003642 ret.add(info.first);
3643 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003644 }
satok3da92232011-01-11 22:46:30 +09003645 return ret;
satokf3db1af2010-11-23 13:34:33 +09003646 }
satokf3db1af2010-11-23 13:34:33 +09003647 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3648 ret.add(imi);
3649 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3650 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003651 }
3652 }
satokf3db1af2010-11-23 13:34:33 +09003653 return ret;
satok4e4569d2010-11-19 18:45:53 +09003654 }
3655 }
3656
satok42c5a162011-05-26 16:46:14 +09003657 @Override
satokb66d2872010-11-10 01:04:04 +09003658 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003659 // TODO: Make this work even for non-current users?
3660 if (!calledFromValidUser()) {
3661 return false;
3662 }
satokb66d2872010-11-10 01:04:04 +09003663 synchronized (mMethodMap) {
3664 if (subtype != null && mCurMethodId != null) {
3665 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003666 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003667 if (subtypeId != NOT_A_SUBTYPE_ID) {
3668 setInputMethodLocked(mCurMethodId, subtypeId);
3669 return true;
3670 }
3671 }
3672 return false;
3673 }
3674 }
3675
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003676 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003677 private static class InputMethodFileManager {
3678 private static final String SYSTEM_PATH = "system";
3679 private static final String INPUT_METHOD_PATH = "inputmethod";
3680 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3681 private static final String NODE_SUBTYPES = "subtypes";
3682 private static final String NODE_SUBTYPE = "subtype";
3683 private static final String NODE_IMI = "imi";
3684 private static final String ATTR_ID = "id";
3685 private static final String ATTR_LABEL = "label";
3686 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003687 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003688 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003689 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003690 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3691 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3692 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003693 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003694 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3695 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003696 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003697 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003698 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003699 if (methodMap == null) {
3700 throw new NullPointerException("methodMap is null");
3701 }
3702 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003703 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003704 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3705 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003706 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003707 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003708 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3709 }
3710 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3711 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3712 if (!subtypeFile.exists()) {
3713 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003714 writeAdditionalInputMethodSubtypes(
3715 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003716 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003717 readAdditionalInputMethodSubtypes(
3718 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003719 }
3720 }
3721
3722 private void deleteAllInputMethodSubtypes(String imiId) {
3723 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003724 mAdditionalSubtypesMap.remove(imiId);
3725 writeAdditionalInputMethodSubtypes(
3726 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003727 }
3728 }
3729
3730 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003731 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003732 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003733 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003734 final int N = additionalSubtypes.length;
3735 for (int i = 0; i < N; ++i) {
3736 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003737 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003738 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003739 } else {
3740 Slog.w(TAG, "Duplicated subtype definition found: "
3741 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003742 }
3743 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003744 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3745 writeAdditionalInputMethodSubtypes(
3746 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003747 }
3748 }
3749
3750 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3751 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003752 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003753 }
3754 }
3755
3756 private static void writeAdditionalInputMethodSubtypes(
3757 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3758 HashMap<String, InputMethodInfo> methodMap) {
3759 // Safety net for the case that this function is called before methodMap is set.
3760 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3761 FileOutputStream fos = null;
3762 try {
3763 fos = subtypesFile.startWrite();
3764 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003765 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003766 out.startDocument(null, true);
3767 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3768 out.startTag(null, NODE_SUBTYPES);
3769 for (String imiId : allSubtypes.keySet()) {
3770 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3771 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3772 continue;
3773 }
3774 out.startTag(null, NODE_IMI);
3775 out.attribute(null, ATTR_ID, imiId);
3776 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3777 final int N = subtypesList.size();
3778 for (int i = 0; i < N; ++i) {
3779 final InputMethodSubtype subtype = subtypesList.get(i);
3780 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003781 if (subtype.hasSubtypeId()) {
3782 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3783 String.valueOf(subtype.getSubtypeId()));
3784 }
satoke7c6998e2011-06-03 17:57:59 +09003785 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3786 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3787 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003788 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3789 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003790 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3791 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3792 out.attribute(null, ATTR_IS_AUXILIARY,
3793 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003794 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3795 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003796 out.endTag(null, NODE_SUBTYPE);
3797 }
3798 out.endTag(null, NODE_IMI);
3799 }
3800 out.endTag(null, NODE_SUBTYPES);
3801 out.endDocument();
3802 subtypesFile.finishWrite(fos);
3803 } catch (java.io.IOException e) {
3804 Slog.w(TAG, "Error writing subtypes", e);
3805 if (fos != null) {
3806 subtypesFile.failWrite(fos);
3807 }
3808 }
3809 }
3810
3811 private static void readAdditionalInputMethodSubtypes(
3812 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3813 if (allSubtypes == null || subtypesFile == null) return;
3814 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003815 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003816 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003817 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003818 int type = parser.getEventType();
3819 // Skip parsing until START_TAG
3820 while ((type = parser.next()) != XmlPullParser.START_TAG
3821 && type != XmlPullParser.END_DOCUMENT) {}
3822 String firstNodeName = parser.getName();
3823 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3824 throw new XmlPullParserException("Xml doesn't start with subtypes");
3825 }
3826 final int depth =parser.getDepth();
3827 String currentImiId = null;
3828 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3829 while (((type = parser.next()) != XmlPullParser.END_TAG
3830 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3831 if (type != XmlPullParser.START_TAG)
3832 continue;
3833 final String nodeName = parser.getName();
3834 if (NODE_IMI.equals(nodeName)) {
3835 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3836 if (TextUtils.isEmpty(currentImiId)) {
3837 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3838 continue;
3839 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003840 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003841 allSubtypes.put(currentImiId, tempSubtypesArray);
3842 } else if (NODE_SUBTYPE.equals(nodeName)) {
3843 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3844 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3845 continue;
3846 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003847 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003848 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003849 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003850 parser.getAttributeValue(null, ATTR_LABEL));
3851 final String imeSubtypeLocale =
3852 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003853 final String languageTag =
3854 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003855 final String imeSubtypeMode =
3856 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3857 final String imeSubtypeExtraValue =
3858 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003859 final boolean isAuxiliary = "1".equals(String.valueOf(
3860 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003861 final boolean isAsciiCapable = "1".equals(String.valueOf(
3862 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003863 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003864 .setSubtypeNameResId(label)
3865 .setSubtypeIconResId(icon)
3866 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003867 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003868 .setSubtypeMode(imeSubtypeMode)
3869 .setSubtypeExtraValue(imeSubtypeExtraValue)
3870 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003871 .setIsAsciiCapable(isAsciiCapable);
3872 final String subtypeIdString =
3873 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3874 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003875 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003876 }
3877 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003878 }
3879 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003880 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3881 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003882 return;
satoke7c6998e2011-06-03 17:57:59 +09003883 }
3884 }
3885 }
3886
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003887 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3888 @NonNull
3889 private final Handler mHandler;
3890
3891 LocalServiceImpl(@NonNull final Handler handler) {
3892 mHandler = handler;
3893 }
3894
3895 @Override
3896 public void setInteractive(boolean interactive) {
3897 // Do everything in handler so as not to block the caller.
3898 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3899 interactive ? 1 : 0, 0));
3900 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003901
3902 @Override
3903 public void switchInputMethod(boolean forwardDirection) {
3904 // Do everything in handler so as not to block the caller.
3905 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3906 forwardDirection ? 1 : 0, 0));
3907 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003908
3909 @Override
3910 public void hideCurrentInputMethod() {
3911 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3912 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3913 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003914 }
3915
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003916 private static String imeWindowStatusToString(final int imeWindowVis) {
3917 final StringBuilder sb = new StringBuilder();
3918 boolean first = true;
3919 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3920 sb.append("Active");
3921 first = false;
3922 }
3923 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3924 if (!first) {
3925 sb.append("|");
3926 }
3927 sb.append("Visible");
3928 }
3929 return sb.toString();
3930 }
3931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07003933 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
3934 @Nullable Uri contentUri, @Nullable String packageName) {
3935 if (!calledFromValidUser()) {
3936 return null;
3937 }
3938
3939 if (token == null) {
3940 throw new NullPointerException("token");
3941 }
3942 if (packageName == null) {
3943 throw new NullPointerException("packageName");
3944 }
3945 if (contentUri == null) {
3946 throw new NullPointerException("contentUri");
3947 }
3948 final String contentUriScheme = contentUri.getScheme();
3949 if (!"content".equals(contentUriScheme)) {
3950 throw new InvalidParameterException("contentUri must have content scheme");
3951 }
3952
3953 synchronized (mMethodMap) {
3954 final int uid = Binder.getCallingUid();
3955 if (mCurMethodId == null) {
3956 return null;
3957 }
3958 if (mCurToken != token) {
3959 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
3960 + " token=" + token);
3961 return null;
3962 }
3963 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
3964 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
3965 // nature of our system. Let's compare it with our internal record.
3966 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
3967 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
3968 + mCurAttribute.packageName + " packageName=" + packageName);
3969 return null;
3970 }
3971 final int imeUserId = UserHandle.getUserId(uid);
3972 final int appUserId = UserHandle.getUserId(mCurClient.uid);
3973 return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
3974 appUserId);
3975 }
3976 }
3977
3978 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3980 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3981 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3984 + Binder.getCallingPid()
3985 + ", uid=" + Binder.getCallingUid());
3986 return;
3987 }
3988
3989 IInputMethod method;
3990 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003991 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 synchronized (mMethodMap) {
3996 p.println("Current Input Method Manager state:");
3997 int N = mMethodList.size();
3998 p.println(" Input Methods:");
3999 for (int i=0; i<N; i++) {
4000 InputMethodInfo info = mMethodList.get(i);
4001 p.println(" InputMethod #" + i + ":");
4002 info.dump(p, " ");
4003 }
4004 p.println(" Clients:");
4005 for (ClientState ci : mClients.values()) {
4006 p.println(" Client " + ci + ":");
4007 p.println(" client=" + ci.client);
4008 p.println(" inputContext=" + ci.inputContext);
4009 p.println(" sessionRequested=" + ci.sessionRequested);
4010 p.println(" curSession=" + ci.curSession);
4011 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004012 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004014 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4015 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004016 focusedWindowClient = mCurFocusedWindowClient;
4017 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4019 + " mBoundToMethod=" + mBoundToMethod);
4020 p.println(" mCurToken=" + mCurToken);
4021 p.println(" mCurIntent=" + mCurIntent);
4022 method = mCurMethod;
4023 p.println(" mCurMethod=" + mCurMethod);
4024 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004025 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 p.println(" mShowRequested=" + mShowRequested
4027 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4028 + " mShowForced=" + mShowForced
4029 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004030 p.println(" mCurUserActionNotificationSequenceNumber="
4031 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004032 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004033 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004034 p.println(" mSwitchingController:");
4035 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004036 p.println(" mSettings:");
4037 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004039
Jeff Brownb88102f2010-09-08 11:49:43 -07004040 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 pw.flush();
4043 try {
4044 client.client.asBinder().dump(fd, args);
4045 } catch (RemoteException e) {
4046 p.println("Input method client dead: " + e);
4047 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004048 } else {
4049 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004051
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004052 if (focusedWindowClient != null && client != focusedWindowClient) {
4053 p.println(" ");
4054 p.println("Warning: Current input method client doesn't match the last focused. "
4055 + "window.");
4056 p.println("Dumping input method client in the last focused window just in case.");
4057 p.println(" ");
4058 pw.flush();
4059 try {
4060 focusedWindowClient.client.asBinder().dump(fd, args);
4061 } catch (RemoteException e) {
4062 p.println("Input method client in focused window dead: " + e);
4063 }
4064 }
4065
Jeff Brownb88102f2010-09-08 11:49:43 -07004066 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 pw.flush();
4069 try {
4070 method.asBinder().dump(fd, args);
4071 } catch (RemoteException e) {
4072 p.println("Input method service dead: " + e);
4073 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004074 } else {
4075 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 }
4077 }
4078}