blob: 69e481fc9e1ce2da65eae241eda8db709ed56c29 [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
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050031import com.android.internal.notification.SystemNotificationChannels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070033import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070034import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090035import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import com.android.internal.view.IInputContext;
37import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import com.android.internal.view.IInputMethodClient;
39import com.android.internal.view.IInputMethodManager;
40import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070041import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080043import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080044import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
satoke7c6998e2011-06-03 17:57:59 +090046import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090048import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070050import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070051import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070052import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080053import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080054import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070055import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070057import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090058import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090059import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090060import android.app.Notification;
61import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070062import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090063import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080065import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.ContentResolver;
67import android.content.Context;
68import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090070import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090072import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070074import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.content.pm.PackageManager;
77import android.content.pm.ResolveInfo;
78import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070079import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.content.res.Resources;
81import android.content.res.TypedArray;
82import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080083import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080084import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080085import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070086import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040088import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070089import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090090import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.Handler;
92import android.os.IBinder;
93import android.os.IInterface;
94import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070095import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070097import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080099import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.os.ServiceManager;
101import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900102import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800103import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.provider.Settings;
105import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900106import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700107import android.util.ArrayMap;
108import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700109import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900111import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900112import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.util.PrintWriterPrinter;
114import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900115import android.util.Slog;
116import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900117import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700119import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.view.LayoutInflater;
121import android.view.View;
122import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700123import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700125import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900126import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800128import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700129import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import android.view.inputmethod.InputMethod;
131import android.view.inputmethod.InputMethodInfo;
132import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700133import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900134import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900135import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900136import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900137import android.widget.CompoundButton;
138import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900139import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900140import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900141import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700142import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
satoke7c6998e2011-06-03 17:57:59 +0900144import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900146import java.io.FileInputStream;
147import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148import java.io.IOException;
149import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700150import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100151import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700152import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800153import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900155import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800156import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157import java.util.HashMap;
158import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800159import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800160import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800161import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
163/**
164 * This class provides a system service that manages input methods.
165 */
166public class InputMethodManagerService extends IInputMethodManager.Stub
167 implements ServiceConnection, Handler.Callback {
168 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700169 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700170 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700172 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
173 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
174 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_UNBIND_INPUT = 1000;
177 static final int MSG_BIND_INPUT = 1010;
178 static final int MSG_SHOW_SOFT_INPUT = 1020;
179 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700180 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 static final int MSG_ATTACH_TOKEN = 1040;
182 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
Yohei Yukawa33e81792015-11-17 21:14:42 -0800186 static final int MSG_UNBIND_CLIENT = 3000;
187 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700188 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700189 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900190 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800191 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800192 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
satok01038492012-04-09 21:08:27 +0900194 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
195
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700196 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
197
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700198 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
satokf9f01002011-05-19 21:31:50 +0900200 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
201
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900202 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900203 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900204
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700205 @Retention(SOURCE)
206 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
207 private @interface HardKeyboardBehavior {
208 int WIRELESS_AFFORDANCE = 0;
209 int WIRED_AFFORDANCE = 1;
210 }
satok4e4569d2010-11-19 18:45:53 +0900211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800213 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900215 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700218 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700220 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900221 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900222 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900223 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800224 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900226 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 // All known input methods. mMethodMap also serves as the global
229 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700230 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
231 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900232 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700233 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900234 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
Yohei Yukawae0733062017-02-09 22:49:35 -0800236 /**
237 * Tracks how many times {@link #mMethodMap} was updated.
238 */
239 @GuardedBy("mMethodMap")
240 private int mMethodMapUpdateCount = 0;
241
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700242 // Used to bring IME service up to visible adjustment while it is being shown.
243 final ServiceConnection mVisibleConnection = new ServiceConnection() {
244 @Override public void onServiceConnected(ComponentName name, IBinder service) {
245 }
246
247 @Override public void onServiceDisconnected(ComponentName name) {
248 }
249 };
250 boolean mVisibleBound = false;
251
satok7cfc0ed2011-06-20 21:29:36 +0900252 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700253 private NotificationManager mNotificationManager;
254 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700255 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400256 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700257 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900258 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900259 private boolean mNotificationShown;
260
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900261 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 final ClientState client;
263 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700264
265 IInputMethodSession session;
266 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 @Override
269 public String toString() {
270 return "SessionState{uid " + client.uid + " pid " + client.pid
271 + " method " + Integer.toHexString(
272 System.identityHashCode(method))
273 + " session " + Integer.toHexString(
274 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700275 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 + "}";
277 }
278
279 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700280 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 client = _client;
282 method = _method;
283 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700284 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 }
286 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
Jeff Brownc28867a2013-03-26 15:42:39 -0700288 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 final IInputMethodClient client;
290 final IInputContext inputContext;
291 final int uid;
292 final int pid;
293 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 boolean sessionRequested;
296 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 @Override
299 public String toString() {
300 return "ClientState{" + Integer.toHexString(
301 System.identityHashCode(this)) + " uid " + uid
302 + " pid " + pid + "}";
303 }
304
305 ClientState(IInputMethodClient _client, IInputContext _inputContext,
306 int _uid, int _pid) {
307 client = _client;
308 inputContext = _inputContext;
309 uid = _uid;
310 pid = _pid;
311 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
312 }
313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700315 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700318 * Set once the system is ready to run third party code.
319 */
320 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700322 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700323 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
324 * method. This is to be synchronized with the secure settings keyed with
325 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
326 *
327 * <p>This can be transiently {@code null} when the system is re-initializing input method
328 * settings, e.g., the system locale is just changed.</p>
329 *
330 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
331 * {@link InputMethodManagerService}.</p>
332 *
333 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700335 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * The current binding sequence number, incremented every time there is
340 * a new bind performed.
341 */
342 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * The client that is currently bound to an input method.
346 */
347 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800350 * The last window token that we confirmed to be focused. This is always updated upon reports
351 * from the input method client. If the window state is already changed before the report is
352 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700353 */
354 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700356 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800357 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
358 *
359 * @see #mCurFocusedWindow
360 */
361 int mCurFocusedWindowSoftInputMode;
362
363 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800364 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
365 */
366 ClientState mCurFocusedWindowClient;
367
368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 * The input context last provided by the current client.
370 */
371 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700374 * The missing method flags for the input context last provided by the current client.
375 *
376 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
377 */
378 int mCurInputContextMissingMethods;
379
380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * The attributes last provided by the current client.
382 */
383 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700386 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700388 *
389 * <p>This can be {@code null} when no input method is connected.</p>
390 *
391 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700393 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
satokab751aa2010-09-14 19:17:36 +0900397 * The current subtype of the current input method.
398 */
399 private InputMethodSubtype mCurrentSubtype;
400
satok4e4569d2010-11-19 18:45:53 +0900401 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900402 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700403 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900404
John Spurlocke0980502013-10-25 11:59:29 -0400405 // Was the keyguard locked when this client became current?
406 private boolean mCurClientInKeyguard;
407
satokab751aa2010-09-14 19:17:36 +0900408 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * Set to true if our ServiceConnection is currently actively bound to
410 * a service (whether or not we have gotten its IBinder back yet).
411 */
412 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Set if the client has asked for the input method to be shown.
416 */
417 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * Set if we were explicitly told to show the input method.
421 */
422 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * Set if we were forced to be shown.
426 */
427 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
430 * Set if we last told the input method to show itself.
431 */
432 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800435 * {@code true} if the current input method is in fullscreen mode.
436 */
437 boolean mInFullscreenMode;
438
439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 * The Intent used to connect to the current input method.
441 */
442 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 /**
445 * The token we have made for the currently active input method, to
446 * identify it in the future.
447 */
448 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
451 * If non-null, this is the input method service we are currently connected
452 * to.
453 */
454 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 /**
457 * Time that we last initiated a bind to the input method, to determine
458 * if we should try to disconnect and reconnect to it.
459 */
460 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 /**
463 * Have we called mCurMethod.bindInput()?
464 */
465 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
468 * Currently enabled session. Only touched by service thread, not
469 * protected by a lock.
470 */
471 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700474 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700476 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900478 int mCurUserActionNotificationSequenceNumber = 0;
479
Joe Onorato857fd9b2011-01-27 15:08:35 -0800480 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900481
482 /**
483 * A set of status bits regarding the active IME.
484 *
485 * <p>This value is a combination of following two bits:</p>
486 * <dl>
487 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
488 * <dd>
489 * If this bit is ON, connected IME is ready to accept touch/key events.
490 * </dd>
491 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
492 * <dd>
493 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
494 * </dd>
495 * </dl>
496 * <em>Do not update this value outside of setImeWindowStatus.</em>
497 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800498 int mImeWindowVis;
499
Ken Wakasa05dbb652011-08-22 15:22:43 +0900500 private AlertDialog.Builder mDialogBuilder;
501 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700502 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900503 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700504 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900505 private InputMethodInfo[] mIms;
506 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800507 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700508 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800509 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900510 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
511 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500512 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700513 @HardKeyboardBehavior
514 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800515
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800516 /**
517 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
518 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
519 * will not affect those tasks that are already posted.
520 *
521 * <p>Posting {@link #MSG_START_INPUT} message basically means that
522 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
523 * back in the current IME process shortly, which will also affect what the current IME starts
524 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
525 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
526 * logical input session between the client application and the current IME.</p>
527 *
528 * <p>Be careful to not keep strong references to this object forever, which can prevent
529 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
530 * </p>
531 */
532 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800533 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
534
535 final int mSequenceNumber;
536 final long mTimestamp;
537 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800538 @NonNull
539 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800540 @NonNull
541 final String mImeId;
542 // @InputMethodClient.StartInputReason
543 final int mStartInputReason;
544 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800545 @Nullable
546 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800547 @NonNull
548 final EditorInfo mEditorInfo;
549 final int mTargetWindowSoftInputMode;
550 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800551
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800552 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
553 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
554 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
555 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
556 mSequenceNumber = sSequenceNumber.getAndIncrement();
557 mTimestamp = SystemClock.uptimeMillis();
558 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800559 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800560 mImeId = imeId;
561 mStartInputReason = startInputReason;
562 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800563 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800564 mEditorInfo = editorInfo;
565 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
566 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800567 }
568 }
569
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800570 @GuardedBy("mMethodMap")
571 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800572
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800573 /**
574 * A ring buffer to store the history of {@link StartInputInfo}.
575 */
576 private static final class StartInputHistory {
577 /**
578 * Entry size for non low-RAM devices.
579 *
580 * <p>TODO: Consider to follow what other system services have been doing to manage
581 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
582 */
583 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
584
585 /**
586 * Entry size for non low-RAM devices.
587 *
588 * <p>TODO: Consider to follow what other system services have been doing to manage
589 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
590 */
591 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
592
593 private static int getEntrySize() {
594 if (ActivityManager.isLowRamDeviceStatic()) {
595 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
596 } else {
597 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
598 }
599 }
600
601 /**
602 * Backing store for the ring bugger.
603 */
604 private final Entry[] mEntries = new Entry[getEntrySize()];
605
606 /**
607 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
608 * write.
609 */
610 private int mNextIndex = 0;
611
612 /**
613 * Recyclable entry to store the information in {@link StartInputInfo}.
614 */
615 private static final class Entry {
616 int mSequenceNumber;
617 long mTimestamp;
618 long mWallTime;
619 @NonNull
620 String mImeTokenString;
621 @NonNull
622 String mImeId;
623 /* @InputMethodClient.StartInputReason */
624 int mStartInputReason;
625 boolean mRestarting;
626 @NonNull
627 String mTargetWindowString;
628 @NonNull
629 EditorInfo mEditorInfo;
630 int mTargetWindowSoftInputMode;
631 int mClientBindSequenceNumber;
632
633 Entry(@NonNull StartInputInfo original) {
634 set(original);
635 }
636
637 void set(@NonNull StartInputInfo original) {
638 mSequenceNumber = original.mSequenceNumber;
639 mTimestamp = original.mTimestamp;
640 mWallTime = original.mWallTime;
641 // Intentionally convert to String so as not to keep a strong reference to a Binder
642 // object.
643 mImeTokenString = String.valueOf(original.mImeToken);
644 mImeId = original.mImeId;
645 mStartInputReason = original.mStartInputReason;
646 mRestarting = original.mRestarting;
647 // Intentionally convert to String so as not to keep a strong reference to a Binder
648 // object.
649 mTargetWindowString = String.valueOf(original.mTargetWindow);
650 mEditorInfo = original.mEditorInfo;
651 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
652 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
653 }
654 }
655
656 /**
657 * Add a new entry and discard the oldest entry as needed.
658 * @param info {@lin StartInputInfo} to be added.
659 */
660 void addEntry(@NonNull StartInputInfo info) {
661 final int index = mNextIndex;
662 if (mEntries[index] == null) {
663 mEntries[index] = new Entry(info);
664 } else {
665 mEntries[index].set(info);
666 }
667 mNextIndex = (mNextIndex + 1) % mEntries.length;
668 }
669
670 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
671 final SimpleDateFormat dataFormat =
672 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
673
674 for (int i = 0; i < mEntries.length; ++i) {
675 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
676 if (entry == null) {
677 continue;
678 }
679 pw.print(prefix);
680 pw.println("StartInput #" + entry.mSequenceNumber + ":");
681
682 pw.print(prefix);
683 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
684 + " (timestamp=" + entry.mTimestamp + ")"
685 + " reason="
686 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
687 + " restarting=" + entry.mRestarting);
688
689 pw.print(prefix);
690 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
691
692 pw.print(prefix);
693 pw.println(" targetWin=" + entry.mTargetWindowString
694 + " [" + entry.mEditorInfo.packageName + "]"
695 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
696
697 pw.print(prefix);
698 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
699 entry.mTargetWindowSoftInputMode));
700
701 pw.print(prefix);
702 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
703 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
704 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
705 + " fieldName=" + entry.mEditorInfo.fieldName
706 + " actionId=" + entry.mEditorInfo.actionId
707 + " actionLabel=" + entry.mEditorInfo.actionLabel);
708 }
709 }
710 }
711
712 @GuardedBy("mMethodMap")
713 @NonNull
714 private final StartInputHistory mStartInputHistory = new StartInputHistory();
715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700717 int mUserId;
718 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700719 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800720 String mLastEnabled = "";
721
Yohei Yukawa81482972015-06-04 00:58:59 -0700722 /**
723 * <em>This constructor must be called within the lock.</em>
724 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 SettingsObserver(Handler handler) {
726 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700727 }
728
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800729 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700730 if (mRegistered && mUserId == userId) {
731 return;
732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700734 if (mRegistered) {
735 mContext.getContentResolver().unregisterContentObserver(this);
736 mRegistered = false;
737 }
738 if (mUserId != userId) {
739 mLastEnabled = "";
740 mUserId = userId;
741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700743 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900744 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700745 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900746 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700747 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700748 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700749 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800750 resolver.registerContentObserver(Settings.Secure.getUriFor(
751 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700752 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754
Michael Wright7b5a96b2014-08-09 19:28:42 -0700755 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800756 final Uri showImeUri = Settings.Secure.getUriFor(
757 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
758 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
759 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700761 if (showImeUri.equals(uri)) {
762 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800763 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
764 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
765 mContext.getContentResolver(),
766 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
767 0, mUserId) == 1;
768 if (mAccessibilityRequestingNoSoftKeyboard) {
769 final boolean showRequested = mShowRequested;
770 hideCurrentInputLocked(0, null);
771 mShowRequested = showRequested;
772 } else if (mShowRequested) {
773 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
774 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700775 } else {
776 boolean enabledChanged = false;
777 String newEnabled = mSettings.getEnabledInputMethodsStr();
778 if (!mLastEnabled.equals(newEnabled)) {
779 mLastEnabled = newEnabled;
780 enabledChanged = true;
781 }
782 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700786
787 @Override
788 public String toString() {
789 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
790 + " mLastEnabled=" + mLastEnabled + "}";
791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793
Yohei Yukawa79247822017-01-23 15:26:15 -0800794 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900795 @Override
796 public void onReceive(Context context, Intent intent) {
797 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700798 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900799 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700800 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900801 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800802 } else if (Intent.ACTION_USER_ADDED.equals(action)
803 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100804 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800805 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700806 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
807 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
808 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
809 final String prevValue = intent.getStringExtra(
810 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
811 final String newValue = intent.getStringExtra(
812 Intent.EXTRA_SETTING_NEW_VALUE);
813 restoreEnabledInputMethods(mContext, prevValue, newValue);
814 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800815 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800816 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900817 } else {
818 Slog.w(TAG, "Unexpected intent " + intent);
819 }
820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800822
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800823 /**
824 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
825 *
826 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
827 * the users. We should ignore this event if this is about any background user's locale.</p>
828 *
829 * <p>Caution: This method must not be called when system is not ready.</p>
830 */
831 void onActionLocaleChanged() {
832 synchronized (mMethodMap) {
833 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
834 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
835 return;
836 }
837 buildInputMethodListLocked(true);
838 // If the locale is changed, needs to reset the default ime
839 resetDefaultImeLocked(mContext);
840 updateFromSettingsLocked(true);
841 mLastSystemLocales = possibleNewLocale;
842 }
843 }
844
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700845 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
846 // does not attempt to validate on the fly with any installed device policy, so must only
847 // be run in the context of initial device setup.
848 //
849 // TODO: Move this method to InputMethodUtils with adding unit tests.
850 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
851 if (DEBUG_RESTORE) {
852 Slog.i(TAG, "Restoring enabled input methods:");
853 Slog.i(TAG, "prev=" + prevValue);
854 Slog.i(TAG, " new=" + newValue);
855 }
856 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900857 ArrayMap<String, ArraySet<String>> prevMap =
858 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
859 ArrayMap<String, ArraySet<String>> newMap =
860 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700861
862 // Merge the restored ime+subtype enabled states into the live state
863 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
864 final String imeId = entry.getKey();
865 ArraySet<String> prevSubtypes = prevMap.get(imeId);
866 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700867 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700868 prevMap.put(imeId, prevSubtypes);
869 }
870 prevSubtypes.addAll(entry.getValue());
871 }
872
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700873 final String mergedImesAndSubtypesString =
874 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700875 if (DEBUG_RESTORE) {
876 Slog.i(TAG, "Merged IME string:");
877 Slog.i(TAG, " " + mergedImesAndSubtypesString);
878 }
879 Settings.Secure.putString(context.getContentResolver(),
880 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
881 }
882
Yohei Yukawac4e44912017-02-09 19:30:22 -0800883 final class MyPackageMonitor extends PackageMonitor {
884 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800885 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800886 *
887 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
888 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
889 * dynamically unless the entire package is updated, which also always triggers package
890 * rescanning.</p>
891 */
892 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800893 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
894
895 /**
896 * Packages that are appeared, disappeared, or modified for whatever reason.
897 *
898 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
899 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
900 * duplicate elements for our use case.</p>
901 *
902 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
903 * which should be bound to {@link #getRegisteredHandler()}.</p>
904 */
905 private final ArrayList<String> mChangedPackages = new ArrayList<>();
906
907 /**
908 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
909 *
910 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
911 * which should be bound to {@link #getRegisteredHandler()}.</p>
912 */
913 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800914
915 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800916 void clearKnownImePackageNamesLocked() {
917 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800918 }
919
920 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800921 final void addKnownImePackageNameLocked(@NonNull String packageName) {
922 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800923 }
924
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800925 @GuardedBy("mMethodMap")
926 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900927 final int userId = getChangingUserId();
928 final boolean retval = userId == mSettings.getCurrentUserId();
929 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900930 if (!retval) {
931 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
932 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900933 }
934 return retval;
935 }
936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800938 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800940 if (!isChangingPackagesOfCurrentUserLocked()) {
941 return false;
942 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 final int N = mMethodList.size();
945 if (curInputMethodId != null) {
946 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800947 InputMethodInfo imi = mMethodList.get(i);
948 if (imi.getId().equals(curInputMethodId)) {
949 for (String pkg : packages) {
950 if (imi.getPackageName().equals(pkg)) {
951 if (!doit) {
952 return true;
953 }
satok723a27e2010-11-11 14:58:11 +0900954 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800955 chooseNewDefaultIMELocked();
956 return true;
957 }
958 }
959 }
960 }
961 }
962 }
963 return false;
964 }
965
966 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800967 public void onBeginPackageChanges() {
968 clearPackageChangeState();
969 }
970
971 @Override
972 public void onPackageAppeared(String packageName, int reason) {
973 if (!mImePackageAppeared) {
974 final PackageManager pm = mContext.getPackageManager();
975 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
976 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
977 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
978 // No need to lock this because we access it only on getRegisteredHandler().
979 if (!services.isEmpty()) {
980 mImePackageAppeared = true;
981 }
982 }
983 // No need to lock this because we access it only on getRegisteredHandler().
984 mChangedPackages.add(packageName);
985 }
986
987 @Override
988 public void onPackageDisappeared(String packageName, int reason) {
989 // No need to lock this because we access it only on getRegisteredHandler().
990 mChangedPackages.add(packageName);
991 }
992
993 @Override
994 public void onPackageModified(String packageName) {
995 // No need to lock this because we access it only on getRegisteredHandler().
996 mChangedPackages.add(packageName);
997 }
998
999 @Override
1000 public void onPackagesSuspended(String[] packages) {
1001 // No need to lock this because we access it only on getRegisteredHandler().
1002 for (String packageName : packages) {
1003 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001004 }
1005 }
1006
1007 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001008 public void onPackagesUnsuspended(String[] packages) {
1009 // No need to lock this because we access it only on getRegisteredHandler().
1010 for (String packageName : packages) {
1011 mChangedPackages.add(packageName);
1012 }
1013 }
1014
1015 @Override
1016 public void onFinishPackageChanges() {
1017 onFinishPackageChangesInternal();
1018 clearPackageChangeState();
1019 }
1020
1021 private void clearPackageChangeState() {
1022 // No need to lock them because we access these fields only on getRegisteredHandler().
1023 mChangedPackages.clear();
1024 mImePackageAppeared = false;
1025 }
1026
1027 private boolean shouldRebuildInputMethodListLocked() {
1028 // This method is guaranteed to be called only by getRegisteredHandler().
1029
1030 // If there is any new package that contains at least one IME, then rebuilt the list
1031 // of IMEs.
1032 if (mImePackageAppeared) {
1033 return true;
1034 }
1035
1036 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1037 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1038 // is an option, but it may still do some extra operations that we do not need here.
1039 final int N = mChangedPackages.size();
1040 for (int i = 0; i < N; ++i) {
1041 final String packageName = mChangedPackages.get(i);
1042 if (mKnownImePackageNames.contains(packageName)) {
1043 return true;
1044 }
1045 }
1046 return false;
1047 }
1048
1049 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001050 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001051 if (!isChangingPackagesOfCurrentUserLocked()) {
1052 return;
1053 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001054 if (!shouldRebuildInputMethodListLocked()) {
1055 return;
1056 }
1057
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001058 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001059 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001060 final int N = mMethodList.size();
1061 if (curInputMethodId != null) {
1062 for (int i=0; i<N; i++) {
1063 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001064 final String imiId = imi.getId();
1065 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001066 curIm = imi;
1067 }
satoke7c6998e2011-06-03 17:57:59 +09001068
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001069 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001070 if (isPackageModified(imi.getPackageName())) {
1071 mFileManager.deleteAllInputMethodSubtypes(imiId);
1072 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001073 if (change == PACKAGE_TEMPORARY_CHANGE
1074 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001075 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001076 + imi.getComponent());
1077 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079 }
1080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
Yohei Yukawa94e33302016-02-12 19:37:03 -08001082 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001085
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001086 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001087 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001088 if (change == PACKAGE_TEMPORARY_CHANGE
1089 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001090 ServiceInfo si = null;
1091 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001092 si = mIPackageManager.getServiceInfo(
1093 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1094 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001095 }
1096 if (si == null) {
1097 // Uh oh, current input method is no longer around!
1098 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001099 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001100 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001101 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001102 changed = true;
1103 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001104 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001105 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001106 }
1107 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001108 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001109 }
satokab751aa2010-09-14 19:17:36 +09001110
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001111 if (curIm == null) {
1112 // We currently don't have a default input method... is
1113 // one now available?
1114 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001115 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1116 // Even if the current input method is still available, mCurrentSubtype could
1117 // be obsolete when the package is modified in practice.
1118 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001119 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001120
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001121 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001122 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
1124 }
1125 }
1126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001127
Jeff Brownc28867a2013-03-26 15:42:39 -07001128 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001129 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001130 private final IInputMethod mMethod;
1131 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001132
Jeff Brownc28867a2013-03-26 15:42:39 -07001133 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1134 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001135 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001136 mMethod = method;
1137 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001139
satoke7c6998e2011-06-03 17:57:59 +09001140 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001141 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001142 long ident = Binder.clearCallingIdentity();
1143 try {
1144 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1145 } finally {
1146 Binder.restoreCallingIdentity(ident);
1147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001150
satok01038492012-04-09 21:08:27 +09001151 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001152 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001153 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001154 public void onHardKeyboardStatusChange(boolean available) {
1155 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1156 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001157 }
1158
Michael Wright7b5a96b2014-08-09 19:28:42 -07001159 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001160 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001161 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001162 }
1163 synchronized(mMethodMap) {
1164 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1165 && mSwitchingDialog.isShowing()) {
1166 mSwitchingDialogTitleView.findViewById(
1167 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1168 available ? View.VISIBLE : View.GONE);
1169 }
1170 }
1171 }
1172 }
1173
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001174 public static final class Lifecycle extends SystemService {
1175 private InputMethodManagerService mService;
1176
1177 public Lifecycle(Context context) {
1178 super(context);
1179 mService = new InputMethodManagerService(context);
1180 }
1181
1182 @Override
1183 public void onStart() {
1184 LocalServices.addService(InputMethodManagerInternal.class,
1185 new LocalServiceImpl(mService.mHandler));
1186 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1187 }
1188
1189 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001190 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001191 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001192 // TODO: Dispatch this to a worker thread as needed.
1193 mService.onSwitchUser(userHandle);
1194 }
1195
1196 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001197 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001198 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001199 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001200 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1201 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1202 .getService(Context.STATUS_BAR_SERVICE);
1203 mService.systemRunning(statusBarService);
1204 }
1205 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001206
1207 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001208 public void onUnlockUser(final @UserIdInt int userHandle) {
1209 // Called on ActivityManager thread.
1210 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001211 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001212 }
1213 }
1214
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001215 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001216 synchronized(mMethodMap) {
1217 final int currentUserId = mSettings.getCurrentUserId();
1218 if (DEBUG) {
1219 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1220 }
1221 if (userId != currentUserId) {
1222 return;
1223 }
1224 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001225 if (mSystemReady) {
1226 // We need to rebuild IMEs.
1227 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1228 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1229 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001230 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001231 }
1232
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001233 void onSwitchUser(@UserIdInt int userId) {
1234 synchronized (mMethodMap) {
1235 switchUserLocked(userId);
1236 }
1237 }
1238
Seigo Nonaka7309b122015-08-17 18:34:13 -07001239 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001240 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001242 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001244 // Note: SettingsObserver doesn't register observers in its constructor.
1245 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 mIWindowManager = IWindowManager.Stub.asInterface(
1247 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001248 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001249 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001250 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public void executeMessage(Message msg) {
1252 handleMessage(msg);
1253 }
Mita Yuned218c72012-12-06 17:18:25 -08001254 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001255 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001256 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001257 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001258 mHasFeature = context.getPackageManager().hasSystemFeature(
1259 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001260 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001261 mHardKeyboardBehavior = mContext.getResources().getInteger(
1262 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001263
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001264 Bundle extras = new Bundle();
1265 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001266 mImeSwitcherNotification =
1267 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1268 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1269 .setWhen(0)
1270 .setOngoing(true)
1271 .addExtras(extras)
1272 .setCategory(Notification.CATEGORY_SYSTEM)
1273 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001274
satok7cfc0ed2011-06-20 21:29:36 +09001275 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001276 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001277
1278 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001279
satok7cfc0ed2011-06-20 21:29:36 +09001280 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001281 int userId = 0;
1282 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001283 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001284 } catch (RemoteException e) {
1285 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1286 }
satok913a8922010-08-26 21:53:41 +09001287
satokd87c2592010-09-29 11:52:06 +09001288 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001289 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001290 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001291
Kenny Guy2a764942014-04-02 13:29:20 +01001292 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001293 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001294 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1295 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001296 }
1297
satok5b927c432012-05-01 20:09:34 +09001298 private void resetDefaultImeLocked(Context context) {
1299 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001300 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001301 return;
1302 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001303 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001304 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001305 if (suitableImes.isEmpty()) {
1306 Slog.i(TAG, "No default found");
1307 return;
satok5b927c432012-05-01 20:09:34 +09001308 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001309 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001310 if (DEBUG) {
1311 Slog.i(TAG, "Default found, using " + defIm.getId());
1312 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001313 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001314 }
1315
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001316 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001317 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1318 + " currentUserId=" + mSettings.getCurrentUserId());
1319
Yohei Yukawa81482972015-06-04 00:58:59 -07001320 // ContentObserver should be registered again when the user is changed
1321 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001322
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001323 // If the system is not ready or the device is not yed unlocked by the user, then we use
1324 // copy-on-write settings.
1325 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001326 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001327 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001328 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001329 // InputMethodFileManager should be reset when the user is changed
1330 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001331 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001332
1333 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1334 + " defaultImiId=" + defaultImiId);
1335
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001336 // For secondary users, the list of enabled IMEs may not have been updated since the
1337 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1338 // not be empty even if the IME has been uninstalled by the primary user.
1339 // Even in such cases, IMMS works fine because it will find the most applicable
1340 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001341 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001342 mLastSystemLocales = mRes.getConfiguration().getLocales();
1343
1344 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1345 if (mSystemReady) {
1346 hideCurrentInputLocked(0, null);
1347 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1348 buildInputMethodListLocked(initialUserSwitch);
1349 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1350 // This is the first time of the user switch and
1351 // set the current ime to the proper one.
1352 resetDefaultImeLocked(mContext);
1353 }
1354 updateFromSettingsLocked(true);
1355 try {
1356 startInputInnerLocked();
1357 } catch (RuntimeException e) {
1358 Slog.w(TAG, "Unexpected exception", e);
1359 }
1360 }
1361
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001362 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001363 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1364 mSettings.getEnabledInputMethodListLocked(), newUserId,
1365 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001366 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001367
1368 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1369 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001370 }
1371
Kenny Guy2a764942014-04-02 13:29:20 +01001372 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001373 mSettings.setCurrentProfileIds(
1374 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001375 }
1376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 @Override
1378 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1379 throws RemoteException {
1380 try {
1381 return super.onTransact(code, data, reply, flags);
1382 } catch (RuntimeException e) {
1383 // The input method manager only throws security exceptions, so let's
1384 // log all others.
1385 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001386 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388 throw e;
1389 }
1390 }
1391
Svetoslav Ganova0027152013-06-25 14:59:53 -07001392 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001393 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001394 if (DEBUG) {
1395 Slog.d(TAG, "--- systemReady");
1396 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001397 if (!mSystemReady) {
1398 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001399 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001400 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001401 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001402 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001403 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1404 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001405 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001406 if (mStatusBar != null) {
1407 mStatusBar.setIconVisibility(mSlotIme, false);
1408 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001409 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001410 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1411 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001412 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001413 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001414 mHardKeyboardListener);
1415 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001416
1417 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1418 mSettingsObserver.registerContentObserverLocked(currentUserId);
1419
1420 final IntentFilter broadcastFilter = new IntentFilter();
1421 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1422 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1423 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1424 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1425 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1426 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1427
1428 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1429 resetDefaultImeLocked(mContext);
1430 updateFromSettingsLocked(true);
1431 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1432 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1433 mContext.getBasePackageName());
1434
Dianne Hackborncc278702009-09-02 23:07:23 -07001435 try {
1436 startInputInnerLocked();
1437 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001438 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001439 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001440 }
1441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001444 // ---------------------------------------------------------------------------------------
1445 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1446 // 1) it comes from the system process
1447 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1448 private boolean calledFromValidUser() {
1449 final int uid = Binder.getCallingUid();
1450 final int userId = UserHandle.getUserId(uid);
1451 if (DEBUG) {
1452 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1453 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1454 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001455 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1456 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001457 }
Kenny Guy2a764942014-04-02 13:29:20 +01001458 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001459 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001460 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001461
1462 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1463 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1464 // must not manage background users' states in any functions.
1465 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1466 // by a token.
1467 if (mContext.checkCallingOrSelfPermission(
1468 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1469 == PackageManager.PERMISSION_GRANTED) {
1470 if (DEBUG) {
1471 Slog.d(TAG, "--- Access granted because the calling process has "
1472 + "the INTERACT_ACROSS_USERS_FULL permission");
1473 }
1474 return true;
1475 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001476 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001477 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1478 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001479 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001480 }
1481
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001482
1483 /**
1484 * Returns true iff the caller is identified to be the current input method with the token.
1485 * @param token The window token given to the input method when it was started.
1486 * @return true if and only if non-null valid token is specified.
1487 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001488 private boolean calledWithValidToken(@Nullable IBinder token) {
1489 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1490 if (DEBUG) {
1491 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1492 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1493 }
1494 return false;
1495 }
1496 if (token == null || token != mCurToken) {
1497 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1498 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1499 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001500 return false;
1501 }
1502 return true;
1503 }
1504
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 private boolean bindCurrentInputMethodService(
1506 Intent service, ServiceConnection conn, int flags) {
1507 if (service == null || conn == null) {
1508 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1509 return false;
1510 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001511 return mContext.bindServiceAsUser(service, conn, flags,
1512 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001513 }
1514
satoke7c6998e2011-06-03 17:57:59 +09001515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001517 // TODO: Make this work even for non-current users?
1518 if (!calledFromValidUser()) {
1519 return Collections.emptyList();
1520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001522 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524 }
1525
satoke7c6998e2011-06-03 17:57:59 +09001526 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001528 // TODO: Make this work even for non-current users?
1529 if (!calledFromValidUser()) {
1530 return Collections.emptyList();
1531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001533 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
1535 }
1536
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001537 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001538 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001539 * @return enabled subtypes of the specified imi
1540 */
satoke7c6998e2011-06-03 17:57:59 +09001541 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001542 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001543 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001544 // TODO: Make this work even for non-current users?
1545 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001546 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001547 }
satok67ddf9c2010-11-17 09:45:54 +09001548 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001549 final InputMethodInfo imi;
1550 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001551 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001552 } else {
1553 imi = mMethodMap.get(imiId);
1554 }
1555 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001556 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001557 }
1558 return mSettings.getEnabledInputMethodSubtypeListLocked(
1559 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001560 }
1561 }
1562
satoke7c6998e2011-06-03 17:57:59 +09001563 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 public void addClient(IInputMethodClient client,
1565 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001566 if (!calledFromValidUser()) {
1567 return;
1568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 synchronized (mMethodMap) {
1570 mClients.put(client.asBinder(), new ClientState(client,
1571 inputContext, uid, pid));
1572 }
1573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
satoke7c6998e2011-06-03 17:57:59 +09001575 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001577 if (!calledFromValidUser()) {
1578 return;
1579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001581 ClientState cs = mClients.remove(client.asBinder());
1582 if (cs != null) {
1583 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001584 if (mCurClient == cs) {
1585 mCurClient = null;
1586 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001587 if (mCurFocusedWindowClient == cs) {
1588 mCurFocusedWindowClient = null;
1589 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 void executeOrSendMessage(IInterface target, Message msg) {
1595 if (target.asBinder() instanceof Binder) {
1596 mCaller.sendMessage(msg);
1597 } else {
1598 handleMessage(msg);
1599 msg.recycle();
1600 }
1601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
Yohei Yukawa33e81792015-11-17 21:14:42 -08001603 void unbindCurrentClientLocked(
1604 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001606 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 + mCurClient.client.asBinder());
1608 if (mBoundToMethod) {
1609 mBoundToMethod = false;
1610 if (mCurMethod != null) {
1611 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1612 MSG_UNBIND_INPUT, mCurMethod));
1613 }
1614 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001615
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001616 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1617 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001618 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1619 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001622
The Android Open Source Project10592532009-03-18 17:39:46 -07001623 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 private int getImeShowFlags() {
1628 int flags = 0;
1629 if (mShowForced) {
1630 flags |= InputMethod.SHOW_FORCED
1631 | InputMethod.SHOW_EXPLICIT;
1632 } else if (mShowExplicitlyRequested) {
1633 flags |= InputMethod.SHOW_EXPLICIT;
1634 }
1635 return flags;
1636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 private int getAppShowFlags() {
1639 int flags = 0;
1640 if (mShowForced) {
1641 flags |= InputMethodManager.SHOW_FORCED;
1642 } else if (!mShowExplicitlyRequested) {
1643 flags |= InputMethodManager.SHOW_IMPLICIT;
1644 }
1645 return flags;
1646 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001647
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001648 InputBindResult attachNewInputLocked(
1649 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 if (!mBoundToMethod) {
1651 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1652 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1653 mBoundToMethod = true;
1654 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001655
1656 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001657 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1658 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1659 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001660 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001661 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001664 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001665 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001666 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001668 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001669 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001671 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001672 (session.channel != null ? session.channel.dup() : null),
1673 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001675
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001676 InputBindResult startInputLocked(
1677 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001678 IInputMethodClient client, IInputContext inputContext,
1679 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001680 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 // If no method is currently selected, do nothing.
1682 if (mCurMethodId == null) {
1683 return mNoBinding;
1684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 ClientState cs = mClients.get(client.asBinder());
1687 if (cs == null) {
1688 throw new IllegalArgumentException("unknown client "
1689 + client.asBinder());
1690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001691
Yohei Yukawa74750f22016-03-22 12:54:22 -07001692 if (attribute == null) {
1693 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1694 + " uid=" + cs.uid + " pid=" + cs.pid);
1695 return null;
1696 }
1697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 try {
1699 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1700 // Check with the window manager to make sure this client actually
1701 // has a window with focus. If not, reject. This is thread safe
1702 // because if the focus changes some time before or after, the
1703 // next client receiving focus that has any interest in input will
1704 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001705 if (DEBUG) {
1706 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1707 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 return null;
1710 }
1711 } catch (RemoteException e) {
1712 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001713
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001714 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001715 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001716 }
1717
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001718 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001719 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001720 @NonNull EditorInfo attribute, int controlFlags,
1721 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001722 // If no method is currently selected, do nothing.
1723 if (mCurMethodId == null) {
1724 return mNoBinding;
1725 }
1726
Yohei Yukawad57ba672015-06-08 16:39:46 -07001727 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1728 attribute.packageName)) {
1729 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1730 + " uid=" + cs.uid + " package=" + attribute.packageName);
1731 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001732 }
1733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001735 // Was the keyguard locked when switching over to the new client?
1736 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 // If the client is changing, we need to switch over to the new
1738 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001739 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001740 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001741 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742
1743 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001744 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001745 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001746 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
1748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 // Bump up the sequence for this client and attach it.
1751 mCurSeq++;
1752 if (mCurSeq <= 0) mCurSeq = 1;
1753 mCurClient = cs;
1754 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001755 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 // Check if the input method is changing.
1759 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1760 if (cs.curSession != null) {
1761 // Fast case: if we are already connected to the input method,
1762 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001763 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001764 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
1766 if (mHaveConnection) {
1767 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 // Return to client, and we will get back with it when
1769 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001770 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001771 return new InputBindResult(null, null, mCurId, mCurSeq,
1772 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 } else if (SystemClock.uptimeMillis()
1774 < (mLastBindTime+TIME_TO_RECONNECT)) {
1775 // In this case we have connected to the service, but
1776 // don't yet have its interface. If it hasn't been too
1777 // long since we did the connection, we'll return to
1778 // the client and wait to get the service interface so
1779 // we can report back. If it has been too long, we want
1780 // to fall through so we can try a disconnect/reconnect
1781 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001782 return new InputBindResult(null, null, mCurId, mCurSeq,
1783 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1786 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788 }
1789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001790
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001791 return startInputInnerLocked();
1792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001793
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001794 InputBindResult startInputInnerLocked() {
1795 if (mCurMethodId == null) {
1796 return mNoBinding;
1797 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001798
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001799 if (!mSystemReady) {
1800 // If the system is not yet ready, we shouldn't be running third
1801 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001802 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1803 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1807 if (info == null) {
1808 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001810
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001811 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1814 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001815 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1816 com.android.internal.R.string.input_method_binding_label);
1817 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1818 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001819 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001820 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1821 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 mLastBindTime = SystemClock.uptimeMillis();
1823 mHaveConnection = true;
1824 mCurId = info.getId();
1825 mCurToken = new Binder();
1826 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001827 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001828 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 } catch (RemoteException e) {
1830 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001831 return new InputBindResult(null, null, mCurId, mCurSeq,
1832 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 } else {
1834 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001835 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 + mCurIntent);
1837 }
1838 return null;
1839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001840
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001841 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001842 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001843 IInputMethodClient client, IInputContext inputContext,
1844 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001845 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001846 if (!calledFromValidUser()) {
1847 return null;
1848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001850 if (DEBUG) {
1851 Slog.v(TAG, "startInput: reason="
1852 + InputMethodClient.getStartInputReason(startInputReason)
1853 + " client = " + client.asBinder()
1854 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001855 + " missingMethods="
1856 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001857 + " attribute=" + attribute
1858 + " controlFlags=#" + Integer.toHexString(controlFlags));
1859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 final long ident = Binder.clearCallingIdentity();
1861 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001862 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1863 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 } finally {
1865 Binder.restoreCallingIdentity(ident);
1866 }
1867 }
1868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001869
satoke7c6998e2011-06-03 17:57:59 +09001870 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 public void finishInput(IInputMethodClient client) {
1872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001873
satoke7c6998e2011-06-03 17:57:59 +09001874 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 public void onServiceConnected(ComponentName name, IBinder service) {
1876 synchronized (mMethodMap) {
1877 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1878 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001879 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001880 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001881 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001882 return;
1883 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001884 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001885 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1886 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001888 clearClientSessionLocked(mCurClient);
1889 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891 }
1892 }
1893 }
1894
Jeff Brownc28867a2013-03-26 15:42:39 -07001895 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1896 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 synchronized (mMethodMap) {
1898 if (mCurMethod != null && method != null
1899 && mCurMethod.asBinder() == method.asBinder()) {
1900 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001901 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001903 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001904 InputBindResult res = attachNewInputLocked(
1905 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 if (res.method != null) {
1907 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001908 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001910 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
1912 }
1913 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001914
1915 // Session abandoned. Close its associated input channel.
1916 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001918
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001919 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001920 if (mVisibleBound) {
1921 mContext.unbindService(mVisibleConnection);
1922 mVisibleBound = false;
1923 }
1924
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001925 if (mHaveConnection) {
1926 mContext.unbindService(this);
1927 mHaveConnection = false;
1928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001929
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001930 if (mCurToken != null) {
1931 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001932 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001933 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001934 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001935 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001936 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001937 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001938 } catch (RemoteException e) {
1939 }
1940 mCurToken = null;
1941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001942
The Android Open Source Project10592532009-03-18 17:39:46 -07001943 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001944 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001946
Yohei Yukawa33e81792015-11-17 21:14:42 -08001947 void resetCurrentMethodAndClient(
1948 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001949 mCurMethodId = null;
1950 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001951 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001952 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001953
1954 void requestClientSessionLocked(ClientState cs) {
1955 if (!cs.sessionRequested) {
1956 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1957 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1958 cs.sessionRequested = true;
1959 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1960 MSG_CREATE_SESSION, mCurMethod, channels[1],
1961 new MethodCallback(this, mCurMethod, channels[0])));
1962 }
1963 }
1964
1965 void clearClientSessionLocked(ClientState cs) {
1966 finishSessionLocked(cs.curSession);
1967 cs.curSession = null;
1968 cs.sessionRequested = false;
1969 }
1970
1971 private void finishSessionLocked(SessionState sessionState) {
1972 if (sessionState != null) {
1973 if (sessionState.session != null) {
1974 try {
1975 sessionState.session.finishSession();
1976 } catch (RemoteException e) {
1977 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001978 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001979 }
1980 sessionState.session = null;
1981 }
1982 if (sessionState.channel != null) {
1983 sessionState.channel.dispose();
1984 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001985 }
1986 }
1987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001988
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001989 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 if (mCurMethod != null) {
1991 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001992 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001994
Jeff Brownc28867a2013-03-26 15:42:39 -07001995 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001996 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 mCurMethod = null;
1998 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001999 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002000 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002001 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002002 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002004
satoke7c6998e2011-06-03 17:57:59 +09002005 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002007 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2008 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2009 // which is irregular but can eventually happen for everyone just by continuing using the
2010 // device. Thus it is important to make sure that all the internal states are properly
2011 // refreshed when this method is called back. Running
2012 // adb install -r <APK that implements the current IME>
2013 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002015 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 + " mCurIntent=" + mCurIntent);
2017 if (mCurMethod != null && mCurIntent != null
2018 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002019 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 // We consider this to be a new bind attempt, since the system
2021 // should now try to restart the service for us.
2022 mLastBindTime = SystemClock.uptimeMillis();
2023 mShowRequested = mInputShown;
2024 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002025 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 }
2027 }
2028 }
2029
satokf9f01002011-05-19 21:31:50 +09002030 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002032 synchronized (mMethodMap) {
2033 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002034 return;
2035 }
2036 final long ident = Binder.clearCallingIdentity();
2037 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002039 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002040 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002041 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002044 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002045 CharSequence contentDescription = null;
2046 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002047 // Use PackageManager to load label
2048 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002049 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002050 mIPackageManager.getApplicationInfo(packageName, 0,
2051 mSettings.getCurrentUserId()));
2052 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002053 /* ignore */
2054 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002055 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002056 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002057 contentDescription != null
2058 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002059 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002062 } finally {
2063 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
2066 }
2067
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002068 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002069 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002070 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09002071 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002072 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002073 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002074 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2075 // When physical keyboard is attached, we show the ime switcher (or notification if
2076 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2077 // exists in the IME switcher dialog. Might be OK to remove this condition once
2078 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2079 return true;
2080 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002081 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2082 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002083 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002084
2085 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2086 final int N = imis.size();
2087 if (N > 2) return true;
2088 if (N < 1) return false;
2089 int nonAuxCount = 0;
2090 int auxCount = 0;
2091 InputMethodSubtype nonAuxSubtype = null;
2092 InputMethodSubtype auxSubtype = null;
2093 for(int i = 0; i < N; ++i) {
2094 final InputMethodInfo imi = imis.get(i);
2095 final List<InputMethodSubtype> subtypes =
2096 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2097 final int subtypeCount = subtypes.size();
2098 if (subtypeCount == 0) {
2099 ++nonAuxCount;
2100 } else {
2101 for (int j = 0; j < subtypeCount; ++j) {
2102 final InputMethodSubtype subtype = subtypes.get(j);
2103 if (!subtype.isAuxiliary()) {
2104 ++nonAuxCount;
2105 nonAuxSubtype = subtype;
2106 } else {
2107 ++auxCount;
2108 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002109 }
2110 }
satok7cfc0ed2011-06-20 21:29:36 +09002111 }
2112 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002113 if (nonAuxCount > 1 || auxCount > 1) {
2114 return true;
2115 } else if (nonAuxCount == 1 && auxCount == 1) {
2116 if (nonAuxSubtype != null && auxSubtype != null
2117 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2118 || auxSubtype.overridesImplicitlyEnabledSubtype()
2119 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2120 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2121 return false;
2122 }
2123 return true;
2124 }
2125 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002126 }
2127
John Spurlocke0980502013-10-25 11:59:29 -04002128 private boolean isKeyguardLocked() {
2129 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2130 }
2131
satokdbf29502011-08-25 15:28:23 +09002132 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002133 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002134 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2135 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002136 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002137 return;
2138 }
2139
Yohei Yukawa69e68022017-02-13 12:04:50 -08002140 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002141 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002142 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002143 mImeWindowVis = vis;
2144 mBackDisposition = backDisposition;
2145 updateSystemUiLocked(token, vis, backDisposition);
2146 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002147 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2148 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawabbb10e82017-02-17 20:13:52 -08002149 info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002150 }
2151
2152 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2153 synchronized (mMethodMap) {
2154 updateSystemUiLocked(token, vis, backDisposition);
2155 }
2156 }
2157
2158 // Caution! This method is called in this class. Handle multi-user carefully
2159 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2160 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002161 return;
2162 }
2163
2164 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2165 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002166 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002167 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002168 // apply policy for binder calls
2169 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2170 vis = 0;
satok06487a52010-10-29 11:37:18 +09002171 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002172 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2173 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2174 if (mStatusBar != null) {
2175 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2176 needsToShowImeSwitcher);
2177 }
2178 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2179 if (imi != null && needsToShowImeSwitcher) {
2180 // Used to load label
2181 final CharSequence title = mRes.getText(
2182 com.android.internal.R.string.select_input_method);
2183 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2184 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002185 mImeSwitcherNotification.setContentTitle(title)
2186 .setContentText(summary)
2187 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002188 try {
2189 if ((mNotificationManager != null)
2190 && !mIWindowManager.hasNavigationBar()) {
2191 if (DEBUG) {
2192 Slog.d(TAG, "--- show notification: label = " + summary);
2193 }
2194 mNotificationManager.notifyAsUser(null,
2195 com.android.internal.R.string.select_input_method,
2196 mImeSwitcherNotification.build(), UserHandle.ALL);
2197 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002198 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002199 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002200 }
2201 } else {
2202 if (mNotificationShown && mNotificationManager != null) {
2203 if (DEBUG) {
2204 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002205 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002206 mNotificationManager.cancelAsUser(null,
2207 com.android.internal.R.string.select_input_method, UserHandle.ALL);
2208 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002209 }
satok06487a52010-10-29 11:37:18 +09002210 }
2211 } finally {
2212 Binder.restoreCallingIdentity(ident);
2213 }
2214 }
2215
satoke7c6998e2011-06-03 17:57:59 +09002216 @Override
satokf9f01002011-05-19 21:31:50 +09002217 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002218 if (!calledFromValidUser()) {
2219 return;
2220 }
satokf9f01002011-05-19 21:31:50 +09002221 synchronized (mMethodMap) {
2222 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2223 for (int i = 0; i < spans.length; ++i) {
2224 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002225 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002226 mSecureSuggestionSpans.put(ss, currentImi);
2227 }
2228 }
2229 }
2230 }
2231
satoke7c6998e2011-06-03 17:57:59 +09002232 @Override
satokf9f01002011-05-19 21:31:50 +09002233 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002234 if (!calledFromValidUser()) {
2235 return false;
2236 }
satokf9f01002011-05-19 21:31:50 +09002237 synchronized (mMethodMap) {
2238 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2239 // TODO: Do not send the intent if the process of the targetImi is already dead.
2240 if (targetImi != null) {
2241 final String[] suggestions = span.getSuggestions();
2242 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002243 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002244 final Intent intent = new Intent();
2245 // Ensures that only a class in the original IME package will receive the
2246 // notification.
satok42c5a162011-05-26 16:46:14 +09002247 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002248 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2249 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2250 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2251 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002252 final long ident = Binder.clearCallingIdentity();
2253 try {
2254 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2255 } finally {
2256 Binder.restoreCallingIdentity(ident);
2257 }
satokf9f01002011-05-19 21:31:50 +09002258 return true;
2259 }
2260 }
2261 return false;
2262 }
2263
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002264 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002265 updateInputMethodsFromSettingsLocked(enabledMayChange);
2266 updateKeyboardFromSettingsLocked();
2267 }
2268
2269 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002270 if (enabledMayChange) {
2271 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2272 for (int i=0; i<enabled.size(); i++) {
2273 // We allow the user to select "disabled until used" apps, so if they
2274 // are enabling one of those here we now need to make it enabled.
2275 InputMethodInfo imm = enabled.get(i);
2276 try {
2277 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2278 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2279 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002280 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002281 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002282 if (DEBUG) {
2283 Slog.d(TAG, "Update state(" + imm.getId()
2284 + "): DISABLED_UNTIL_USED -> DEFAULT");
2285 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002286 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2287 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002288 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2289 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002290 }
2291 } catch (RemoteException e) {
2292 }
2293 }
2294 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002295 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2296 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2297 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2298 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002299 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002300 // There is no input method selected, try to choose new applicable input method.
2301 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002302 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002303 }
2304 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002306 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002308 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002309 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 }
satokf3db1af2010-11-23 13:34:33 +09002311 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002312 } else {
2313 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002314 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002316 // Here is not the perfect place to reset the switching controller. Ideally
2317 // mSwitchingController and mSettings should be able to share the same state.
2318 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2319 // the same enabled IMEs list.
2320 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002321
2322 }
2323
2324 public void updateKeyboardFromSettingsLocked() {
2325 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2326 if (mSwitchingDialog != null
2327 && mSwitchingDialogTitleView != null
2328 && mSwitchingDialog.isShowing()) {
2329 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2330 com.android.internal.R.id.hard_keyboard_switch);
2331 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002334
Yohei Yukawab097b822015-12-01 10:43:08 -08002335 private void notifyInputMethodSubtypeChanged(final int userId,
2336 @Nullable final InputMethodInfo inputMethodInfo,
2337 @Nullable final InputMethodSubtype subtype) {
2338 final InputManagerInternal inputManagerInternal =
2339 LocalServices.getService(InputManagerInternal.class);
2340 if (inputManagerInternal != null) {
2341 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2342 }
2343 }
2344
satokab751aa2010-09-14 19:17:36 +09002345 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 InputMethodInfo info = mMethodMap.get(id);
2347 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002348 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002350
satokd81e9502012-05-21 12:58:45 +09002351 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002353 final int subtypeCount = info.getSubtypeCount();
2354 if (subtypeCount <= 0) {
2355 return;
satokcd7cd292010-11-20 15:46:23 +09002356 }
satokd81e9502012-05-21 12:58:45 +09002357 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2358 final InputMethodSubtype newSubtype;
2359 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2360 newSubtype = info.getSubtypeAt(subtypeId);
2361 } else {
2362 // If subtype is null, try to find the most applicable one from
2363 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002364 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002365 }
2366 if (newSubtype == null || oldSubtype == null) {
2367 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2368 + ", new subtype = " + newSubtype);
2369 return;
2370 }
2371 if (newSubtype != oldSubtype) {
2372 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2373 if (mCurMethod != null) {
2374 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002375 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002376 mCurMethod.changeInputMethodSubtype(newSubtype);
2377 } catch (RemoteException e) {
2378 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002379 return;
satokab751aa2010-09-14 19:17:36 +09002380 }
2381 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002382 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 return;
2385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002386
satokd81e9502012-05-21 12:58:45 +09002387 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 final long ident = Binder.clearCallingIdentity();
2389 try {
satokab751aa2010-09-14 19:17:36 +09002390 // Set a subtype to this input method.
2391 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002392 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2393 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2394 // because mCurMethodId is stored as a history in
2395 // setSelectedInputMethodAndSubtypeLocked().
2396 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002398 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002400 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002402 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002404 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 } finally {
2406 Binder.restoreCallingIdentity(ident);
2407 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002408
2409 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2410 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002412
satok42c5a162011-05-26 16:46:14 +09002413 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002414 public boolean showSoftInput(IInputMethodClient client, int flags,
2415 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002416 if (!calledFromValidUser()) {
2417 return false;
2418 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002419 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 long ident = Binder.clearCallingIdentity();
2421 try {
2422 synchronized (mMethodMap) {
2423 if (mCurClient == null || client == null
2424 || mCurClient.client.asBinder() != client.asBinder()) {
2425 try {
2426 // We need to check if this is the current client with
2427 // focus in the window manager, to allow this call to
2428 // be made before input is started in it.
2429 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002430 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002431 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 }
2433 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002434 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
2436 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002437
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002438 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002439 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 }
2441 } finally {
2442 Binder.restoreCallingIdentity(ident);
2443 }
2444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002445
The Android Open Source Project4df24232009-03-05 14:34:35 -08002446 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002448 if (mAccessibilityRequestingNoSoftKeyboard) {
2449 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
Anna Galusza9b278112016-01-04 11:37:37 -08002451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2453 mShowExplicitlyRequested = true;
2454 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002455 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2456 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002458
Dianne Hackborncc278702009-09-02 23:07:23 -07002459 if (!mSystemReady) {
2460 return false;
2461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002462
The Android Open Source Project4df24232009-03-05 14:34:35 -08002463 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002465 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002466 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2467 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2468 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002470 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002471 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002472 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002473 | Context.BIND_TREAT_LIKE_ACTIVITY
2474 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002475 mVisibleBound = true;
2476 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002477 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002479 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 // The client has asked to have the input method shown, but
2481 // we have been sitting here too long with a connection to the
2482 // service and no interface received, so let's disconnect/connect
2483 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002484 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002486 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002488 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002489 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002490 } else {
2491 if (DEBUG) {
2492 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2493 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002496
The Android Open Source Project4df24232009-03-05 14:34:35 -08002497 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002499
satok42c5a162011-05-26 16:46:14 +09002500 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002501 public boolean hideSoftInput(IInputMethodClient client, int flags,
2502 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002503 if (!calledFromValidUser()) {
2504 return false;
2505 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002506 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 long ident = Binder.clearCallingIdentity();
2508 try {
2509 synchronized (mMethodMap) {
2510 if (mCurClient == null || client == null
2511 || mCurClient.client.asBinder() != client.asBinder()) {
2512 try {
2513 // We need to check if this is the current client with
2514 // focus in the window manager, to allow this call to
2515 // be made before input is started in it.
2516 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002517 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2518 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002519 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 }
2521 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002522 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 }
2524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002525
Joe Onorato8a9b2202010-02-26 18:56:32 -08002526 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002527 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
2529 } finally {
2530 Binder.restoreCallingIdentity(ident);
2531 }
2532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002533
The Android Open Source Project4df24232009-03-05 14:34:35 -08002534 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2536 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002537 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 -08002538 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 }
2540 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002541 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 -08002542 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002544
2545 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2546 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2547 // to be updated with the new value sent from IME process. Even in such a transient state
2548 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2549 // application process as a valid request, and have even promised such a behavior with CTS
2550 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2551 // IMMS#InputShown indicates that the software keyboard is shown.
2552 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2553 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2554 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002555 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002556 if (shouldHideSoftInput) {
2557 // The IME will report its visible state again after the following message finally
2558 // delivered to the IME process as an IPC. Hence the inconsistency between
2559 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2560 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002561 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2562 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2563 res = true;
2564 } else {
2565 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002567 if (mHaveConnection && mVisibleBound) {
2568 mContext.unbindService(mVisibleConnection);
2569 mVisibleBound = false;
2570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 mInputShown = false;
2572 mShowRequested = false;
2573 mShowExplicitlyRequested = false;
2574 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002575 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002577
satok42c5a162011-05-26 16:46:14 +09002578 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002579 public InputBindResult startInputOrWindowGainedFocus(
2580 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2581 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002582 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002583 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002584 if (windowToken != null) {
2585 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002586 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002587 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002588 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2589 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002590 }
2591 }
2592
2593 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002594 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002595 IInputMethodClient client, IBinder windowToken, int controlFlags,
2596 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002597 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2598 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002599 // Needs to check the validity before clearing calling identity
2600 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002601 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 long ident = Binder.clearCallingIdentity();
2603 try {
2604 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002605 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2606 + InputMethodClient.getStartInputReason(startInputReason)
2607 + " client=" + client.asBinder()
2608 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002609 + " missingMethods="
2610 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002611 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002612 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002613 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002614 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002615
Dianne Hackborn7663d802012-02-24 13:08:49 -08002616 ClientState cs = mClients.get(client.asBinder());
2617 if (cs == null) {
2618 throw new IllegalArgumentException("unknown client "
2619 + client.asBinder());
2620 }
2621
2622 try {
2623 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2624 // Check with the window manager to make sure this client actually
2625 // has a window with focus. If not, reject. This is thread safe
2626 // because if the focus changes some time before or after, the
2627 // next client receiving focus that has any interest in input will
2628 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002629 if (DEBUG) {
2630 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2631 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2632 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002633 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002635 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002637
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002638 if (!calledFromValidUser) {
2639 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2640 Slog.w(TAG, "If you want to interect with IME, you need "
2641 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2642 hideCurrentInputLocked(0, null);
2643 return null;
2644 }
2645
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002646 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002647 if (DEBUG) {
2648 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2649 + " attribute=" + attribute + ", token = " + windowToken);
2650 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002651 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002652 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002653 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002654 }
2655 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002656 }
2657 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002658 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002659 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002660
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002661 // Should we auto-show the IME even if the caller has not
2662 // specified what should be done with it?
2663 // We only do this automatically if the window can resize
2664 // to accommodate the IME (so what the user sees will give
2665 // them good context without input information being obscured
2666 // by the IME) or if running on a large screen where there
2667 // is more room for the target window + IME.
2668 final boolean doAutoShow =
2669 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2670 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2671 || mRes.getConfiguration().isLayoutSizeAtLeast(
2672 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002673 final boolean isTextEditor =
2674 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2675
2676 // We want to start input before showing the IME, but after closing
2677 // it. We want to do this after closing it to help the IME disappear
2678 // more quickly (not get stuck behind it initializing itself for the
2679 // new focused input, even if its window wants to hide the IME).
2680 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2683 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002684 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2686 // There is no focus view, and this window will
2687 // be behind any soft input window, so hide the
2688 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002689 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002690 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002692 } else if (isTextEditor && doAutoShow && (softInputMode &
2693 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 // There is a focus view, and we are navigating forward
2695 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002696 // We only do this automatically if the window can resize
2697 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002698 // them good context without input information being obscured
2699 // by the IME) or if running on a large screen where there
2700 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002701 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002702 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002703 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002704 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002705 didStart = true;
2706 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002707 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 }
2709 break;
2710 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2711 // Do nothing.
2712 break;
2713 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2714 if ((softInputMode &
2715 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002716 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002717 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 }
2719 break;
2720 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002721 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002722 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 break;
2724 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2725 if ((softInputMode &
2726 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002727 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002728 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002729 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002730 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002731 didStart = true;
2732 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002733 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 }
2735 break;
2736 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002737 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002738 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002739 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002740 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002741 didStart = true;
2742 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002743 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 break;
2745 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002746
2747 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002748 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002749 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 }
2752 } finally {
2753 Binder.restoreCallingIdentity(ident);
2754 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002755
2756 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002758
satok42c5a162011-05-26 16:46:14 +09002759 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002760 public void showInputMethodPickerFromClient(
2761 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002762 if (!calledFromValidUser()) {
2763 return;
2764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 synchronized (mMethodMap) {
2766 if (mCurClient == null || client == null
2767 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002768 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002769 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 }
2771
satok440aab52010-11-25 09:43:11 +09002772 // Always call subtype picker, because subtype picker is a superset of input method
2773 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002774 mHandler.sendMessage(mCaller.obtainMessageI(
2775 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002776 }
2777 }
2778
satok42c5a162011-05-26 16:46:14 +09002779 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002781 if (!calledFromValidUser()) {
2782 return;
2783 }
satok28203512010-11-24 11:06:49 +09002784 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2785 }
2786
satok42c5a162011-05-26 16:46:14 +09002787 @Override
satok28203512010-11-24 11:06:49 +09002788 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002789 if (!calledFromValidUser()) {
2790 return;
2791 }
satok28203512010-11-24 11:06:49 +09002792 synchronized (mMethodMap) {
2793 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002794 setInputMethodWithSubtypeIdLocked(token, id,
2795 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2796 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002797 } else {
2798 setInputMethod(token, id);
2799 }
2800 }
satokab751aa2010-09-14 19:17:36 +09002801 }
2802
satok42c5a162011-05-26 16:46:14 +09002803 @Override
satokb416a712010-11-25 20:42:14 +09002804 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002805 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002806 if (!calledFromValidUser()) {
2807 return;
2808 }
satokb416a712010-11-25 20:42:14 +09002809 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002810 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2811 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002812 }
2813 }
2814
satok4fc87d62011-05-20 16:13:43 +09002815 @Override
satok735cf382010-11-11 20:40:09 +09002816 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002817 if (!calledFromValidUser()) {
2818 return false;
2819 }
satok735cf382010-11-11 20:40:09 +09002820 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002821 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002822 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002823 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002824 lastImi = mMethodMap.get(lastIme.first);
2825 } else {
2826 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002827 }
satok4fc87d62011-05-20 16:13:43 +09002828 String targetLastImiId = null;
2829 int subtypeId = NOT_A_SUBTYPE_ID;
2830 if (lastIme != null && lastImi != null) {
2831 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002832 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002833 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2834 : mCurrentSubtype.hashCode();
2835 // If the last IME is the same as the current IME and the last subtype is not
2836 // defined, there is no need to switch to the last IME.
2837 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2838 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002839 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002840 }
2841 }
2842
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002843 if (TextUtils.isEmpty(targetLastImiId)
2844 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002845 // This is a safety net. If the currentSubtype can't be added to the history
2846 // and the framework couldn't find the last ime, we will make the last ime be
2847 // the most applicable enabled keyboard subtype of the system imes.
2848 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2849 if (enabled != null) {
2850 final int N = enabled.size();
2851 final String locale = mCurrentSubtype == null
2852 ? mRes.getConfiguration().locale.toString()
2853 : mCurrentSubtype.getLocale();
2854 for (int i = 0; i < N; ++i) {
2855 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002856 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002857 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002858 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2859 InputMethodUtils.getSubtypes(imi),
2860 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002861 if (keyboardSubtype != null) {
2862 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002863 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002864 imi, keyboardSubtype.hashCode());
2865 if(keyboardSubtype.getLocale().equals(locale)) {
2866 break;
2867 }
2868 }
2869 }
2870 }
2871 }
2872 }
2873
2874 if (!TextUtils.isEmpty(targetLastImiId)) {
2875 if (DEBUG) {
2876 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2877 + ", from: " + mCurMethodId + ", " + subtypeId);
2878 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002879 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002880 return true;
2881 } else {
2882 return false;
2883 }
satok735cf382010-11-11 20:40:09 +09002884 }
2885 }
2886
satoke7c6998e2011-06-03 17:57:59 +09002887 @Override
satok688bd472012-02-09 20:09:17 +09002888 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002889 if (!calledFromValidUser()) {
2890 return false;
2891 }
satok688bd472012-02-09 20:09:17 +09002892 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002893 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002894 return false;
2895 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002896 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002897 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2898 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002899 if (nextSubtype == null) {
2900 return false;
2901 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002902 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2903 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002904 return true;
2905 }
2906 }
2907
2908 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002909 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2910 if (!calledFromValidUser()) {
2911 return false;
2912 }
2913 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002914 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002915 return false;
2916 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002917 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002918 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2919 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002920 if (nextSubtype == null) {
2921 return false;
2922 }
2923 return true;
2924 }
2925 }
2926
2927 @Override
satok68f1b782011-04-11 14:26:04 +09002928 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002929 if (!calledFromValidUser()) {
2930 return null;
2931 }
satok68f1b782011-04-11 14:26:04 +09002932 synchronized (mMethodMap) {
2933 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2934 // TODO: Handle the case of the last IME with no subtypes
2935 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2936 || TextUtils.isEmpty(lastIme.second)) return null;
2937 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2938 if (lastImi == null) return null;
2939 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002940 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002941 final int lastSubtypeId =
2942 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002943 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2944 return null;
2945 }
2946 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002947 } catch (NumberFormatException e) {
2948 return null;
2949 }
2950 }
2951 }
2952
satoke7c6998e2011-06-03 17:57:59 +09002953 @Override
satokee5e77c2011-09-02 18:50:15 +09002954 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002955 if (!calledFromValidUser()) {
2956 return;
2957 }
satok91e88122011-07-18 11:11:42 +09002958 // By this IPC call, only a process which shares the same uid with the IME can add
2959 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002960 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002961 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002962 if (!mSystemReady) {
2963 return;
2964 }
satok91e88122011-07-18 11:11:42 +09002965 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002966 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002967 final String[] packageInfos;
2968 try {
2969 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2970 } catch (RemoteException e) {
2971 Slog.e(TAG, "Failed to get package infos");
2972 return;
2973 }
satok91e88122011-07-18 11:11:42 +09002974 if (packageInfos != null) {
2975 final int packageNum = packageInfos.length;
2976 for (int i = 0; i < packageNum; ++i) {
2977 if (packageInfos[i].equals(imi.getPackageName())) {
2978 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002979 final long ident = Binder.clearCallingIdentity();
2980 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002981 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002982 } finally {
2983 Binder.restoreCallingIdentity(ident);
2984 }
satokee5e77c2011-09-02 18:50:15 +09002985 return;
satok91e88122011-07-18 11:11:42 +09002986 }
2987 }
2988 }
satoke7c6998e2011-06-03 17:57:59 +09002989 }
satokee5e77c2011-09-02 18:50:15 +09002990 return;
satoke7c6998e2011-06-03 17:57:59 +09002991 }
2992
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002993 @Override
2994 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002995 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002996 }
2997
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002998 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002999 public void clearLastInputMethodWindowForTransition(IBinder token) {
3000 if (!calledFromValidUser()) {
3001 return;
3002 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003003 synchronized (mMethodMap) {
3004 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003005 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003006 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003007 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003008 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003009 }
3010
3011 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003012 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003013 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003014 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003015 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003016 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003017 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3018 if (DEBUG) {
3019 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3020 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3021 + " actual: " + sequenceNumber);
3022 }
3023 return;
3024 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003025 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3026 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003027 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003028 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003029 }
3030 }
3031
satok28203512010-11-24 11:06:49 +09003032 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003034 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3035 }
3036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003038 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3039 if (token == null) {
3040 if (mContext.checkCallingOrSelfPermission(
3041 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3042 != PackageManager.PERMISSION_GRANTED) {
3043 throw new SecurityException(
3044 "Using null token requires permission "
3045 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003047 } else if (mCurToken != token) {
3048 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3049 + " token: " + token);
3050 return;
3051 }
3052
3053 final long ident = Binder.clearCallingIdentity();
3054 try {
3055 setInputMethodLocked(id, subtypeId);
3056 } finally {
3057 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
3059 }
3060
satok42c5a162011-05-26 16:46:14 +09003061 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003063 if (!calledFromValidUser()) {
3064 return;
3065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003067 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068 return;
3069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 long ident = Binder.clearCallingIdentity();
3071 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003072 hideCurrentInputLocked(flags, null);
3073 } finally {
3074 Binder.restoreCallingIdentity(ident);
3075 }
3076 }
3077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003078
satok42c5a162011-05-26 16:46:14 +09003079 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003080 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003081 if (!calledFromValidUser()) {
3082 return;
3083 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003084 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003085 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003086 return;
3087 }
3088 long ident = Binder.clearCallingIdentity();
3089 try {
3090 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 } finally {
3092 Binder.restoreCallingIdentity(ident);
3093 }
3094 }
3095 }
3096
3097 void setEnabledSessionInMainThread(SessionState session) {
3098 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003099 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003101 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003102 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 } catch (RemoteException e) {
3104 }
3105 }
3106 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003107 if (mEnabledSession != null && mEnabledSession.session != null) {
3108 try {
3109 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3110 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3111 } catch (RemoteException e) {
3112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 }
3114 }
3115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003116
satok42c5a162011-05-26 16:46:14 +09003117 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003119 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003121 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003122 final boolean showAuxSubtypes;
3123 switch (msg.arg1) {
3124 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3125 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3126 // implemented so that auxiliary subtypes will be excluded when the soft
3127 // keyboard is invisible.
3128 showAuxSubtypes = mInputShown;
3129 break;
3130 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3131 showAuxSubtypes = true;
3132 break;
3133 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3134 showAuxSubtypes = false;
3135 break;
3136 default:
3137 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3138 return false;
3139 }
3140 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003141 return true;
3142
satok47a44912010-10-06 16:03:58 +09003143 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003144 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003145 return true;
3146
3147 case MSG_SHOW_IM_CONFIG:
3148 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003149 return true;
3150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 case MSG_UNBIND_INPUT:
3154 try {
3155 ((IInputMethod)msg.obj).unbindInput();
3156 } catch (RemoteException e) {
3157 // There is nothing interesting about the method dying.
3158 }
3159 return true;
3160 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003161 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 try {
3163 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3164 } catch (RemoteException e) {
3165 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003166 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 return true;
3168 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003169 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003171 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003172 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003173 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 } catch (RemoteException e) {
3175 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003176 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 return true;
3178 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003179 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003181 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003182 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003183 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 } catch (RemoteException e) {
3185 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003186 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003188 case MSG_HIDE_CURRENT_INPUT_METHOD:
3189 synchronized (mMethodMap) {
3190 hideCurrentInputLocked(0, null);
3191 }
3192 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003194 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003196 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3198 } catch (RemoteException e) {
3199 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003200 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003202 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003203 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003204 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003205 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003207 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003209 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003210 // Dispose the channel if the input method is not local to this process
3211 // because the remote proxy will get its own copy when unparceled.
3212 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003213 channel.dispose();
3214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003216 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003220
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003221 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003222 final int missingMethods = msg.arg1;
3223 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003224 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003225 final IBinder startInputToken = (IBinder) args.arg1;
3226 final SessionState session = (SessionState) args.arg2;
3227 final IInputContext inputContext = (IInputContext) args.arg3;
3228 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003231 session.method.startInput(startInputToken, inputContext, missingMethods,
3232 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 } catch (RemoteException e) {
3234 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003235 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003240
Yohei Yukawa33e81792015-11-17 21:14:42 -08003241 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003243 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 } catch (RemoteException e) {
3245 // There is nothing interesting about the last client dying.
3246 }
3247 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003248 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003249 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003250 IInputMethodClient client = (IInputMethodClient)args.arg1;
3251 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003253 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003255 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003256 } finally {
3257 // Dispose the channel if the input method is not local to this process
3258 // because the remote proxy will get its own copy when unparceled.
3259 if (res.channel != null && Binder.isProxy(client)) {
3260 res.channel.dispose();
3261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003263 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003265 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003266 case MSG_SET_ACTIVE:
3267 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003268 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003269 } catch (RemoteException e) {
3270 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3271 + ((ClientState)msg.obj).pid + " uid "
3272 + ((ClientState)msg.obj).uid);
3273 }
3274 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003275 case MSG_SET_INTERACTIVE:
3276 handleSetInteractive(msg.arg1 != 0);
3277 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003278 case MSG_SWITCH_IME:
3279 handleSwitchInputMethod(msg.arg1 != 0);
3280 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003281 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3282 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003283 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003284 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003285 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003286 } catch (RemoteException e) {
3287 Slog.w(TAG, "Got RemoteException sending "
3288 + "setUserActionNotificationSequenceNumber("
3289 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003290 + clientState.pid + " uid "
3291 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003292 }
3293 return true;
3294 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003295 case MSG_REPORT_FULLSCREEN_MODE: {
3296 final boolean fullscreen = msg.arg1 != 0;
3297 final ClientState clientState = (ClientState)msg.obj;
3298 try {
3299 clientState.client.reportFullscreenMode(fullscreen);
3300 } catch (RemoteException e) {
3301 Slog.w(TAG, "Got RemoteException sending "
3302 + "reportFullscreen(" + fullscreen + ") notification to pid="
3303 + clientState.pid + " uid=" + clientState.uid);
3304 }
3305 return true;
3306 }
satok01038492012-04-09 21:08:27 +09003307
3308 // --------------------------------------------------------------
3309 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003310 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003311 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003312 case MSG_SYSTEM_UNLOCK_USER:
3313 final int userId = msg.arg1;
3314 onUnlockUser(userId);
3315 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 }
3317 return false;
3318 }
3319
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003320 private void handleSetInteractive(final boolean interactive) {
3321 synchronized (mMethodMap) {
3322 mIsInteractive = interactive;
3323 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3324
3325 // Inform the current client of the change in active status
3326 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003327 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3328 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3329 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003330 }
3331 }
3332 }
3333
Yohei Yukawaae61f712015-12-09 13:00:10 -08003334 private void handleSwitchInputMethod(final boolean forwardDirection) {
3335 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003336 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003337 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003338 if (nextSubtype == null) {
3339 return;
3340 }
3341 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003342 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3343 if (newInputMethodInfo == null) {
3344 return;
3345 }
3346 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3347 newInputMethodInfo, mCurrentSubtype);
3348 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003349 if (mSubtypeSwitchedByShortCutToast == null) {
3350 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3351 Toast.LENGTH_SHORT);
3352 } else {
3353 mSubtypeSwitchedByShortCutToast.setText(toastText);
3354 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003355 mSubtypeSwitchedByShortCutToast.show();
3356 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003357 }
3358 }
3359
satokdc9ddae2011-10-06 12:22:36 +09003360 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003361 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3362 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003363 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003364 if (DEBUG) {
3365 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3366 }
satok723a27e2010-11-11 14:58:11 +09003367 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003368 return true;
3369 }
3370
3371 return false;
3372 }
3373
Yohei Yukawa94e33302016-02-12 19:37:03 -08003374 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003375 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003376 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003377 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003378 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003379 if (!mSystemReady) {
3380 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3381 return;
3382 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003383 mMethodList.clear();
3384 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003385 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003386 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003387
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003388 // Use for queryIntentServicesAsUser
3389 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003391 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3392 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3393 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003394 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003396 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3397 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003398
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003399 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003400 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 for (int i = 0; i < services.size(); ++i) {
3402 ResolveInfo ri = services.get(i);
3403 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003404 final String imeId = InputMethodInfo.computeId(ri);
3405 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3406 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 + ": it does not require the permission "
3408 + android.Manifest.permission.BIND_INPUT_METHOD);
3409 continue;
3410 }
3411
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003412 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003414 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 try {
satoke7c6998e2011-06-03 17:57:59 +09003416 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003417 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003418 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003419 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420
3421 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003422 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003424 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003425 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 }
3427 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003428
Yohei Yukawac4e44912017-02-09 19:30:22 -08003429 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3430 // negatives when the package state remains to be the same but only the component state is
3431 // changed.
3432 {
3433 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3434 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3435 // conservative, but it seems we cannot use it for now (Issue 35176630).
3436 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3437 new Intent(InputMethod.SERVICE_INTERFACE),
3438 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3439 final int N = allInputMethodServices.size();
3440 for (int i = 0; i < N; ++i) {
3441 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003442 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3443 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003444 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003445 }
3446 }
3447
Yohei Yukawa859df052016-02-17 07:56:46 -08003448 // TODO: The following code should find better place to live.
3449 if (!resetDefaultEnabledIme) {
3450 boolean enabledImeFound = false;
3451 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3452 final int N = enabledImes.size();
3453 for (int i = 0; i < N; ++i) {
3454 final InputMethodInfo imi = enabledImes.get(i);
3455 if (mMethodList.contains(imi)) {
3456 enabledImeFound = true;
3457 break;
3458 }
3459 }
3460 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003461 if (DEBUG) {
3462 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3463 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003464 resetDefaultEnabledIme = true;
3465 resetSelectedInputMethodAndSubtypeLocked("");
3466 }
3467 }
3468
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003469 if (resetDefaultEnabledIme) {
3470 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003471 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003472 final int N = defaultEnabledIme.size();
3473 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003474 final InputMethodInfo imi = defaultEnabledIme.get(i);
3475 if (DEBUG) {
3476 Slog.d(TAG, "--- enable ime = " + imi);
3477 }
3478 setInputMethodEnabledLocked(imi.getId(), true);
3479 }
3480 }
3481
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003482 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003483 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003484 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003485 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3486 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003487 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003488 }
3489 } else {
3490 // Double check that the default IME is certainly enabled.
3491 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003492 }
3493 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003494 // Here is not the perfect place to reset the switching controller. Ideally
3495 // mSwitchingController and mSettings should be able to share the same state.
3496 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3497 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003498 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003502
satok217f5482010-12-15 05:19:19 +09003503 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003504 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003505 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003506 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3507 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003508 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003509 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003510 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003511 final int userId;
3512 synchronized (mMethodMap) {
3513 userId = mSettings.getCurrentUserId();
3514 }
3515 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003516 }
3517
3518 private void showConfigureInputMethods() {
3519 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3520 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3521 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3522 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003523 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003524 }
3525
satok2c93efc2012-04-02 19:33:47 +09003526 private boolean isScreenLocked() {
3527 return mKeyguardManager != null
3528 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3529 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003530
Seigo Nonaka14e13912015-05-06 21:04:13 -07003531 private void showInputMethodMenu(boolean showAuxSubtypes) {
3532 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003535 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003536
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003537 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003538 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003539 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003540
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003541 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003542 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003543 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3544 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003545 if (immis == null || immis.size() == 0) {
3546 return;
3547 }
3548
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003549 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550
satok688bd472012-02-09 20:09:17 +09003551 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003552 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003553 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003554
satokc3690562012-01-10 20:14:43 +09003555 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003556 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003557 if (currentSubtype != null) {
3558 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003559 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3560 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003561 }
3562 }
3563
Ken Wakasa761eb372011-03-04 19:06:18 +09003564 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003565 mIms = new InputMethodInfo[N];
3566 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003567 int checkedItem = 0;
3568 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003569 final ImeSubtypeListItem item = imList.get(i);
3570 mIms[i] = item.mImi;
3571 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003572 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003573 int subtypeId = mSubtypeIds[i];
3574 if ((subtypeId == NOT_A_SUBTYPE_ID)
3575 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3576 || (subtypeId == lastInputMethodSubtypeId)) {
3577 checkedItem = i;
3578 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003581
3582 final Context settingsContext = new ContextThemeWrapper(context,
3583 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3584
3585 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003586 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3587 @Override
3588 public void onCancel(DialogInterface dialog) {
3589 hideInputMethodMenu();
3590 }
3591 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003592
3593 final Context dialogContext = mDialogBuilder.getContext();
3594 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3595 com.android.internal.R.styleable.DialogPreference,
3596 com.android.internal.R.attr.alertDialogStyle, 0);
3597 final Drawable dialogIcon = a.getDrawable(
3598 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3599 a.recycle();
3600
3601 mDialogBuilder.setIcon(dialogIcon);
3602
Yohei Yukawad34e1482016-02-11 08:03:52 -08003603 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003604 final View tv = inflater.inflate(
3605 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3606 mDialogBuilder.setCustomTitle(tv);
3607
3608 // Setup layout for a toggle switch of the hardware keyboard
3609 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003610 mSwitchingDialogTitleView
3611 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003612 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003613 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003614 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003615 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003616 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003617 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3618 @Override
3619 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003620 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003621 // Ensure that the input method dialog is dismissed when changing
3622 // the hardware keyboard state.
3623 hideInputMethodMenu();
3624 }
3625 });
3626
Alan Viverette505e3ab2014-11-24 15:22:11 -08003627 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003628 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3629 final OnClickListener choiceListener = new OnClickListener() {
3630 @Override
3631 public void onClick(final DialogInterface dialog, final int which) {
3632 synchronized (mMethodMap) {
3633 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3634 || mSubtypeIds.length <= which) {
3635 return;
satok01038492012-04-09 21:08:27 +09003636 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003637 final InputMethodInfo im = mIms[which];
3638 int subtypeId = mSubtypeIds[which];
3639 adapter.mCheckedItem = which;
3640 adapter.notifyDataSetChanged();
3641 hideInputMethodMenu();
3642 if (im != null) {
3643 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3644 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003645 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003646 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003648 }
3649 }
3650 };
3651 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003654 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003655 final Window w = mSwitchingDialog.getWindow();
3656 final WindowManager.LayoutParams attrs = w.getAttributes();
3657 w.setType(TYPE_INPUT_METHOD_DIALOG);
3658 // Use an alternate token for the dialog for that window manager can group the token
3659 // with other IME windows based on type vs. grouping based on whichever token happens
3660 // to get selected by the system later on.
3661 attrs.token = mSwitchingDialogToken;
3662 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3663 attrs.setTitle("Select input method");
3664 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003665 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 mSwitchingDialog.show();
3667 }
3668 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003669
Ken Wakasa05dbb652011-08-22 15:22:43 +09003670 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3671 private final LayoutInflater mInflater;
3672 private final int mTextViewResourceId;
3673 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003674 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003675 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3676 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3677 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003678
Ken Wakasa05dbb652011-08-22 15:22:43 +09003679 mTextViewResourceId = textViewResourceId;
3680 mItemsList = itemsList;
3681 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003682 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003683 }
3684
3685 @Override
3686 public View getView(int position, View convertView, ViewGroup parent) {
3687 final View view = convertView != null ? convertView
3688 : mInflater.inflate(mTextViewResourceId, null);
3689 if (position < 0 || position >= mItemsList.size()) return view;
3690 final ImeSubtypeListItem item = mItemsList.get(position);
3691 final CharSequence imeName = item.mImeName;
3692 final CharSequence subtypeName = item.mSubtypeName;
3693 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3694 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3695 if (TextUtils.isEmpty(subtypeName)) {
3696 firstTextView.setText(imeName);
3697 secondTextView.setVisibility(View.GONE);
3698 } else {
3699 firstTextView.setText(subtypeName);
3700 secondTextView.setText(imeName);
3701 secondTextView.setVisibility(View.VISIBLE);
3702 }
3703 final RadioButton radioButton =
3704 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3705 radioButton.setChecked(position == mCheckedItem);
3706 return view;
3707 }
3708 }
3709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003711 synchronized (mMethodMap) {
3712 hideInputMethodMenuLocked();
3713 }
3714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003715
The Android Open Source Project10592532009-03-18 17:39:46 -07003716 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003717 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718
The Android Open Source Project10592532009-03-18 17:39:46 -07003719 if (mSwitchingDialog != null) {
3720 mSwitchingDialog.dismiss();
3721 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003723
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003724 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003725 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003726 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003730
satok42c5a162011-05-26 16:46:14 +09003731 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003733 // TODO: Make this work even for non-current users?
3734 if (!calledFromValidUser()) {
3735 return false;
3736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 synchronized (mMethodMap) {
3738 if (mContext.checkCallingOrSelfPermission(
3739 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3740 != PackageManager.PERMISSION_GRANTED) {
3741 throw new SecurityException(
3742 "Requires permission "
3743 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3744 }
Anna Galusza9b278112016-01-04 11:37:37 -08003745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 long ident = Binder.clearCallingIdentity();
3747 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003748 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 } finally {
3750 Binder.restoreCallingIdentity(ident);
3751 }
3752 }
3753 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003754
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003755 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3756 // Make sure this is a valid input method.
3757 InputMethodInfo imm = mMethodMap.get(id);
3758 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003759 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003760 }
3761
satokd87c2592010-09-29 11:52:06 +09003762 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3763 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003764
satokd87c2592010-09-29 11:52:06 +09003765 if (enabled) {
3766 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3767 if (pair.first.equals(id)) {
3768 // We are enabling this input method, but it is already enabled.
3769 // Nothing to do. The previous state was enabled.
3770 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003771 }
3772 }
satokd87c2592010-09-29 11:52:06 +09003773 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3774 // Previous state was disabled.
3775 return false;
3776 } else {
3777 StringBuilder builder = new StringBuilder();
3778 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3779 builder, enabledInputMethodsList, id)) {
3780 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003781 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003782 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3783 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3784 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003785 }
3786 // Previous state was enabled.
3787 return true;
3788 } else {
3789 // We are disabling the input method but it is already disabled.
3790 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003791 return false;
3792 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003793 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003794 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003795
satok723a27e2010-11-11 14:58:11 +09003796 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3797 boolean setSubtypeOnly) {
3798 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003799 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003800
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003801 mCurUserActionNotificationSequenceNumber =
3802 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3803 if (DEBUG) {
3804 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3805 + mCurUserActionNotificationSequenceNumber);
3806 }
3807
3808 if (mCurClient != null && mCurClient.client != null) {
3809 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3810 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003811 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003812 }
3813
satok723a27e2010-11-11 14:58:11 +09003814 // Set Subtype here
3815 if (imi == null || subtypeId < 0) {
3816 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003817 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003818 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003819 if (subtypeId < imi.getSubtypeCount()) {
3820 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3821 mSettings.putSelectedSubtype(subtype.hashCode());
3822 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003823 } else {
3824 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003825 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003826 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003827 }
satokab751aa2010-09-14 19:17:36 +09003828 }
satok723a27e2010-11-11 14:58:11 +09003829
Yohei Yukawa68645a62016-02-17 07:54:20 -08003830 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003831 // Set InputMethod here
3832 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3833 }
3834 }
3835
3836 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3837 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3838 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3839 // newDefaultIme is empty when there is no candidate for the selected IME.
3840 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3841 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3842 if (subtypeHashCode != null) {
3843 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003844 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003845 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003846 } catch (NumberFormatException e) {
3847 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3848 }
3849 }
3850 }
3851 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003852 }
3853
satok4e4569d2010-11-19 18:45:53 +09003854 // If there are no selected shortcuts, tries finding the most applicable ones.
3855 private Pair<InputMethodInfo, InputMethodSubtype>
3856 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3857 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3858 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003859 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003860 boolean foundInSystemIME = false;
3861
3862 // Search applicable subtype for each InputMethodInfo
3863 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003864 final String imiId = imi.getId();
3865 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3866 continue;
3867 }
satokcd7cd292010-11-20 15:46:23 +09003868 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003869 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003870 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003871 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003872 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003873 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003874 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003875 }
satokdf31ae62011-01-15 06:19:44 +09003876 // 2. Search by the system locale from enabledSubtypes.
3877 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003878 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003879 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003880 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003881 }
satoka86f5e42011-09-02 17:12:42 +09003882 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003883 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003884 final ArrayList<InputMethodSubtype> subtypesForSearch =
3885 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003886 ? InputMethodUtils.getSubtypes(imi)
3887 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003888 // 4. Search by the current subtype's locale from all subtypes.
3889 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003890 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003891 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003892 }
3893 // 5. Search by the system locale from all subtypes.
3894 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003895 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003896 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003897 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003898 }
satokcd7cd292010-11-20 15:46:23 +09003899 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003900 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003901 // The current input method is the most applicable IME.
3902 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003903 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003904 break;
satok7599a7f2010-12-22 13:45:23 +09003905 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003906 // The system input method is 2nd applicable IME.
3907 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003908 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003909 if ((imi.getServiceInfo().applicationInfo.flags
3910 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3911 foundInSystemIME = true;
3912 }
satok4e4569d2010-11-19 18:45:53 +09003913 }
3914 }
3915 }
3916 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003917 if (mostApplicableIMI != null) {
3918 Slog.w(TAG, "Most applicable shortcut input method was:"
3919 + mostApplicableIMI.getId());
3920 if (mostApplicableSubtype != null) {
3921 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3922 + "," + mostApplicableSubtype.getMode() + ","
3923 + mostApplicableSubtype.getLocale());
3924 }
3925 }
satok4e4569d2010-11-19 18:45:53 +09003926 }
satokcd7cd292010-11-20 15:46:23 +09003927 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003928 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003929 } else {
3930 return null;
3931 }
3932 }
3933
satokab751aa2010-09-14 19:17:36 +09003934 /**
3935 * @return Return the current subtype of this input method.
3936 */
satok42c5a162011-05-26 16:46:14 +09003937 @Override
satokab751aa2010-09-14 19:17:36 +09003938 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003939 // TODO: Make this work even for non-current users?
3940 if (!calledFromValidUser()) {
3941 return null;
3942 }
3943 synchronized (mMethodMap) {
3944 return getCurrentInputMethodSubtypeLocked();
3945 }
3946 }
3947
3948 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003949 if (mCurMethodId == null) {
3950 return null;
3951 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003952 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003953 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3954 if (imi == null || imi.getSubtypeCount() == 0) {
3955 return null;
satok4e4569d2010-11-19 18:45:53 +09003956 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003957 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003958 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3959 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003960 if (subtypeId == NOT_A_SUBTYPE_ID) {
3961 // If there are no selected subtypes, the framework will try to find
3962 // the most applicable subtype from explicitly or implicitly enabled
3963 // subtypes.
3964 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003965 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003966 // If there is only one explicitly or implicitly enabled subtype,
3967 // just returns it.
3968 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3969 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3970 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003971 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003972 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003973 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003974 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003975 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003976 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3977 true);
satok4e4569d2010-11-19 18:45:53 +09003978 }
satok3ef8b292010-11-23 06:06:29 +09003979 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003980 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003981 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003982 }
3983 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003984 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003985 }
3986
satok4e4569d2010-11-19 18:45:53 +09003987 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003988 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003989 @Override
satok4e4569d2010-11-19 18:45:53 +09003990 public List getShortcutInputMethodsAndSubtypes() {
3991 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003992 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003993 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003994 // If there are no selected shortcut subtypes, the framework will try to find
3995 // the most applicable subtype from all subtypes whose mode is
3996 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003997 Pair<InputMethodInfo, InputMethodSubtype> info =
3998 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003999 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004000 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004001 ret.add(info.first);
4002 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004003 }
satok3da92232011-01-11 22:46:30 +09004004 return ret;
satokf3db1af2010-11-23 13:34:33 +09004005 }
satokf3db1af2010-11-23 13:34:33 +09004006 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4007 ret.add(imi);
4008 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4009 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004010 }
4011 }
satokf3db1af2010-11-23 13:34:33 +09004012 return ret;
satok4e4569d2010-11-19 18:45:53 +09004013 }
4014 }
4015
satok42c5a162011-05-26 16:46:14 +09004016 @Override
satokb66d2872010-11-10 01:04:04 +09004017 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004018 // TODO: Make this work even for non-current users?
4019 if (!calledFromValidUser()) {
4020 return false;
4021 }
satokb66d2872010-11-10 01:04:04 +09004022 synchronized (mMethodMap) {
4023 if (subtype != null && mCurMethodId != null) {
4024 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004025 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004026 if (subtypeId != NOT_A_SUBTYPE_ID) {
4027 setInputMethodLocked(mCurMethodId, subtypeId);
4028 return true;
4029 }
4030 }
4031 return false;
4032 }
4033 }
4034
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004035 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004036 private static class InputMethodFileManager {
4037 private static final String SYSTEM_PATH = "system";
4038 private static final String INPUT_METHOD_PATH = "inputmethod";
4039 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4040 private static final String NODE_SUBTYPES = "subtypes";
4041 private static final String NODE_SUBTYPE = "subtype";
4042 private static final String NODE_IMI = "imi";
4043 private static final String ATTR_ID = "id";
4044 private static final String ATTR_LABEL = "label";
4045 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004046 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004047 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004048 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004049 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4050 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4051 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004052 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004053 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4054 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004055 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004056 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004057 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004058 if (methodMap == null) {
4059 throw new NullPointerException("methodMap is null");
4060 }
4061 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004062 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004063 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4064 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004065 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004066 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004067 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4068 }
4069 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4070 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4071 if (!subtypeFile.exists()) {
4072 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004073 writeAdditionalInputMethodSubtypes(
4074 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004075 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004076 readAdditionalInputMethodSubtypes(
4077 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004078 }
4079 }
4080
4081 private void deleteAllInputMethodSubtypes(String imiId) {
4082 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004083 mAdditionalSubtypesMap.remove(imiId);
4084 writeAdditionalInputMethodSubtypes(
4085 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004086 }
4087 }
4088
4089 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004090 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004091 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004092 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004093 final int N = additionalSubtypes.length;
4094 for (int i = 0; i < N; ++i) {
4095 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004096 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004097 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004098 } else {
4099 Slog.w(TAG, "Duplicated subtype definition found: "
4100 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004101 }
4102 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004103 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4104 writeAdditionalInputMethodSubtypes(
4105 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004106 }
4107 }
4108
4109 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4110 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004111 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004112 }
4113 }
4114
4115 private static void writeAdditionalInputMethodSubtypes(
4116 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4117 HashMap<String, InputMethodInfo> methodMap) {
4118 // Safety net for the case that this function is called before methodMap is set.
4119 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4120 FileOutputStream fos = null;
4121 try {
4122 fos = subtypesFile.startWrite();
4123 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004124 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004125 out.startDocument(null, true);
4126 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4127 out.startTag(null, NODE_SUBTYPES);
4128 for (String imiId : allSubtypes.keySet()) {
4129 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4130 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4131 continue;
4132 }
4133 out.startTag(null, NODE_IMI);
4134 out.attribute(null, ATTR_ID, imiId);
4135 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4136 final int N = subtypesList.size();
4137 for (int i = 0; i < N; ++i) {
4138 final InputMethodSubtype subtype = subtypesList.get(i);
4139 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004140 if (subtype.hasSubtypeId()) {
4141 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4142 String.valueOf(subtype.getSubtypeId()));
4143 }
satoke7c6998e2011-06-03 17:57:59 +09004144 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4145 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4146 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004147 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4148 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004149 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4150 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4151 out.attribute(null, ATTR_IS_AUXILIARY,
4152 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004153 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4154 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004155 out.endTag(null, NODE_SUBTYPE);
4156 }
4157 out.endTag(null, NODE_IMI);
4158 }
4159 out.endTag(null, NODE_SUBTYPES);
4160 out.endDocument();
4161 subtypesFile.finishWrite(fos);
4162 } catch (java.io.IOException e) {
4163 Slog.w(TAG, "Error writing subtypes", e);
4164 if (fos != null) {
4165 subtypesFile.failWrite(fos);
4166 }
4167 }
4168 }
4169
4170 private static void readAdditionalInputMethodSubtypes(
4171 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4172 if (allSubtypes == null || subtypesFile == null) return;
4173 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004174 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004175 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004176 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004177 int type = parser.getEventType();
4178 // Skip parsing until START_TAG
4179 while ((type = parser.next()) != XmlPullParser.START_TAG
4180 && type != XmlPullParser.END_DOCUMENT) {}
4181 String firstNodeName = parser.getName();
4182 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4183 throw new XmlPullParserException("Xml doesn't start with subtypes");
4184 }
4185 final int depth =parser.getDepth();
4186 String currentImiId = null;
4187 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4188 while (((type = parser.next()) != XmlPullParser.END_TAG
4189 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4190 if (type != XmlPullParser.START_TAG)
4191 continue;
4192 final String nodeName = parser.getName();
4193 if (NODE_IMI.equals(nodeName)) {
4194 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4195 if (TextUtils.isEmpty(currentImiId)) {
4196 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4197 continue;
4198 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004199 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004200 allSubtypes.put(currentImiId, tempSubtypesArray);
4201 } else if (NODE_SUBTYPE.equals(nodeName)) {
4202 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4203 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4204 continue;
4205 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004206 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004207 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004208 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004209 parser.getAttributeValue(null, ATTR_LABEL));
4210 final String imeSubtypeLocale =
4211 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004212 final String languageTag =
4213 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004214 final String imeSubtypeMode =
4215 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4216 final String imeSubtypeExtraValue =
4217 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004218 final boolean isAuxiliary = "1".equals(String.valueOf(
4219 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004220 final boolean isAsciiCapable = "1".equals(String.valueOf(
4221 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004222 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004223 .setSubtypeNameResId(label)
4224 .setSubtypeIconResId(icon)
4225 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004226 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004227 .setSubtypeMode(imeSubtypeMode)
4228 .setSubtypeExtraValue(imeSubtypeExtraValue)
4229 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004230 .setIsAsciiCapable(isAsciiCapable);
4231 final String subtypeIdString =
4232 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4233 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004234 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004235 }
4236 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004237 }
4238 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004239 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4240 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004241 return;
satoke7c6998e2011-06-03 17:57:59 +09004242 }
4243 }
4244 }
4245
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004246 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4247 @NonNull
4248 private final Handler mHandler;
4249
4250 LocalServiceImpl(@NonNull final Handler handler) {
4251 mHandler = handler;
4252 }
4253
4254 @Override
4255 public void setInteractive(boolean interactive) {
4256 // Do everything in handler so as not to block the caller.
4257 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4258 interactive ? 1 : 0, 0));
4259 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004260
4261 @Override
4262 public void switchInputMethod(boolean forwardDirection) {
4263 // Do everything in handler so as not to block the caller.
4264 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4265 forwardDirection ? 1 : 0, 0));
4266 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004267
4268 @Override
4269 public void hideCurrentInputMethod() {
4270 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4271 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4272 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004273 }
4274
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004275 private static String imeWindowStatusToString(final int imeWindowVis) {
4276 final StringBuilder sb = new StringBuilder();
4277 boolean first = true;
4278 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4279 sb.append("Active");
4280 first = false;
4281 }
4282 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4283 if (!first) {
4284 sb.append("|");
4285 }
4286 sb.append("Visible");
4287 }
4288 return sb.toString();
4289 }
4290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004292 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4293 @Nullable Uri contentUri, @Nullable String packageName) {
4294 if (!calledFromValidUser()) {
4295 return null;
4296 }
4297
4298 if (token == null) {
4299 throw new NullPointerException("token");
4300 }
4301 if (packageName == null) {
4302 throw new NullPointerException("packageName");
4303 }
4304 if (contentUri == null) {
4305 throw new NullPointerException("contentUri");
4306 }
4307 final String contentUriScheme = contentUri.getScheme();
4308 if (!"content".equals(contentUriScheme)) {
4309 throw new InvalidParameterException("contentUri must have content scheme");
4310 }
4311
4312 synchronized (mMethodMap) {
4313 final int uid = Binder.getCallingUid();
4314 if (mCurMethodId == null) {
4315 return null;
4316 }
4317 if (mCurToken != token) {
4318 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4319 + " token=" + token);
4320 return null;
4321 }
4322 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4323 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4324 // nature of our system. Let's compare it with our internal record.
4325 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4326 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4327 + mCurAttribute.packageName + " packageName=" + packageName);
4328 return null;
4329 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004330 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004331 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004332 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004333 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004334 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4335 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4336 imeUserId);
4337 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4338 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4339 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4340 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4341 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4342 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4343 // actually allowed to "uid", which is guaranteed to be the IME's one.
4344 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4345 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004346 }
4347 }
4348
4349 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004350 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4351 if (!calledFromValidUser()) {
4352 return;
4353 }
4354 synchronized (mMethodMap) {
4355 if (!calledWithValidToken(token)) {
4356 return;
4357 }
4358 if (mCurClient != null && mCurClient.client != null) {
4359 mInFullscreenMode = fullscreen;
4360 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4361 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4362 }
4363 }
4364 }
4365
4366 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4368 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4369 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4372 + Binder.getCallingPid()
4373 + ", uid=" + Binder.getCallingUid());
4374 return;
4375 }
4376
4377 IInputMethod method;
4378 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004379 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 synchronized (mMethodMap) {
4384 p.println("Current Input Method Manager state:");
4385 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004386 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 for (int i=0; i<N; i++) {
4388 InputMethodInfo info = mMethodList.get(i);
4389 p.println(" InputMethod #" + i + ":");
4390 info.dump(p, " ");
4391 }
4392 p.println(" Clients:");
4393 for (ClientState ci : mClients.values()) {
4394 p.println(" Client " + ci + ":");
4395 p.println(" client=" + ci.client);
4396 p.println(" inputContext=" + ci.inputContext);
4397 p.println(" sessionRequested=" + ci.sessionRequested);
4398 p.println(" curSession=" + ci.curSession);
4399 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004400 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004402 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004403 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4404 + " softInputMode=" +
4405 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4406 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004407 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4409 + " mBoundToMethod=" + mBoundToMethod);
4410 p.println(" mCurToken=" + mCurToken);
4411 p.println(" mCurIntent=" + mCurIntent);
4412 method = mCurMethod;
4413 p.println(" mCurMethod=" + mCurMethod);
4414 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004415 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 p.println(" mShowRequested=" + mShowRequested
4417 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4418 + " mShowForced=" + mShowForced
4419 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004420 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004421 p.println(" mCurUserActionNotificationSequenceNumber="
4422 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004423 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004424 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004425 p.println(" mSwitchingController:");
4426 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004427 p.println(" mSettings:");
4428 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004429
4430 p.println(" mStartInputHistory:");
4431 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004433
Jeff Brownb88102f2010-09-08 11:49:43 -07004434 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 pw.flush();
4437 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004438 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4439 } catch (IOException | RemoteException e) {
4440 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004442 } else {
4443 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004445
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004446 if (focusedWindowClient != null && client != focusedWindowClient) {
4447 p.println(" ");
4448 p.println("Warning: Current input method client doesn't match the last focused. "
4449 + "window.");
4450 p.println("Dumping input method client in the last focused window just in case.");
4451 p.println(" ");
4452 pw.flush();
4453 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004454 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4455 } catch (IOException | RemoteException e) {
4456 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004457 }
4458 }
4459
Jeff Brownb88102f2010-09-08 11:49:43 -07004460 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 pw.flush();
4463 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004464 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4465 } catch (IOException | RemoteException e) {
4466 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004468 } else {
4469 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 }
4471 }
4472}