blob: 0440845e49a03096b2d1d77cd2ef44321bb307d4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050031import com.android.internal.notification.SystemNotificationChannels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070033import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070034import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090035import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import com.android.internal.view.IInputContext;
37import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import com.android.internal.view.IInputMethodClient;
39import com.android.internal.view.IInputMethodManager;
40import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070041import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080043import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080044import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
satoke7c6998e2011-06-03 17:57:59 +090046import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090048import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070050import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070051import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070052import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080053import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080054import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070055import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070057import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090058import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090059import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090060import android.app.Notification;
61import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070062import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090063import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080065import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.ContentResolver;
67import android.content.Context;
68import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090070import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090072import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070074import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090075import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.content.pm.PackageManager;
77import android.content.pm.ResolveInfo;
78import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070079import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.content.res.Resources;
81import android.content.res.TypedArray;
82import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080083import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080084import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080085import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070086import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040088import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070089import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090090import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.Handler;
92import android.os.IBinder;
93import android.os.IInterface;
94import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070095import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070097import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080099import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.os.ServiceManager;
101import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900102import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800103import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.provider.Settings;
105import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900106import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700107import android.util.ArrayMap;
108import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700109import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900111import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900112import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.util.PrintWriterPrinter;
114import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900115import android.util.Slog;
116import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900117import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700119import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.view.LayoutInflater;
121import android.view.View;
122import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700123import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700125import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900126import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800128import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700129import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import android.view.inputmethod.InputMethod;
131import android.view.inputmethod.InputMethodInfo;
132import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700133import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900134import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900135import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900136import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900137import android.widget.CompoundButton;
138import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900139import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900140import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900141import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700142import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
satoke7c6998e2011-06-03 17:57:59 +0900144import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900146import java.io.FileInputStream;
147import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148import java.io.IOException;
149import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700150import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100151import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700152import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800153import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900155import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800156import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157import java.util.HashMap;
158import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800159import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800160import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800161import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
163/**
164 * This class provides a system service that manages input methods.
165 */
166public class InputMethodManagerService extends IInputMethodManager.Stub
167 implements ServiceConnection, Handler.Callback {
168 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700169 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700170 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700172 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
173 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
174 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_UNBIND_INPUT = 1000;
177 static final int MSG_BIND_INPUT = 1010;
178 static final int MSG_SHOW_SOFT_INPUT = 1020;
179 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700180 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 static final int MSG_ATTACH_TOKEN = 1040;
182 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800185
Yohei Yukawa33e81792015-11-17 21:14:42 -0800186 static final int MSG_UNBIND_CLIENT = 3000;
187 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700188 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700189 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900190 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800191 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800192 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
satok01038492012-04-09 21:08:27 +0900194 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
195
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700196 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
197
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700198 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
satokf9f01002011-05-19 21:31:50 +0900200 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
201
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900202 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900203 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900204
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700205 /**
206 * Binding flags for establishing connection to the {@link InputMethodService}.
207 */
208 private static final int IME_CONNECTION_BIND_FLAGS =
209 Context.BIND_AUTO_CREATE
210 | Context.BIND_NOT_VISIBLE
211 | Context.BIND_NOT_FOREGROUND
212 | Context.BIND_SHOWING_UI;
213
214 /**
215 * Binding flags used only while the {@link InputMethodService} is showing window.
216 */
217 private static final int IME_VISIBLE_BIND_FLAGS =
218 Context.BIND_AUTO_CREATE
219 | Context.BIND_TREAT_LIKE_ACTIVITY
220 | Context.BIND_FOREGROUND_SERVICE;
221
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700222 @Retention(SOURCE)
223 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
224 private @interface HardKeyboardBehavior {
225 int WIRELESS_AFFORDANCE = 0;
226 int WIRED_AFFORDANCE = 1;
227 }
satok4e4569d2010-11-19 18:45:53 +0900228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800230 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900232 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700235 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700237 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900238 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900239 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900240 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800241 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800242
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900243 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 // All known input methods. mMethodMap also serves as the global
246 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700247 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
248 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900249 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700250 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900251 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252
Yohei Yukawae0733062017-02-09 22:49:35 -0800253 /**
254 * Tracks how many times {@link #mMethodMap} was updated.
255 */
256 @GuardedBy("mMethodMap")
257 private int mMethodMapUpdateCount = 0;
258
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700259 // Used to bring IME service up to visible adjustment while it is being shown.
260 final ServiceConnection mVisibleConnection = new ServiceConnection() {
261 @Override public void onServiceConnected(ComponentName name, IBinder service) {
262 }
263
264 @Override public void onServiceDisconnected(ComponentName name) {
265 }
266 };
267 boolean mVisibleBound = false;
268
satok7cfc0ed2011-06-20 21:29:36 +0900269 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700270 private NotificationManager mNotificationManager;
271 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700272 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400273 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700274 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900275 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900276 private boolean mNotificationShown;
277
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900278 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 final ClientState client;
280 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700281
282 IInputMethodSession session;
283 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 @Override
286 public String toString() {
287 return "SessionState{uid " + client.uid + " pid " + client.pid
288 + " method " + Integer.toHexString(
289 System.identityHashCode(method))
290 + " session " + Integer.toHexString(
291 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700292 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 + "}";
294 }
295
296 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700297 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 client = _client;
299 method = _method;
300 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700301 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800304
Jeff Brownc28867a2013-03-26 15:42:39 -0700305 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 final IInputMethodClient client;
307 final IInputContext inputContext;
308 final int uid;
309 final int pid;
310 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 boolean sessionRequested;
313 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 @Override
316 public String toString() {
317 return "ClientState{" + Integer.toHexString(
318 System.identityHashCode(this)) + " uid " + uid
319 + " pid " + pid + "}";
320 }
321
322 ClientState(IInputMethodClient _client, IInputContext _inputContext,
323 int _uid, int _pid) {
324 client = _client;
325 inputContext = _inputContext;
326 uid = _uid;
327 pid = _pid;
328 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
329 }
330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700332 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700335 * Set once the system is ready to run third party code.
336 */
337 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700339 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700340 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
341 * method. This is to be synchronized with the secure settings keyed with
342 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
343 *
344 * <p>This can be transiently {@code null} when the system is re-initializing input method
345 * settings, e.g., the system locale is just changed.</p>
346 *
347 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
348 * {@link InputMethodManagerService}.</p>
349 *
350 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700352 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * The current binding sequence number, incremented every time there is
357 * a new bind performed.
358 */
359 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
362 * The client that is currently bound to an input method.
363 */
364 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800367 * The last window token that we confirmed to be focused. This is always updated upon reports
368 * from the input method client. If the window state is already changed before the report is
369 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700370 */
371 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700373 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800374 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
375 *
376 * @see #mCurFocusedWindow
377 */
378 int mCurFocusedWindowSoftInputMode;
379
380 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800381 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
382 */
383 ClientState mCurFocusedWindowClient;
384
385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * The input context last provided by the current client.
387 */
388 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700391 * The missing method flags for the input context last provided by the current client.
392 *
393 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
394 */
395 int mCurInputContextMissingMethods;
396
397 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 * The attributes last provided by the current client.
399 */
400 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700403 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700405 *
406 * <p>This can be {@code null} when no input method is connected.</p>
407 *
408 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700410 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
satokab751aa2010-09-14 19:17:36 +0900414 * The current subtype of the current input method.
415 */
416 private InputMethodSubtype mCurrentSubtype;
417
satok4e4569d2010-11-19 18:45:53 +0900418 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900419 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700420 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900421
John Spurlocke0980502013-10-25 11:59:29 -0400422 // Was the keyguard locked when this client became current?
423 private boolean mCurClientInKeyguard;
424
satokab751aa2010-09-14 19:17:36 +0900425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * Set to true if our ServiceConnection is currently actively bound to
427 * a service (whether or not we have gotten its IBinder back yet).
428 */
429 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 /**
432 * Set if the client has asked for the input method to be shown.
433 */
434 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
437 * Set if we were explicitly told to show the input method.
438 */
439 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 /**
442 * Set if we were forced to be shown.
443 */
444 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 /**
447 * Set if we last told the input method to show itself.
448 */
449 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800452 * {@code true} if the current input method is in fullscreen mode.
453 */
454 boolean mInFullscreenMode;
455
456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 * The Intent used to connect to the current input method.
458 */
459 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 /**
462 * The token we have made for the currently active input method, to
463 * identify it in the future.
464 */
465 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /**
468 * If non-null, this is the input method service we are currently connected
469 * to.
470 */
471 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 /**
474 * Time that we last initiated a bind to the input method, to determine
475 * if we should try to disconnect and reconnect to it.
476 */
477 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 /**
480 * Have we called mCurMethod.bindInput()?
481 */
482 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 /**
485 * Currently enabled session. Only touched by service thread, not
486 * protected by a lock.
487 */
488 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700491 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700493 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800494
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900495 int mCurUserActionNotificationSequenceNumber = 0;
496
Joe Onorato857fd9b2011-01-27 15:08:35 -0800497 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900498
499 /**
500 * A set of status bits regarding the active IME.
501 *
502 * <p>This value is a combination of following two bits:</p>
503 * <dl>
504 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
505 * <dd>
506 * If this bit is ON, connected IME is ready to accept touch/key events.
507 * </dd>
508 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
509 * <dd>
510 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
511 * </dd>
512 * </dl>
513 * <em>Do not update this value outside of setImeWindowStatus.</em>
514 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800515 int mImeWindowVis;
516
Ken Wakasa05dbb652011-08-22 15:22:43 +0900517 private AlertDialog.Builder mDialogBuilder;
518 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700519 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900520 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700521 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900522 private InputMethodInfo[] mIms;
523 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800524 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700525 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800526 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900527 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
528 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500529 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700530 @HardKeyboardBehavior
531 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800532
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800533 /**
534 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
535 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
536 * will not affect those tasks that are already posted.
537 *
538 * <p>Posting {@link #MSG_START_INPUT} message basically means that
539 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
540 * back in the current IME process shortly, which will also affect what the current IME starts
541 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
542 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
543 * logical input session between the client application and the current IME.</p>
544 *
545 * <p>Be careful to not keep strong references to this object forever, which can prevent
546 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
547 * </p>
548 */
549 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800550 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
551
552 final int mSequenceNumber;
553 final long mTimestamp;
554 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800555 @NonNull
556 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800557 @NonNull
558 final String mImeId;
559 // @InputMethodClient.StartInputReason
560 final int mStartInputReason;
561 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800562 @Nullable
563 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800564 @NonNull
565 final EditorInfo mEditorInfo;
566 final int mTargetWindowSoftInputMode;
567 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800568
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800569 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
570 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
571 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
572 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
573 mSequenceNumber = sSequenceNumber.getAndIncrement();
574 mTimestamp = SystemClock.uptimeMillis();
575 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800576 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800577 mImeId = imeId;
578 mStartInputReason = startInputReason;
579 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800580 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800581 mEditorInfo = editorInfo;
582 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
583 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800584 }
585 }
586
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800587 @GuardedBy("mMethodMap")
588 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800589
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800590 /**
591 * A ring buffer to store the history of {@link StartInputInfo}.
592 */
593 private static final class StartInputHistory {
594 /**
595 * Entry size for non low-RAM devices.
596 *
597 * <p>TODO: Consider to follow what other system services have been doing to manage
598 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
599 */
600 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
601
602 /**
603 * Entry size for non low-RAM devices.
604 *
605 * <p>TODO: Consider to follow what other system services have been doing to manage
606 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
607 */
608 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
609
610 private static int getEntrySize() {
611 if (ActivityManager.isLowRamDeviceStatic()) {
612 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
613 } else {
614 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
615 }
616 }
617
618 /**
619 * Backing store for the ring bugger.
620 */
621 private final Entry[] mEntries = new Entry[getEntrySize()];
622
623 /**
624 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
625 * write.
626 */
627 private int mNextIndex = 0;
628
629 /**
630 * Recyclable entry to store the information in {@link StartInputInfo}.
631 */
632 private static final class Entry {
633 int mSequenceNumber;
634 long mTimestamp;
635 long mWallTime;
636 @NonNull
637 String mImeTokenString;
638 @NonNull
639 String mImeId;
640 /* @InputMethodClient.StartInputReason */
641 int mStartInputReason;
642 boolean mRestarting;
643 @NonNull
644 String mTargetWindowString;
645 @NonNull
646 EditorInfo mEditorInfo;
647 int mTargetWindowSoftInputMode;
648 int mClientBindSequenceNumber;
649
650 Entry(@NonNull StartInputInfo original) {
651 set(original);
652 }
653
654 void set(@NonNull StartInputInfo original) {
655 mSequenceNumber = original.mSequenceNumber;
656 mTimestamp = original.mTimestamp;
657 mWallTime = original.mWallTime;
658 // Intentionally convert to String so as not to keep a strong reference to a Binder
659 // object.
660 mImeTokenString = String.valueOf(original.mImeToken);
661 mImeId = original.mImeId;
662 mStartInputReason = original.mStartInputReason;
663 mRestarting = original.mRestarting;
664 // Intentionally convert to String so as not to keep a strong reference to a Binder
665 // object.
666 mTargetWindowString = String.valueOf(original.mTargetWindow);
667 mEditorInfo = original.mEditorInfo;
668 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
669 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
670 }
671 }
672
673 /**
674 * Add a new entry and discard the oldest entry as needed.
675 * @param info {@lin StartInputInfo} to be added.
676 */
677 void addEntry(@NonNull StartInputInfo info) {
678 final int index = mNextIndex;
679 if (mEntries[index] == null) {
680 mEntries[index] = new Entry(info);
681 } else {
682 mEntries[index].set(info);
683 }
684 mNextIndex = (mNextIndex + 1) % mEntries.length;
685 }
686
687 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
688 final SimpleDateFormat dataFormat =
689 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
690
691 for (int i = 0; i < mEntries.length; ++i) {
692 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
693 if (entry == null) {
694 continue;
695 }
696 pw.print(prefix);
697 pw.println("StartInput #" + entry.mSequenceNumber + ":");
698
699 pw.print(prefix);
700 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
701 + " (timestamp=" + entry.mTimestamp + ")"
702 + " reason="
703 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
704 + " restarting=" + entry.mRestarting);
705
706 pw.print(prefix);
707 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
708
709 pw.print(prefix);
710 pw.println(" targetWin=" + entry.mTargetWindowString
711 + " [" + entry.mEditorInfo.packageName + "]"
712 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
713
714 pw.print(prefix);
715 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
716 entry.mTargetWindowSoftInputMode));
717
718 pw.print(prefix);
719 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
720 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
721 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
722 + " fieldName=" + entry.mEditorInfo.fieldName
723 + " actionId=" + entry.mEditorInfo.actionId
724 + " actionLabel=" + entry.mEditorInfo.actionLabel);
725 }
726 }
727 }
728
729 @GuardedBy("mMethodMap")
730 @NonNull
731 private final StartInputHistory mStartInputHistory = new StartInputHistory();
732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700734 int mUserId;
735 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700736 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800737 String mLastEnabled = "";
738
Yohei Yukawa81482972015-06-04 00:58:59 -0700739 /**
740 * <em>This constructor must be called within the lock.</em>
741 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 SettingsObserver(Handler handler) {
743 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700744 }
745
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800746 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700747 if (mRegistered && mUserId == userId) {
748 return;
749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700751 if (mRegistered) {
752 mContext.getContentResolver().unregisterContentObserver(this);
753 mRegistered = false;
754 }
755 if (mUserId != userId) {
756 mLastEnabled = "";
757 mUserId = userId;
758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700760 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900761 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700762 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900763 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700764 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700765 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700766 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800767 resolver.registerContentObserver(Settings.Secure.getUriFor(
768 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700769 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771
Michael Wright7b5a96b2014-08-09 19:28:42 -0700772 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800773 final Uri showImeUri = Settings.Secure.getUriFor(
774 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
775 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
776 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700778 if (showImeUri.equals(uri)) {
779 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800780 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
781 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
782 mContext.getContentResolver(),
783 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
784 0, mUserId) == 1;
785 if (mAccessibilityRequestingNoSoftKeyboard) {
786 final boolean showRequested = mShowRequested;
787 hideCurrentInputLocked(0, null);
788 mShowRequested = showRequested;
789 } else if (mShowRequested) {
790 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
791 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700792 } else {
793 boolean enabledChanged = false;
794 String newEnabled = mSettings.getEnabledInputMethodsStr();
795 if (!mLastEnabled.equals(newEnabled)) {
796 mLastEnabled = newEnabled;
797 enabledChanged = true;
798 }
799 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700803
804 @Override
805 public String toString() {
806 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
807 + " mLastEnabled=" + mLastEnabled + "}";
808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800810
Yohei Yukawa79247822017-01-23 15:26:15 -0800811 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900812 @Override
813 public void onReceive(Context context, Intent intent) {
814 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700815 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900816 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700817 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900818 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800819 } else if (Intent.ACTION_USER_ADDED.equals(action)
820 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100821 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800822 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700823 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
824 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
825 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
826 final String prevValue = intent.getStringExtra(
827 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
828 final String newValue = intent.getStringExtra(
829 Intent.EXTRA_SETTING_NEW_VALUE);
830 restoreEnabledInputMethods(mContext, prevValue, newValue);
831 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800832 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800833 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900834 } else {
835 Slog.w(TAG, "Unexpected intent " + intent);
836 }
837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800839
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800840 /**
841 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
842 *
843 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
844 * the users. We should ignore this event if this is about any background user's locale.</p>
845 *
846 * <p>Caution: This method must not be called when system is not ready.</p>
847 */
848 void onActionLocaleChanged() {
849 synchronized (mMethodMap) {
850 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
851 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
852 return;
853 }
854 buildInputMethodListLocked(true);
855 // If the locale is changed, needs to reset the default ime
856 resetDefaultImeLocked(mContext);
857 updateFromSettingsLocked(true);
858 mLastSystemLocales = possibleNewLocale;
859 }
860 }
861
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700862 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
863 // does not attempt to validate on the fly with any installed device policy, so must only
864 // be run in the context of initial device setup.
865 //
866 // TODO: Move this method to InputMethodUtils with adding unit tests.
867 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
868 if (DEBUG_RESTORE) {
869 Slog.i(TAG, "Restoring enabled input methods:");
870 Slog.i(TAG, "prev=" + prevValue);
871 Slog.i(TAG, " new=" + newValue);
872 }
873 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900874 ArrayMap<String, ArraySet<String>> prevMap =
875 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
876 ArrayMap<String, ArraySet<String>> newMap =
877 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700878
879 // Merge the restored ime+subtype enabled states into the live state
880 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
881 final String imeId = entry.getKey();
882 ArraySet<String> prevSubtypes = prevMap.get(imeId);
883 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700884 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700885 prevMap.put(imeId, prevSubtypes);
886 }
887 prevSubtypes.addAll(entry.getValue());
888 }
889
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700890 final String mergedImesAndSubtypesString =
891 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700892 if (DEBUG_RESTORE) {
893 Slog.i(TAG, "Merged IME string:");
894 Slog.i(TAG, " " + mergedImesAndSubtypesString);
895 }
896 Settings.Secure.putString(context.getContentResolver(),
897 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
898 }
899
Yohei Yukawac4e44912017-02-09 19:30:22 -0800900 final class MyPackageMonitor extends PackageMonitor {
901 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800902 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800903 *
904 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
905 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
906 * dynamically unless the entire package is updated, which also always triggers package
907 * rescanning.</p>
908 */
909 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800910 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
911
912 /**
913 * Packages that are appeared, disappeared, or modified for whatever reason.
914 *
915 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
916 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
917 * duplicate elements for our use case.</p>
918 *
919 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
920 * which should be bound to {@link #getRegisteredHandler()}.</p>
921 */
922 private final ArrayList<String> mChangedPackages = new ArrayList<>();
923
924 /**
925 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
926 *
927 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
928 * which should be bound to {@link #getRegisteredHandler()}.</p>
929 */
930 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800931
932 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800933 void clearKnownImePackageNamesLocked() {
934 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800935 }
936
937 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800938 final void addKnownImePackageNameLocked(@NonNull String packageName) {
939 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800940 }
941
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800942 @GuardedBy("mMethodMap")
943 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 final int userId = getChangingUserId();
945 final boolean retval = userId == mSettings.getCurrentUserId();
946 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900947 if (!retval) {
948 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
949 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900950 }
951 return retval;
952 }
953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800955 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800957 if (!isChangingPackagesOfCurrentUserLocked()) {
958 return false;
959 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900960 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 final int N = mMethodList.size();
962 if (curInputMethodId != null) {
963 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800964 InputMethodInfo imi = mMethodList.get(i);
965 if (imi.getId().equals(curInputMethodId)) {
966 for (String pkg : packages) {
967 if (imi.getPackageName().equals(pkg)) {
968 if (!doit) {
969 return true;
970 }
satok723a27e2010-11-11 14:58:11 +0900971 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800972 chooseNewDefaultIMELocked();
973 return true;
974 }
975 }
976 }
977 }
978 }
979 }
980 return false;
981 }
982
983 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800984 public void onBeginPackageChanges() {
985 clearPackageChangeState();
986 }
987
988 @Override
989 public void onPackageAppeared(String packageName, int reason) {
990 if (!mImePackageAppeared) {
991 final PackageManager pm = mContext.getPackageManager();
992 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
993 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
994 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
995 // No need to lock this because we access it only on getRegisteredHandler().
996 if (!services.isEmpty()) {
997 mImePackageAppeared = true;
998 }
999 }
1000 // No need to lock this because we access it only on getRegisteredHandler().
1001 mChangedPackages.add(packageName);
1002 }
1003
1004 @Override
1005 public void onPackageDisappeared(String packageName, int reason) {
1006 // No need to lock this because we access it only on getRegisteredHandler().
1007 mChangedPackages.add(packageName);
1008 }
1009
1010 @Override
1011 public void onPackageModified(String packageName) {
1012 // No need to lock this because we access it only on getRegisteredHandler().
1013 mChangedPackages.add(packageName);
1014 }
1015
1016 @Override
1017 public void onPackagesSuspended(String[] packages) {
1018 // No need to lock this because we access it only on getRegisteredHandler().
1019 for (String packageName : packages) {
1020 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001021 }
1022 }
1023
1024 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001025 public void onPackagesUnsuspended(String[] packages) {
1026 // No need to lock this because we access it only on getRegisteredHandler().
1027 for (String packageName : packages) {
1028 mChangedPackages.add(packageName);
1029 }
1030 }
1031
1032 @Override
1033 public void onFinishPackageChanges() {
1034 onFinishPackageChangesInternal();
1035 clearPackageChangeState();
1036 }
1037
1038 private void clearPackageChangeState() {
1039 // No need to lock them because we access these fields only on getRegisteredHandler().
1040 mChangedPackages.clear();
1041 mImePackageAppeared = false;
1042 }
1043
1044 private boolean shouldRebuildInputMethodListLocked() {
1045 // This method is guaranteed to be called only by getRegisteredHandler().
1046
1047 // If there is any new package that contains at least one IME, then rebuilt the list
1048 // of IMEs.
1049 if (mImePackageAppeared) {
1050 return true;
1051 }
1052
1053 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1054 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1055 // is an option, but it may still do some extra operations that we do not need here.
1056 final int N = mChangedPackages.size();
1057 for (int i = 0; i < N; ++i) {
1058 final String packageName = mChangedPackages.get(i);
1059 if (mKnownImePackageNames.contains(packageName)) {
1060 return true;
1061 }
1062 }
1063 return false;
1064 }
1065
1066 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001067 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001068 if (!isChangingPackagesOfCurrentUserLocked()) {
1069 return;
1070 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001071 if (!shouldRebuildInputMethodListLocked()) {
1072 return;
1073 }
1074
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001075 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001076 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001077 final int N = mMethodList.size();
1078 if (curInputMethodId != null) {
1079 for (int i=0; i<N; i++) {
1080 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001081 final String imiId = imi.getId();
1082 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001083 curIm = imi;
1084 }
satoke7c6998e2011-06-03 17:57:59 +09001085
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001086 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001087 if (isPackageModified(imi.getPackageName())) {
1088 mFileManager.deleteAllInputMethodSubtypes(imiId);
1089 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001090 if (change == PACKAGE_TEMPORARY_CHANGE
1091 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001092 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001093 + imi.getComponent());
1094 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
1096 }
1097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001098
Yohei Yukawa94e33302016-02-12 19:37:03 -08001099 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001103 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001104 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001105 if (change == PACKAGE_TEMPORARY_CHANGE
1106 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001107 ServiceInfo si = null;
1108 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001109 si = mIPackageManager.getServiceInfo(
1110 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1111 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001112 }
1113 if (si == null) {
1114 // Uh oh, current input method is no longer around!
1115 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001116 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001117 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001118 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001119 changed = true;
1120 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001121 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001122 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001123 }
1124 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001125 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001126 }
satokab751aa2010-09-14 19:17:36 +09001127
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001128 if (curIm == null) {
1129 // We currently don't have a default input method... is
1130 // one now available?
1131 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001132 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1133 // Even if the current input method is still available, mCurrentSubtype could
1134 // be obsolete when the package is modified in practice.
1135 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001138 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001139 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 }
1142 }
1143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001144
Jeff Brownc28867a2013-03-26 15:42:39 -07001145 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001146 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001147 private final IInputMethod mMethod;
1148 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001149
Jeff Brownc28867a2013-03-26 15:42:39 -07001150 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1151 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001152 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001153 mMethod = method;
1154 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001156
satoke7c6998e2011-06-03 17:57:59 +09001157 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001158 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001159 long ident = Binder.clearCallingIdentity();
1160 try {
1161 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1162 } finally {
1163 Binder.restoreCallingIdentity(ident);
1164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
satok01038492012-04-09 21:08:27 +09001168 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001169 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001170 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001171 public void onHardKeyboardStatusChange(boolean available) {
1172 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1173 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001174 }
1175
Michael Wright7b5a96b2014-08-09 19:28:42 -07001176 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001177 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001178 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001179 }
1180 synchronized(mMethodMap) {
1181 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1182 && mSwitchingDialog.isShowing()) {
1183 mSwitchingDialogTitleView.findViewById(
1184 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1185 available ? View.VISIBLE : View.GONE);
1186 }
1187 }
1188 }
1189 }
1190
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001191 public static final class Lifecycle extends SystemService {
1192 private InputMethodManagerService mService;
1193
1194 public Lifecycle(Context context) {
1195 super(context);
1196 mService = new InputMethodManagerService(context);
1197 }
1198
1199 @Override
1200 public void onStart() {
1201 LocalServices.addService(InputMethodManagerInternal.class,
1202 new LocalServiceImpl(mService.mHandler));
1203 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1204 }
1205
1206 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001207 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001208 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001209 // TODO: Dispatch this to a worker thread as needed.
1210 mService.onSwitchUser(userHandle);
1211 }
1212
1213 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001214 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001215 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001216 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001217 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1218 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1219 .getService(Context.STATUS_BAR_SERVICE);
1220 mService.systemRunning(statusBarService);
1221 }
1222 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001223
1224 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001225 public void onUnlockUser(final @UserIdInt int userHandle) {
1226 // Called on ActivityManager thread.
1227 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001228 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001229 }
1230 }
1231
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001232 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001233 synchronized(mMethodMap) {
1234 final int currentUserId = mSettings.getCurrentUserId();
1235 if (DEBUG) {
1236 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1237 }
1238 if (userId != currentUserId) {
1239 return;
1240 }
1241 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001242 if (mSystemReady) {
1243 // We need to rebuild IMEs.
1244 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1245 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1246 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001247 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001248 }
1249
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001250 void onSwitchUser(@UserIdInt int userId) {
1251 synchronized (mMethodMap) {
1252 switchUserLocked(userId);
1253 }
1254 }
1255
Seigo Nonaka7309b122015-08-17 18:34:13 -07001256 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001257 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001259 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001261 // Note: SettingsObserver doesn't register observers in its constructor.
1262 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 mIWindowManager = IWindowManager.Stub.asInterface(
1264 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001265 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001266 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001267 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 public void executeMessage(Message msg) {
1269 handleMessage(msg);
1270 }
Mita Yuned218c72012-12-06 17:18:25 -08001271 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001272 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001273 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001274 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001275 mHasFeature = context.getPackageManager().hasSystemFeature(
1276 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001277 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001278 mHardKeyboardBehavior = mContext.getResources().getInteger(
1279 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001280
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001281 Bundle extras = new Bundle();
1282 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001283 mImeSwitcherNotification =
1284 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1285 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1286 .setWhen(0)
1287 .setOngoing(true)
1288 .addExtras(extras)
1289 .setCategory(Notification.CATEGORY_SYSTEM)
1290 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001291
satok7cfc0ed2011-06-20 21:29:36 +09001292 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001293 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001294
1295 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001296
satok7cfc0ed2011-06-20 21:29:36 +09001297 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001298 int userId = 0;
1299 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001300 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001301 } catch (RemoteException e) {
1302 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1303 }
satok913a8922010-08-26 21:53:41 +09001304
satokd87c2592010-09-29 11:52:06 +09001305 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001306 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001307 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001308
Kenny Guy2a764942014-04-02 13:29:20 +01001309 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001310 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001311 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1312 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001313 }
1314
satok5b927c432012-05-01 20:09:34 +09001315 private void resetDefaultImeLocked(Context context) {
1316 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001317 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001318 return;
1319 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001320 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001321 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001322 if (suitableImes.isEmpty()) {
1323 Slog.i(TAG, "No default found");
1324 return;
satok5b927c432012-05-01 20:09:34 +09001325 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001326 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001327 if (DEBUG) {
1328 Slog.i(TAG, "Default found, using " + defIm.getId());
1329 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001330 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001331 }
1332
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001333 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001334 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1335 + " currentUserId=" + mSettings.getCurrentUserId());
1336
Yohei Yukawa81482972015-06-04 00:58:59 -07001337 // ContentObserver should be registered again when the user is changed
1338 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001339
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001340 // If the system is not ready or the device is not yed unlocked by the user, then we use
1341 // copy-on-write settings.
1342 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001343 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001344 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001345 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001346 // InputMethodFileManager should be reset when the user is changed
1347 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001348 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001349
1350 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1351 + " defaultImiId=" + defaultImiId);
1352
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001353 // For secondary users, the list of enabled IMEs may not have been updated since the
1354 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1355 // not be empty even if the IME has been uninstalled by the primary user.
1356 // Even in such cases, IMMS works fine because it will find the most applicable
1357 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001358 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001359 mLastSystemLocales = mRes.getConfiguration().getLocales();
1360
1361 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1362 if (mSystemReady) {
1363 hideCurrentInputLocked(0, null);
1364 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1365 buildInputMethodListLocked(initialUserSwitch);
1366 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1367 // This is the first time of the user switch and
1368 // set the current ime to the proper one.
1369 resetDefaultImeLocked(mContext);
1370 }
1371 updateFromSettingsLocked(true);
1372 try {
1373 startInputInnerLocked();
1374 } catch (RuntimeException e) {
1375 Slog.w(TAG, "Unexpected exception", e);
1376 }
1377 }
1378
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001379 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001380 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1381 mSettings.getEnabledInputMethodListLocked(), newUserId,
1382 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001383 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001384
1385 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1386 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001387 }
1388
Kenny Guy2a764942014-04-02 13:29:20 +01001389 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001390 mSettings.setCurrentProfileIds(
1391 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001392 }
1393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 @Override
1395 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1396 throws RemoteException {
1397 try {
1398 return super.onTransact(code, data, reply, flags);
1399 } catch (RuntimeException e) {
1400 // The input method manager only throws security exceptions, so let's
1401 // log all others.
1402 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001403 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405 throw e;
1406 }
1407 }
1408
Svetoslav Ganova0027152013-06-25 14:59:53 -07001409 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001410 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001411 if (DEBUG) {
1412 Slog.d(TAG, "--- systemReady");
1413 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001414 if (!mSystemReady) {
1415 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001416 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001417 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001418 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001419 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001420 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1421 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001422 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001423 if (mStatusBar != null) {
1424 mStatusBar.setIconVisibility(mSlotIme, false);
1425 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001426 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001427 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1428 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001429 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001430 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001431 mHardKeyboardListener);
1432 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001433
1434 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1435 mSettingsObserver.registerContentObserverLocked(currentUserId);
1436
1437 final IntentFilter broadcastFilter = new IntentFilter();
1438 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1439 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1440 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1441 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1442 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1443 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1444
1445 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1446 resetDefaultImeLocked(mContext);
1447 updateFromSettingsLocked(true);
1448 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1449 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1450 mContext.getBasePackageName());
1451
Dianne Hackborncc278702009-09-02 23:07:23 -07001452 try {
1453 startInputInnerLocked();
1454 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001456 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001457 }
1458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001460
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001461 // ---------------------------------------------------------------------------------------
1462 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1463 // 1) it comes from the system process
1464 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1465 private boolean calledFromValidUser() {
1466 final int uid = Binder.getCallingUid();
1467 final int userId = UserHandle.getUserId(uid);
1468 if (DEBUG) {
1469 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1470 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1471 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001472 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1473 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001474 }
Kenny Guy2a764942014-04-02 13:29:20 +01001475 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001476 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001477 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001478
1479 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1480 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1481 // must not manage background users' states in any functions.
1482 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1483 // by a token.
1484 if (mContext.checkCallingOrSelfPermission(
1485 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1486 == PackageManager.PERMISSION_GRANTED) {
1487 if (DEBUG) {
1488 Slog.d(TAG, "--- Access granted because the calling process has "
1489 + "the INTERACT_ACROSS_USERS_FULL permission");
1490 }
1491 return true;
1492 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001493 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001494 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1495 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001496 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001497 }
1498
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001499
1500 /**
1501 * Returns true iff the caller is identified to be the current input method with the token.
1502 * @param token The window token given to the input method when it was started.
1503 * @return true if and only if non-null valid token is specified.
1504 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001505 private boolean calledWithValidToken(@Nullable IBinder token) {
1506 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1507 if (DEBUG) {
1508 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1509 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1510 }
1511 return false;
1512 }
1513 if (token == null || token != mCurToken) {
1514 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1515 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1516 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001517 return false;
1518 }
1519 return true;
1520 }
1521
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001522 private boolean bindCurrentInputMethodService(
1523 Intent service, ServiceConnection conn, int flags) {
1524 if (service == null || conn == null) {
1525 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1526 return false;
1527 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001528 return mContext.bindServiceAsUser(service, conn, flags,
1529 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001530 }
1531
satoke7c6998e2011-06-03 17:57:59 +09001532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001534 // TODO: Make this work even for non-current users?
1535 if (!calledFromValidUser()) {
1536 return Collections.emptyList();
1537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001539 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541 }
1542
satoke7c6998e2011-06-03 17:57:59 +09001543 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001545 // TODO: Make this work even for non-current users?
1546 if (!calledFromValidUser()) {
1547 return Collections.emptyList();
1548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001550 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552 }
1553
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001554 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001555 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001556 * @return enabled subtypes of the specified imi
1557 */
satoke7c6998e2011-06-03 17:57:59 +09001558 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001559 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001560 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001561 // TODO: Make this work even for non-current users?
1562 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001563 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001564 }
satok67ddf9c2010-11-17 09:45:54 +09001565 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001566 final InputMethodInfo imi;
1567 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001568 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001569 } else {
1570 imi = mMethodMap.get(imiId);
1571 }
1572 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001573 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001574 }
1575 return mSettings.getEnabledInputMethodSubtypeListLocked(
1576 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001577 }
1578 }
1579
satoke7c6998e2011-06-03 17:57:59 +09001580 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 public void addClient(IInputMethodClient client,
1582 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001583 if (!calledFromValidUser()) {
1584 return;
1585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 synchronized (mMethodMap) {
1587 mClients.put(client.asBinder(), new ClientState(client,
1588 inputContext, uid, pid));
1589 }
1590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001591
satoke7c6998e2011-06-03 17:57:59 +09001592 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001594 if (!calledFromValidUser()) {
1595 return;
1596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001598 ClientState cs = mClients.remove(client.asBinder());
1599 if (cs != null) {
1600 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001601 if (mCurClient == cs) {
1602 mCurClient = null;
1603 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001604 if (mCurFocusedWindowClient == cs) {
1605 mCurFocusedWindowClient = null;
1606 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 }
1609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 void executeOrSendMessage(IInterface target, Message msg) {
1612 if (target.asBinder() instanceof Binder) {
1613 mCaller.sendMessage(msg);
1614 } else {
1615 handleMessage(msg);
1616 msg.recycle();
1617 }
1618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001619
Yohei Yukawa33e81792015-11-17 21:14:42 -08001620 void unbindCurrentClientLocked(
1621 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001623 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 + mCurClient.client.asBinder());
1625 if (mBoundToMethod) {
1626 mBoundToMethod = false;
1627 if (mCurMethod != null) {
1628 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1629 MSG_UNBIND_INPUT, mCurMethod));
1630 }
1631 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001632
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001633 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1634 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001635 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1636 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001639
The Android Open Source Project10592532009-03-18 17:39:46 -07001640 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
1642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 private int getImeShowFlags() {
1645 int flags = 0;
1646 if (mShowForced) {
1647 flags |= InputMethod.SHOW_FORCED
1648 | InputMethod.SHOW_EXPLICIT;
1649 } else if (mShowExplicitlyRequested) {
1650 flags |= InputMethod.SHOW_EXPLICIT;
1651 }
1652 return flags;
1653 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 private int getAppShowFlags() {
1656 int flags = 0;
1657 if (mShowForced) {
1658 flags |= InputMethodManager.SHOW_FORCED;
1659 } else if (!mShowExplicitlyRequested) {
1660 flags |= InputMethodManager.SHOW_IMPLICIT;
1661 }
1662 return flags;
1663 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001664
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001665 InputBindResult attachNewInputLocked(
1666 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 if (!mBoundToMethod) {
1668 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1669 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1670 mBoundToMethod = true;
1671 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001672
1673 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001674 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1675 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1676 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001677 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001678 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001681 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001682 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001683 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001685 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001686 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001688 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001689 (session.channel != null ? session.channel.dup() : null),
1690 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001692
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001693 InputBindResult startInputLocked(
1694 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001695 IInputMethodClient client, IInputContext inputContext,
1696 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001697 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 // If no method is currently selected, do nothing.
1699 if (mCurMethodId == null) {
1700 return mNoBinding;
1701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 ClientState cs = mClients.get(client.asBinder());
1704 if (cs == null) {
1705 throw new IllegalArgumentException("unknown client "
1706 + client.asBinder());
1707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001708
Yohei Yukawa74750f22016-03-22 12:54:22 -07001709 if (attribute == null) {
1710 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1711 + " uid=" + cs.uid + " pid=" + cs.pid);
1712 return null;
1713 }
1714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 try {
1716 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1717 // Check with the window manager to make sure this client actually
1718 // has a window with focus. If not, reject. This is thread safe
1719 // because if the focus changes some time before or after, the
1720 // next client receiving focus that has any interest in input will
1721 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001722 if (DEBUG) {
1723 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1724 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 return null;
1727 }
1728 } catch (RemoteException e) {
1729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001730
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001731 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001732 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001733 }
1734
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001735 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001736 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001737 @NonNull EditorInfo attribute, int controlFlags,
1738 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001739 // If no method is currently selected, do nothing.
1740 if (mCurMethodId == null) {
1741 return mNoBinding;
1742 }
1743
Yohei Yukawad57ba672015-06-08 16:39:46 -07001744 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1745 attribute.packageName)) {
1746 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1747 + " uid=" + cs.uid + " package=" + attribute.packageName);
1748 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001749 }
1750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001752 // Was the keyguard locked when switching over to the new client?
1753 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 // If the client is changing, we need to switch over to the new
1755 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001756 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001757 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001758 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759
1760 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001761 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001762 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001763 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 // Bump up the sequence for this client and attach it.
1768 mCurSeq++;
1769 if (mCurSeq <= 0) mCurSeq = 1;
1770 mCurClient = cs;
1771 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001772 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 // Check if the input method is changing.
1776 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1777 if (cs.curSession != null) {
1778 // Fast case: if we are already connected to the input method,
1779 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001780 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001781 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783 if (mHaveConnection) {
1784 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 // Return to client, and we will get back with it when
1786 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001787 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001788 return new InputBindResult(null, null, mCurId, mCurSeq,
1789 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 } else if (SystemClock.uptimeMillis()
1791 < (mLastBindTime+TIME_TO_RECONNECT)) {
1792 // In this case we have connected to the service, but
1793 // don't yet have its interface. If it hasn't been too
1794 // long since we did the connection, we'll return to
1795 // the client and wait to get the service interface so
1796 // we can report back. If it has been too long, we want
1797 // to fall through so we can try a disconnect/reconnect
1798 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001799 return new InputBindResult(null, null, mCurId, mCurSeq,
1800 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1803 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 }
1805 }
1806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001807
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001808 return startInputInnerLocked();
1809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001810
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001811 InputBindResult startInputInnerLocked() {
1812 if (mCurMethodId == null) {
1813 return mNoBinding;
1814 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001815
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001816 if (!mSystemReady) {
1817 // If the system is not yet ready, we shouldn't be running third
1818 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001819 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1820 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1824 if (info == null) {
1825 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001828 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1831 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001832 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1833 com.android.internal.R.string.input_method_binding_label);
1834 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1835 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawaa67a4592017-03-30 15:57:02 -07001836 if (bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 mLastBindTime = SystemClock.uptimeMillis();
1838 mHaveConnection = true;
1839 mCurId = info.getId();
1840 mCurToken = new Binder();
1841 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001842 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001843 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 } catch (RemoteException e) {
1845 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001846 return new InputBindResult(null, null, mCurId, mCurSeq,
1847 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 } else {
1849 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001850 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 + mCurIntent);
1852 }
1853 return null;
1854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001856 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001857 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001858 IInputMethodClient client, IInputContext inputContext,
1859 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001860 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001861 if (!calledFromValidUser()) {
1862 return null;
1863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001865 if (DEBUG) {
1866 Slog.v(TAG, "startInput: reason="
1867 + InputMethodClient.getStartInputReason(startInputReason)
1868 + " client = " + client.asBinder()
1869 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001870 + " missingMethods="
1871 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001872 + " attribute=" + attribute
1873 + " controlFlags=#" + Integer.toHexString(controlFlags));
1874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 final long ident = Binder.clearCallingIdentity();
1876 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001877 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1878 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 } finally {
1880 Binder.restoreCallingIdentity(ident);
1881 }
1882 }
1883 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001884
satoke7c6998e2011-06-03 17:57:59 +09001885 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 public void finishInput(IInputMethodClient client) {
1887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001888
satoke7c6998e2011-06-03 17:57:59 +09001889 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 public void onServiceConnected(ComponentName name, IBinder service) {
1891 synchronized (mMethodMap) {
1892 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1893 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001894 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001895 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001896 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001897 return;
1898 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001899 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001900 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1901 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001903 clearClientSessionLocked(mCurClient);
1904 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 }
1906 }
1907 }
1908 }
1909
Jeff Brownc28867a2013-03-26 15:42:39 -07001910 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1911 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 synchronized (mMethodMap) {
1913 if (mCurMethod != null && method != null
1914 && mCurMethod.asBinder() == method.asBinder()) {
1915 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001916 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001918 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001919 InputBindResult res = attachNewInputLocked(
1920 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 if (res.method != null) {
1922 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001923 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001925 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
1927 }
1928 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001929
1930 // Session abandoned. Close its associated input channel.
1931 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001933
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001934 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001935 if (mVisibleBound) {
1936 mContext.unbindService(mVisibleConnection);
1937 mVisibleBound = false;
1938 }
1939
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001940 if (mHaveConnection) {
1941 mContext.unbindService(this);
1942 mHaveConnection = false;
1943 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001944
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001945 if (mCurToken != null) {
1946 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001947 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001948 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001949 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001950 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001951 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001952 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001953 } catch (RemoteException e) {
1954 }
1955 mCurToken = null;
1956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001957
The Android Open Source Project10592532009-03-18 17:39:46 -07001958 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001959 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001961
Yohei Yukawa33e81792015-11-17 21:14:42 -08001962 void resetCurrentMethodAndClient(
1963 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001964 mCurMethodId = null;
1965 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001966 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001967 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001968
1969 void requestClientSessionLocked(ClientState cs) {
1970 if (!cs.sessionRequested) {
1971 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1972 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1973 cs.sessionRequested = true;
1974 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1975 MSG_CREATE_SESSION, mCurMethod, channels[1],
1976 new MethodCallback(this, mCurMethod, channels[0])));
1977 }
1978 }
1979
1980 void clearClientSessionLocked(ClientState cs) {
1981 finishSessionLocked(cs.curSession);
1982 cs.curSession = null;
1983 cs.sessionRequested = false;
1984 }
1985
1986 private void finishSessionLocked(SessionState sessionState) {
1987 if (sessionState != null) {
1988 if (sessionState.session != null) {
1989 try {
1990 sessionState.session.finishSession();
1991 } catch (RemoteException e) {
1992 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001993 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001994 }
1995 sessionState.session = null;
1996 }
1997 if (sessionState.channel != null) {
1998 sessionState.channel.dispose();
1999 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002000 }
2001 }
2002 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002003
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002004 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 if (mCurMethod != null) {
2006 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002007 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002009
Jeff Brownc28867a2013-03-26 15:42:39 -07002010 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002011 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 mCurMethod = null;
2013 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002014 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002015 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002016 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002017 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002019
satoke7c6998e2011-06-03 17:57:59 +09002020 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002022 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2023 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2024 // which is irregular but can eventually happen for everyone just by continuing using the
2025 // device. Thus it is important to make sure that all the internal states are properly
2026 // refreshed when this method is called back. Running
2027 // adb install -r <APK that implements the current IME>
2028 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002030 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 + " mCurIntent=" + mCurIntent);
2032 if (mCurMethod != null && mCurIntent != null
2033 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002034 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 // We consider this to be a new bind attempt, since the system
2036 // should now try to restart the service for us.
2037 mLastBindTime = SystemClock.uptimeMillis();
2038 mShowRequested = mInputShown;
2039 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002040 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 }
2042 }
2043 }
2044
satokf9f01002011-05-19 21:31:50 +09002045 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002047 synchronized (mMethodMap) {
2048 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002049 return;
2050 }
2051 final long ident = Binder.clearCallingIdentity();
2052 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002054 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002055 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002056 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002059 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002060 CharSequence contentDescription = null;
2061 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002062 // Use PackageManager to load label
2063 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002064 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002065 mIPackageManager.getApplicationInfo(packageName, 0,
2066 mSettings.getCurrentUserId()));
2067 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002068 /* ignore */
2069 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002070 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002071 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002072 contentDescription != null
2073 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002074 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002077 } finally {
2078 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
2081 }
2082
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002083 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002084 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002085 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09002086 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002087 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002088 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002089 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2090 // When physical keyboard is attached, we show the ime switcher (or notification if
2091 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2092 // exists in the IME switcher dialog. Might be OK to remove this condition once
2093 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2094 return true;
2095 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002096 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2097 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002098 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002099
2100 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2101 final int N = imis.size();
2102 if (N > 2) return true;
2103 if (N < 1) return false;
2104 int nonAuxCount = 0;
2105 int auxCount = 0;
2106 InputMethodSubtype nonAuxSubtype = null;
2107 InputMethodSubtype auxSubtype = null;
2108 for(int i = 0; i < N; ++i) {
2109 final InputMethodInfo imi = imis.get(i);
2110 final List<InputMethodSubtype> subtypes =
2111 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2112 final int subtypeCount = subtypes.size();
2113 if (subtypeCount == 0) {
2114 ++nonAuxCount;
2115 } else {
2116 for (int j = 0; j < subtypeCount; ++j) {
2117 final InputMethodSubtype subtype = subtypes.get(j);
2118 if (!subtype.isAuxiliary()) {
2119 ++nonAuxCount;
2120 nonAuxSubtype = subtype;
2121 } else {
2122 ++auxCount;
2123 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002124 }
2125 }
satok7cfc0ed2011-06-20 21:29:36 +09002126 }
2127 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002128 if (nonAuxCount > 1 || auxCount > 1) {
2129 return true;
2130 } else if (nonAuxCount == 1 && auxCount == 1) {
2131 if (nonAuxSubtype != null && auxSubtype != null
2132 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2133 || auxSubtype.overridesImplicitlyEnabledSubtype()
2134 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2135 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2136 return false;
2137 }
2138 return true;
2139 }
2140 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002141 }
2142
John Spurlocke0980502013-10-25 11:59:29 -04002143 private boolean isKeyguardLocked() {
2144 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2145 }
2146
satokdbf29502011-08-25 15:28:23 +09002147 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002148 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002149 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2150 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002151 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002152 return;
2153 }
2154
Yohei Yukawa69e68022017-02-13 12:04:50 -08002155 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002156 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002157 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002158 mImeWindowVis = vis;
2159 mBackDisposition = backDisposition;
2160 updateSystemUiLocked(token, vis, backDisposition);
2161 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002162 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2163 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawabbb10e82017-02-17 20:13:52 -08002164 info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002165 }
2166
2167 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2168 synchronized (mMethodMap) {
2169 updateSystemUiLocked(token, vis, backDisposition);
2170 }
2171 }
2172
2173 // Caution! This method is called in this class. Handle multi-user carefully
2174 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2175 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002176 return;
2177 }
2178
2179 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2180 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002181 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002182 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002183 // apply policy for binder calls
2184 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2185 vis = 0;
satok06487a52010-10-29 11:37:18 +09002186 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002187 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2188 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2189 if (mStatusBar != null) {
2190 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2191 needsToShowImeSwitcher);
2192 }
2193 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2194 if (imi != null && needsToShowImeSwitcher) {
2195 // Used to load label
2196 final CharSequence title = mRes.getText(
2197 com.android.internal.R.string.select_input_method);
2198 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2199 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002200 mImeSwitcherNotification.setContentTitle(title)
2201 .setContentText(summary)
2202 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002203 try {
2204 if ((mNotificationManager != null)
2205 && !mIWindowManager.hasNavigationBar()) {
2206 if (DEBUG) {
2207 Slog.d(TAG, "--- show notification: label = " + summary);
2208 }
2209 mNotificationManager.notifyAsUser(null,
2210 com.android.internal.R.string.select_input_method,
2211 mImeSwitcherNotification.build(), UserHandle.ALL);
2212 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002213 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002214 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002215 }
2216 } else {
2217 if (mNotificationShown && mNotificationManager != null) {
2218 if (DEBUG) {
2219 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002220 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002221 mNotificationManager.cancelAsUser(null,
2222 com.android.internal.R.string.select_input_method, UserHandle.ALL);
2223 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002224 }
satok06487a52010-10-29 11:37:18 +09002225 }
2226 } finally {
2227 Binder.restoreCallingIdentity(ident);
2228 }
2229 }
2230
satoke7c6998e2011-06-03 17:57:59 +09002231 @Override
satokf9f01002011-05-19 21:31:50 +09002232 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002233 if (!calledFromValidUser()) {
2234 return;
2235 }
satokf9f01002011-05-19 21:31:50 +09002236 synchronized (mMethodMap) {
2237 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2238 for (int i = 0; i < spans.length; ++i) {
2239 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002240 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002241 mSecureSuggestionSpans.put(ss, currentImi);
2242 }
2243 }
2244 }
2245 }
2246
satoke7c6998e2011-06-03 17:57:59 +09002247 @Override
satokf9f01002011-05-19 21:31:50 +09002248 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002249 if (!calledFromValidUser()) {
2250 return false;
2251 }
satokf9f01002011-05-19 21:31:50 +09002252 synchronized (mMethodMap) {
2253 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2254 // TODO: Do not send the intent if the process of the targetImi is already dead.
2255 if (targetImi != null) {
2256 final String[] suggestions = span.getSuggestions();
2257 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002258 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002259 final Intent intent = new Intent();
2260 // Ensures that only a class in the original IME package will receive the
2261 // notification.
satok42c5a162011-05-26 16:46:14 +09002262 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002263 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2264 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2265 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2266 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002267 final long ident = Binder.clearCallingIdentity();
2268 try {
2269 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2270 } finally {
2271 Binder.restoreCallingIdentity(ident);
2272 }
satokf9f01002011-05-19 21:31:50 +09002273 return true;
2274 }
2275 }
2276 return false;
2277 }
2278
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002279 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002280 updateInputMethodsFromSettingsLocked(enabledMayChange);
2281 updateKeyboardFromSettingsLocked();
2282 }
2283
2284 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002285 if (enabledMayChange) {
2286 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2287 for (int i=0; i<enabled.size(); i++) {
2288 // We allow the user to select "disabled until used" apps, so if they
2289 // are enabling one of those here we now need to make it enabled.
2290 InputMethodInfo imm = enabled.get(i);
2291 try {
2292 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2293 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2294 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002295 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002296 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002297 if (DEBUG) {
2298 Slog.d(TAG, "Update state(" + imm.getId()
2299 + "): DISABLED_UNTIL_USED -> DEFAULT");
2300 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002301 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2302 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002303 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2304 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002305 }
2306 } catch (RemoteException e) {
2307 }
2308 }
2309 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002310 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2311 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2312 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2313 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002314 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002315 // There is no input method selected, try to choose new applicable input method.
2316 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002317 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002318 }
2319 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002321 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002323 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002324 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 }
satokf3db1af2010-11-23 13:34:33 +09002326 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002327 } else {
2328 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002329 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002331 // Here is not the perfect place to reset the switching controller. Ideally
2332 // mSwitchingController and mSettings should be able to share the same state.
2333 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2334 // the same enabled IMEs list.
2335 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002336
2337 }
2338
2339 public void updateKeyboardFromSettingsLocked() {
2340 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2341 if (mSwitchingDialog != null
2342 && mSwitchingDialogTitleView != null
2343 && mSwitchingDialog.isShowing()) {
2344 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2345 com.android.internal.R.id.hard_keyboard_switch);
2346 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002349
Yohei Yukawab097b822015-12-01 10:43:08 -08002350 private void notifyInputMethodSubtypeChanged(final int userId,
2351 @Nullable final InputMethodInfo inputMethodInfo,
2352 @Nullable final InputMethodSubtype subtype) {
2353 final InputManagerInternal inputManagerInternal =
2354 LocalServices.getService(InputManagerInternal.class);
2355 if (inputManagerInternal != null) {
2356 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2357 }
2358 }
2359
satokab751aa2010-09-14 19:17:36 +09002360 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 InputMethodInfo info = mMethodMap.get(id);
2362 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002363 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002365
satokd81e9502012-05-21 12:58:45 +09002366 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002368 final int subtypeCount = info.getSubtypeCount();
2369 if (subtypeCount <= 0) {
2370 return;
satokcd7cd292010-11-20 15:46:23 +09002371 }
satokd81e9502012-05-21 12:58:45 +09002372 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2373 final InputMethodSubtype newSubtype;
2374 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2375 newSubtype = info.getSubtypeAt(subtypeId);
2376 } else {
2377 // If subtype is null, try to find the most applicable one from
2378 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002379 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002380 }
2381 if (newSubtype == null || oldSubtype == null) {
2382 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2383 + ", new subtype = " + newSubtype);
2384 return;
2385 }
2386 if (newSubtype != oldSubtype) {
2387 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2388 if (mCurMethod != null) {
2389 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002390 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002391 mCurMethod.changeInputMethodSubtype(newSubtype);
2392 } catch (RemoteException e) {
2393 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002394 return;
satokab751aa2010-09-14 19:17:36 +09002395 }
2396 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002397 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 return;
2400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002401
satokd81e9502012-05-21 12:58:45 +09002402 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 final long ident = Binder.clearCallingIdentity();
2404 try {
satokab751aa2010-09-14 19:17:36 +09002405 // Set a subtype to this input method.
2406 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002407 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2408 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2409 // because mCurMethodId is stored as a history in
2410 // setSelectedInputMethodAndSubtypeLocked().
2411 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002413 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002415 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002417 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002419 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 } finally {
2421 Binder.restoreCallingIdentity(ident);
2422 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002423
2424 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2425 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002427
satok42c5a162011-05-26 16:46:14 +09002428 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002429 public boolean showSoftInput(IInputMethodClient client, int flags,
2430 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002431 if (!calledFromValidUser()) {
2432 return false;
2433 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002434 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 long ident = Binder.clearCallingIdentity();
2436 try {
2437 synchronized (mMethodMap) {
2438 if (mCurClient == null || client == null
2439 || mCurClient.client.asBinder() != client.asBinder()) {
2440 try {
2441 // We need to check if this is the current client with
2442 // focus in the window manager, to allow this call to
2443 // be made before input is started in it.
2444 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002445 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002446 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002449 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
2451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002452
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002453 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002454 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 }
2456 } finally {
2457 Binder.restoreCallingIdentity(ident);
2458 }
2459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002460
The Android Open Source Project4df24232009-03-05 14:34:35 -08002461 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002463 if (mAccessibilityRequestingNoSoftKeyboard) {
2464 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 }
Anna Galusza9b278112016-01-04 11:37:37 -08002466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2468 mShowExplicitlyRequested = true;
2469 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002470 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2471 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002473
Dianne Hackborncc278702009-09-02 23:07:23 -07002474 if (!mSystemReady) {
2475 return false;
2476 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002477
The Android Open Source Project4df24232009-03-05 14:34:35 -08002478 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002480 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002481 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2482 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2483 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002485 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002486 bindCurrentInputMethodService(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002487 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002488 mVisibleBound = true;
2489 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002490 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002492 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 // The client has asked to have the input method shown, but
2494 // we have been sitting here too long with a connection to the
2495 // service and no interface received, so let's disconnect/connect
2496 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002497 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002499 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 mContext.unbindService(this);
Yohei Yukawaa67a4592017-03-30 15:57:02 -07002501 bindCurrentInputMethodService(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002502 } else {
2503 if (DEBUG) {
2504 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2505 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002508
The Android Open Source Project4df24232009-03-05 14:34:35 -08002509 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002511
satok42c5a162011-05-26 16:46:14 +09002512 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002513 public boolean hideSoftInput(IInputMethodClient client, int flags,
2514 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002515 if (!calledFromValidUser()) {
2516 return false;
2517 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002518 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 long ident = Binder.clearCallingIdentity();
2520 try {
2521 synchronized (mMethodMap) {
2522 if (mCurClient == null || client == null
2523 || mCurClient.client.asBinder() != client.asBinder()) {
2524 try {
2525 // We need to check if this is the current client with
2526 // focus in the window manager, to allow this call to
2527 // be made before input is started in it.
2528 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002529 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2530 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002531 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 }
2533 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002534 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 }
2536 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002537
Joe Onorato8a9b2202010-02-26 18:56:32 -08002538 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002539 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
2541 } finally {
2542 Binder.restoreCallingIdentity(ident);
2543 }
2544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002545
The Android Open Source Project4df24232009-03-05 14:34:35 -08002546 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2548 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002549 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 -08002550 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 }
2552 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002553 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 -08002554 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002556
2557 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2558 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2559 // to be updated with the new value sent from IME process. Even in such a transient state
2560 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2561 // application process as a valid request, and have even promised such a behavior with CTS
2562 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2563 // IMMS#InputShown indicates that the software keyboard is shown.
2564 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2565 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2566 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002567 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002568 if (shouldHideSoftInput) {
2569 // The IME will report its visible state again after the following message finally
2570 // delivered to the IME process as an IPC. Hence the inconsistency between
2571 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2572 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002573 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2574 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2575 res = true;
2576 } else {
2577 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002579 if (mHaveConnection && mVisibleBound) {
2580 mContext.unbindService(mVisibleConnection);
2581 mVisibleBound = false;
2582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 mInputShown = false;
2584 mShowRequested = false;
2585 mShowExplicitlyRequested = false;
2586 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002587 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002589
satok42c5a162011-05-26 16:46:14 +09002590 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002591 public InputBindResult startInputOrWindowGainedFocus(
2592 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2593 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002594 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002595 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002596 if (windowToken != null) {
2597 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002598 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002599 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002600 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2601 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002602 }
2603 }
2604
2605 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002606 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002607 IInputMethodClient client, IBinder windowToken, int controlFlags,
2608 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002609 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2610 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002611 // Needs to check the validity before clearing calling identity
2612 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002613 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 long ident = Binder.clearCallingIdentity();
2615 try {
2616 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002617 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2618 + InputMethodClient.getStartInputReason(startInputReason)
2619 + " client=" + client.asBinder()
2620 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002621 + " missingMethods="
2622 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002623 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002624 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002625 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002626 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002627
Dianne Hackborn7663d802012-02-24 13:08:49 -08002628 ClientState cs = mClients.get(client.asBinder());
2629 if (cs == null) {
2630 throw new IllegalArgumentException("unknown client "
2631 + client.asBinder());
2632 }
2633
2634 try {
2635 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2636 // Check with the window manager to make sure this client actually
2637 // has a window with focus. If not, reject. This is thread safe
2638 // because if the focus changes some time before or after, the
2639 // next client receiving focus that has any interest in input will
2640 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002641 if (DEBUG) {
2642 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2643 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2644 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002645 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002647 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002649
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002650 if (!calledFromValidUser) {
2651 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2652 Slog.w(TAG, "If you want to interect with IME, you need "
2653 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2654 hideCurrentInputLocked(0, null);
2655 return null;
2656 }
2657
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002658 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002659 if (DEBUG) {
2660 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2661 + " attribute=" + attribute + ", token = " + windowToken);
2662 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002663 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002664 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002665 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002666 }
2667 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002668 }
2669 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002670 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002671 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002672
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002673 // Should we auto-show the IME even if the caller has not
2674 // specified what should be done with it?
2675 // We only do this automatically if the window can resize
2676 // to accommodate the IME (so what the user sees will give
2677 // them good context without input information being obscured
2678 // by the IME) or if running on a large screen where there
2679 // is more room for the target window + IME.
2680 final boolean doAutoShow =
2681 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2682 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2683 || mRes.getConfiguration().isLayoutSizeAtLeast(
2684 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002685 final boolean isTextEditor =
2686 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2687
2688 // We want to start input before showing the IME, but after closing
2689 // it. We want to do this after closing it to help the IME disappear
2690 // more quickly (not get stuck behind it initializing itself for the
2691 // new focused input, even if its window wants to hide the IME).
2692 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2695 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002696 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2698 // There is no focus view, and this window will
2699 // be behind any soft input window, so hide the
2700 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002701 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002702 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002704 } else if (isTextEditor && doAutoShow && (softInputMode &
2705 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 // There is a focus view, and we are navigating forward
2707 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002708 // We only do this automatically if the window can resize
2709 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002710 // them good context without input information being obscured
2711 // by the IME) or if running on a large screen where there
2712 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002713 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002714 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002715 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002716 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002717 didStart = true;
2718 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002719 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 }
2721 break;
2722 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2723 // Do nothing.
2724 break;
2725 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2726 if ((softInputMode &
2727 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002728 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002729 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 }
2731 break;
2732 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002733 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002734 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 break;
2736 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2737 if ((softInputMode &
2738 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002739 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002740 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002741 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002742 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002743 didStart = true;
2744 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002745 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 }
2747 break;
2748 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002749 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002750 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002751 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002752 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002753 didStart = true;
2754 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002755 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 break;
2757 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002758
2759 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002760 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002761 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 }
2764 } finally {
2765 Binder.restoreCallingIdentity(ident);
2766 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002767
2768 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002770
satok42c5a162011-05-26 16:46:14 +09002771 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002772 public void showInputMethodPickerFromClient(
2773 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002774 if (!calledFromValidUser()) {
2775 return;
2776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 synchronized (mMethodMap) {
2778 if (mCurClient == null || client == null
2779 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002780 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002781 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 }
2783
satok440aab52010-11-25 09:43:11 +09002784 // Always call subtype picker, because subtype picker is a superset of input method
2785 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002786 mHandler.sendMessage(mCaller.obtainMessageI(
2787 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002788 }
2789 }
2790
satok42c5a162011-05-26 16:46:14 +09002791 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002793 if (!calledFromValidUser()) {
2794 return;
2795 }
satok28203512010-11-24 11:06:49 +09002796 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2797 }
2798
satok42c5a162011-05-26 16:46:14 +09002799 @Override
satok28203512010-11-24 11:06:49 +09002800 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002801 if (!calledFromValidUser()) {
2802 return;
2803 }
satok28203512010-11-24 11:06:49 +09002804 synchronized (mMethodMap) {
2805 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002806 setInputMethodWithSubtypeIdLocked(token, id,
2807 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2808 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002809 } else {
2810 setInputMethod(token, id);
2811 }
2812 }
satokab751aa2010-09-14 19:17:36 +09002813 }
2814
satok42c5a162011-05-26 16:46:14 +09002815 @Override
satokb416a712010-11-25 20:42:14 +09002816 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002817 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002818 if (!calledFromValidUser()) {
2819 return;
2820 }
satokb416a712010-11-25 20:42:14 +09002821 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002822 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2823 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002824 }
2825 }
2826
satok4fc87d62011-05-20 16:13:43 +09002827 @Override
satok735cf382010-11-11 20:40:09 +09002828 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002829 if (!calledFromValidUser()) {
2830 return false;
2831 }
satok735cf382010-11-11 20:40:09 +09002832 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002833 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002834 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002835 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002836 lastImi = mMethodMap.get(lastIme.first);
2837 } else {
2838 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002839 }
satok4fc87d62011-05-20 16:13:43 +09002840 String targetLastImiId = null;
2841 int subtypeId = NOT_A_SUBTYPE_ID;
2842 if (lastIme != null && lastImi != null) {
2843 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002844 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002845 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2846 : mCurrentSubtype.hashCode();
2847 // If the last IME is the same as the current IME and the last subtype is not
2848 // defined, there is no need to switch to the last IME.
2849 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2850 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002851 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002852 }
2853 }
2854
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002855 if (TextUtils.isEmpty(targetLastImiId)
2856 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002857 // This is a safety net. If the currentSubtype can't be added to the history
2858 // and the framework couldn't find the last ime, we will make the last ime be
2859 // the most applicable enabled keyboard subtype of the system imes.
2860 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2861 if (enabled != null) {
2862 final int N = enabled.size();
2863 final String locale = mCurrentSubtype == null
2864 ? mRes.getConfiguration().locale.toString()
2865 : mCurrentSubtype.getLocale();
2866 for (int i = 0; i < N; ++i) {
2867 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002868 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002869 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002870 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2871 InputMethodUtils.getSubtypes(imi),
2872 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002873 if (keyboardSubtype != null) {
2874 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002875 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002876 imi, keyboardSubtype.hashCode());
2877 if(keyboardSubtype.getLocale().equals(locale)) {
2878 break;
2879 }
2880 }
2881 }
2882 }
2883 }
2884 }
2885
2886 if (!TextUtils.isEmpty(targetLastImiId)) {
2887 if (DEBUG) {
2888 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2889 + ", from: " + mCurMethodId + ", " + subtypeId);
2890 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002891 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002892 return true;
2893 } else {
2894 return false;
2895 }
satok735cf382010-11-11 20:40:09 +09002896 }
2897 }
2898
satoke7c6998e2011-06-03 17:57:59 +09002899 @Override
satok688bd472012-02-09 20:09:17 +09002900 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002901 if (!calledFromValidUser()) {
2902 return false;
2903 }
satok688bd472012-02-09 20:09:17 +09002904 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002905 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002906 return false;
2907 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002908 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002909 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2910 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002911 if (nextSubtype == null) {
2912 return false;
2913 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002914 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2915 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002916 return true;
2917 }
2918 }
2919
2920 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002921 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2922 if (!calledFromValidUser()) {
2923 return false;
2924 }
2925 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002926 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002927 return false;
2928 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002929 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002930 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2931 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002932 if (nextSubtype == null) {
2933 return false;
2934 }
2935 return true;
2936 }
2937 }
2938
2939 @Override
satok68f1b782011-04-11 14:26:04 +09002940 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002941 if (!calledFromValidUser()) {
2942 return null;
2943 }
satok68f1b782011-04-11 14:26:04 +09002944 synchronized (mMethodMap) {
2945 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2946 // TODO: Handle the case of the last IME with no subtypes
2947 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2948 || TextUtils.isEmpty(lastIme.second)) return null;
2949 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2950 if (lastImi == null) return null;
2951 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002952 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002953 final int lastSubtypeId =
2954 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002955 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2956 return null;
2957 }
2958 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002959 } catch (NumberFormatException e) {
2960 return null;
2961 }
2962 }
2963 }
2964
satoke7c6998e2011-06-03 17:57:59 +09002965 @Override
satokee5e77c2011-09-02 18:50:15 +09002966 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002967 if (!calledFromValidUser()) {
2968 return;
2969 }
satok91e88122011-07-18 11:11:42 +09002970 // By this IPC call, only a process which shares the same uid with the IME can add
2971 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002972 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002973 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002974 if (!mSystemReady) {
2975 return;
2976 }
satok91e88122011-07-18 11:11:42 +09002977 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002978 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002979 final String[] packageInfos;
2980 try {
2981 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2982 } catch (RemoteException e) {
2983 Slog.e(TAG, "Failed to get package infos");
2984 return;
2985 }
satok91e88122011-07-18 11:11:42 +09002986 if (packageInfos != null) {
2987 final int packageNum = packageInfos.length;
2988 for (int i = 0; i < packageNum; ++i) {
2989 if (packageInfos[i].equals(imi.getPackageName())) {
2990 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002991 final long ident = Binder.clearCallingIdentity();
2992 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002993 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002994 } finally {
2995 Binder.restoreCallingIdentity(ident);
2996 }
satokee5e77c2011-09-02 18:50:15 +09002997 return;
satok91e88122011-07-18 11:11:42 +09002998 }
2999 }
3000 }
satoke7c6998e2011-06-03 17:57:59 +09003001 }
satokee5e77c2011-09-02 18:50:15 +09003002 return;
satoke7c6998e2011-06-03 17:57:59 +09003003 }
3004
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003005 @Override
3006 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07003007 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003008 }
3009
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003010 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003011 public void clearLastInputMethodWindowForTransition(IBinder token) {
3012 if (!calledFromValidUser()) {
3013 return;
3014 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003015 synchronized (mMethodMap) {
3016 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003017 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003018 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003019 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003020 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003021 }
3022
3023 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003024 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003025 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003026 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003027 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003028 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003029 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3030 if (DEBUG) {
3031 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3032 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3033 + " actual: " + sequenceNumber);
3034 }
3035 return;
3036 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003037 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3038 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003039 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003040 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003041 }
3042 }
3043
satok28203512010-11-24 11:06:49 +09003044 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003046 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3047 }
3048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003050 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3051 if (token == null) {
3052 if (mContext.checkCallingOrSelfPermission(
3053 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3054 != PackageManager.PERMISSION_GRANTED) {
3055 throw new SecurityException(
3056 "Using null token requires permission "
3057 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003059 } else if (mCurToken != token) {
3060 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3061 + " token: " + token);
3062 return;
3063 }
3064
3065 final long ident = Binder.clearCallingIdentity();
3066 try {
3067 setInputMethodLocked(id, subtypeId);
3068 } finally {
3069 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 }
3071 }
3072
satok42c5a162011-05-26 16:46:14 +09003073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003075 if (!calledFromValidUser()) {
3076 return;
3077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003079 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 return;
3081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 long ident = Binder.clearCallingIdentity();
3083 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003084 hideCurrentInputLocked(flags, null);
3085 } finally {
3086 Binder.restoreCallingIdentity(ident);
3087 }
3088 }
3089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003090
satok42c5a162011-05-26 16:46:14 +09003091 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003092 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003093 if (!calledFromValidUser()) {
3094 return;
3095 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003096 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003097 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003098 return;
3099 }
3100 long ident = Binder.clearCallingIdentity();
3101 try {
3102 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 } finally {
3104 Binder.restoreCallingIdentity(ident);
3105 }
3106 }
3107 }
3108
3109 void setEnabledSessionInMainThread(SessionState session) {
3110 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003111 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003113 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003114 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 } catch (RemoteException e) {
3116 }
3117 }
3118 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003119 if (mEnabledSession != null && mEnabledSession.session != null) {
3120 try {
3121 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3122 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3123 } catch (RemoteException e) {
3124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 }
3126 }
3127 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003128
satok42c5a162011-05-26 16:46:14 +09003129 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003131 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003133 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003134 final boolean showAuxSubtypes;
3135 switch (msg.arg1) {
3136 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3137 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3138 // implemented so that auxiliary subtypes will be excluded when the soft
3139 // keyboard is invisible.
3140 showAuxSubtypes = mInputShown;
3141 break;
3142 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3143 showAuxSubtypes = true;
3144 break;
3145 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3146 showAuxSubtypes = false;
3147 break;
3148 default:
3149 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3150 return false;
3151 }
3152 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003153 return true;
3154
satok47a44912010-10-06 16:03:58 +09003155 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003156 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003157 return true;
3158
3159 case MSG_SHOW_IM_CONFIG:
3160 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003161 return true;
3162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 case MSG_UNBIND_INPUT:
3166 try {
3167 ((IInputMethod)msg.obj).unbindInput();
3168 } catch (RemoteException e) {
3169 // There is nothing interesting about the method dying.
3170 }
3171 return true;
3172 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003173 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 try {
3175 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3176 } catch (RemoteException e) {
3177 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003178 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 return true;
3180 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003181 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003183 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003184 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003185 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 } catch (RemoteException e) {
3187 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003188 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 return true;
3190 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003191 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003193 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003194 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003195 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 } catch (RemoteException e) {
3197 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003198 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003200 case MSG_HIDE_CURRENT_INPUT_METHOD:
3201 synchronized (mMethodMap) {
3202 hideCurrentInputLocked(0, null);
3203 }
3204 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003206 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003208 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3210 } catch (RemoteException e) {
3211 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003212 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003214 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003215 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003216 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003217 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003219 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003221 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003222 // Dispose the channel if the input method is not local to this process
3223 // because the remote proxy will get its own copy when unparceled.
3224 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003225 channel.dispose();
3226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003228 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003232
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003233 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003234 final int missingMethods = msg.arg1;
3235 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003236 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003237 final IBinder startInputToken = (IBinder) args.arg1;
3238 final SessionState session = (SessionState) args.arg2;
3239 final IInputContext inputContext = (IInputContext) args.arg3;
3240 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003243 session.method.startInput(startInputToken, inputContext, missingMethods,
3244 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 } catch (RemoteException e) {
3246 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003247 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003252
Yohei Yukawa33e81792015-11-17 21:14:42 -08003253 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003255 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 } catch (RemoteException e) {
3257 // There is nothing interesting about the last client dying.
3258 }
3259 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003260 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003261 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003262 IInputMethodClient client = (IInputMethodClient)args.arg1;
3263 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003265 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003267 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003268 } finally {
3269 // Dispose the channel if the input method is not local to this process
3270 // because the remote proxy will get its own copy when unparceled.
3271 if (res.channel != null && Binder.isProxy(client)) {
3272 res.channel.dispose();
3273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003275 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003277 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003278 case MSG_SET_ACTIVE:
3279 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003280 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003281 } catch (RemoteException e) {
3282 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3283 + ((ClientState)msg.obj).pid + " uid "
3284 + ((ClientState)msg.obj).uid);
3285 }
3286 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003287 case MSG_SET_INTERACTIVE:
3288 handleSetInteractive(msg.arg1 != 0);
3289 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003290 case MSG_SWITCH_IME:
3291 handleSwitchInputMethod(msg.arg1 != 0);
3292 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003293 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3294 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003295 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003296 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003297 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003298 } catch (RemoteException e) {
3299 Slog.w(TAG, "Got RemoteException sending "
3300 + "setUserActionNotificationSequenceNumber("
3301 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003302 + clientState.pid + " uid "
3303 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003304 }
3305 return true;
3306 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003307 case MSG_REPORT_FULLSCREEN_MODE: {
3308 final boolean fullscreen = msg.arg1 != 0;
3309 final ClientState clientState = (ClientState)msg.obj;
3310 try {
3311 clientState.client.reportFullscreenMode(fullscreen);
3312 } catch (RemoteException e) {
3313 Slog.w(TAG, "Got RemoteException sending "
3314 + "reportFullscreen(" + fullscreen + ") notification to pid="
3315 + clientState.pid + " uid=" + clientState.uid);
3316 }
3317 return true;
3318 }
satok01038492012-04-09 21:08:27 +09003319
3320 // --------------------------------------------------------------
3321 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003322 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003323 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003324 case MSG_SYSTEM_UNLOCK_USER:
3325 final int userId = msg.arg1;
3326 onUnlockUser(userId);
3327 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 }
3329 return false;
3330 }
3331
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003332 private void handleSetInteractive(final boolean interactive) {
3333 synchronized (mMethodMap) {
3334 mIsInteractive = interactive;
3335 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3336
3337 // Inform the current client of the change in active status
3338 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003339 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3340 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3341 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003342 }
3343 }
3344 }
3345
Yohei Yukawaae61f712015-12-09 13:00:10 -08003346 private void handleSwitchInputMethod(final boolean forwardDirection) {
3347 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003348 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003349 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003350 if (nextSubtype == null) {
3351 return;
3352 }
3353 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003354 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3355 if (newInputMethodInfo == null) {
3356 return;
3357 }
3358 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3359 newInputMethodInfo, mCurrentSubtype);
3360 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003361 if (mSubtypeSwitchedByShortCutToast == null) {
3362 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3363 Toast.LENGTH_SHORT);
3364 } else {
3365 mSubtypeSwitchedByShortCutToast.setText(toastText);
3366 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003367 mSubtypeSwitchedByShortCutToast.show();
3368 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003369 }
3370 }
3371
satokdc9ddae2011-10-06 12:22:36 +09003372 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003373 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3374 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003375 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003376 if (DEBUG) {
3377 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3378 }
satok723a27e2010-11-11 14:58:11 +09003379 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003380 return true;
3381 }
3382
3383 return false;
3384 }
3385
Yohei Yukawa94e33302016-02-12 19:37:03 -08003386 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003387 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003388 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003389 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003390 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003391 if (!mSystemReady) {
3392 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3393 return;
3394 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003395 mMethodList.clear();
3396 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003397 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003398 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003399
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003400 // Use for queryIntentServicesAsUser
3401 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003403 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3404 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3405 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003406 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003408 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3409 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003410
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003411 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003412 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 for (int i = 0; i < services.size(); ++i) {
3414 ResolveInfo ri = services.get(i);
3415 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003416 final String imeId = InputMethodInfo.computeId(ri);
3417 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3418 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 + ": it does not require the permission "
3420 + android.Manifest.permission.BIND_INPUT_METHOD);
3421 continue;
3422 }
3423
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003424 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003426 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 try {
satoke7c6998e2011-06-03 17:57:59 +09003428 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003429 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003430 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003431 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432
3433 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003434 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003436 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003437 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 }
3439 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003440
Yohei Yukawac4e44912017-02-09 19:30:22 -08003441 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3442 // negatives when the package state remains to be the same but only the component state is
3443 // changed.
3444 {
3445 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3446 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3447 // conservative, but it seems we cannot use it for now (Issue 35176630).
3448 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3449 new Intent(InputMethod.SERVICE_INTERFACE),
3450 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3451 final int N = allInputMethodServices.size();
3452 for (int i = 0; i < N; ++i) {
3453 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003454 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3455 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003456 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003457 }
3458 }
3459
Yohei Yukawa859df052016-02-17 07:56:46 -08003460 // TODO: The following code should find better place to live.
3461 if (!resetDefaultEnabledIme) {
3462 boolean enabledImeFound = false;
3463 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3464 final int N = enabledImes.size();
3465 for (int i = 0; i < N; ++i) {
3466 final InputMethodInfo imi = enabledImes.get(i);
3467 if (mMethodList.contains(imi)) {
3468 enabledImeFound = true;
3469 break;
3470 }
3471 }
3472 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003473 if (DEBUG) {
3474 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3475 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003476 resetDefaultEnabledIme = true;
3477 resetSelectedInputMethodAndSubtypeLocked("");
3478 }
3479 }
3480
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003481 if (resetDefaultEnabledIme) {
3482 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003483 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003484 final int N = defaultEnabledIme.size();
3485 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003486 final InputMethodInfo imi = defaultEnabledIme.get(i);
3487 if (DEBUG) {
3488 Slog.d(TAG, "--- enable ime = " + imi);
3489 }
3490 setInputMethodEnabledLocked(imi.getId(), true);
3491 }
3492 }
3493
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003494 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003495 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003496 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003497 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3498 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003499 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003500 }
3501 } else {
3502 // Double check that the default IME is certainly enabled.
3503 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003504 }
3505 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003506 // Here is not the perfect place to reset the switching controller. Ideally
3507 // mSwitchingController and mSettings should be able to share the same state.
3508 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3509 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003510 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003514
satok217f5482010-12-15 05:19:19 +09003515 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003516 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003517 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003518 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3519 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003520 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003521 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003522 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003523 final int userId;
3524 synchronized (mMethodMap) {
3525 userId = mSettings.getCurrentUserId();
3526 }
3527 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003528 }
3529
3530 private void showConfigureInputMethods() {
3531 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3532 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3533 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3534 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003535 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003536 }
3537
satok2c93efc2012-04-02 19:33:47 +09003538 private boolean isScreenLocked() {
3539 return mKeyguardManager != null
3540 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3541 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003542
Seigo Nonaka14e13912015-05-06 21:04:13 -07003543 private void showInputMethodMenu(boolean showAuxSubtypes) {
3544 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003547 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003548
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003549 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003550 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003551 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003552
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003553 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003554 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003555 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3556 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003557 if (immis == null || immis.size() == 0) {
3558 return;
3559 }
3560
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003561 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562
satok688bd472012-02-09 20:09:17 +09003563 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003564 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003565 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003566
satokc3690562012-01-10 20:14:43 +09003567 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003568 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003569 if (currentSubtype != null) {
3570 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003571 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3572 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003573 }
3574 }
3575
Ken Wakasa761eb372011-03-04 19:06:18 +09003576 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003577 mIms = new InputMethodInfo[N];
3578 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003579 int checkedItem = 0;
3580 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003581 final ImeSubtypeListItem item = imList.get(i);
3582 mIms[i] = item.mImi;
3583 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003584 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003585 int subtypeId = mSubtypeIds[i];
3586 if ((subtypeId == NOT_A_SUBTYPE_ID)
3587 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3588 || (subtypeId == lastInputMethodSubtypeId)) {
3589 checkedItem = i;
3590 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003593
3594 final Context settingsContext = new ContextThemeWrapper(context,
3595 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3596
3597 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003598 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3599 @Override
3600 public void onCancel(DialogInterface dialog) {
3601 hideInputMethodMenu();
3602 }
3603 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003604
3605 final Context dialogContext = mDialogBuilder.getContext();
3606 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3607 com.android.internal.R.styleable.DialogPreference,
3608 com.android.internal.R.attr.alertDialogStyle, 0);
3609 final Drawable dialogIcon = a.getDrawable(
3610 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3611 a.recycle();
3612
3613 mDialogBuilder.setIcon(dialogIcon);
3614
Yohei Yukawad34e1482016-02-11 08:03:52 -08003615 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003616 final View tv = inflater.inflate(
3617 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3618 mDialogBuilder.setCustomTitle(tv);
3619
3620 // Setup layout for a toggle switch of the hardware keyboard
3621 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003622 mSwitchingDialogTitleView
3623 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003624 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003625 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003626 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003627 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003628 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003629 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3630 @Override
3631 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003632 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003633 // Ensure that the input method dialog is dismissed when changing
3634 // the hardware keyboard state.
3635 hideInputMethodMenu();
3636 }
3637 });
3638
Alan Viverette505e3ab2014-11-24 15:22:11 -08003639 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003640 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3641 final OnClickListener choiceListener = new OnClickListener() {
3642 @Override
3643 public void onClick(final DialogInterface dialog, final int which) {
3644 synchronized (mMethodMap) {
3645 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3646 || mSubtypeIds.length <= which) {
3647 return;
satok01038492012-04-09 21:08:27 +09003648 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003649 final InputMethodInfo im = mIms[which];
3650 int subtypeId = mSubtypeIds[which];
3651 adapter.mCheckedItem = which;
3652 adapter.notifyDataSetChanged();
3653 hideInputMethodMenu();
3654 if (im != null) {
3655 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3656 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003657 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003658 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003660 }
3661 }
3662 };
3663 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003666 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003667 final Window w = mSwitchingDialog.getWindow();
3668 final WindowManager.LayoutParams attrs = w.getAttributes();
3669 w.setType(TYPE_INPUT_METHOD_DIALOG);
3670 // Use an alternate token for the dialog for that window manager can group the token
3671 // with other IME windows based on type vs. grouping based on whichever token happens
3672 // to get selected by the system later on.
3673 attrs.token = mSwitchingDialogToken;
3674 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3675 attrs.setTitle("Select input method");
3676 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003677 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 mSwitchingDialog.show();
3679 }
3680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003681
Ken Wakasa05dbb652011-08-22 15:22:43 +09003682 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3683 private final LayoutInflater mInflater;
3684 private final int mTextViewResourceId;
3685 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003686 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003687 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3688 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3689 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003690
Ken Wakasa05dbb652011-08-22 15:22:43 +09003691 mTextViewResourceId = textViewResourceId;
3692 mItemsList = itemsList;
3693 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003694 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003695 }
3696
3697 @Override
3698 public View getView(int position, View convertView, ViewGroup parent) {
3699 final View view = convertView != null ? convertView
3700 : mInflater.inflate(mTextViewResourceId, null);
3701 if (position < 0 || position >= mItemsList.size()) return view;
3702 final ImeSubtypeListItem item = mItemsList.get(position);
3703 final CharSequence imeName = item.mImeName;
3704 final CharSequence subtypeName = item.mSubtypeName;
3705 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3706 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3707 if (TextUtils.isEmpty(subtypeName)) {
3708 firstTextView.setText(imeName);
3709 secondTextView.setVisibility(View.GONE);
3710 } else {
3711 firstTextView.setText(subtypeName);
3712 secondTextView.setText(imeName);
3713 secondTextView.setVisibility(View.VISIBLE);
3714 }
3715 final RadioButton radioButton =
3716 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3717 radioButton.setChecked(position == mCheckedItem);
3718 return view;
3719 }
3720 }
3721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003723 synchronized (mMethodMap) {
3724 hideInputMethodMenuLocked();
3725 }
3726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003727
The Android Open Source Project10592532009-03-18 17:39:46 -07003728 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003729 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730
The Android Open Source Project10592532009-03-18 17:39:46 -07003731 if (mSwitchingDialog != null) {
3732 mSwitchingDialog.dismiss();
3733 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003735
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003736 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003737 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003738 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003742
satok42c5a162011-05-26 16:46:14 +09003743 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003745 // TODO: Make this work even for non-current users?
3746 if (!calledFromValidUser()) {
3747 return false;
3748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 synchronized (mMethodMap) {
3750 if (mContext.checkCallingOrSelfPermission(
3751 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3752 != PackageManager.PERMISSION_GRANTED) {
3753 throw new SecurityException(
3754 "Requires permission "
3755 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3756 }
Anna Galusza9b278112016-01-04 11:37:37 -08003757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 long ident = Binder.clearCallingIdentity();
3759 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003760 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 } finally {
3762 Binder.restoreCallingIdentity(ident);
3763 }
3764 }
3765 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003766
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003767 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3768 // Make sure this is a valid input method.
3769 InputMethodInfo imm = mMethodMap.get(id);
3770 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003771 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003772 }
3773
satokd87c2592010-09-29 11:52:06 +09003774 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3775 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003776
satokd87c2592010-09-29 11:52:06 +09003777 if (enabled) {
3778 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3779 if (pair.first.equals(id)) {
3780 // We are enabling this input method, but it is already enabled.
3781 // Nothing to do. The previous state was enabled.
3782 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003783 }
3784 }
satokd87c2592010-09-29 11:52:06 +09003785 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3786 // Previous state was disabled.
3787 return false;
3788 } else {
3789 StringBuilder builder = new StringBuilder();
3790 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3791 builder, enabledInputMethodsList, id)) {
3792 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003793 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003794 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3795 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3796 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003797 }
3798 // Previous state was enabled.
3799 return true;
3800 } else {
3801 // We are disabling the input method but it is already disabled.
3802 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003803 return false;
3804 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003805 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003806 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003807
satok723a27e2010-11-11 14:58:11 +09003808 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3809 boolean setSubtypeOnly) {
3810 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003811 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003812
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003813 mCurUserActionNotificationSequenceNumber =
3814 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3815 if (DEBUG) {
3816 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3817 + mCurUserActionNotificationSequenceNumber);
3818 }
3819
3820 if (mCurClient != null && mCurClient.client != null) {
3821 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3822 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003823 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003824 }
3825
satok723a27e2010-11-11 14:58:11 +09003826 // Set Subtype here
3827 if (imi == null || subtypeId < 0) {
3828 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003829 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003830 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003831 if (subtypeId < imi.getSubtypeCount()) {
3832 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3833 mSettings.putSelectedSubtype(subtype.hashCode());
3834 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003835 } else {
3836 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003837 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003838 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003839 }
satokab751aa2010-09-14 19:17:36 +09003840 }
satok723a27e2010-11-11 14:58:11 +09003841
Yohei Yukawa68645a62016-02-17 07:54:20 -08003842 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003843 // Set InputMethod here
3844 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3845 }
3846 }
3847
3848 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3849 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3850 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3851 // newDefaultIme is empty when there is no candidate for the selected IME.
3852 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3853 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3854 if (subtypeHashCode != null) {
3855 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003856 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003857 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003858 } catch (NumberFormatException e) {
3859 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3860 }
3861 }
3862 }
3863 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003864 }
3865
satok4e4569d2010-11-19 18:45:53 +09003866 // If there are no selected shortcuts, tries finding the most applicable ones.
3867 private Pair<InputMethodInfo, InputMethodSubtype>
3868 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3869 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3870 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003871 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003872 boolean foundInSystemIME = false;
3873
3874 // Search applicable subtype for each InputMethodInfo
3875 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003876 final String imiId = imi.getId();
3877 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3878 continue;
3879 }
satokcd7cd292010-11-20 15:46:23 +09003880 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003881 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003882 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003883 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003884 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003885 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003886 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003887 }
satokdf31ae62011-01-15 06:19:44 +09003888 // 2. Search by the system locale from enabledSubtypes.
3889 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003890 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003891 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003892 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003893 }
satoka86f5e42011-09-02 17:12:42 +09003894 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003895 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003896 final ArrayList<InputMethodSubtype> subtypesForSearch =
3897 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003898 ? InputMethodUtils.getSubtypes(imi)
3899 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003900 // 4. Search by the current subtype's locale from all subtypes.
3901 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003902 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003903 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003904 }
3905 // 5. Search by the system locale from all subtypes.
3906 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003907 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003908 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003909 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003910 }
satokcd7cd292010-11-20 15:46:23 +09003911 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003912 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003913 // The current input method is the most applicable IME.
3914 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003915 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003916 break;
satok7599a7f2010-12-22 13:45:23 +09003917 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003918 // The system input method is 2nd applicable IME.
3919 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003920 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003921 if ((imi.getServiceInfo().applicationInfo.flags
3922 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3923 foundInSystemIME = true;
3924 }
satok4e4569d2010-11-19 18:45:53 +09003925 }
3926 }
3927 }
3928 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003929 if (mostApplicableIMI != null) {
3930 Slog.w(TAG, "Most applicable shortcut input method was:"
3931 + mostApplicableIMI.getId());
3932 if (mostApplicableSubtype != null) {
3933 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3934 + "," + mostApplicableSubtype.getMode() + ","
3935 + mostApplicableSubtype.getLocale());
3936 }
3937 }
satok4e4569d2010-11-19 18:45:53 +09003938 }
satokcd7cd292010-11-20 15:46:23 +09003939 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003940 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003941 } else {
3942 return null;
3943 }
3944 }
3945
satokab751aa2010-09-14 19:17:36 +09003946 /**
3947 * @return Return the current subtype of this input method.
3948 */
satok42c5a162011-05-26 16:46:14 +09003949 @Override
satokab751aa2010-09-14 19:17:36 +09003950 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003951 // TODO: Make this work even for non-current users?
3952 if (!calledFromValidUser()) {
3953 return null;
3954 }
3955 synchronized (mMethodMap) {
3956 return getCurrentInputMethodSubtypeLocked();
3957 }
3958 }
3959
3960 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003961 if (mCurMethodId == null) {
3962 return null;
3963 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003964 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003965 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3966 if (imi == null || imi.getSubtypeCount() == 0) {
3967 return null;
satok4e4569d2010-11-19 18:45:53 +09003968 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003969 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003970 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3971 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003972 if (subtypeId == NOT_A_SUBTYPE_ID) {
3973 // If there are no selected subtypes, the framework will try to find
3974 // the most applicable subtype from explicitly or implicitly enabled
3975 // subtypes.
3976 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003977 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003978 // If there is only one explicitly or implicitly enabled subtype,
3979 // just returns it.
3980 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3981 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3982 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003983 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003984 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003985 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003986 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003987 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003988 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3989 true);
satok4e4569d2010-11-19 18:45:53 +09003990 }
satok3ef8b292010-11-23 06:06:29 +09003991 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003992 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003993 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003994 }
3995 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003996 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003997 }
3998
satok4e4569d2010-11-19 18:45:53 +09003999 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09004000 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09004001 @Override
satok4e4569d2010-11-19 18:45:53 +09004002 public List getShortcutInputMethodsAndSubtypes() {
4003 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004004 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09004005 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09004006 // If there are no selected shortcut subtypes, the framework will try to find
4007 // the most applicable subtype from all subtypes whose mode is
4008 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09004009 Pair<InputMethodInfo, InputMethodSubtype> info =
4010 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004011 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004012 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004013 ret.add(info.first);
4014 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004015 }
satok3da92232011-01-11 22:46:30 +09004016 return ret;
satokf3db1af2010-11-23 13:34:33 +09004017 }
satokf3db1af2010-11-23 13:34:33 +09004018 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4019 ret.add(imi);
4020 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4021 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004022 }
4023 }
satokf3db1af2010-11-23 13:34:33 +09004024 return ret;
satok4e4569d2010-11-19 18:45:53 +09004025 }
4026 }
4027
satok42c5a162011-05-26 16:46:14 +09004028 @Override
satokb66d2872010-11-10 01:04:04 +09004029 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004030 // TODO: Make this work even for non-current users?
4031 if (!calledFromValidUser()) {
4032 return false;
4033 }
satokb66d2872010-11-10 01:04:04 +09004034 synchronized (mMethodMap) {
4035 if (subtype != null && mCurMethodId != null) {
4036 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004037 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004038 if (subtypeId != NOT_A_SUBTYPE_ID) {
4039 setInputMethodLocked(mCurMethodId, subtypeId);
4040 return true;
4041 }
4042 }
4043 return false;
4044 }
4045 }
4046
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004047 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004048 private static class InputMethodFileManager {
4049 private static final String SYSTEM_PATH = "system";
4050 private static final String INPUT_METHOD_PATH = "inputmethod";
4051 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4052 private static final String NODE_SUBTYPES = "subtypes";
4053 private static final String NODE_SUBTYPE = "subtype";
4054 private static final String NODE_IMI = "imi";
4055 private static final String ATTR_ID = "id";
4056 private static final String ATTR_LABEL = "label";
4057 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004058 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004059 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004060 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004061 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4062 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4063 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004064 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004065 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4066 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004067 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004068 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004069 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004070 if (methodMap == null) {
4071 throw new NullPointerException("methodMap is null");
4072 }
4073 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004074 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004075 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4076 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004077 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004078 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004079 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4080 }
4081 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4082 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4083 if (!subtypeFile.exists()) {
4084 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004085 writeAdditionalInputMethodSubtypes(
4086 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004087 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004088 readAdditionalInputMethodSubtypes(
4089 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004090 }
4091 }
4092
4093 private void deleteAllInputMethodSubtypes(String imiId) {
4094 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004095 mAdditionalSubtypesMap.remove(imiId);
4096 writeAdditionalInputMethodSubtypes(
4097 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004098 }
4099 }
4100
4101 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004102 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004103 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004104 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004105 final int N = additionalSubtypes.length;
4106 for (int i = 0; i < N; ++i) {
4107 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004108 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004109 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004110 } else {
4111 Slog.w(TAG, "Duplicated subtype definition found: "
4112 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004113 }
4114 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004115 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4116 writeAdditionalInputMethodSubtypes(
4117 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004118 }
4119 }
4120
4121 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4122 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004123 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004124 }
4125 }
4126
4127 private static void writeAdditionalInputMethodSubtypes(
4128 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4129 HashMap<String, InputMethodInfo> methodMap) {
4130 // Safety net for the case that this function is called before methodMap is set.
4131 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4132 FileOutputStream fos = null;
4133 try {
4134 fos = subtypesFile.startWrite();
4135 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004136 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004137 out.startDocument(null, true);
4138 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4139 out.startTag(null, NODE_SUBTYPES);
4140 for (String imiId : allSubtypes.keySet()) {
4141 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4142 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4143 continue;
4144 }
4145 out.startTag(null, NODE_IMI);
4146 out.attribute(null, ATTR_ID, imiId);
4147 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4148 final int N = subtypesList.size();
4149 for (int i = 0; i < N; ++i) {
4150 final InputMethodSubtype subtype = subtypesList.get(i);
4151 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004152 if (subtype.hasSubtypeId()) {
4153 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4154 String.valueOf(subtype.getSubtypeId()));
4155 }
satoke7c6998e2011-06-03 17:57:59 +09004156 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4157 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4158 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004159 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4160 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004161 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4162 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4163 out.attribute(null, ATTR_IS_AUXILIARY,
4164 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004165 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4166 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004167 out.endTag(null, NODE_SUBTYPE);
4168 }
4169 out.endTag(null, NODE_IMI);
4170 }
4171 out.endTag(null, NODE_SUBTYPES);
4172 out.endDocument();
4173 subtypesFile.finishWrite(fos);
4174 } catch (java.io.IOException e) {
4175 Slog.w(TAG, "Error writing subtypes", e);
4176 if (fos != null) {
4177 subtypesFile.failWrite(fos);
4178 }
4179 }
4180 }
4181
4182 private static void readAdditionalInputMethodSubtypes(
4183 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4184 if (allSubtypes == null || subtypesFile == null) return;
4185 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004186 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004187 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004188 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004189 int type = parser.getEventType();
4190 // Skip parsing until START_TAG
4191 while ((type = parser.next()) != XmlPullParser.START_TAG
4192 && type != XmlPullParser.END_DOCUMENT) {}
4193 String firstNodeName = parser.getName();
4194 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4195 throw new XmlPullParserException("Xml doesn't start with subtypes");
4196 }
4197 final int depth =parser.getDepth();
4198 String currentImiId = null;
4199 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4200 while (((type = parser.next()) != XmlPullParser.END_TAG
4201 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4202 if (type != XmlPullParser.START_TAG)
4203 continue;
4204 final String nodeName = parser.getName();
4205 if (NODE_IMI.equals(nodeName)) {
4206 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4207 if (TextUtils.isEmpty(currentImiId)) {
4208 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4209 continue;
4210 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004211 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004212 allSubtypes.put(currentImiId, tempSubtypesArray);
4213 } else if (NODE_SUBTYPE.equals(nodeName)) {
4214 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4215 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4216 continue;
4217 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004218 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004219 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004220 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004221 parser.getAttributeValue(null, ATTR_LABEL));
4222 final String imeSubtypeLocale =
4223 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004224 final String languageTag =
4225 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004226 final String imeSubtypeMode =
4227 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4228 final String imeSubtypeExtraValue =
4229 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004230 final boolean isAuxiliary = "1".equals(String.valueOf(
4231 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004232 final boolean isAsciiCapable = "1".equals(String.valueOf(
4233 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004234 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004235 .setSubtypeNameResId(label)
4236 .setSubtypeIconResId(icon)
4237 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004238 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004239 .setSubtypeMode(imeSubtypeMode)
4240 .setSubtypeExtraValue(imeSubtypeExtraValue)
4241 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004242 .setIsAsciiCapable(isAsciiCapable);
4243 final String subtypeIdString =
4244 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4245 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004246 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004247 }
4248 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004249 }
4250 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004251 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4252 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004253 return;
satoke7c6998e2011-06-03 17:57:59 +09004254 }
4255 }
4256 }
4257
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004258 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4259 @NonNull
4260 private final Handler mHandler;
4261
4262 LocalServiceImpl(@NonNull final Handler handler) {
4263 mHandler = handler;
4264 }
4265
4266 @Override
4267 public void setInteractive(boolean interactive) {
4268 // Do everything in handler so as not to block the caller.
4269 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4270 interactive ? 1 : 0, 0));
4271 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004272
4273 @Override
4274 public void switchInputMethod(boolean forwardDirection) {
4275 // Do everything in handler so as not to block the caller.
4276 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4277 forwardDirection ? 1 : 0, 0));
4278 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004279
4280 @Override
4281 public void hideCurrentInputMethod() {
4282 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4283 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4284 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004285 }
4286
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004287 private static String imeWindowStatusToString(final int imeWindowVis) {
4288 final StringBuilder sb = new StringBuilder();
4289 boolean first = true;
4290 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4291 sb.append("Active");
4292 first = false;
4293 }
4294 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4295 if (!first) {
4296 sb.append("|");
4297 }
4298 sb.append("Visible");
4299 }
4300 return sb.toString();
4301 }
4302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004304 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4305 @Nullable Uri contentUri, @Nullable String packageName) {
4306 if (!calledFromValidUser()) {
4307 return null;
4308 }
4309
4310 if (token == null) {
4311 throw new NullPointerException("token");
4312 }
4313 if (packageName == null) {
4314 throw new NullPointerException("packageName");
4315 }
4316 if (contentUri == null) {
4317 throw new NullPointerException("contentUri");
4318 }
4319 final String contentUriScheme = contentUri.getScheme();
4320 if (!"content".equals(contentUriScheme)) {
4321 throw new InvalidParameterException("contentUri must have content scheme");
4322 }
4323
4324 synchronized (mMethodMap) {
4325 final int uid = Binder.getCallingUid();
4326 if (mCurMethodId == null) {
4327 return null;
4328 }
4329 if (mCurToken != token) {
4330 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4331 + " token=" + token);
4332 return null;
4333 }
4334 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4335 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4336 // nature of our system. Let's compare it with our internal record.
4337 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4338 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4339 + mCurAttribute.packageName + " packageName=" + packageName);
4340 return null;
4341 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004342 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004343 final int imeUserId = UserHandle.getUserId(uid);
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 appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004346 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4347 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4348 imeUserId);
4349 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4350 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4351 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4352 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4353 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4354 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4355 // actually allowed to "uid", which is guaranteed to be the IME's one.
4356 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4357 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004358 }
4359 }
4360
4361 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004362 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4363 if (!calledFromValidUser()) {
4364 return;
4365 }
4366 synchronized (mMethodMap) {
4367 if (!calledWithValidToken(token)) {
4368 return;
4369 }
4370 if (mCurClient != null && mCurClient.client != null) {
4371 mInFullscreenMode = fullscreen;
4372 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4373 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4374 }
4375 }
4376 }
4377
4378 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4380 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4381 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4384 + Binder.getCallingPid()
4385 + ", uid=" + Binder.getCallingUid());
4386 return;
4387 }
4388
4389 IInputMethod method;
4390 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004391 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 synchronized (mMethodMap) {
4396 p.println("Current Input Method Manager state:");
4397 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004398 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 for (int i=0; i<N; i++) {
4400 InputMethodInfo info = mMethodList.get(i);
4401 p.println(" InputMethod #" + i + ":");
4402 info.dump(p, " ");
4403 }
4404 p.println(" Clients:");
4405 for (ClientState ci : mClients.values()) {
4406 p.println(" Client " + ci + ":");
4407 p.println(" client=" + ci.client);
4408 p.println(" inputContext=" + ci.inputContext);
4409 p.println(" sessionRequested=" + ci.sessionRequested);
4410 p.println(" curSession=" + ci.curSession);
4411 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004412 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004414 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004415 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4416 + " softInputMode=" +
4417 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4418 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004419 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4421 + " mBoundToMethod=" + mBoundToMethod);
4422 p.println(" mCurToken=" + mCurToken);
4423 p.println(" mCurIntent=" + mCurIntent);
4424 method = mCurMethod;
4425 p.println(" mCurMethod=" + mCurMethod);
4426 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004427 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 p.println(" mShowRequested=" + mShowRequested
4429 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4430 + " mShowForced=" + mShowForced
4431 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004432 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004433 p.println(" mCurUserActionNotificationSequenceNumber="
4434 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004435 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004436 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004437 p.println(" mSwitchingController:");
4438 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004439 p.println(" mSettings:");
4440 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004441
4442 p.println(" mStartInputHistory:");
4443 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004445
Jeff Brownb88102f2010-09-08 11:49:43 -07004446 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 pw.flush();
4449 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004450 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4451 } catch (IOException | RemoteException e) {
4452 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004454 } else {
4455 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004457
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004458 if (focusedWindowClient != null && client != focusedWindowClient) {
4459 p.println(" ");
4460 p.println("Warning: Current input method client doesn't match the last focused. "
4461 + "window.");
4462 p.println("Dumping input method client in the last focused window just in case.");
4463 p.println(" ");
4464 pw.flush();
4465 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004466 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4467 } catch (IOException | RemoteException e) {
4468 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004469 }
4470 }
4471
Jeff Brownb88102f2010-09-08 11:49:43 -07004472 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 pw.flush();
4475 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004476 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4477 } catch (IOException | RemoteException e) {
4478 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004480 } else {
4481 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 }
4483 }
4484}