blob: 39bfedae69d810497c14c4c0ea97ccd6043b4bed [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;
Chris Wren282cfef2017-03-27 15:01:44 -040031import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050032import com.android.internal.notification.SystemNotificationChannels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070034import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070035import com.android.internal.os.TransferPipe;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060036import com.android.internal.util.DumpUtils;
satoke7c6998e2011-06-03 17:57:59 +090037import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import com.android.internal.view.IInputContext;
39import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import com.android.internal.view.IInputMethodClient;
41import com.android.internal.view.IInputMethodManager;
42import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070043import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080045import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080046import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
satoke7c6998e2011-06-03 17:57:59 +090048import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090050import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070052import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070053import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070054import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080055import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080056import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070057import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070059import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090060import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090061import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090062import android.app.Notification;
63import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070064import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090065import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080067import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.ContentResolver;
69import android.content.Context;
70import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090072import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090074import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070076import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090077import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.pm.PackageManager;
79import android.content.pm.ResolveInfo;
80import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070081import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.content.res.Resources;
83import android.content.res.TypedArray;
84import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080085import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080086import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080087import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070088import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040090import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070091import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090092import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.os.Handler;
94import android.os.IBinder;
95import android.os.IInterface;
96import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070097import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070099import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800101import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.os.ServiceManager;
103import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900104import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800105import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.provider.Settings;
107import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900108import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700109import android.util.ArrayMap;
110import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700111import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900113import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900114import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import android.util.PrintWriterPrinter;
116import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900117import android.util.Slog;
118import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900119import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700121import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900122import android.view.LayoutInflater;
123import android.view.View;
124import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700125import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700127import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900128import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800130import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700131import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import android.view.inputmethod.InputMethod;
133import android.view.inputmethod.InputMethodInfo;
134import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700135import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900136import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900137import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900138import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900139import android.widget.CompoundButton;
140import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900141import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900142import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900143import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700144import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
satoke7c6998e2011-06-03 17:57:59 +0900146import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900148import java.io.FileInputStream;
149import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.io.IOException;
151import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700152import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100153import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700154import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800155import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900157import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800158import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159import java.util.HashMap;
160import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800161import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800162import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800163import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
165/**
166 * This class provides a system service that manages input methods.
167 */
168public class InputMethodManagerService extends IInputMethodManager.Stub
169 implements ServiceConnection, Handler.Callback {
170 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700171 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700172 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700174 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
175 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
176 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 static final int MSG_UNBIND_INPUT = 1000;
179 static final int MSG_BIND_INPUT = 1010;
180 static final int MSG_SHOW_SOFT_INPUT = 1020;
181 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700182 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 static final int MSG_ATTACH_TOKEN = 1040;
184 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
Yohei Yukawa33e81792015-11-17 21:14:42 -0800188 static final int MSG_UNBIND_CLIENT = 3000;
189 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700190 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700191 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900192 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800193 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800194 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
satok01038492012-04-09 21:08:27 +0900196 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
197
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700198 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
199
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700200 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
satokf9f01002011-05-19 21:31:50 +0900202 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
203
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900204 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900205 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900206
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700207 /**
208 * Binding flags for establishing connection to the {@link InputMethodService}.
209 */
210 private static final int IME_CONNECTION_BIND_FLAGS =
211 Context.BIND_AUTO_CREATE
212 | Context.BIND_NOT_VISIBLE
213 | Context.BIND_NOT_FOREGROUND
214 | Context.BIND_SHOWING_UI;
215
216 /**
217 * Binding flags used only while the {@link InputMethodService} is showing window.
218 */
219 private static final int IME_VISIBLE_BIND_FLAGS =
220 Context.BIND_AUTO_CREATE
221 | Context.BIND_TREAT_LIKE_ACTIVITY
222 | Context.BIND_FOREGROUND_SERVICE;
223
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700224 @Retention(SOURCE)
225 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
226 private @interface HardKeyboardBehavior {
227 int WIRELESS_AFFORDANCE = 0;
228 int WIRED_AFFORDANCE = 1;
229 }
satok4e4569d2010-11-19 18:45:53 +0900230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800232 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900234 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700237 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700239 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900240 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900241 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900242 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800243 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900245 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 // All known input methods. mMethodMap also serves as the global
248 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700249 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
250 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900251 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700252 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900253 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
Yohei Yukawae0733062017-02-09 22:49:35 -0800255 /**
256 * Tracks how many times {@link #mMethodMap} was updated.
257 */
258 @GuardedBy("mMethodMap")
259 private int mMethodMapUpdateCount = 0;
260
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700261 // Used to bring IME service up to visible adjustment while it is being shown.
262 final ServiceConnection mVisibleConnection = new ServiceConnection() {
263 @Override public void onServiceConnected(ComponentName name, IBinder service) {
264 }
265
266 @Override public void onServiceDisconnected(ComponentName name) {
267 }
268 };
269 boolean mVisibleBound = false;
270
satok7cfc0ed2011-06-20 21:29:36 +0900271 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700272 private NotificationManager mNotificationManager;
273 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700274 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400275 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700276 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900277 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900278 private boolean mNotificationShown;
279
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900280 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 final ClientState client;
282 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700283
284 IInputMethodSession session;
285 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 @Override
288 public String toString() {
289 return "SessionState{uid " + client.uid + " pid " + client.pid
290 + " method " + Integer.toHexString(
291 System.identityHashCode(method))
292 + " session " + Integer.toHexString(
293 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700294 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 + "}";
296 }
297
298 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700299 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 client = _client;
301 method = _method;
302 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700303 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 }
305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
Jeff Brownc28867a2013-03-26 15:42:39 -0700307 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 final IInputMethodClient client;
309 final IInputContext inputContext;
310 final int uid;
311 final int pid;
312 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 boolean sessionRequested;
315 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 @Override
318 public String toString() {
319 return "ClientState{" + Integer.toHexString(
320 System.identityHashCode(this)) + " uid " + uid
321 + " pid " + pid + "}";
322 }
323
324 ClientState(IInputMethodClient _client, IInputContext _inputContext,
325 int _uid, int _pid) {
326 client = _client;
327 inputContext = _inputContext;
328 uid = _uid;
329 pid = _pid;
330 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
331 }
332 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700334 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700337 * Set once the system is ready to run third party code.
338 */
339 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700341 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700342 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
343 * method. This is to be synchronized with the secure settings keyed with
344 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
345 *
346 * <p>This can be transiently {@code null} when the system is re-initializing input method
347 * settings, e.g., the system locale is just changed.</p>
348 *
349 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
350 * {@link InputMethodManagerService}.</p>
351 *
352 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700354 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 /**
358 * The current binding sequence number, incremented every time there is
359 * a new bind performed.
360 */
361 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * The client that is currently bound to an input method.
365 */
366 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800369 * The last window token that we confirmed to be focused. This is always updated upon reports
370 * from the input method client. If the window state is already changed before the report is
371 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700372 */
373 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700375 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800376 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
377 *
378 * @see #mCurFocusedWindow
379 */
380 int mCurFocusedWindowSoftInputMode;
381
382 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800383 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
384 */
385 ClientState mCurFocusedWindowClient;
386
387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * The input context last provided by the current client.
389 */
390 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700393 * The missing method flags for the input context last provided by the current client.
394 *
395 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
396 */
397 int mCurInputContextMissingMethods;
398
399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * The attributes last provided by the current client.
401 */
402 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700405 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700407 *
408 * <p>This can be {@code null} when no input method is connected.</p>
409 *
410 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700412 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
satokab751aa2010-09-14 19:17:36 +0900416 * The current subtype of the current input method.
417 */
418 private InputMethodSubtype mCurrentSubtype;
419
satok4e4569d2010-11-19 18:45:53 +0900420 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900421 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700422 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900423
John Spurlocke0980502013-10-25 11:59:29 -0400424 // Was the keyguard locked when this client became current?
425 private boolean mCurClientInKeyguard;
426
satokab751aa2010-09-14 19:17:36 +0900427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 * Set to true if our ServiceConnection is currently actively bound to
429 * a service (whether or not we have gotten its IBinder back yet).
430 */
431 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 /**
434 * Set if the client has asked for the input method to be shown.
435 */
436 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 /**
439 * Set if we were explicitly told to show the input method.
440 */
441 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 /**
444 * Set if we were forced to be shown.
445 */
446 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 /**
449 * Set if we last told the input method to show itself.
450 */
451 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800454 * {@code true} if the current input method is in fullscreen mode.
455 */
456 boolean mInFullscreenMode;
457
458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 * The Intent used to connect to the current input method.
460 */
461 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
464 * The token we have made for the currently active input method, to
465 * identify it in the future.
466 */
467 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 /**
470 * If non-null, this is the input method service we are currently connected
471 * to.
472 */
473 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 /**
476 * Time that we last initiated a bind to the input method, to determine
477 * if we should try to disconnect and reconnect to it.
478 */
479 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 /**
482 * Have we called mCurMethod.bindInput()?
483 */
484 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 /**
487 * Currently enabled session. Only touched by service thread, not
488 * protected by a lock.
489 */
490 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700493 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700495 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900497 int mCurUserActionNotificationSequenceNumber = 0;
498
Joe Onorato857fd9b2011-01-27 15:08:35 -0800499 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900500
501 /**
502 * A set of status bits regarding the active IME.
503 *
504 * <p>This value is a combination of following two bits:</p>
505 * <dl>
506 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
507 * <dd>
508 * If this bit is ON, connected IME is ready to accept touch/key events.
509 * </dd>
510 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
511 * <dd>
512 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
513 * </dd>
514 * </dl>
515 * <em>Do not update this value outside of setImeWindowStatus.</em>
516 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800517 int mImeWindowVis;
518
Ken Wakasa05dbb652011-08-22 15:22:43 +0900519 private AlertDialog.Builder mDialogBuilder;
520 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700521 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900522 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700523 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900524 private InputMethodInfo[] mIms;
525 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800526 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700527 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800528 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900529 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
530 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500531 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700532 @HardKeyboardBehavior
533 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800534
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800535 /**
536 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
537 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
538 * will not affect those tasks that are already posted.
539 *
540 * <p>Posting {@link #MSG_START_INPUT} message basically means that
541 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
542 * back in the current IME process shortly, which will also affect what the current IME starts
543 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
544 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
545 * logical input session between the client application and the current IME.</p>
546 *
547 * <p>Be careful to not keep strong references to this object forever, which can prevent
548 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
549 * </p>
550 */
551 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800552 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
553
554 final int mSequenceNumber;
555 final long mTimestamp;
556 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800557 @NonNull
558 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800559 @NonNull
560 final String mImeId;
561 // @InputMethodClient.StartInputReason
562 final int mStartInputReason;
563 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800564 @Nullable
565 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800566 @NonNull
567 final EditorInfo mEditorInfo;
568 final int mTargetWindowSoftInputMode;
569 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800570
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800571 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
572 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
573 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
574 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
575 mSequenceNumber = sSequenceNumber.getAndIncrement();
576 mTimestamp = SystemClock.uptimeMillis();
577 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800578 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800579 mImeId = imeId;
580 mStartInputReason = startInputReason;
581 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800582 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800583 mEditorInfo = editorInfo;
584 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
585 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800586 }
587 }
588
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800589 @GuardedBy("mMethodMap")
590 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800591
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800592 /**
593 * A ring buffer to store the history of {@link StartInputInfo}.
594 */
595 private static final class StartInputHistory {
596 /**
597 * Entry size for non low-RAM devices.
598 *
599 * <p>TODO: Consider to follow what other system services have been doing to manage
600 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
601 */
602 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
603
604 /**
605 * Entry size for non low-RAM devices.
606 *
607 * <p>TODO: Consider to follow what other system services have been doing to manage
608 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
609 */
610 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
611
612 private static int getEntrySize() {
613 if (ActivityManager.isLowRamDeviceStatic()) {
614 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
615 } else {
616 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
617 }
618 }
619
620 /**
621 * Backing store for the ring bugger.
622 */
623 private final Entry[] mEntries = new Entry[getEntrySize()];
624
625 /**
626 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
627 * write.
628 */
629 private int mNextIndex = 0;
630
631 /**
632 * Recyclable entry to store the information in {@link StartInputInfo}.
633 */
634 private static final class Entry {
635 int mSequenceNumber;
636 long mTimestamp;
637 long mWallTime;
638 @NonNull
639 String mImeTokenString;
640 @NonNull
641 String mImeId;
642 /* @InputMethodClient.StartInputReason */
643 int mStartInputReason;
644 boolean mRestarting;
645 @NonNull
646 String mTargetWindowString;
647 @NonNull
648 EditorInfo mEditorInfo;
649 int mTargetWindowSoftInputMode;
650 int mClientBindSequenceNumber;
651
652 Entry(@NonNull StartInputInfo original) {
653 set(original);
654 }
655
656 void set(@NonNull StartInputInfo original) {
657 mSequenceNumber = original.mSequenceNumber;
658 mTimestamp = original.mTimestamp;
659 mWallTime = original.mWallTime;
660 // Intentionally convert to String so as not to keep a strong reference to a Binder
661 // object.
662 mImeTokenString = String.valueOf(original.mImeToken);
663 mImeId = original.mImeId;
664 mStartInputReason = original.mStartInputReason;
665 mRestarting = original.mRestarting;
666 // Intentionally convert to String so as not to keep a strong reference to a Binder
667 // object.
668 mTargetWindowString = String.valueOf(original.mTargetWindow);
669 mEditorInfo = original.mEditorInfo;
670 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
671 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
672 }
673 }
674
675 /**
676 * Add a new entry and discard the oldest entry as needed.
677 * @param info {@lin StartInputInfo} to be added.
678 */
679 void addEntry(@NonNull StartInputInfo info) {
680 final int index = mNextIndex;
681 if (mEntries[index] == null) {
682 mEntries[index] = new Entry(info);
683 } else {
684 mEntries[index].set(info);
685 }
686 mNextIndex = (mNextIndex + 1) % mEntries.length;
687 }
688
689 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
690 final SimpleDateFormat dataFormat =
691 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
692
693 for (int i = 0; i < mEntries.length; ++i) {
694 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
695 if (entry == null) {
696 continue;
697 }
698 pw.print(prefix);
699 pw.println("StartInput #" + entry.mSequenceNumber + ":");
700
701 pw.print(prefix);
702 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
703 + " (timestamp=" + entry.mTimestamp + ")"
704 + " reason="
705 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
706 + " restarting=" + entry.mRestarting);
707
708 pw.print(prefix);
709 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
710
711 pw.print(prefix);
712 pw.println(" targetWin=" + entry.mTargetWindowString
713 + " [" + entry.mEditorInfo.packageName + "]"
714 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
715
716 pw.print(prefix);
717 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
718 entry.mTargetWindowSoftInputMode));
719
720 pw.print(prefix);
721 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
722 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
723 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
724 + " fieldName=" + entry.mEditorInfo.fieldName
725 + " actionId=" + entry.mEditorInfo.actionId
726 + " actionLabel=" + entry.mEditorInfo.actionLabel);
727 }
728 }
729 }
730
731 @GuardedBy("mMethodMap")
732 @NonNull
733 private final StartInputHistory mStartInputHistory = new StartInputHistory();
734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700736 int mUserId;
737 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700738 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800739 String mLastEnabled = "";
740
Yohei Yukawa81482972015-06-04 00:58:59 -0700741 /**
742 * <em>This constructor must be called within the lock.</em>
743 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 SettingsObserver(Handler handler) {
745 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700746 }
747
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800748 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700749 if (mRegistered && mUserId == userId) {
750 return;
751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700753 if (mRegistered) {
754 mContext.getContentResolver().unregisterContentObserver(this);
755 mRegistered = false;
756 }
757 if (mUserId != userId) {
758 mLastEnabled = "";
759 mUserId = userId;
760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700762 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900763 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700764 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900765 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700766 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700767 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700768 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800769 resolver.registerContentObserver(Settings.Secure.getUriFor(
770 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700771 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
Michael Wright7b5a96b2014-08-09 19:28:42 -0700774 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800775 final Uri showImeUri = Settings.Secure.getUriFor(
776 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
777 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
778 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700780 if (showImeUri.equals(uri)) {
781 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800782 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
783 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
784 mContext.getContentResolver(),
785 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
786 0, mUserId) == 1;
787 if (mAccessibilityRequestingNoSoftKeyboard) {
788 final boolean showRequested = mShowRequested;
789 hideCurrentInputLocked(0, null);
790 mShowRequested = showRequested;
791 } else if (mShowRequested) {
792 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
793 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700794 } else {
795 boolean enabledChanged = false;
796 String newEnabled = mSettings.getEnabledInputMethodsStr();
797 if (!mLastEnabled.equals(newEnabled)) {
798 mLastEnabled = newEnabled;
799 enabledChanged = true;
800 }
801 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700805
806 @Override
807 public String toString() {
808 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
809 + " mLastEnabled=" + mLastEnabled + "}";
810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800812
Yohei Yukawa79247822017-01-23 15:26:15 -0800813 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900814 @Override
815 public void onReceive(Context context, Intent intent) {
816 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700817 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900818 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700819 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900820 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800821 } else if (Intent.ACTION_USER_ADDED.equals(action)
822 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100823 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800824 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700825 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
826 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
827 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
828 final String prevValue = intent.getStringExtra(
829 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
830 final String newValue = intent.getStringExtra(
831 Intent.EXTRA_SETTING_NEW_VALUE);
832 restoreEnabledInputMethods(mContext, prevValue, newValue);
833 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800834 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800835 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 } else {
837 Slog.w(TAG, "Unexpected intent " + intent);
838 }
839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800841
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800842 /**
843 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
844 *
845 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
846 * the users. We should ignore this event if this is about any background user's locale.</p>
847 *
848 * <p>Caution: This method must not be called when system is not ready.</p>
849 */
850 void onActionLocaleChanged() {
851 synchronized (mMethodMap) {
852 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
853 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
854 return;
855 }
856 buildInputMethodListLocked(true);
857 // If the locale is changed, needs to reset the default ime
858 resetDefaultImeLocked(mContext);
859 updateFromSettingsLocked(true);
860 mLastSystemLocales = possibleNewLocale;
861 }
862 }
863
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700864 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
865 // does not attempt to validate on the fly with any installed device policy, so must only
866 // be run in the context of initial device setup.
867 //
868 // TODO: Move this method to InputMethodUtils with adding unit tests.
869 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
870 if (DEBUG_RESTORE) {
871 Slog.i(TAG, "Restoring enabled input methods:");
872 Slog.i(TAG, "prev=" + prevValue);
873 Slog.i(TAG, " new=" + newValue);
874 }
875 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900876 ArrayMap<String, ArraySet<String>> prevMap =
877 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
878 ArrayMap<String, ArraySet<String>> newMap =
879 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700880
881 // Merge the restored ime+subtype enabled states into the live state
882 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
883 final String imeId = entry.getKey();
884 ArraySet<String> prevSubtypes = prevMap.get(imeId);
885 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700886 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700887 prevMap.put(imeId, prevSubtypes);
888 }
889 prevSubtypes.addAll(entry.getValue());
890 }
891
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700892 final String mergedImesAndSubtypesString =
893 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700894 if (DEBUG_RESTORE) {
895 Slog.i(TAG, "Merged IME string:");
896 Slog.i(TAG, " " + mergedImesAndSubtypesString);
897 }
898 Settings.Secure.putString(context.getContentResolver(),
899 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
900 }
901
Yohei Yukawac4e44912017-02-09 19:30:22 -0800902 final class MyPackageMonitor extends PackageMonitor {
903 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800904 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800905 *
906 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
907 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
908 * dynamically unless the entire package is updated, which also always triggers package
909 * rescanning.</p>
910 */
911 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800912 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
913
914 /**
915 * Packages that are appeared, disappeared, or modified for whatever reason.
916 *
917 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
918 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
919 * duplicate elements for our use case.</p>
920 *
921 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
922 * which should be bound to {@link #getRegisteredHandler()}.</p>
923 */
924 private final ArrayList<String> mChangedPackages = new ArrayList<>();
925
926 /**
927 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
928 *
929 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
930 * which should be bound to {@link #getRegisteredHandler()}.</p>
931 */
932 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800933
934 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800935 void clearKnownImePackageNamesLocked() {
936 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800937 }
938
939 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800940 final void addKnownImePackageNameLocked(@NonNull String packageName) {
941 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800942 }
943
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800944 @GuardedBy("mMethodMap")
945 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900946 final int userId = getChangingUserId();
947 final boolean retval = userId == mSettings.getCurrentUserId();
948 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900949 if (!retval) {
950 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
951 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900952 }
953 return retval;
954 }
955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800957 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800959 if (!isChangingPackagesOfCurrentUserLocked()) {
960 return false;
961 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 final int N = mMethodList.size();
964 if (curInputMethodId != null) {
965 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800966 InputMethodInfo imi = mMethodList.get(i);
967 if (imi.getId().equals(curInputMethodId)) {
968 for (String pkg : packages) {
969 if (imi.getPackageName().equals(pkg)) {
970 if (!doit) {
971 return true;
972 }
satok723a27e2010-11-11 14:58:11 +0900973 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800974 chooseNewDefaultIMELocked();
975 return true;
976 }
977 }
978 }
979 }
980 }
981 }
982 return false;
983 }
984
985 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800986 public void onBeginPackageChanges() {
987 clearPackageChangeState();
988 }
989
990 @Override
991 public void onPackageAppeared(String packageName, int reason) {
992 if (!mImePackageAppeared) {
993 final PackageManager pm = mContext.getPackageManager();
994 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
995 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
996 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
997 // No need to lock this because we access it only on getRegisteredHandler().
998 if (!services.isEmpty()) {
999 mImePackageAppeared = true;
1000 }
1001 }
1002 // No need to lock this because we access it only on getRegisteredHandler().
1003 mChangedPackages.add(packageName);
1004 }
1005
1006 @Override
1007 public void onPackageDisappeared(String packageName, int reason) {
1008 // No need to lock this because we access it only on getRegisteredHandler().
1009 mChangedPackages.add(packageName);
1010 }
1011
1012 @Override
1013 public void onPackageModified(String packageName) {
1014 // No need to lock this because we access it only on getRegisteredHandler().
1015 mChangedPackages.add(packageName);
1016 }
1017
1018 @Override
1019 public void onPackagesSuspended(String[] packages) {
1020 // No need to lock this because we access it only on getRegisteredHandler().
1021 for (String packageName : packages) {
1022 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001023 }
1024 }
1025
1026 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001027 public void onPackagesUnsuspended(String[] packages) {
1028 // No need to lock this because we access it only on getRegisteredHandler().
1029 for (String packageName : packages) {
1030 mChangedPackages.add(packageName);
1031 }
1032 }
1033
1034 @Override
1035 public void onFinishPackageChanges() {
1036 onFinishPackageChangesInternal();
1037 clearPackageChangeState();
1038 }
1039
1040 private void clearPackageChangeState() {
1041 // No need to lock them because we access these fields only on getRegisteredHandler().
1042 mChangedPackages.clear();
1043 mImePackageAppeared = false;
1044 }
1045
1046 private boolean shouldRebuildInputMethodListLocked() {
1047 // This method is guaranteed to be called only by getRegisteredHandler().
1048
1049 // If there is any new package that contains at least one IME, then rebuilt the list
1050 // of IMEs.
1051 if (mImePackageAppeared) {
1052 return true;
1053 }
1054
1055 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1056 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1057 // is an option, but it may still do some extra operations that we do not need here.
1058 final int N = mChangedPackages.size();
1059 for (int i = 0; i < N; ++i) {
1060 final String packageName = mChangedPackages.get(i);
1061 if (mKnownImePackageNames.contains(packageName)) {
1062 return true;
1063 }
1064 }
1065 return false;
1066 }
1067
1068 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001069 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001070 if (!isChangingPackagesOfCurrentUserLocked()) {
1071 return;
1072 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001073 if (!shouldRebuildInputMethodListLocked()) {
1074 return;
1075 }
1076
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001077 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001078 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001079 final int N = mMethodList.size();
1080 if (curInputMethodId != null) {
1081 for (int i=0; i<N; i++) {
1082 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001083 final String imiId = imi.getId();
1084 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001085 curIm = imi;
1086 }
satoke7c6998e2011-06-03 17:57:59 +09001087
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001088 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001089 if (isPackageModified(imi.getPackageName())) {
1090 mFileManager.deleteAllInputMethodSubtypes(imiId);
1091 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001092 if (change == PACKAGE_TEMPORARY_CHANGE
1093 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001094 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001095 + imi.getComponent());
1096 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098 }
1099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
Yohei Yukawa94e33302016-02-12 19:37:03 -08001101 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001104
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001105 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001106 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001107 if (change == PACKAGE_TEMPORARY_CHANGE
1108 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001109 ServiceInfo si = null;
1110 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001111 si = mIPackageManager.getServiceInfo(
1112 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1113 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001114 }
1115 if (si == null) {
1116 // Uh oh, current input method is no longer around!
1117 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001118 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001119 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001120 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001121 changed = true;
1122 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001123 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001124 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001125 }
1126 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001127 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001128 }
satokab751aa2010-09-14 19:17:36 +09001129
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001130 if (curIm == null) {
1131 // We currently don't have a default input method... is
1132 // one now available?
1133 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001134 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1135 // Even if the current input method is still available, mCurrentSubtype could
1136 // be obsolete when the package is modified in practice.
1137 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001139
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001140 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001141 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 }
1143 }
1144 }
1145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001146
Jeff Brownc28867a2013-03-26 15:42:39 -07001147 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001148 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001149 private final IInputMethod mMethod;
1150 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
Jeff Brownc28867a2013-03-26 15:42:39 -07001152 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1153 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001154 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001155 mMethod = method;
1156 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
satoke7c6998e2011-06-03 17:57:59 +09001159 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001160 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001161 long ident = Binder.clearCallingIdentity();
1162 try {
1163 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1164 } finally {
1165 Binder.restoreCallingIdentity(ident);
1166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
1168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001169
satok01038492012-04-09 21:08:27 +09001170 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001171 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001172 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001173 public void onHardKeyboardStatusChange(boolean available) {
1174 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1175 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001176 }
1177
Michael Wright7b5a96b2014-08-09 19:28:42 -07001178 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001179 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001180 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001181 }
1182 synchronized(mMethodMap) {
1183 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1184 && mSwitchingDialog.isShowing()) {
1185 mSwitchingDialogTitleView.findViewById(
1186 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1187 available ? View.VISIBLE : View.GONE);
1188 }
1189 }
1190 }
1191 }
1192
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001193 public static final class Lifecycle extends SystemService {
1194 private InputMethodManagerService mService;
1195
1196 public Lifecycle(Context context) {
1197 super(context);
1198 mService = new InputMethodManagerService(context);
1199 }
1200
1201 @Override
1202 public void onStart() {
1203 LocalServices.addService(InputMethodManagerInternal.class,
1204 new LocalServiceImpl(mService.mHandler));
1205 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1206 }
1207
1208 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001209 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001210 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001211 // TODO: Dispatch this to a worker thread as needed.
1212 mService.onSwitchUser(userHandle);
1213 }
1214
1215 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001216 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001217 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001218 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001219 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1220 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1221 .getService(Context.STATUS_BAR_SERVICE);
1222 mService.systemRunning(statusBarService);
1223 }
1224 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001225
1226 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001227 public void onUnlockUser(final @UserIdInt int userHandle) {
1228 // Called on ActivityManager thread.
1229 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001230 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001231 }
1232 }
1233
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001234 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001235 synchronized(mMethodMap) {
1236 final int currentUserId = mSettings.getCurrentUserId();
1237 if (DEBUG) {
1238 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1239 }
1240 if (userId != currentUserId) {
1241 return;
1242 }
1243 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001244 if (mSystemReady) {
1245 // We need to rebuild IMEs.
1246 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1247 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1248 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001249 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001250 }
1251
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001252 void onSwitchUser(@UserIdInt int userId) {
1253 synchronized (mMethodMap) {
1254 switchUserLocked(userId);
1255 }
1256 }
1257
Seigo Nonaka7309b122015-08-17 18:34:13 -07001258 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001259 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001261 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001263 // Note: SettingsObserver doesn't register observers in its constructor.
1264 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 mIWindowManager = IWindowManager.Stub.asInterface(
1266 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001267 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001268 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001269 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 public void executeMessage(Message msg) {
1271 handleMessage(msg);
1272 }
Mita Yuned218c72012-12-06 17:18:25 -08001273 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001274 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001275 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001276 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001277 mHasFeature = context.getPackageManager().hasSystemFeature(
1278 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001279 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001280 mHardKeyboardBehavior = mContext.getResources().getInteger(
1281 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001282
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001283 Bundle extras = new Bundle();
1284 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001285 mImeSwitcherNotification =
1286 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1287 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1288 .setWhen(0)
1289 .setOngoing(true)
1290 .addExtras(extras)
1291 .setCategory(Notification.CATEGORY_SYSTEM)
1292 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001293
satok7cfc0ed2011-06-20 21:29:36 +09001294 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001295 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001296
1297 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001298
satok7cfc0ed2011-06-20 21:29:36 +09001299 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001300 int userId = 0;
1301 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001302 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001303 } catch (RemoteException e) {
1304 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1305 }
satok913a8922010-08-26 21:53:41 +09001306
satokd87c2592010-09-29 11:52:06 +09001307 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001308 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001309 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001310
Kenny Guy2a764942014-04-02 13:29:20 +01001311 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001312 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001313 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1314 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001315 }
1316
satok5b927c432012-05-01 20:09:34 +09001317 private void resetDefaultImeLocked(Context context) {
1318 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001319 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001320 return;
1321 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001322 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001323 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001324 if (suitableImes.isEmpty()) {
1325 Slog.i(TAG, "No default found");
1326 return;
satok5b927c432012-05-01 20:09:34 +09001327 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001328 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001329 if (DEBUG) {
1330 Slog.i(TAG, "Default found, using " + defIm.getId());
1331 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001332 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001333 }
1334
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001335 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001336 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1337 + " currentUserId=" + mSettings.getCurrentUserId());
1338
Yohei Yukawa81482972015-06-04 00:58:59 -07001339 // ContentObserver should be registered again when the user is changed
1340 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001341
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001342 // If the system is not ready or the device is not yed unlocked by the user, then we use
1343 // copy-on-write settings.
1344 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001345 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001346 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001347 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001348 // InputMethodFileManager should be reset when the user is changed
1349 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001350 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001351
1352 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1353 + " defaultImiId=" + defaultImiId);
1354
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001355 // For secondary users, the list of enabled IMEs may not have been updated since the
1356 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1357 // not be empty even if the IME has been uninstalled by the primary user.
1358 // Even in such cases, IMMS works fine because it will find the most applicable
1359 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001360 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001361 mLastSystemLocales = mRes.getConfiguration().getLocales();
1362
1363 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1364 if (mSystemReady) {
1365 hideCurrentInputLocked(0, null);
1366 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1367 buildInputMethodListLocked(initialUserSwitch);
1368 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1369 // This is the first time of the user switch and
1370 // set the current ime to the proper one.
1371 resetDefaultImeLocked(mContext);
1372 }
1373 updateFromSettingsLocked(true);
1374 try {
1375 startInputInnerLocked();
1376 } catch (RuntimeException e) {
1377 Slog.w(TAG, "Unexpected exception", e);
1378 }
1379 }
1380
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001381 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001382 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1383 mSettings.getEnabledInputMethodListLocked(), newUserId,
1384 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001385 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001386
1387 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1388 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001389 }
1390
Kenny Guy2a764942014-04-02 13:29:20 +01001391 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001392 mSettings.setCurrentProfileIds(
1393 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001394 }
1395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 @Override
1397 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1398 throws RemoteException {
1399 try {
1400 return super.onTransact(code, data, reply, flags);
1401 } catch (RuntimeException e) {
1402 // The input method manager only throws security exceptions, so let's
1403 // log all others.
1404 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001405 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407 throw e;
1408 }
1409 }
1410
Svetoslav Ganova0027152013-06-25 14:59:53 -07001411 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001412 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001413 if (DEBUG) {
1414 Slog.d(TAG, "--- systemReady");
1415 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001416 if (!mSystemReady) {
1417 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001418 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001419 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001420 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001421 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001422 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1423 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001424 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001425 if (mStatusBar != null) {
1426 mStatusBar.setIconVisibility(mSlotIme, false);
1427 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001428 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001429 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1430 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001431 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001432 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001433 mHardKeyboardListener);
1434 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001435
1436 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1437 mSettingsObserver.registerContentObserverLocked(currentUserId);
1438
1439 final IntentFilter broadcastFilter = new IntentFilter();
1440 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1441 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1442 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1443 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1444 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1445 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1446
1447 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1448 resetDefaultImeLocked(mContext);
1449 updateFromSettingsLocked(true);
1450 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1451 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1452 mContext.getBasePackageName());
1453
Dianne Hackborncc278702009-09-02 23:07:23 -07001454 try {
1455 startInputInnerLocked();
1456 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001457 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001458 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001459 }
1460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001463 // ---------------------------------------------------------------------------------------
1464 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1465 // 1) it comes from the system process
1466 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1467 private boolean calledFromValidUser() {
1468 final int uid = Binder.getCallingUid();
1469 final int userId = UserHandle.getUserId(uid);
1470 if (DEBUG) {
1471 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1472 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1473 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001474 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1475 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001476 }
Kenny Guy2a764942014-04-02 13:29:20 +01001477 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001478 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001479 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001480
1481 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1482 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1483 // must not manage background users' states in any functions.
1484 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1485 // by a token.
1486 if (mContext.checkCallingOrSelfPermission(
1487 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1488 == PackageManager.PERMISSION_GRANTED) {
1489 if (DEBUG) {
1490 Slog.d(TAG, "--- Access granted because the calling process has "
1491 + "the INTERACT_ACROSS_USERS_FULL permission");
1492 }
1493 return true;
1494 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001495 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001496 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1497 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001498 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001499 }
1500
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001501
1502 /**
1503 * Returns true iff the caller is identified to be the current input method with the token.
1504 * @param token The window token given to the input method when it was started.
1505 * @return true if and only if non-null valid token is specified.
1506 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001507 private boolean calledWithValidToken(@Nullable IBinder token) {
1508 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1509 if (DEBUG) {
1510 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1511 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1512 }
1513 return false;
1514 }
1515 if (token == null || token != mCurToken) {
1516 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1517 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1518 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001519 return false;
1520 }
1521 return true;
1522 }
1523
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001524 private boolean bindCurrentInputMethodService(
1525 Intent service, ServiceConnection conn, int flags) {
1526 if (service == null || conn == null) {
1527 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1528 return false;
1529 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001530 return mContext.bindServiceAsUser(service, conn, flags,
1531 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001532 }
1533
satoke7c6998e2011-06-03 17:57:59 +09001534 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001536 // TODO: Make this work even for non-current users?
1537 if (!calledFromValidUser()) {
1538 return Collections.emptyList();
1539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001541 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 }
1543 }
1544
satoke7c6998e2011-06-03 17:57:59 +09001545 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001547 // TODO: Make this work even for non-current users?
1548 if (!calledFromValidUser()) {
1549 return Collections.emptyList();
1550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001552 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554 }
1555
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001556 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001557 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001558 * @return enabled subtypes of the specified imi
1559 */
satoke7c6998e2011-06-03 17:57:59 +09001560 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001561 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001562 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001563 // TODO: Make this work even for non-current users?
1564 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001565 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001566 }
satok67ddf9c2010-11-17 09:45:54 +09001567 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001568 final InputMethodInfo imi;
1569 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001570 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001571 } else {
1572 imi = mMethodMap.get(imiId);
1573 }
1574 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001575 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001576 }
1577 return mSettings.getEnabledInputMethodSubtypeListLocked(
1578 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001579 }
1580 }
1581
satoke7c6998e2011-06-03 17:57:59 +09001582 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 public void addClient(IInputMethodClient client,
1584 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001585 if (!calledFromValidUser()) {
1586 return;
1587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 synchronized (mMethodMap) {
1589 mClients.put(client.asBinder(), new ClientState(client,
1590 inputContext, uid, pid));
1591 }
1592 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001593
satoke7c6998e2011-06-03 17:57:59 +09001594 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001596 if (!calledFromValidUser()) {
1597 return;
1598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001600 ClientState cs = mClients.remove(client.asBinder());
1601 if (cs != null) {
1602 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001603 if (mCurClient == cs) {
1604 mCurClient = null;
1605 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001606 if (mCurFocusedWindowClient == cs) {
1607 mCurFocusedWindowClient = null;
1608 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 }
1611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 void executeOrSendMessage(IInterface target, Message msg) {
1614 if (target.asBinder() instanceof Binder) {
1615 mCaller.sendMessage(msg);
1616 } else {
1617 handleMessage(msg);
1618 msg.recycle();
1619 }
1620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001621
Yohei Yukawa33e81792015-11-17 21:14:42 -08001622 void unbindCurrentClientLocked(
1623 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001625 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 + mCurClient.client.asBinder());
1627 if (mBoundToMethod) {
1628 mBoundToMethod = false;
1629 if (mCurMethod != null) {
1630 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1631 MSG_UNBIND_INPUT, mCurMethod));
1632 }
1633 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001634
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001635 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1636 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001637 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1638 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
The Android Open Source Project10592532009-03-18 17:39:46 -07001642 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
1644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 private int getImeShowFlags() {
1647 int flags = 0;
1648 if (mShowForced) {
1649 flags |= InputMethod.SHOW_FORCED
1650 | InputMethod.SHOW_EXPLICIT;
1651 } else if (mShowExplicitlyRequested) {
1652 flags |= InputMethod.SHOW_EXPLICIT;
1653 }
1654 return flags;
1655 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 private int getAppShowFlags() {
1658 int flags = 0;
1659 if (mShowForced) {
1660 flags |= InputMethodManager.SHOW_FORCED;
1661 } else if (!mShowExplicitlyRequested) {
1662 flags |= InputMethodManager.SHOW_IMPLICIT;
1663 }
1664 return flags;
1665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001666
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001667 InputBindResult attachNewInputLocked(
1668 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 if (!mBoundToMethod) {
1670 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1671 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1672 mBoundToMethod = true;
1673 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001674
1675 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001676 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1677 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1678 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001679 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001680 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001683 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001684 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001685 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001687 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001688 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001690 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001691 (session.channel != null ? session.channel.dup() : null),
1692 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001694
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001695 InputBindResult startInputLocked(
1696 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001697 IInputMethodClient client, IInputContext inputContext,
1698 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001699 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 // If no method is currently selected, do nothing.
1701 if (mCurMethodId == null) {
1702 return mNoBinding;
1703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 ClientState cs = mClients.get(client.asBinder());
1706 if (cs == null) {
1707 throw new IllegalArgumentException("unknown client "
1708 + client.asBinder());
1709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710
Yohei Yukawa74750f22016-03-22 12:54:22 -07001711 if (attribute == null) {
1712 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1713 + " uid=" + cs.uid + " pid=" + cs.pid);
1714 return null;
1715 }
1716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 try {
1718 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1719 // Check with the window manager to make sure this client actually
1720 // has a window with focus. If not, reject. This is thread safe
1721 // because if the focus changes some time before or after, the
1722 // next client receiving focus that has any interest in input will
1723 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001724 if (DEBUG) {
1725 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1726 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 return null;
1729 }
1730 } catch (RemoteException e) {
1731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001733 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001734 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001735 }
1736
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001737 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001738 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001739 @NonNull EditorInfo attribute, int controlFlags,
1740 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001741 // If no method is currently selected, do nothing.
1742 if (mCurMethodId == null) {
1743 return mNoBinding;
1744 }
1745
Yohei Yukawad57ba672015-06-08 16:39:46 -07001746 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1747 attribute.packageName)) {
1748 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1749 + " uid=" + cs.uid + " package=" + attribute.packageName);
1750 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001751 }
1752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001754 // Was the keyguard locked when switching over to the new client?
1755 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 // If the client is changing, we need to switch over to the new
1757 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001758 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001759 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001760 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761
1762 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001763 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001764 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001765 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
1767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 // Bump up the sequence for this client and attach it.
1770 mCurSeq++;
1771 if (mCurSeq <= 0) mCurSeq = 1;
1772 mCurClient = cs;
1773 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001774 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 // Check if the input method is changing.
1778 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1779 if (cs.curSession != null) {
1780 // Fast case: if we are already connected to the input method,
1781 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001782 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001783 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785 if (mHaveConnection) {
1786 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 // Return to client, and we will get back with it when
1788 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001789 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001790 return new InputBindResult(null, null, mCurId, mCurSeq,
1791 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 } else if (SystemClock.uptimeMillis()
1793 < (mLastBindTime+TIME_TO_RECONNECT)) {
1794 // In this case we have connected to the service, but
1795 // don't yet have its interface. If it hasn't been too
1796 // long since we did the connection, we'll return to
1797 // the client and wait to get the service interface so
1798 // we can report back. If it has been too long, we want
1799 // to fall through so we can try a disconnect/reconnect
1800 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001801 return new InputBindResult(null, null, mCurId, mCurSeq,
1802 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001804 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1805 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 }
1807 }
1808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001809
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001810 return startInputInnerLocked();
1811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001812
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001813 InputBindResult startInputInnerLocked() {
1814 if (mCurMethodId == null) {
1815 return mNoBinding;
1816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001817
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001818 if (!mSystemReady) {
1819 // If the system is not yet ready, we shouldn't be running third
1820 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001821 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1822 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1826 if (info == null) {
1827 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1828 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001829
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001830 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1833 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001834 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1835 com.android.internal.R.string.input_method_binding_label);
1836 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1837 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001838 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 mLastBindTime = SystemClock.uptimeMillis();
1840 mHaveConnection = true;
1841 mCurId = info.getId();
1842 mCurToken = new Binder();
1843 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001844 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001845 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 } catch (RemoteException e) {
1847 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001848 return new InputBindResult(null, null, mCurId, mCurSeq,
1849 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 } else {
1851 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001852 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 + mCurIntent);
1854 }
1855 return null;
1856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001857
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001858 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001859 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001860 IInputMethodClient client, IInputContext inputContext,
1861 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001862 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001863 if (!calledFromValidUser()) {
1864 return null;
1865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001867 if (DEBUG) {
1868 Slog.v(TAG, "startInput: reason="
1869 + InputMethodClient.getStartInputReason(startInputReason)
1870 + " client = " + client.asBinder()
1871 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001872 + " missingMethods="
1873 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001874 + " attribute=" + attribute
1875 + " controlFlags=#" + Integer.toHexString(controlFlags));
1876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 final long ident = Binder.clearCallingIdentity();
1878 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001879 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1880 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 } finally {
1882 Binder.restoreCallingIdentity(ident);
1883 }
1884 }
1885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001886
satoke7c6998e2011-06-03 17:57:59 +09001887 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 public void finishInput(IInputMethodClient client) {
1889 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001890
satoke7c6998e2011-06-03 17:57:59 +09001891 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 public void onServiceConnected(ComponentName name, IBinder service) {
1893 synchronized (mMethodMap) {
1894 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1895 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001896 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001897 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001898 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001899 return;
1900 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001901 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001902 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1903 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001905 clearClientSessionLocked(mCurClient);
1906 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
1908 }
1909 }
1910 }
1911
Jeff Brownc28867a2013-03-26 15:42:39 -07001912 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1913 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 synchronized (mMethodMap) {
1915 if (mCurMethod != null && method != null
1916 && mCurMethod.asBinder() == method.asBinder()) {
1917 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001918 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001920 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001921 InputBindResult res = attachNewInputLocked(
1922 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 if (res.method != null) {
1924 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001925 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001927 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
1929 }
1930 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001931
1932 // Session abandoned. Close its associated input channel.
1933 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001935
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001936 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001937 if (mVisibleBound) {
1938 mContext.unbindService(mVisibleConnection);
1939 mVisibleBound = false;
1940 }
1941
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001942 if (mHaveConnection) {
1943 mContext.unbindService(this);
1944 mHaveConnection = false;
1945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001946
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001947 if (mCurToken != null) {
1948 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001949 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001950 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001951 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001952 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001953 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001954 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001955 } catch (RemoteException e) {
1956 }
1957 mCurToken = null;
1958 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001959
The Android Open Source Project10592532009-03-18 17:39:46 -07001960 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001961 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001963
Yohei Yukawa33e81792015-11-17 21:14:42 -08001964 void resetCurrentMethodAndClient(
1965 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001966 mCurMethodId = null;
1967 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001968 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001969 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001970
1971 void requestClientSessionLocked(ClientState cs) {
1972 if (!cs.sessionRequested) {
1973 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1974 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1975 cs.sessionRequested = true;
1976 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1977 MSG_CREATE_SESSION, mCurMethod, channels[1],
1978 new MethodCallback(this, mCurMethod, channels[0])));
1979 }
1980 }
1981
1982 void clearClientSessionLocked(ClientState cs) {
1983 finishSessionLocked(cs.curSession);
1984 cs.curSession = null;
1985 cs.sessionRequested = false;
1986 }
1987
1988 private void finishSessionLocked(SessionState sessionState) {
1989 if (sessionState != null) {
1990 if (sessionState.session != null) {
1991 try {
1992 sessionState.session.finishSession();
1993 } catch (RemoteException e) {
1994 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001995 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001996 }
1997 sessionState.session = null;
1998 }
1999 if (sessionState.channel != null) {
2000 sessionState.channel.dispose();
2001 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002002 }
2003 }
2004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002006 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 if (mCurMethod != null) {
2008 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002009 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002011
Jeff Brownc28867a2013-03-26 15:42:39 -07002012 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002013 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 mCurMethod = null;
2015 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002016 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002017 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002018 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002019 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002021
satoke7c6998e2011-06-03 17:57:59 +09002022 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002024 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2025 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2026 // which is irregular but can eventually happen for everyone just by continuing using the
2027 // device. Thus it is important to make sure that all the internal states are properly
2028 // refreshed when this method is called back. Running
2029 // adb install -r <APK that implements the current IME>
2030 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002032 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 + " mCurIntent=" + mCurIntent);
2034 if (mCurMethod != null && mCurIntent != null
2035 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002036 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 // We consider this to be a new bind attempt, since the system
2038 // should now try to restart the service for us.
2039 mLastBindTime = SystemClock.uptimeMillis();
2040 mShowRequested = mInputShown;
2041 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002042 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
2044 }
2045 }
2046
satokf9f01002011-05-19 21:31:50 +09002047 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002049 synchronized (mMethodMap) {
2050 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002051 return;
2052 }
2053 final long ident = Binder.clearCallingIdentity();
2054 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002056 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002057 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002058 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002061 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002062 CharSequence contentDescription = null;
2063 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002064 // Use PackageManager to load label
2065 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002066 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002067 mIPackageManager.getApplicationInfo(packageName, 0,
2068 mSettings.getCurrentUserId()));
2069 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002070 /* ignore */
2071 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002072 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002073 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002074 contentDescription != null
2075 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002076 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002079 } finally {
2080 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083 }
2084
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002085 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002086 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002087 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09002088 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002089 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002090 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002091 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2092 // When physical keyboard is attached, we show the ime switcher (or notification if
2093 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2094 // exists in the IME switcher dialog. Might be OK to remove this condition once
2095 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2096 return true;
2097 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002098 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2099 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002100 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002101
2102 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2103 final int N = imis.size();
2104 if (N > 2) return true;
2105 if (N < 1) return false;
2106 int nonAuxCount = 0;
2107 int auxCount = 0;
2108 InputMethodSubtype nonAuxSubtype = null;
2109 InputMethodSubtype auxSubtype = null;
2110 for(int i = 0; i < N; ++i) {
2111 final InputMethodInfo imi = imis.get(i);
2112 final List<InputMethodSubtype> subtypes =
2113 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2114 final int subtypeCount = subtypes.size();
2115 if (subtypeCount == 0) {
2116 ++nonAuxCount;
2117 } else {
2118 for (int j = 0; j < subtypeCount; ++j) {
2119 final InputMethodSubtype subtype = subtypes.get(j);
2120 if (!subtype.isAuxiliary()) {
2121 ++nonAuxCount;
2122 nonAuxSubtype = subtype;
2123 } else {
2124 ++auxCount;
2125 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002126 }
2127 }
satok7cfc0ed2011-06-20 21:29:36 +09002128 }
2129 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002130 if (nonAuxCount > 1 || auxCount > 1) {
2131 return true;
2132 } else if (nonAuxCount == 1 && auxCount == 1) {
2133 if (nonAuxSubtype != null && auxSubtype != null
2134 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2135 || auxSubtype.overridesImplicitlyEnabledSubtype()
2136 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2137 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2138 return false;
2139 }
2140 return true;
2141 }
2142 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002143 }
2144
John Spurlocke0980502013-10-25 11:59:29 -04002145 private boolean isKeyguardLocked() {
2146 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2147 }
2148
satokdbf29502011-08-25 15:28:23 +09002149 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002150 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002151 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2152 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002153 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002154 return;
2155 }
2156
Yohei Yukawa69e68022017-02-13 12:04:50 -08002157 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002158 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002159 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002160 mImeWindowVis = vis;
2161 mBackDisposition = backDisposition;
2162 updateSystemUiLocked(token, vis, backDisposition);
2163 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002164 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2165 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawabbb10e82017-02-17 20:13:52 -08002166 info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002167 }
2168
2169 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2170 synchronized (mMethodMap) {
2171 updateSystemUiLocked(token, vis, backDisposition);
2172 }
2173 }
2174
2175 // Caution! This method is called in this class. Handle multi-user carefully
2176 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2177 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002178 return;
2179 }
2180
2181 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2182 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002183 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002184 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002185 // apply policy for binder calls
2186 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2187 vis = 0;
satok06487a52010-10-29 11:37:18 +09002188 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002189 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2190 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2191 if (mStatusBar != null) {
2192 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2193 needsToShowImeSwitcher);
2194 }
2195 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2196 if (imi != null && needsToShowImeSwitcher) {
2197 // Used to load label
2198 final CharSequence title = mRes.getText(
2199 com.android.internal.R.string.select_input_method);
2200 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2201 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002202 mImeSwitcherNotification.setContentTitle(title)
2203 .setContentText(summary)
2204 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002205 try {
2206 if ((mNotificationManager != null)
2207 && !mIWindowManager.hasNavigationBar()) {
2208 if (DEBUG) {
2209 Slog.d(TAG, "--- show notification: label = " + summary);
2210 }
2211 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002212 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002213 mImeSwitcherNotification.build(), UserHandle.ALL);
2214 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002215 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002216 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002217 }
2218 } else {
2219 if (mNotificationShown && mNotificationManager != null) {
2220 if (DEBUG) {
2221 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002222 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002223 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002224 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002225 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002226 }
satok06487a52010-10-29 11:37:18 +09002227 }
2228 } finally {
2229 Binder.restoreCallingIdentity(ident);
2230 }
2231 }
2232
satoke7c6998e2011-06-03 17:57:59 +09002233 @Override
satokf9f01002011-05-19 21:31:50 +09002234 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002235 if (!calledFromValidUser()) {
2236 return;
2237 }
satokf9f01002011-05-19 21:31:50 +09002238 synchronized (mMethodMap) {
2239 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2240 for (int i = 0; i < spans.length; ++i) {
2241 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002242 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002243 mSecureSuggestionSpans.put(ss, currentImi);
2244 }
2245 }
2246 }
2247 }
2248
satoke7c6998e2011-06-03 17:57:59 +09002249 @Override
satokf9f01002011-05-19 21:31:50 +09002250 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002251 if (!calledFromValidUser()) {
2252 return false;
2253 }
satokf9f01002011-05-19 21:31:50 +09002254 synchronized (mMethodMap) {
2255 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2256 // TODO: Do not send the intent if the process of the targetImi is already dead.
2257 if (targetImi != null) {
2258 final String[] suggestions = span.getSuggestions();
2259 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002260 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002261 final Intent intent = new Intent();
2262 // Ensures that only a class in the original IME package will receive the
2263 // notification.
satok42c5a162011-05-26 16:46:14 +09002264 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002265 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2266 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2267 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2268 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002269 final long ident = Binder.clearCallingIdentity();
2270 try {
2271 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2272 } finally {
2273 Binder.restoreCallingIdentity(ident);
2274 }
satokf9f01002011-05-19 21:31:50 +09002275 return true;
2276 }
2277 }
2278 return false;
2279 }
2280
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002281 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002282 updateInputMethodsFromSettingsLocked(enabledMayChange);
2283 updateKeyboardFromSettingsLocked();
2284 }
2285
2286 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002287 if (enabledMayChange) {
2288 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2289 for (int i=0; i<enabled.size(); i++) {
2290 // We allow the user to select "disabled until used" apps, so if they
2291 // are enabling one of those here we now need to make it enabled.
2292 InputMethodInfo imm = enabled.get(i);
2293 try {
2294 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2295 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2296 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002297 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002298 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002299 if (DEBUG) {
2300 Slog.d(TAG, "Update state(" + imm.getId()
2301 + "): DISABLED_UNTIL_USED -> DEFAULT");
2302 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002303 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2304 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002305 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2306 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002307 }
2308 } catch (RemoteException e) {
2309 }
2310 }
2311 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002312 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2313 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2314 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2315 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002316 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002317 // There is no input method selected, try to choose new applicable input method.
2318 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002319 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002320 }
2321 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002323 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002325 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002326 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 }
satokf3db1af2010-11-23 13:34:33 +09002328 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002329 } else {
2330 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002331 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002333 // Here is not the perfect place to reset the switching controller. Ideally
2334 // mSwitchingController and mSettings should be able to share the same state.
2335 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2336 // the same enabled IMEs list.
2337 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002338
2339 }
2340
2341 public void updateKeyboardFromSettingsLocked() {
2342 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2343 if (mSwitchingDialog != null
2344 && mSwitchingDialogTitleView != null
2345 && mSwitchingDialog.isShowing()) {
2346 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2347 com.android.internal.R.id.hard_keyboard_switch);
2348 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002351
Yohei Yukawab097b822015-12-01 10:43:08 -08002352 private void notifyInputMethodSubtypeChanged(final int userId,
2353 @Nullable final InputMethodInfo inputMethodInfo,
2354 @Nullable final InputMethodSubtype subtype) {
2355 final InputManagerInternal inputManagerInternal =
2356 LocalServices.getService(InputManagerInternal.class);
2357 if (inputManagerInternal != null) {
2358 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2359 }
2360 }
2361
satokab751aa2010-09-14 19:17:36 +09002362 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 InputMethodInfo info = mMethodMap.get(id);
2364 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002365 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
satokd81e9502012-05-21 12:58:45 +09002368 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002370 final int subtypeCount = info.getSubtypeCount();
2371 if (subtypeCount <= 0) {
2372 return;
satokcd7cd292010-11-20 15:46:23 +09002373 }
satokd81e9502012-05-21 12:58:45 +09002374 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2375 final InputMethodSubtype newSubtype;
2376 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2377 newSubtype = info.getSubtypeAt(subtypeId);
2378 } else {
2379 // If subtype is null, try to find the most applicable one from
2380 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002381 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002382 }
2383 if (newSubtype == null || oldSubtype == null) {
2384 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2385 + ", new subtype = " + newSubtype);
2386 return;
2387 }
2388 if (newSubtype != oldSubtype) {
2389 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2390 if (mCurMethod != null) {
2391 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002392 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002393 mCurMethod.changeInputMethodSubtype(newSubtype);
2394 } catch (RemoteException e) {
2395 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002396 return;
satokab751aa2010-09-14 19:17:36 +09002397 }
2398 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002399 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 return;
2402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002403
satokd81e9502012-05-21 12:58:45 +09002404 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 final long ident = Binder.clearCallingIdentity();
2406 try {
satokab751aa2010-09-14 19:17:36 +09002407 // Set a subtype to this input method.
2408 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002409 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2410 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2411 // because mCurMethodId is stored as a history in
2412 // setSelectedInputMethodAndSubtypeLocked().
2413 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002415 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002417 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002419 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002421 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 } finally {
2423 Binder.restoreCallingIdentity(ident);
2424 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002425
2426 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2427 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002429
satok42c5a162011-05-26 16:46:14 +09002430 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002431 public boolean showSoftInput(IInputMethodClient client, int flags,
2432 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002433 if (!calledFromValidUser()) {
2434 return false;
2435 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002436 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 long ident = Binder.clearCallingIdentity();
2438 try {
2439 synchronized (mMethodMap) {
2440 if (mCurClient == null || client == null
2441 || mCurClient.client.asBinder() != client.asBinder()) {
2442 try {
2443 // We need to check if this is the current client with
2444 // focus in the window manager, to allow this call to
2445 // be made before input is started in it.
2446 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002447 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002448 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 }
2450 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002451 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 }
2453 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002454
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002455 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002456 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 }
2458 } finally {
2459 Binder.restoreCallingIdentity(ident);
2460 }
2461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002462
The Android Open Source Project4df24232009-03-05 14:34:35 -08002463 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002465 if (mAccessibilityRequestingNoSoftKeyboard) {
2466 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
Anna Galusza9b278112016-01-04 11:37:37 -08002468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2470 mShowExplicitlyRequested = true;
2471 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002472 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2473 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002475
Dianne Hackborncc278702009-09-02 23:07:23 -07002476 if (!mSystemReady) {
2477 return false;
2478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002479
The Android Open Source Project4df24232009-03-05 14:34:35 -08002480 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002482 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002483 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2484 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2485 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002487 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002488 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002489 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002490 mVisibleBound = true;
2491 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002492 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002494 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 // The client has asked to have the input method shown, but
2496 // we have been sitting here too long with a connection to the
2497 // service and no interface received, so let's disconnect/connect
2498 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002499 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002501 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002503 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002504 } else {
2505 if (DEBUG) {
2506 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2507 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002510
The Android Open Source Project4df24232009-03-05 14:34:35 -08002511 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002513
satok42c5a162011-05-26 16:46:14 +09002514 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002515 public boolean hideSoftInput(IInputMethodClient client, int flags,
2516 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002517 if (!calledFromValidUser()) {
2518 return false;
2519 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002520 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 long ident = Binder.clearCallingIdentity();
2522 try {
2523 synchronized (mMethodMap) {
2524 if (mCurClient == null || client == null
2525 || mCurClient.client.asBinder() != client.asBinder()) {
2526 try {
2527 // We need to check if this is the current client with
2528 // focus in the window manager, to allow this call to
2529 // be made before input is started in it.
2530 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002531 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2532 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002533 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 }
2535 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002536 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 }
2538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002539
Joe Onorato8a9b2202010-02-26 18:56:32 -08002540 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002541 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 }
2543 } finally {
2544 Binder.restoreCallingIdentity(ident);
2545 }
2546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002547
The Android Open Source Project4df24232009-03-05 14:34:35 -08002548 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2550 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002551 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 -08002552 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 }
2554 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002555 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 -08002556 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002558
2559 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2560 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2561 // to be updated with the new value sent from IME process. Even in such a transient state
2562 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2563 // application process as a valid request, and have even promised such a behavior with CTS
2564 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2565 // IMMS#InputShown indicates that the software keyboard is shown.
2566 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2567 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2568 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002569 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002570 if (shouldHideSoftInput) {
2571 // The IME will report its visible state again after the following message finally
2572 // delivered to the IME process as an IPC. Hence the inconsistency between
2573 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2574 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002575 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2576 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2577 res = true;
2578 } else {
2579 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002581 if (mHaveConnection && mVisibleBound) {
2582 mContext.unbindService(mVisibleConnection);
2583 mVisibleBound = false;
2584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 mInputShown = false;
2586 mShowRequested = false;
2587 mShowExplicitlyRequested = false;
2588 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002589 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002591
satok42c5a162011-05-26 16:46:14 +09002592 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002593 public InputBindResult startInputOrWindowGainedFocus(
2594 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2595 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002596 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002597 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002598 if (windowToken != null) {
2599 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002600 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002601 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002602 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2603 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002604 }
2605 }
2606
2607 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002608 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002609 IInputMethodClient client, IBinder windowToken, int controlFlags,
2610 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002611 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2612 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002613 // Needs to check the validity before clearing calling identity
2614 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002615 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 long ident = Binder.clearCallingIdentity();
2617 try {
2618 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002619 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2620 + InputMethodClient.getStartInputReason(startInputReason)
2621 + " client=" + client.asBinder()
2622 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002623 + " missingMethods="
2624 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002625 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002626 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002627 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002628 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002629
Dianne Hackborn7663d802012-02-24 13:08:49 -08002630 ClientState cs = mClients.get(client.asBinder());
2631 if (cs == null) {
2632 throw new IllegalArgumentException("unknown client "
2633 + client.asBinder());
2634 }
2635
2636 try {
2637 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2638 // Check with the window manager to make sure this client actually
2639 // has a window with focus. If not, reject. This is thread safe
2640 // because if the focus changes some time before or after, the
2641 // next client receiving focus that has any interest in input will
2642 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002643 if (DEBUG) {
2644 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2645 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2646 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002647 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002649 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002651
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002652 if (!calledFromValidUser) {
2653 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2654 Slog.w(TAG, "If you want to interect with IME, you need "
2655 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2656 hideCurrentInputLocked(0, null);
2657 return null;
2658 }
2659
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002660 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002661 if (DEBUG) {
2662 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2663 + " attribute=" + attribute + ", token = " + windowToken);
2664 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002665 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002666 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002667 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002668 }
2669 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002670 }
2671 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002672 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002673 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002674
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002675 // Should we auto-show the IME even if the caller has not
2676 // specified what should be done with it?
2677 // We only do this automatically if the window can resize
2678 // to accommodate the IME (so what the user sees will give
2679 // them good context without input information being obscured
2680 // by the IME) or if running on a large screen where there
2681 // is more room for the target window + IME.
2682 final boolean doAutoShow =
2683 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2684 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2685 || mRes.getConfiguration().isLayoutSizeAtLeast(
2686 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002687 final boolean isTextEditor =
2688 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2689
2690 // We want to start input before showing the IME, but after closing
2691 // it. We want to do this after closing it to help the IME disappear
2692 // more quickly (not get stuck behind it initializing itself for the
2693 // new focused input, even if its window wants to hide the IME).
2694 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2697 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002698 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2700 // There is no focus view, and this window will
2701 // be behind any soft input window, so hide the
2702 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002703 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002704 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002706 } else if (isTextEditor && doAutoShow && (softInputMode &
2707 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 // There is a focus view, and we are navigating forward
2709 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002710 // We only do this automatically if the window can resize
2711 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002712 // them good context without input information being obscured
2713 // by the IME) or if running on a large screen where there
2714 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002715 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002716 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002717 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002718 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002719 didStart = true;
2720 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002721 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 }
2723 break;
2724 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2725 // Do nothing.
2726 break;
2727 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2728 if ((softInputMode &
2729 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002730 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002731 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 }
2733 break;
2734 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002735 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002736 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 break;
2738 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2739 if ((softInputMode &
2740 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002741 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002742 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002743 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002744 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002745 didStart = true;
2746 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002747 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
2749 break;
2750 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002751 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002752 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002753 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002754 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002755 didStart = true;
2756 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002757 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 break;
2759 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002760
2761 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002762 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002763 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 }
2766 } finally {
2767 Binder.restoreCallingIdentity(ident);
2768 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002769
2770 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002772
satok42c5a162011-05-26 16:46:14 +09002773 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002774 public void showInputMethodPickerFromClient(
2775 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002776 if (!calledFromValidUser()) {
2777 return;
2778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 synchronized (mMethodMap) {
2780 if (mCurClient == null || client == null
2781 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002782 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002783 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 }
2785
satok440aab52010-11-25 09:43:11 +09002786 // Always call subtype picker, because subtype picker is a superset of input method
2787 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002788 mHandler.sendMessage(mCaller.obtainMessageI(
2789 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002790 }
2791 }
2792
satok42c5a162011-05-26 16:46:14 +09002793 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002795 if (!calledFromValidUser()) {
2796 return;
2797 }
satok28203512010-11-24 11:06:49 +09002798 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2799 }
2800
satok42c5a162011-05-26 16:46:14 +09002801 @Override
satok28203512010-11-24 11:06:49 +09002802 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002803 if (!calledFromValidUser()) {
2804 return;
2805 }
satok28203512010-11-24 11:06:49 +09002806 synchronized (mMethodMap) {
2807 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002808 setInputMethodWithSubtypeIdLocked(token, id,
2809 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2810 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002811 } else {
2812 setInputMethod(token, id);
2813 }
2814 }
satokab751aa2010-09-14 19:17:36 +09002815 }
2816
satok42c5a162011-05-26 16:46:14 +09002817 @Override
satokb416a712010-11-25 20:42:14 +09002818 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002819 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002820 if (!calledFromValidUser()) {
2821 return;
2822 }
satokb416a712010-11-25 20:42:14 +09002823 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002824 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2825 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002826 }
2827 }
2828
satok4fc87d62011-05-20 16:13:43 +09002829 @Override
satok735cf382010-11-11 20:40:09 +09002830 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002831 if (!calledFromValidUser()) {
2832 return false;
2833 }
satok735cf382010-11-11 20:40:09 +09002834 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002835 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002836 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002837 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002838 lastImi = mMethodMap.get(lastIme.first);
2839 } else {
2840 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002841 }
satok4fc87d62011-05-20 16:13:43 +09002842 String targetLastImiId = null;
2843 int subtypeId = NOT_A_SUBTYPE_ID;
2844 if (lastIme != null && lastImi != null) {
2845 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002846 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002847 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2848 : mCurrentSubtype.hashCode();
2849 // If the last IME is the same as the current IME and the last subtype is not
2850 // defined, there is no need to switch to the last IME.
2851 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2852 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002853 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002854 }
2855 }
2856
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002857 if (TextUtils.isEmpty(targetLastImiId)
2858 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002859 // This is a safety net. If the currentSubtype can't be added to the history
2860 // and the framework couldn't find the last ime, we will make the last ime be
2861 // the most applicable enabled keyboard subtype of the system imes.
2862 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2863 if (enabled != null) {
2864 final int N = enabled.size();
2865 final String locale = mCurrentSubtype == null
2866 ? mRes.getConfiguration().locale.toString()
2867 : mCurrentSubtype.getLocale();
2868 for (int i = 0; i < N; ++i) {
2869 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002870 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002871 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002872 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2873 InputMethodUtils.getSubtypes(imi),
2874 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002875 if (keyboardSubtype != null) {
2876 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002877 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002878 imi, keyboardSubtype.hashCode());
2879 if(keyboardSubtype.getLocale().equals(locale)) {
2880 break;
2881 }
2882 }
2883 }
2884 }
2885 }
2886 }
2887
2888 if (!TextUtils.isEmpty(targetLastImiId)) {
2889 if (DEBUG) {
2890 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2891 + ", from: " + mCurMethodId + ", " + subtypeId);
2892 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002893 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002894 return true;
2895 } else {
2896 return false;
2897 }
satok735cf382010-11-11 20:40:09 +09002898 }
2899 }
2900
satoke7c6998e2011-06-03 17:57:59 +09002901 @Override
satok688bd472012-02-09 20:09:17 +09002902 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002903 if (!calledFromValidUser()) {
2904 return false;
2905 }
satok688bd472012-02-09 20:09:17 +09002906 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002907 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002908 return false;
2909 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002910 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002911 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2912 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002913 if (nextSubtype == null) {
2914 return false;
2915 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002916 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2917 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002918 return true;
2919 }
2920 }
2921
2922 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002923 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2924 if (!calledFromValidUser()) {
2925 return false;
2926 }
2927 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002928 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002929 return false;
2930 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002931 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002932 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2933 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002934 if (nextSubtype == null) {
2935 return false;
2936 }
2937 return true;
2938 }
2939 }
2940
2941 @Override
satok68f1b782011-04-11 14:26:04 +09002942 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002943 if (!calledFromValidUser()) {
2944 return null;
2945 }
satok68f1b782011-04-11 14:26:04 +09002946 synchronized (mMethodMap) {
2947 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2948 // TODO: Handle the case of the last IME with no subtypes
2949 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2950 || TextUtils.isEmpty(lastIme.second)) return null;
2951 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2952 if (lastImi == null) return null;
2953 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002954 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002955 final int lastSubtypeId =
2956 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002957 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2958 return null;
2959 }
2960 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002961 } catch (NumberFormatException e) {
2962 return null;
2963 }
2964 }
2965 }
2966
satoke7c6998e2011-06-03 17:57:59 +09002967 @Override
satokee5e77c2011-09-02 18:50:15 +09002968 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002969 if (!calledFromValidUser()) {
2970 return;
2971 }
satok91e88122011-07-18 11:11:42 +09002972 // By this IPC call, only a process which shares the same uid with the IME can add
2973 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002974 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002975 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002976 if (!mSystemReady) {
2977 return;
2978 }
satok91e88122011-07-18 11:11:42 +09002979 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002980 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002981 final String[] packageInfos;
2982 try {
2983 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2984 } catch (RemoteException e) {
2985 Slog.e(TAG, "Failed to get package infos");
2986 return;
2987 }
satok91e88122011-07-18 11:11:42 +09002988 if (packageInfos != null) {
2989 final int packageNum = packageInfos.length;
2990 for (int i = 0; i < packageNum; ++i) {
2991 if (packageInfos[i].equals(imi.getPackageName())) {
2992 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002993 final long ident = Binder.clearCallingIdentity();
2994 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002995 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002996 } finally {
2997 Binder.restoreCallingIdentity(ident);
2998 }
satokee5e77c2011-09-02 18:50:15 +09002999 return;
satok91e88122011-07-18 11:11:42 +09003000 }
3001 }
3002 }
satoke7c6998e2011-06-03 17:57:59 +09003003 }
satokee5e77c2011-09-02 18:50:15 +09003004 return;
satoke7c6998e2011-06-03 17:57:59 +09003005 }
3006
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003007 @Override
3008 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003009 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003010 }
3011
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003012 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003013 public void clearLastInputMethodWindowForTransition(IBinder token) {
3014 if (!calledFromValidUser()) {
3015 return;
3016 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003017 synchronized (mMethodMap) {
3018 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003019 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003020 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003021 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003022 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003023 }
3024
3025 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003026 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003027 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003028 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003029 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003030 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003031 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3032 if (DEBUG) {
3033 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3034 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3035 + " actual: " + sequenceNumber);
3036 }
3037 return;
3038 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003039 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3040 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003041 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003042 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003043 }
3044 }
3045
satok28203512010-11-24 11:06:49 +09003046 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003048 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3049 }
3050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003052 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3053 if (token == null) {
3054 if (mContext.checkCallingOrSelfPermission(
3055 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3056 != PackageManager.PERMISSION_GRANTED) {
3057 throw new SecurityException(
3058 "Using null token requires permission "
3059 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003061 } else if (mCurToken != token) {
3062 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3063 + " token: " + token);
3064 return;
3065 }
3066
3067 final long ident = Binder.clearCallingIdentity();
3068 try {
3069 setInputMethodLocked(id, subtypeId);
3070 } finally {
3071 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 }
3073 }
3074
satok42c5a162011-05-26 16:46:14 +09003075 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003077 if (!calledFromValidUser()) {
3078 return;
3079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003081 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 return;
3083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 long ident = Binder.clearCallingIdentity();
3085 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003086 hideCurrentInputLocked(flags, null);
3087 } finally {
3088 Binder.restoreCallingIdentity(ident);
3089 }
3090 }
3091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003092
satok42c5a162011-05-26 16:46:14 +09003093 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003094 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003095 if (!calledFromValidUser()) {
3096 return;
3097 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003098 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003099 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003100 return;
3101 }
3102 long ident = Binder.clearCallingIdentity();
3103 try {
3104 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 } finally {
3106 Binder.restoreCallingIdentity(ident);
3107 }
3108 }
3109 }
3110
3111 void setEnabledSessionInMainThread(SessionState session) {
3112 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003113 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003115 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003116 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 } catch (RemoteException e) {
3118 }
3119 }
3120 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003121 if (mEnabledSession != null && mEnabledSession.session != null) {
3122 try {
3123 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3124 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3125 } catch (RemoteException e) {
3126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 }
3128 }
3129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003130
satok42c5a162011-05-26 16:46:14 +09003131 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003133 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003135 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003136 final boolean showAuxSubtypes;
3137 switch (msg.arg1) {
3138 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3139 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3140 // implemented so that auxiliary subtypes will be excluded when the soft
3141 // keyboard is invisible.
3142 showAuxSubtypes = mInputShown;
3143 break;
3144 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3145 showAuxSubtypes = true;
3146 break;
3147 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3148 showAuxSubtypes = false;
3149 break;
3150 default:
3151 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3152 return false;
3153 }
3154 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003155 return true;
3156
satok47a44912010-10-06 16:03:58 +09003157 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003158 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003159 return true;
3160
3161 case MSG_SHOW_IM_CONFIG:
3162 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003163 return true;
3164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 case MSG_UNBIND_INPUT:
3168 try {
3169 ((IInputMethod)msg.obj).unbindInput();
3170 } catch (RemoteException e) {
3171 // There is nothing interesting about the method dying.
3172 }
3173 return true;
3174 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003175 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 try {
3177 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3178 } catch (RemoteException e) {
3179 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003180 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 return true;
3182 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003183 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003185 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003186 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003187 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 } catch (RemoteException e) {
3189 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003190 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 return true;
3192 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003193 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003195 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003196 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003197 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 } 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;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003202 case MSG_HIDE_CURRENT_INPUT_METHOD:
3203 synchronized (mMethodMap) {
3204 hideCurrentInputLocked(0, null);
3205 }
3206 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003208 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003210 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3212 } catch (RemoteException e) {
3213 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003214 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003216 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003217 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003218 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003219 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003221 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003223 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003224 // Dispose the channel if the input method is not local to this process
3225 // because the remote proxy will get its own copy when unparceled.
3226 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003227 channel.dispose();
3228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003230 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003234
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003235 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003236 final int missingMethods = msg.arg1;
3237 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003238 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003239 final IBinder startInputToken = (IBinder) args.arg1;
3240 final SessionState session = (SessionState) args.arg2;
3241 final IInputContext inputContext = (IInputContext) args.arg3;
3242 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003245 session.method.startInput(startInputToken, inputContext, missingMethods,
3246 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 } catch (RemoteException e) {
3248 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003249 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003254
Yohei Yukawa33e81792015-11-17 21:14:42 -08003255 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003257 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 } catch (RemoteException e) {
3259 // There is nothing interesting about the last client dying.
3260 }
3261 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003262 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003263 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003264 IInputMethodClient client = (IInputMethodClient)args.arg1;
3265 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003267 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003269 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003270 } finally {
3271 // Dispose the channel if the input method is not local to this process
3272 // because the remote proxy will get its own copy when unparceled.
3273 if (res.channel != null && Binder.isProxy(client)) {
3274 res.channel.dispose();
3275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003277 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003279 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003280 case MSG_SET_ACTIVE:
3281 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003282 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003283 } catch (RemoteException e) {
3284 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3285 + ((ClientState)msg.obj).pid + " uid "
3286 + ((ClientState)msg.obj).uid);
3287 }
3288 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003289 case MSG_SET_INTERACTIVE:
3290 handleSetInteractive(msg.arg1 != 0);
3291 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003292 case MSG_SWITCH_IME:
3293 handleSwitchInputMethod(msg.arg1 != 0);
3294 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003295 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3296 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003297 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003298 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003299 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003300 } catch (RemoteException e) {
3301 Slog.w(TAG, "Got RemoteException sending "
3302 + "setUserActionNotificationSequenceNumber("
3303 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003304 + clientState.pid + " uid "
3305 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003306 }
3307 return true;
3308 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003309 case MSG_REPORT_FULLSCREEN_MODE: {
3310 final boolean fullscreen = msg.arg1 != 0;
3311 final ClientState clientState = (ClientState)msg.obj;
3312 try {
3313 clientState.client.reportFullscreenMode(fullscreen);
3314 } catch (RemoteException e) {
3315 Slog.w(TAG, "Got RemoteException sending "
3316 + "reportFullscreen(" + fullscreen + ") notification to pid="
3317 + clientState.pid + " uid=" + clientState.uid);
3318 }
3319 return true;
3320 }
satok01038492012-04-09 21:08:27 +09003321
3322 // --------------------------------------------------------------
3323 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003324 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003325 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003326 case MSG_SYSTEM_UNLOCK_USER:
3327 final int userId = msg.arg1;
3328 onUnlockUser(userId);
3329 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 }
3331 return false;
3332 }
3333
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003334 private void handleSetInteractive(final boolean interactive) {
3335 synchronized (mMethodMap) {
3336 mIsInteractive = interactive;
3337 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3338
3339 // Inform the current client of the change in active status
3340 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003341 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3342 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3343 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003344 }
3345 }
3346 }
3347
Yohei Yukawaae61f712015-12-09 13:00:10 -08003348 private void handleSwitchInputMethod(final boolean forwardDirection) {
3349 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003350 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003351 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003352 if (nextSubtype == null) {
3353 return;
3354 }
3355 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003356 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3357 if (newInputMethodInfo == null) {
3358 return;
3359 }
3360 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3361 newInputMethodInfo, mCurrentSubtype);
3362 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003363 if (mSubtypeSwitchedByShortCutToast == null) {
3364 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3365 Toast.LENGTH_SHORT);
3366 } else {
3367 mSubtypeSwitchedByShortCutToast.setText(toastText);
3368 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003369 mSubtypeSwitchedByShortCutToast.show();
3370 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003371 }
3372 }
3373
satokdc9ddae2011-10-06 12:22:36 +09003374 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003375 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3376 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003377 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003378 if (DEBUG) {
3379 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3380 }
satok723a27e2010-11-11 14:58:11 +09003381 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003382 return true;
3383 }
3384
3385 return false;
3386 }
3387
Yohei Yukawa94e33302016-02-12 19:37:03 -08003388 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003389 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003390 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003391 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003392 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003393 if (!mSystemReady) {
3394 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3395 return;
3396 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003397 mMethodList.clear();
3398 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003399 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003400 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003401
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003402 // Use for queryIntentServicesAsUser
3403 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003405 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3406 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3407 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003408 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003410 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3411 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003412
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003413 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003414 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 for (int i = 0; i < services.size(); ++i) {
3416 ResolveInfo ri = services.get(i);
3417 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003418 final String imeId = InputMethodInfo.computeId(ri);
3419 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3420 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 + ": it does not require the permission "
3422 + android.Manifest.permission.BIND_INPUT_METHOD);
3423 continue;
3424 }
3425
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003426 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003428 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 try {
satoke7c6998e2011-06-03 17:57:59 +09003430 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003431 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003432 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003433 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434
3435 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003436 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003438 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003439 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 }
3441 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003442
Yohei Yukawac4e44912017-02-09 19:30:22 -08003443 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3444 // negatives when the package state remains to be the same but only the component state is
3445 // changed.
3446 {
3447 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3448 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3449 // conservative, but it seems we cannot use it for now (Issue 35176630).
3450 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3451 new Intent(InputMethod.SERVICE_INTERFACE),
3452 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3453 final int N = allInputMethodServices.size();
3454 for (int i = 0; i < N; ++i) {
3455 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003456 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3457 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003458 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003459 }
3460 }
3461
Yohei Yukawa859df052016-02-17 07:56:46 -08003462 // TODO: The following code should find better place to live.
3463 if (!resetDefaultEnabledIme) {
3464 boolean enabledImeFound = false;
3465 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3466 final int N = enabledImes.size();
3467 for (int i = 0; i < N; ++i) {
3468 final InputMethodInfo imi = enabledImes.get(i);
3469 if (mMethodList.contains(imi)) {
3470 enabledImeFound = true;
3471 break;
3472 }
3473 }
3474 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003475 if (DEBUG) {
3476 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3477 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003478 resetDefaultEnabledIme = true;
3479 resetSelectedInputMethodAndSubtypeLocked("");
3480 }
3481 }
3482
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003483 if (resetDefaultEnabledIme) {
3484 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003485 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003486 final int N = defaultEnabledIme.size();
3487 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003488 final InputMethodInfo imi = defaultEnabledIme.get(i);
3489 if (DEBUG) {
3490 Slog.d(TAG, "--- enable ime = " + imi);
3491 }
3492 setInputMethodEnabledLocked(imi.getId(), true);
3493 }
3494 }
3495
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003496 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003497 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003498 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003499 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3500 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003501 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003502 }
3503 } else {
3504 // Double check that the default IME is certainly enabled.
3505 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003506 }
3507 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003508 // Here is not the perfect place to reset the switching controller. Ideally
3509 // mSwitchingController and mSettings should be able to share the same state.
3510 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3511 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003512 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003516
satok217f5482010-12-15 05:19:19 +09003517 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003518 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003519 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003520 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3521 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003522 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003523 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003524 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003525 final int userId;
3526 synchronized (mMethodMap) {
3527 userId = mSettings.getCurrentUserId();
3528 }
3529 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003530 }
3531
3532 private void showConfigureInputMethods() {
3533 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3534 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3535 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3536 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003537 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003538 }
3539
satok2c93efc2012-04-02 19:33:47 +09003540 private boolean isScreenLocked() {
3541 return mKeyguardManager != null
3542 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3543 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003544
Seigo Nonaka14e13912015-05-06 21:04:13 -07003545 private void showInputMethodMenu(boolean showAuxSubtypes) {
3546 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003549 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003550
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003551 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003552 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003553 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003554
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003555 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003556 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003557 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3558 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003559 if (immis == null || immis.size() == 0) {
3560 return;
3561 }
3562
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003563 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564
satok688bd472012-02-09 20:09:17 +09003565 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003566 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003567 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003568
satokc3690562012-01-10 20:14:43 +09003569 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003570 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003571 if (currentSubtype != null) {
3572 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003573 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3574 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003575 }
3576 }
3577
Ken Wakasa761eb372011-03-04 19:06:18 +09003578 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003579 mIms = new InputMethodInfo[N];
3580 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003581 int checkedItem = 0;
3582 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003583 final ImeSubtypeListItem item = imList.get(i);
3584 mIms[i] = item.mImi;
3585 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003586 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003587 int subtypeId = mSubtypeIds[i];
3588 if ((subtypeId == NOT_A_SUBTYPE_ID)
3589 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3590 || (subtypeId == lastInputMethodSubtypeId)) {
3591 checkedItem = i;
3592 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003595
3596 final Context settingsContext = new ContextThemeWrapper(context,
3597 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3598
3599 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003600 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3601 @Override
3602 public void onCancel(DialogInterface dialog) {
3603 hideInputMethodMenu();
3604 }
3605 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003606
3607 final Context dialogContext = mDialogBuilder.getContext();
3608 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3609 com.android.internal.R.styleable.DialogPreference,
3610 com.android.internal.R.attr.alertDialogStyle, 0);
3611 final Drawable dialogIcon = a.getDrawable(
3612 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3613 a.recycle();
3614
3615 mDialogBuilder.setIcon(dialogIcon);
3616
Yohei Yukawad34e1482016-02-11 08:03:52 -08003617 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003618 final View tv = inflater.inflate(
3619 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3620 mDialogBuilder.setCustomTitle(tv);
3621
3622 // Setup layout for a toggle switch of the hardware keyboard
3623 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003624 mSwitchingDialogTitleView
3625 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003626 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003627 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003628 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003629 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003630 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003631 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3632 @Override
3633 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003634 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003635 // Ensure that the input method dialog is dismissed when changing
3636 // the hardware keyboard state.
3637 hideInputMethodMenu();
3638 }
3639 });
3640
Alan Viverette505e3ab2014-11-24 15:22:11 -08003641 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003642 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3643 final OnClickListener choiceListener = new OnClickListener() {
3644 @Override
3645 public void onClick(final DialogInterface dialog, final int which) {
3646 synchronized (mMethodMap) {
3647 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3648 || mSubtypeIds.length <= which) {
3649 return;
satok01038492012-04-09 21:08:27 +09003650 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003651 final InputMethodInfo im = mIms[which];
3652 int subtypeId = mSubtypeIds[which];
3653 adapter.mCheckedItem = which;
3654 adapter.notifyDataSetChanged();
3655 hideInputMethodMenu();
3656 if (im != null) {
3657 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3658 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003659 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003660 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003662 }
3663 }
3664 };
3665 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003668 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003669 final Window w = mSwitchingDialog.getWindow();
3670 final WindowManager.LayoutParams attrs = w.getAttributes();
3671 w.setType(TYPE_INPUT_METHOD_DIALOG);
3672 // Use an alternate token for the dialog for that window manager can group the token
3673 // with other IME windows based on type vs. grouping based on whichever token happens
3674 // to get selected by the system later on.
3675 attrs.token = mSwitchingDialogToken;
3676 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3677 attrs.setTitle("Select input method");
3678 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003679 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 mSwitchingDialog.show();
3681 }
3682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003683
Ken Wakasa05dbb652011-08-22 15:22:43 +09003684 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3685 private final LayoutInflater mInflater;
3686 private final int mTextViewResourceId;
3687 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003688 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003689 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3690 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3691 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003692
Ken Wakasa05dbb652011-08-22 15:22:43 +09003693 mTextViewResourceId = textViewResourceId;
3694 mItemsList = itemsList;
3695 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003696 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003697 }
3698
3699 @Override
3700 public View getView(int position, View convertView, ViewGroup parent) {
3701 final View view = convertView != null ? convertView
3702 : mInflater.inflate(mTextViewResourceId, null);
3703 if (position < 0 || position >= mItemsList.size()) return view;
3704 final ImeSubtypeListItem item = mItemsList.get(position);
3705 final CharSequence imeName = item.mImeName;
3706 final CharSequence subtypeName = item.mSubtypeName;
3707 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3708 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3709 if (TextUtils.isEmpty(subtypeName)) {
3710 firstTextView.setText(imeName);
3711 secondTextView.setVisibility(View.GONE);
3712 } else {
3713 firstTextView.setText(subtypeName);
3714 secondTextView.setText(imeName);
3715 secondTextView.setVisibility(View.VISIBLE);
3716 }
3717 final RadioButton radioButton =
3718 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3719 radioButton.setChecked(position == mCheckedItem);
3720 return view;
3721 }
3722 }
3723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003725 synchronized (mMethodMap) {
3726 hideInputMethodMenuLocked();
3727 }
3728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003729
The Android Open Source Project10592532009-03-18 17:39:46 -07003730 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003731 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732
The Android Open Source Project10592532009-03-18 17:39:46 -07003733 if (mSwitchingDialog != null) {
3734 mSwitchingDialog.dismiss();
3735 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003737
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003738 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003739 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003740 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003744
satok42c5a162011-05-26 16:46:14 +09003745 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003747 // TODO: Make this work even for non-current users?
3748 if (!calledFromValidUser()) {
3749 return false;
3750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 synchronized (mMethodMap) {
3752 if (mContext.checkCallingOrSelfPermission(
3753 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3754 != PackageManager.PERMISSION_GRANTED) {
3755 throw new SecurityException(
3756 "Requires permission "
3757 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3758 }
Anna Galusza9b278112016-01-04 11:37:37 -08003759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 long ident = Binder.clearCallingIdentity();
3761 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003762 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 } finally {
3764 Binder.restoreCallingIdentity(ident);
3765 }
3766 }
3767 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003768
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003769 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3770 // Make sure this is a valid input method.
3771 InputMethodInfo imm = mMethodMap.get(id);
3772 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003773 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003774 }
3775
satokd87c2592010-09-29 11:52:06 +09003776 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3777 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003778
satokd87c2592010-09-29 11:52:06 +09003779 if (enabled) {
3780 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3781 if (pair.first.equals(id)) {
3782 // We are enabling this input method, but it is already enabled.
3783 // Nothing to do. The previous state was enabled.
3784 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003785 }
3786 }
satokd87c2592010-09-29 11:52:06 +09003787 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3788 // Previous state was disabled.
3789 return false;
3790 } else {
3791 StringBuilder builder = new StringBuilder();
3792 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3793 builder, enabledInputMethodsList, id)) {
3794 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003795 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003796 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3797 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3798 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003799 }
3800 // Previous state was enabled.
3801 return true;
3802 } else {
3803 // We are disabling the input method but it is already disabled.
3804 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003805 return false;
3806 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003807 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003808 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003809
satok723a27e2010-11-11 14:58:11 +09003810 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3811 boolean setSubtypeOnly) {
3812 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003813 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003814
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003815 mCurUserActionNotificationSequenceNumber =
3816 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3817 if (DEBUG) {
3818 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3819 + mCurUserActionNotificationSequenceNumber);
3820 }
3821
3822 if (mCurClient != null && mCurClient.client != null) {
3823 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3824 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003825 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003826 }
3827
satok723a27e2010-11-11 14:58:11 +09003828 // Set Subtype here
3829 if (imi == null || subtypeId < 0) {
3830 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003831 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003832 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003833 if (subtypeId < imi.getSubtypeCount()) {
3834 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3835 mSettings.putSelectedSubtype(subtype.hashCode());
3836 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003837 } else {
3838 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003839 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003840 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003841 }
satokab751aa2010-09-14 19:17:36 +09003842 }
satok723a27e2010-11-11 14:58:11 +09003843
Yohei Yukawa68645a62016-02-17 07:54:20 -08003844 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003845 // Set InputMethod here
3846 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3847 }
3848 }
3849
3850 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3851 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3852 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3853 // newDefaultIme is empty when there is no candidate for the selected IME.
3854 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3855 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3856 if (subtypeHashCode != null) {
3857 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003858 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003859 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003860 } catch (NumberFormatException e) {
3861 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3862 }
3863 }
3864 }
3865 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003866 }
3867
satok4e4569d2010-11-19 18:45:53 +09003868 // If there are no selected shortcuts, tries finding the most applicable ones.
3869 private Pair<InputMethodInfo, InputMethodSubtype>
3870 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3871 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3872 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003873 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003874 boolean foundInSystemIME = false;
3875
3876 // Search applicable subtype for each InputMethodInfo
3877 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003878 final String imiId = imi.getId();
3879 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3880 continue;
3881 }
satokcd7cd292010-11-20 15:46:23 +09003882 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003883 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003884 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003885 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003886 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003887 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003888 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003889 }
satokdf31ae62011-01-15 06:19:44 +09003890 // 2. Search by the system locale from enabledSubtypes.
3891 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003892 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003893 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003894 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003895 }
satoka86f5e42011-09-02 17:12:42 +09003896 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003897 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003898 final ArrayList<InputMethodSubtype> subtypesForSearch =
3899 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003900 ? InputMethodUtils.getSubtypes(imi)
3901 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003902 // 4. Search by the current subtype's locale from all subtypes.
3903 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003904 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003905 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003906 }
3907 // 5. Search by the system locale from all subtypes.
3908 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003909 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003910 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003911 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003912 }
satokcd7cd292010-11-20 15:46:23 +09003913 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003914 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003915 // The current input method is the most applicable IME.
3916 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003917 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003918 break;
satok7599a7f2010-12-22 13:45:23 +09003919 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003920 // The system input method is 2nd applicable IME.
3921 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003922 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003923 if ((imi.getServiceInfo().applicationInfo.flags
3924 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3925 foundInSystemIME = true;
3926 }
satok4e4569d2010-11-19 18:45:53 +09003927 }
3928 }
3929 }
3930 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003931 if (mostApplicableIMI != null) {
3932 Slog.w(TAG, "Most applicable shortcut input method was:"
3933 + mostApplicableIMI.getId());
3934 if (mostApplicableSubtype != null) {
3935 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3936 + "," + mostApplicableSubtype.getMode() + ","
3937 + mostApplicableSubtype.getLocale());
3938 }
3939 }
satok4e4569d2010-11-19 18:45:53 +09003940 }
satokcd7cd292010-11-20 15:46:23 +09003941 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003942 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003943 } else {
3944 return null;
3945 }
3946 }
3947
satokab751aa2010-09-14 19:17:36 +09003948 /**
3949 * @return Return the current subtype of this input method.
3950 */
satok42c5a162011-05-26 16:46:14 +09003951 @Override
satokab751aa2010-09-14 19:17:36 +09003952 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003953 // TODO: Make this work even for non-current users?
3954 if (!calledFromValidUser()) {
3955 return null;
3956 }
3957 synchronized (mMethodMap) {
3958 return getCurrentInputMethodSubtypeLocked();
3959 }
3960 }
3961
3962 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003963 if (mCurMethodId == null) {
3964 return null;
3965 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003966 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003967 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3968 if (imi == null || imi.getSubtypeCount() == 0) {
3969 return null;
satok4e4569d2010-11-19 18:45:53 +09003970 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003971 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003972 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3973 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003974 if (subtypeId == NOT_A_SUBTYPE_ID) {
3975 // If there are no selected subtypes, the framework will try to find
3976 // the most applicable subtype from explicitly or implicitly enabled
3977 // subtypes.
3978 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003979 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003980 // If there is only one explicitly or implicitly enabled subtype,
3981 // just returns it.
3982 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3983 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3984 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003985 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003986 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003987 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003988 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003989 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003990 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3991 true);
satok4e4569d2010-11-19 18:45:53 +09003992 }
satok3ef8b292010-11-23 06:06:29 +09003993 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003994 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003995 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003996 }
3997 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003998 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003999 }
4000
satok4e4569d2010-11-19 18:45:53 +09004001 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004002 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004003 @Override
satok4e4569d2010-11-19 18:45:53 +09004004 public List getShortcutInputMethodsAndSubtypes() {
4005 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004006 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004007 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004008 // If there are no selected shortcut subtypes, the framework will try to find
4009 // the most applicable subtype from all subtypes whose mode is
4010 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004011 Pair<InputMethodInfo, InputMethodSubtype> info =
4012 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004013 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004014 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004015 ret.add(info.first);
4016 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004017 }
satok3da92232011-01-11 22:46:30 +09004018 return ret;
satokf3db1af2010-11-23 13:34:33 +09004019 }
satokf3db1af2010-11-23 13:34:33 +09004020 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4021 ret.add(imi);
4022 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4023 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004024 }
4025 }
satokf3db1af2010-11-23 13:34:33 +09004026 return ret;
satok4e4569d2010-11-19 18:45:53 +09004027 }
4028 }
4029
satok42c5a162011-05-26 16:46:14 +09004030 @Override
satokb66d2872010-11-10 01:04:04 +09004031 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004032 // TODO: Make this work even for non-current users?
4033 if (!calledFromValidUser()) {
4034 return false;
4035 }
satokb66d2872010-11-10 01:04:04 +09004036 synchronized (mMethodMap) {
4037 if (subtype != null && mCurMethodId != null) {
4038 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004039 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004040 if (subtypeId != NOT_A_SUBTYPE_ID) {
4041 setInputMethodLocked(mCurMethodId, subtypeId);
4042 return true;
4043 }
4044 }
4045 return false;
4046 }
4047 }
4048
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004049 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004050 private static class InputMethodFileManager {
4051 private static final String SYSTEM_PATH = "system";
4052 private static final String INPUT_METHOD_PATH = "inputmethod";
4053 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4054 private static final String NODE_SUBTYPES = "subtypes";
4055 private static final String NODE_SUBTYPE = "subtype";
4056 private static final String NODE_IMI = "imi";
4057 private static final String ATTR_ID = "id";
4058 private static final String ATTR_LABEL = "label";
4059 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004060 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004061 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004062 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004063 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4064 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4065 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004066 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004067 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4068 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004069 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004070 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004071 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004072 if (methodMap == null) {
4073 throw new NullPointerException("methodMap is null");
4074 }
4075 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004076 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004077 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4078 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004079 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004080 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004081 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4082 }
4083 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4084 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4085 if (!subtypeFile.exists()) {
4086 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004087 writeAdditionalInputMethodSubtypes(
4088 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004089 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004090 readAdditionalInputMethodSubtypes(
4091 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004092 }
4093 }
4094
4095 private void deleteAllInputMethodSubtypes(String imiId) {
4096 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004097 mAdditionalSubtypesMap.remove(imiId);
4098 writeAdditionalInputMethodSubtypes(
4099 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004100 }
4101 }
4102
4103 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004104 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004105 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004106 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004107 final int N = additionalSubtypes.length;
4108 for (int i = 0; i < N; ++i) {
4109 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004110 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004111 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004112 } else {
4113 Slog.w(TAG, "Duplicated subtype definition found: "
4114 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004115 }
4116 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004117 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4118 writeAdditionalInputMethodSubtypes(
4119 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004120 }
4121 }
4122
4123 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4124 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004125 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004126 }
4127 }
4128
4129 private static void writeAdditionalInputMethodSubtypes(
4130 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4131 HashMap<String, InputMethodInfo> methodMap) {
4132 // Safety net for the case that this function is called before methodMap is set.
4133 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4134 FileOutputStream fos = null;
4135 try {
4136 fos = subtypesFile.startWrite();
4137 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004138 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004139 out.startDocument(null, true);
4140 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4141 out.startTag(null, NODE_SUBTYPES);
4142 for (String imiId : allSubtypes.keySet()) {
4143 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4144 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4145 continue;
4146 }
4147 out.startTag(null, NODE_IMI);
4148 out.attribute(null, ATTR_ID, imiId);
4149 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4150 final int N = subtypesList.size();
4151 for (int i = 0; i < N; ++i) {
4152 final InputMethodSubtype subtype = subtypesList.get(i);
4153 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004154 if (subtype.hasSubtypeId()) {
4155 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4156 String.valueOf(subtype.getSubtypeId()));
4157 }
satoke7c6998e2011-06-03 17:57:59 +09004158 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4159 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4160 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004161 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4162 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004163 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4164 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4165 out.attribute(null, ATTR_IS_AUXILIARY,
4166 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004167 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4168 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004169 out.endTag(null, NODE_SUBTYPE);
4170 }
4171 out.endTag(null, NODE_IMI);
4172 }
4173 out.endTag(null, NODE_SUBTYPES);
4174 out.endDocument();
4175 subtypesFile.finishWrite(fos);
4176 } catch (java.io.IOException e) {
4177 Slog.w(TAG, "Error writing subtypes", e);
4178 if (fos != null) {
4179 subtypesFile.failWrite(fos);
4180 }
4181 }
4182 }
4183
4184 private static void readAdditionalInputMethodSubtypes(
4185 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4186 if (allSubtypes == null || subtypesFile == null) return;
4187 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004188 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004189 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004190 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004191 int type = parser.getEventType();
4192 // Skip parsing until START_TAG
4193 while ((type = parser.next()) != XmlPullParser.START_TAG
4194 && type != XmlPullParser.END_DOCUMENT) {}
4195 String firstNodeName = parser.getName();
4196 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4197 throw new XmlPullParserException("Xml doesn't start with subtypes");
4198 }
4199 final int depth =parser.getDepth();
4200 String currentImiId = null;
4201 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4202 while (((type = parser.next()) != XmlPullParser.END_TAG
4203 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4204 if (type != XmlPullParser.START_TAG)
4205 continue;
4206 final String nodeName = parser.getName();
4207 if (NODE_IMI.equals(nodeName)) {
4208 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4209 if (TextUtils.isEmpty(currentImiId)) {
4210 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4211 continue;
4212 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004213 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004214 allSubtypes.put(currentImiId, tempSubtypesArray);
4215 } else if (NODE_SUBTYPE.equals(nodeName)) {
4216 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4217 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4218 continue;
4219 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004220 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004221 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004222 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004223 parser.getAttributeValue(null, ATTR_LABEL));
4224 final String imeSubtypeLocale =
4225 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004226 final String languageTag =
4227 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004228 final String imeSubtypeMode =
4229 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4230 final String imeSubtypeExtraValue =
4231 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004232 final boolean isAuxiliary = "1".equals(String.valueOf(
4233 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004234 final boolean isAsciiCapable = "1".equals(String.valueOf(
4235 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004236 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004237 .setSubtypeNameResId(label)
4238 .setSubtypeIconResId(icon)
4239 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004240 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004241 .setSubtypeMode(imeSubtypeMode)
4242 .setSubtypeExtraValue(imeSubtypeExtraValue)
4243 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004244 .setIsAsciiCapable(isAsciiCapable);
4245 final String subtypeIdString =
4246 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4247 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004248 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004249 }
4250 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004251 }
4252 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004253 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4254 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004255 return;
satoke7c6998e2011-06-03 17:57:59 +09004256 }
4257 }
4258 }
4259
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004260 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4261 @NonNull
4262 private final Handler mHandler;
4263
4264 LocalServiceImpl(@NonNull final Handler handler) {
4265 mHandler = handler;
4266 }
4267
4268 @Override
4269 public void setInteractive(boolean interactive) {
4270 // Do everything in handler so as not to block the caller.
4271 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4272 interactive ? 1 : 0, 0));
4273 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004274
4275 @Override
4276 public void switchInputMethod(boolean forwardDirection) {
4277 // Do everything in handler so as not to block the caller.
4278 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4279 forwardDirection ? 1 : 0, 0));
4280 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004281
4282 @Override
4283 public void hideCurrentInputMethod() {
4284 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4285 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4286 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004287 }
4288
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004289 private static String imeWindowStatusToString(final int imeWindowVis) {
4290 final StringBuilder sb = new StringBuilder();
4291 boolean first = true;
4292 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4293 sb.append("Active");
4294 first = false;
4295 }
4296 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4297 if (!first) {
4298 sb.append("|");
4299 }
4300 sb.append("Visible");
4301 }
4302 return sb.toString();
4303 }
4304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004306 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4307 @Nullable Uri contentUri, @Nullable String packageName) {
4308 if (!calledFromValidUser()) {
4309 return null;
4310 }
4311
4312 if (token == null) {
4313 throw new NullPointerException("token");
4314 }
4315 if (packageName == null) {
4316 throw new NullPointerException("packageName");
4317 }
4318 if (contentUri == null) {
4319 throw new NullPointerException("contentUri");
4320 }
4321 final String contentUriScheme = contentUri.getScheme();
4322 if (!"content".equals(contentUriScheme)) {
4323 throw new InvalidParameterException("contentUri must have content scheme");
4324 }
4325
4326 synchronized (mMethodMap) {
4327 final int uid = Binder.getCallingUid();
4328 if (mCurMethodId == null) {
4329 return null;
4330 }
4331 if (mCurToken != token) {
4332 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4333 + " token=" + token);
4334 return null;
4335 }
4336 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4337 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4338 // nature of our system. Let's compare it with our internal record.
4339 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4340 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4341 + mCurAttribute.packageName + " packageName=" + packageName);
4342 return null;
4343 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004344 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004345 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004346 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004347 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004348 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4349 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4350 imeUserId);
4351 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4352 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4353 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4354 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4355 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4356 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4357 // actually allowed to "uid", which is guaranteed to be the IME's one.
4358 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4359 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004360 }
4361 }
4362
4363 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004364 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4365 if (!calledFromValidUser()) {
4366 return;
4367 }
4368 synchronized (mMethodMap) {
4369 if (!calledWithValidToken(token)) {
4370 return;
4371 }
4372 if (mCurClient != null && mCurClient.client != null) {
4373 mInFullscreenMode = fullscreen;
4374 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4375 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4376 }
4377 }
4378 }
4379
4380 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004382 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383
4384 IInputMethod method;
4385 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004386 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 synchronized (mMethodMap) {
4391 p.println("Current Input Method Manager state:");
4392 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004393 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 for (int i=0; i<N; i++) {
4395 InputMethodInfo info = mMethodList.get(i);
4396 p.println(" InputMethod #" + i + ":");
4397 info.dump(p, " ");
4398 }
4399 p.println(" Clients:");
4400 for (ClientState ci : mClients.values()) {
4401 p.println(" Client " + ci + ":");
4402 p.println(" client=" + ci.client);
4403 p.println(" inputContext=" + ci.inputContext);
4404 p.println(" sessionRequested=" + ci.sessionRequested);
4405 p.println(" curSession=" + ci.curSession);
4406 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004407 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004409 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004410 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4411 + " softInputMode=" +
4412 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4413 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004414 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4416 + " mBoundToMethod=" + mBoundToMethod);
4417 p.println(" mCurToken=" + mCurToken);
4418 p.println(" mCurIntent=" + mCurIntent);
4419 method = mCurMethod;
4420 p.println(" mCurMethod=" + mCurMethod);
4421 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004422 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 p.println(" mShowRequested=" + mShowRequested
4424 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4425 + " mShowForced=" + mShowForced
4426 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004427 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004428 p.println(" mCurUserActionNotificationSequenceNumber="
4429 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004430 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004431 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004432 p.println(" mSwitchingController:");
4433 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004434 p.println(" mSettings:");
4435 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004436
4437 p.println(" mStartInputHistory:");
4438 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004440
Jeff Brownb88102f2010-09-08 11:49:43 -07004441 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 pw.flush();
4444 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004445 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4446 } catch (IOException | RemoteException e) {
4447 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004449 } else {
4450 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004452
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004453 if (focusedWindowClient != null && client != focusedWindowClient) {
4454 p.println(" ");
4455 p.println("Warning: Current input method client doesn't match the last focused. "
4456 + "window.");
4457 p.println("Dumping input method client in the last focused window just in case.");
4458 p.println(" ");
4459 pw.flush();
4460 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004461 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4462 } catch (IOException | RemoteException e) {
4463 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004464 }
4465 }
4466
Jeff Brownb88102f2010-09-08 11:49:43 -07004467 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 pw.flush();
4470 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004471 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4472 } catch (IOException | RemoteException e) {
4473 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004475 } else {
4476 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 }
4478 }
4479}