blob: c5f9599ac816e709817837d076115f04a8c6bab0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070018import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale3a931692016-11-02 16:49:48 -070019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070020import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwale3a931692016-11-02 16:49:48 -070021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Yohei Yukawac4e44912017-02-09 19:30:22 -080024import com.android.internal.annotations.GuardedBy;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080025import com.android.internal.content.PackageMonitor;
Yohei Yukawa25e08132016-06-22 16:31:41 -070026import com.android.internal.inputmethod.IInputContentUriToken;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090027import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090028import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090029import com.android.internal.inputmethod.InputMethodUtils;
30import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
Chris Wren282cfef2017-03-27 15:01:44 -040031import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050032import com.android.internal.notification.SystemNotificationChannels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070034import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070035import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090036import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import com.android.internal.view.IInputContext;
38import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import com.android.internal.view.IInputMethodClient;
40import com.android.internal.view.IInputMethodManager;
41import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070042import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080044import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080045import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
satoke7c6998e2011-06-03 17:57:59 +090047import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090049import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070051import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070052import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070053import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080054import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080055import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070056import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070058import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090059import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090060import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090061import android.app.Notification;
62import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070063import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090064import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080066import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.ContentResolver;
68import android.content.Context;
69import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090071import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090073import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070075import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090076import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.content.pm.PackageManager;
78import android.content.pm.ResolveInfo;
79import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070080import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.content.res.Resources;
82import android.content.res.TypedArray;
83import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080084import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080085import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080086import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070087import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040089import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070090import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090091import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.os.Handler;
93import android.os.IBinder;
94import android.os.IInterface;
95import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070096import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070098import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800100import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.os.ServiceManager;
102import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900103import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800104import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.provider.Settings;
106import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900107import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700108import android.util.ArrayMap;
109import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700110import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900112import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900113import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.util.PrintWriterPrinter;
115import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900116import android.util.Slog;
117import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900118import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700120import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900121import android.view.LayoutInflater;
122import android.view.View;
123import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700124import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700126import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900127import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800129import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700130import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import android.view.inputmethod.InputMethod;
132import android.view.inputmethod.InputMethodInfo;
133import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700134import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900135import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900136import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900137import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900138import android.widget.CompoundButton;
139import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900140import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900141import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900142import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700143import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
satoke7c6998e2011-06-03 17:57:59 +0900145import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900147import java.io.FileInputStream;
148import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149import java.io.IOException;
150import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700151import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100152import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700153import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800154import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900156import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800157import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158import java.util.HashMap;
159import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800160import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800161import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800162import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164/**
165 * This class provides a system service that manages input methods.
166 */
167public class InputMethodManagerService extends IInputMethodManager.Stub
168 implements ServiceConnection, Handler.Callback {
169 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700170 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700171 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700173 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
174 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
175 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 static final int MSG_UNBIND_INPUT = 1000;
178 static final int MSG_BIND_INPUT = 1010;
179 static final int MSG_SHOW_SOFT_INPUT = 1020;
180 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700181 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 static final int MSG_ATTACH_TOKEN = 1040;
183 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800186
Yohei Yukawa33e81792015-11-17 21:14:42 -0800187 static final int MSG_UNBIND_CLIENT = 3000;
188 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700189 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700190 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900191 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800192 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800193 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
satok01038492012-04-09 21:08:27 +0900195 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
196
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700197 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
198
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700199 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
satokf9f01002011-05-19 21:31:50 +0900201 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
202
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900203 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900204 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900205
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700206 @Retention(SOURCE)
207 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
208 private @interface HardKeyboardBehavior {
209 int WIRELESS_AFFORDANCE = 0;
210 int WIRED_AFFORDANCE = 1;
211 }
satok4e4569d2010-11-19 18:45:53 +0900212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800214 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900216 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700219 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700221 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900222 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900223 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900224 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800225 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900227 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 // All known input methods. mMethodMap also serves as the global
230 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700231 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
232 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900233 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700234 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900235 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
Yohei Yukawae0733062017-02-09 22:49:35 -0800237 /**
238 * Tracks how many times {@link #mMethodMap} was updated.
239 */
240 @GuardedBy("mMethodMap")
241 private int mMethodMapUpdateCount = 0;
242
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700243 // Used to bring IME service up to visible adjustment while it is being shown.
244 final ServiceConnection mVisibleConnection = new ServiceConnection() {
245 @Override public void onServiceConnected(ComponentName name, IBinder service) {
246 }
247
248 @Override public void onServiceDisconnected(ComponentName name) {
249 }
250 };
251 boolean mVisibleBound = false;
252
satok7cfc0ed2011-06-20 21:29:36 +0900253 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700254 private NotificationManager mNotificationManager;
255 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700256 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400257 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700258 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900259 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900260 private boolean mNotificationShown;
261
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900262 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 final ClientState client;
264 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700265
266 IInputMethodSession session;
267 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 @Override
270 public String toString() {
271 return "SessionState{uid " + client.uid + " pid " + client.pid
272 + " method " + Integer.toHexString(
273 System.identityHashCode(method))
274 + " session " + Integer.toHexString(
275 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700276 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 + "}";
278 }
279
280 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700281 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 client = _client;
283 method = _method;
284 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700285 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
Jeff Brownc28867a2013-03-26 15:42:39 -0700289 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 final IInputMethodClient client;
291 final IInputContext inputContext;
292 final int uid;
293 final int pid;
294 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 boolean sessionRequested;
297 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 @Override
300 public String toString() {
301 return "ClientState{" + Integer.toHexString(
302 System.identityHashCode(this)) + " uid " + uid
303 + " pid " + pid + "}";
304 }
305
306 ClientState(IInputMethodClient _client, IInputContext _inputContext,
307 int _uid, int _pid) {
308 client = _client;
309 inputContext = _inputContext;
310 uid = _uid;
311 pid = _pid;
312 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
313 }
314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700316 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700319 * Set once the system is ready to run third party code.
320 */
321 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700323 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700324 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
325 * method. This is to be synchronized with the secure settings keyed with
326 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
327 *
328 * <p>This can be transiently {@code null} when the system is re-initializing input method
329 * settings, e.g., the system locale is just changed.</p>
330 *
331 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
332 * {@link InputMethodManagerService}.</p>
333 *
334 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700336 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * The current binding sequence number, incremented every time there is
341 * a new bind performed.
342 */
343 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
346 * The client that is currently bound to an input method.
347 */
348 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800351 * The last window token that we confirmed to be focused. This is always updated upon reports
352 * from the input method client. If the window state is already changed before the report is
353 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700354 */
355 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700357 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800358 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
359 *
360 * @see #mCurFocusedWindow
361 */
362 int mCurFocusedWindowSoftInputMode;
363
364 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800365 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
366 */
367 ClientState mCurFocusedWindowClient;
368
369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 * The input context last provided by the current client.
371 */
372 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700375 * The missing method flags for the input context last provided by the current client.
376 *
377 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
378 */
379 int mCurInputContextMissingMethods;
380
381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * The attributes last provided by the current client.
383 */
384 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700387 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700389 *
390 * <p>This can be {@code null} when no input method is connected.</p>
391 *
392 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700394 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 /**
satokab751aa2010-09-14 19:17:36 +0900398 * The current subtype of the current input method.
399 */
400 private InputMethodSubtype mCurrentSubtype;
401
satok4e4569d2010-11-19 18:45:53 +0900402 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900403 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700404 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900405
John Spurlocke0980502013-10-25 11:59:29 -0400406 // Was the keyguard locked when this client became current?
407 private boolean mCurClientInKeyguard;
408
satokab751aa2010-09-14 19:17:36 +0900409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 * Set to true if our ServiceConnection is currently actively bound to
411 * a service (whether or not we have gotten its IBinder back yet).
412 */
413 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
416 * Set if the client has asked for the input method to be shown.
417 */
418 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 /**
421 * Set if we were explicitly told to show the input method.
422 */
423 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * Set if we were forced to be shown.
427 */
428 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * Set if we last told the input method to show itself.
432 */
433 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800436 * {@code true} if the current input method is in fullscreen mode.
437 */
438 boolean mInFullscreenMode;
439
440 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 * The Intent used to connect to the current input method.
442 */
443 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
446 * The token we have made for the currently active input method, to
447 * identify it in the future.
448 */
449 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
452 * If non-null, this is the input method service we are currently connected
453 * to.
454 */
455 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 /**
458 * Time that we last initiated a bind to the input method, to determine
459 * if we should try to disconnect and reconnect to it.
460 */
461 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 /**
464 * Have we called mCurMethod.bindInput()?
465 */
466 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 /**
469 * Currently enabled session. Only touched by service thread, not
470 * protected by a lock.
471 */
472 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700475 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700477 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800478
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900479 int mCurUserActionNotificationSequenceNumber = 0;
480
Joe Onorato857fd9b2011-01-27 15:08:35 -0800481 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900482
483 /**
484 * A set of status bits regarding the active IME.
485 *
486 * <p>This value is a combination of following two bits:</p>
487 * <dl>
488 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
489 * <dd>
490 * If this bit is ON, connected IME is ready to accept touch/key events.
491 * </dd>
492 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
493 * <dd>
494 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
495 * </dd>
496 * </dl>
497 * <em>Do not update this value outside of setImeWindowStatus.</em>
498 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800499 int mImeWindowVis;
500
Ken Wakasa05dbb652011-08-22 15:22:43 +0900501 private AlertDialog.Builder mDialogBuilder;
502 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700503 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900504 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700505 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900506 private InputMethodInfo[] mIms;
507 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800508 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700509 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800510 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900511 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
512 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500513 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700514 @HardKeyboardBehavior
515 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800517 /**
518 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
519 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
520 * will not affect those tasks that are already posted.
521 *
522 * <p>Posting {@link #MSG_START_INPUT} message basically means that
523 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
524 * back in the current IME process shortly, which will also affect what the current IME starts
525 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
526 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
527 * logical input session between the client application and the current IME.</p>
528 *
529 * <p>Be careful to not keep strong references to this object forever, which can prevent
530 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
531 * </p>
532 */
533 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800534 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
535
536 final int mSequenceNumber;
537 final long mTimestamp;
538 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800539 @NonNull
540 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800541 @NonNull
542 final String mImeId;
543 // @InputMethodClient.StartInputReason
544 final int mStartInputReason;
545 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800546 @Nullable
547 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800548 @NonNull
549 final EditorInfo mEditorInfo;
550 final int mTargetWindowSoftInputMode;
551 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800552
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800553 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
554 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
555 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
556 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
557 mSequenceNumber = sSequenceNumber.getAndIncrement();
558 mTimestamp = SystemClock.uptimeMillis();
559 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800560 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800561 mImeId = imeId;
562 mStartInputReason = startInputReason;
563 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800564 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800565 mEditorInfo = editorInfo;
566 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
567 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800568 }
569 }
570
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800571 @GuardedBy("mMethodMap")
572 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800573
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800574 /**
575 * A ring buffer to store the history of {@link StartInputInfo}.
576 */
577 private static final class StartInputHistory {
578 /**
579 * Entry size for non low-RAM devices.
580 *
581 * <p>TODO: Consider to follow what other system services have been doing to manage
582 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
583 */
584 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
585
586 /**
587 * Entry size for non low-RAM devices.
588 *
589 * <p>TODO: Consider to follow what other system services have been doing to manage
590 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
591 */
592 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
593
594 private static int getEntrySize() {
595 if (ActivityManager.isLowRamDeviceStatic()) {
596 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
597 } else {
598 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
599 }
600 }
601
602 /**
603 * Backing store for the ring bugger.
604 */
605 private final Entry[] mEntries = new Entry[getEntrySize()];
606
607 /**
608 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
609 * write.
610 */
611 private int mNextIndex = 0;
612
613 /**
614 * Recyclable entry to store the information in {@link StartInputInfo}.
615 */
616 private static final class Entry {
617 int mSequenceNumber;
618 long mTimestamp;
619 long mWallTime;
620 @NonNull
621 String mImeTokenString;
622 @NonNull
623 String mImeId;
624 /* @InputMethodClient.StartInputReason */
625 int mStartInputReason;
626 boolean mRestarting;
627 @NonNull
628 String mTargetWindowString;
629 @NonNull
630 EditorInfo mEditorInfo;
631 int mTargetWindowSoftInputMode;
632 int mClientBindSequenceNumber;
633
634 Entry(@NonNull StartInputInfo original) {
635 set(original);
636 }
637
638 void set(@NonNull StartInputInfo original) {
639 mSequenceNumber = original.mSequenceNumber;
640 mTimestamp = original.mTimestamp;
641 mWallTime = original.mWallTime;
642 // Intentionally convert to String so as not to keep a strong reference to a Binder
643 // object.
644 mImeTokenString = String.valueOf(original.mImeToken);
645 mImeId = original.mImeId;
646 mStartInputReason = original.mStartInputReason;
647 mRestarting = original.mRestarting;
648 // Intentionally convert to String so as not to keep a strong reference to a Binder
649 // object.
650 mTargetWindowString = String.valueOf(original.mTargetWindow);
651 mEditorInfo = original.mEditorInfo;
652 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
653 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
654 }
655 }
656
657 /**
658 * Add a new entry and discard the oldest entry as needed.
659 * @param info {@lin StartInputInfo} to be added.
660 */
661 void addEntry(@NonNull StartInputInfo info) {
662 final int index = mNextIndex;
663 if (mEntries[index] == null) {
664 mEntries[index] = new Entry(info);
665 } else {
666 mEntries[index].set(info);
667 }
668 mNextIndex = (mNextIndex + 1) % mEntries.length;
669 }
670
671 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
672 final SimpleDateFormat dataFormat =
673 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
674
675 for (int i = 0; i < mEntries.length; ++i) {
676 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
677 if (entry == null) {
678 continue;
679 }
680 pw.print(prefix);
681 pw.println("StartInput #" + entry.mSequenceNumber + ":");
682
683 pw.print(prefix);
684 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
685 + " (timestamp=" + entry.mTimestamp + ")"
686 + " reason="
687 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
688 + " restarting=" + entry.mRestarting);
689
690 pw.print(prefix);
691 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
692
693 pw.print(prefix);
694 pw.println(" targetWin=" + entry.mTargetWindowString
695 + " [" + entry.mEditorInfo.packageName + "]"
696 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
697
698 pw.print(prefix);
699 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
700 entry.mTargetWindowSoftInputMode));
701
702 pw.print(prefix);
703 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
704 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
705 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
706 + " fieldName=" + entry.mEditorInfo.fieldName
707 + " actionId=" + entry.mEditorInfo.actionId
708 + " actionLabel=" + entry.mEditorInfo.actionLabel);
709 }
710 }
711 }
712
713 @GuardedBy("mMethodMap")
714 @NonNull
715 private final StartInputHistory mStartInputHistory = new StartInputHistory();
716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700718 int mUserId;
719 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700720 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800721 String mLastEnabled = "";
722
Yohei Yukawa81482972015-06-04 00:58:59 -0700723 /**
724 * <em>This constructor must be called within the lock.</em>
725 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 SettingsObserver(Handler handler) {
727 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700728 }
729
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800730 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700731 if (mRegistered && mUserId == userId) {
732 return;
733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700735 if (mRegistered) {
736 mContext.getContentResolver().unregisterContentObserver(this);
737 mRegistered = false;
738 }
739 if (mUserId != userId) {
740 mLastEnabled = "";
741 mUserId = userId;
742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700744 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900745 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700746 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900747 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700748 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700749 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700750 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800751 resolver.registerContentObserver(Settings.Secure.getUriFor(
752 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700753 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800755
Michael Wright7b5a96b2014-08-09 19:28:42 -0700756 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800757 final Uri showImeUri = Settings.Secure.getUriFor(
758 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
759 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
760 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700762 if (showImeUri.equals(uri)) {
763 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800764 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
765 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
766 mContext.getContentResolver(),
767 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
768 0, mUserId) == 1;
769 if (mAccessibilityRequestingNoSoftKeyboard) {
770 final boolean showRequested = mShowRequested;
771 hideCurrentInputLocked(0, null);
772 mShowRequested = showRequested;
773 } else if (mShowRequested) {
774 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
775 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700776 } else {
777 boolean enabledChanged = false;
778 String newEnabled = mSettings.getEnabledInputMethodsStr();
779 if (!mLastEnabled.equals(newEnabled)) {
780 mLastEnabled = newEnabled;
781 enabledChanged = true;
782 }
783 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700787
788 @Override
789 public String toString() {
790 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
791 + " mLastEnabled=" + mLastEnabled + "}";
792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800794
Yohei Yukawa79247822017-01-23 15:26:15 -0800795 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900796 @Override
797 public void onReceive(Context context, Intent intent) {
798 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700799 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900800 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700801 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900802 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800803 } else if (Intent.ACTION_USER_ADDED.equals(action)
804 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100805 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800806 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700807 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
808 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
809 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
810 final String prevValue = intent.getStringExtra(
811 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
812 final String newValue = intent.getStringExtra(
813 Intent.EXTRA_SETTING_NEW_VALUE);
814 restoreEnabledInputMethods(mContext, prevValue, newValue);
815 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800816 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800817 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900818 } else {
819 Slog.w(TAG, "Unexpected intent " + intent);
820 }
821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800823
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800824 /**
825 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
826 *
827 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
828 * the users. We should ignore this event if this is about any background user's locale.</p>
829 *
830 * <p>Caution: This method must not be called when system is not ready.</p>
831 */
832 void onActionLocaleChanged() {
833 synchronized (mMethodMap) {
834 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
835 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
836 return;
837 }
838 buildInputMethodListLocked(true);
839 // If the locale is changed, needs to reset the default ime
840 resetDefaultImeLocked(mContext);
841 updateFromSettingsLocked(true);
842 mLastSystemLocales = possibleNewLocale;
843 }
844 }
845
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700846 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
847 // does not attempt to validate on the fly with any installed device policy, so must only
848 // be run in the context of initial device setup.
849 //
850 // TODO: Move this method to InputMethodUtils with adding unit tests.
851 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
852 if (DEBUG_RESTORE) {
853 Slog.i(TAG, "Restoring enabled input methods:");
854 Slog.i(TAG, "prev=" + prevValue);
855 Slog.i(TAG, " new=" + newValue);
856 }
857 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900858 ArrayMap<String, ArraySet<String>> prevMap =
859 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
860 ArrayMap<String, ArraySet<String>> newMap =
861 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700862
863 // Merge the restored ime+subtype enabled states into the live state
864 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
865 final String imeId = entry.getKey();
866 ArraySet<String> prevSubtypes = prevMap.get(imeId);
867 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700868 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700869 prevMap.put(imeId, prevSubtypes);
870 }
871 prevSubtypes.addAll(entry.getValue());
872 }
873
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700874 final String mergedImesAndSubtypesString =
875 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700876 if (DEBUG_RESTORE) {
877 Slog.i(TAG, "Merged IME string:");
878 Slog.i(TAG, " " + mergedImesAndSubtypesString);
879 }
880 Settings.Secure.putString(context.getContentResolver(),
881 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
882 }
883
Yohei Yukawac4e44912017-02-09 19:30:22 -0800884 final class MyPackageMonitor extends PackageMonitor {
885 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800886 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800887 *
888 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
889 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
890 * dynamically unless the entire package is updated, which also always triggers package
891 * rescanning.</p>
892 */
893 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800894 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
895
896 /**
897 * Packages that are appeared, disappeared, or modified for whatever reason.
898 *
899 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
900 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
901 * duplicate elements for our use case.</p>
902 *
903 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
904 * which should be bound to {@link #getRegisteredHandler()}.</p>
905 */
906 private final ArrayList<String> mChangedPackages = new ArrayList<>();
907
908 /**
909 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
910 *
911 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
912 * which should be bound to {@link #getRegisteredHandler()}.</p>
913 */
914 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800915
916 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800917 void clearKnownImePackageNamesLocked() {
918 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800919 }
920
921 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800922 final void addKnownImePackageNameLocked(@NonNull String packageName) {
923 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800924 }
925
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800926 @GuardedBy("mMethodMap")
927 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900928 final int userId = getChangingUserId();
929 final boolean retval = userId == mSettings.getCurrentUserId();
930 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900931 if (!retval) {
932 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
933 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900934 }
935 return retval;
936 }
937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800939 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800941 if (!isChangingPackagesOfCurrentUserLocked()) {
942 return false;
943 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 final int N = mMethodList.size();
946 if (curInputMethodId != null) {
947 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800948 InputMethodInfo imi = mMethodList.get(i);
949 if (imi.getId().equals(curInputMethodId)) {
950 for (String pkg : packages) {
951 if (imi.getPackageName().equals(pkg)) {
952 if (!doit) {
953 return true;
954 }
satok723a27e2010-11-11 14:58:11 +0900955 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800956 chooseNewDefaultIMELocked();
957 return true;
958 }
959 }
960 }
961 }
962 }
963 }
964 return false;
965 }
966
967 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800968 public void onBeginPackageChanges() {
969 clearPackageChangeState();
970 }
971
972 @Override
973 public void onPackageAppeared(String packageName, int reason) {
974 if (!mImePackageAppeared) {
975 final PackageManager pm = mContext.getPackageManager();
976 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
977 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
978 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
979 // No need to lock this because we access it only on getRegisteredHandler().
980 if (!services.isEmpty()) {
981 mImePackageAppeared = true;
982 }
983 }
984 // No need to lock this because we access it only on getRegisteredHandler().
985 mChangedPackages.add(packageName);
986 }
987
988 @Override
989 public void onPackageDisappeared(String packageName, int reason) {
990 // No need to lock this because we access it only on getRegisteredHandler().
991 mChangedPackages.add(packageName);
992 }
993
994 @Override
995 public void onPackageModified(String packageName) {
996 // No need to lock this because we access it only on getRegisteredHandler().
997 mChangedPackages.add(packageName);
998 }
999
1000 @Override
1001 public void onPackagesSuspended(String[] packages) {
1002 // No need to lock this because we access it only on getRegisteredHandler().
1003 for (String packageName : packages) {
1004 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001005 }
1006 }
1007
1008 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001009 public void onPackagesUnsuspended(String[] packages) {
1010 // No need to lock this because we access it only on getRegisteredHandler().
1011 for (String packageName : packages) {
1012 mChangedPackages.add(packageName);
1013 }
1014 }
1015
1016 @Override
1017 public void onFinishPackageChanges() {
1018 onFinishPackageChangesInternal();
1019 clearPackageChangeState();
1020 }
1021
1022 private void clearPackageChangeState() {
1023 // No need to lock them because we access these fields only on getRegisteredHandler().
1024 mChangedPackages.clear();
1025 mImePackageAppeared = false;
1026 }
1027
1028 private boolean shouldRebuildInputMethodListLocked() {
1029 // This method is guaranteed to be called only by getRegisteredHandler().
1030
1031 // If there is any new package that contains at least one IME, then rebuilt the list
1032 // of IMEs.
1033 if (mImePackageAppeared) {
1034 return true;
1035 }
1036
1037 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1038 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1039 // is an option, but it may still do some extra operations that we do not need here.
1040 final int N = mChangedPackages.size();
1041 for (int i = 0; i < N; ++i) {
1042 final String packageName = mChangedPackages.get(i);
1043 if (mKnownImePackageNames.contains(packageName)) {
1044 return true;
1045 }
1046 }
1047 return false;
1048 }
1049
1050 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001051 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001052 if (!isChangingPackagesOfCurrentUserLocked()) {
1053 return;
1054 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001055 if (!shouldRebuildInputMethodListLocked()) {
1056 return;
1057 }
1058
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001059 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001060 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001061 final int N = mMethodList.size();
1062 if (curInputMethodId != null) {
1063 for (int i=0; i<N; i++) {
1064 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001065 final String imiId = imi.getId();
1066 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001067 curIm = imi;
1068 }
satoke7c6998e2011-06-03 17:57:59 +09001069
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001070 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001071 if (isPackageModified(imi.getPackageName())) {
1072 mFileManager.deleteAllInputMethodSubtypes(imiId);
1073 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001074 if (change == PACKAGE_TEMPORARY_CHANGE
1075 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001076 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001077 + imi.getComponent());
1078 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 }
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
Yohei Yukawa94e33302016-02-12 19:37:03 -08001083 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001086
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001087 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001088 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001089 if (change == PACKAGE_TEMPORARY_CHANGE
1090 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001091 ServiceInfo si = null;
1092 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001093 si = mIPackageManager.getServiceInfo(
1094 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1095 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001096 }
1097 if (si == null) {
1098 // Uh oh, current input method is no longer around!
1099 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001100 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001101 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001102 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001103 changed = true;
1104 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001105 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001106 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001107 }
1108 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001109 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001110 }
satokab751aa2010-09-14 19:17:36 +09001111
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001112 if (curIm == null) {
1113 // We currently don't have a default input method... is
1114 // one now available?
1115 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001116 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1117 // Even if the current input method is still available, mCurrentSubtype could
1118 // be obsolete when the package is modified in practice.
1119 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001122 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001123 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125 }
1126 }
1127 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001128
Jeff Brownc28867a2013-03-26 15:42:39 -07001129 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001130 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001131 private final IInputMethod mMethod;
1132 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
Jeff Brownc28867a2013-03-26 15:42:39 -07001134 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1135 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001136 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001137 mMethod = method;
1138 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140
satoke7c6998e2011-06-03 17:57:59 +09001141 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001142 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001143 long ident = Binder.clearCallingIdentity();
1144 try {
1145 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1146 } finally {
1147 Binder.restoreCallingIdentity(ident);
1148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 }
1150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
satok01038492012-04-09 21:08:27 +09001152 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001153 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001154 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001155 public void onHardKeyboardStatusChange(boolean available) {
1156 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1157 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001158 }
1159
Michael Wright7b5a96b2014-08-09 19:28:42 -07001160 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001161 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001162 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001163 }
1164 synchronized(mMethodMap) {
1165 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1166 && mSwitchingDialog.isShowing()) {
1167 mSwitchingDialogTitleView.findViewById(
1168 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1169 available ? View.VISIBLE : View.GONE);
1170 }
1171 }
1172 }
1173 }
1174
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001175 public static final class Lifecycle extends SystemService {
1176 private InputMethodManagerService mService;
1177
1178 public Lifecycle(Context context) {
1179 super(context);
1180 mService = new InputMethodManagerService(context);
1181 }
1182
1183 @Override
1184 public void onStart() {
1185 LocalServices.addService(InputMethodManagerInternal.class,
1186 new LocalServiceImpl(mService.mHandler));
1187 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1188 }
1189
1190 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001191 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001192 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001193 // TODO: Dispatch this to a worker thread as needed.
1194 mService.onSwitchUser(userHandle);
1195 }
1196
1197 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001198 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001199 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001200 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001201 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1202 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1203 .getService(Context.STATUS_BAR_SERVICE);
1204 mService.systemRunning(statusBarService);
1205 }
1206 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001207
1208 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001209 public void onUnlockUser(final @UserIdInt int userHandle) {
1210 // Called on ActivityManager thread.
1211 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001212 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001213 }
1214 }
1215
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001216 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001217 synchronized(mMethodMap) {
1218 final int currentUserId = mSettings.getCurrentUserId();
1219 if (DEBUG) {
1220 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1221 }
1222 if (userId != currentUserId) {
1223 return;
1224 }
1225 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001226 if (mSystemReady) {
1227 // We need to rebuild IMEs.
1228 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1229 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1230 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001231 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001232 }
1233
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001234 void onSwitchUser(@UserIdInt int userId) {
1235 synchronized (mMethodMap) {
1236 switchUserLocked(userId);
1237 }
1238 }
1239
Seigo Nonaka7309b122015-08-17 18:34:13 -07001240 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001241 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001243 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001245 // Note: SettingsObserver doesn't register observers in its constructor.
1246 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 mIWindowManager = IWindowManager.Stub.asInterface(
1248 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001249 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001250 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001251 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 public void executeMessage(Message msg) {
1253 handleMessage(msg);
1254 }
Mita Yuned218c72012-12-06 17:18:25 -08001255 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001256 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001257 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001258 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001259 mHasFeature = context.getPackageManager().hasSystemFeature(
1260 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001261 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001262 mHardKeyboardBehavior = mContext.getResources().getInteger(
1263 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001264
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001265 Bundle extras = new Bundle();
1266 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001267 mImeSwitcherNotification =
1268 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1269 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1270 .setWhen(0)
1271 .setOngoing(true)
1272 .addExtras(extras)
1273 .setCategory(Notification.CATEGORY_SYSTEM)
1274 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001275
satok7cfc0ed2011-06-20 21:29:36 +09001276 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001277 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001278
1279 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001280
satok7cfc0ed2011-06-20 21:29:36 +09001281 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001282 int userId = 0;
1283 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001284 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001285 } catch (RemoteException e) {
1286 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1287 }
satok913a8922010-08-26 21:53:41 +09001288
satokd87c2592010-09-29 11:52:06 +09001289 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001290 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001291 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001292
Kenny Guy2a764942014-04-02 13:29:20 +01001293 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001294 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001295 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1296 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001297 }
1298
satok5b927c432012-05-01 20:09:34 +09001299 private void resetDefaultImeLocked(Context context) {
1300 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001301 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001302 return;
1303 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001304 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001305 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001306 if (suitableImes.isEmpty()) {
1307 Slog.i(TAG, "No default found");
1308 return;
satok5b927c432012-05-01 20:09:34 +09001309 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001310 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001311 if (DEBUG) {
1312 Slog.i(TAG, "Default found, using " + defIm.getId());
1313 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001314 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001315 }
1316
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001317 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001318 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1319 + " currentUserId=" + mSettings.getCurrentUserId());
1320
Yohei Yukawa81482972015-06-04 00:58:59 -07001321 // ContentObserver should be registered again when the user is changed
1322 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001323
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001324 // If the system is not ready or the device is not yed unlocked by the user, then we use
1325 // copy-on-write settings.
1326 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001327 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001328 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001329 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001330 // InputMethodFileManager should be reset when the user is changed
1331 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001332 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001333
1334 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1335 + " defaultImiId=" + defaultImiId);
1336
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001337 // For secondary users, the list of enabled IMEs may not have been updated since the
1338 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1339 // not be empty even if the IME has been uninstalled by the primary user.
1340 // Even in such cases, IMMS works fine because it will find the most applicable
1341 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001342 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001343 mLastSystemLocales = mRes.getConfiguration().getLocales();
1344
1345 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1346 if (mSystemReady) {
1347 hideCurrentInputLocked(0, null);
1348 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1349 buildInputMethodListLocked(initialUserSwitch);
1350 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1351 // This is the first time of the user switch and
1352 // set the current ime to the proper one.
1353 resetDefaultImeLocked(mContext);
1354 }
1355 updateFromSettingsLocked(true);
1356 try {
1357 startInputInnerLocked();
1358 } catch (RuntimeException e) {
1359 Slog.w(TAG, "Unexpected exception", e);
1360 }
1361 }
1362
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001363 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001364 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1365 mSettings.getEnabledInputMethodListLocked(), newUserId,
1366 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001367 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001368
1369 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1370 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001371 }
1372
Kenny Guy2a764942014-04-02 13:29:20 +01001373 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001374 mSettings.setCurrentProfileIds(
1375 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001376 }
1377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 @Override
1379 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1380 throws RemoteException {
1381 try {
1382 return super.onTransact(code, data, reply, flags);
1383 } catch (RuntimeException e) {
1384 // The input method manager only throws security exceptions, so let's
1385 // log all others.
1386 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001387 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389 throw e;
1390 }
1391 }
1392
Svetoslav Ganova0027152013-06-25 14:59:53 -07001393 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001394 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001395 if (DEBUG) {
1396 Slog.d(TAG, "--- systemReady");
1397 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001398 if (!mSystemReady) {
1399 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001400 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001401 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001402 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001403 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001404 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1405 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001406 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001407 if (mStatusBar != null) {
1408 mStatusBar.setIconVisibility(mSlotIme, false);
1409 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001410 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001411 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1412 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001413 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001414 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001415 mHardKeyboardListener);
1416 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001417
1418 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1419 mSettingsObserver.registerContentObserverLocked(currentUserId);
1420
1421 final IntentFilter broadcastFilter = new IntentFilter();
1422 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1423 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1424 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1425 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1426 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1427 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1428
1429 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1430 resetDefaultImeLocked(mContext);
1431 updateFromSettingsLocked(true);
1432 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1433 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1434 mContext.getBasePackageName());
1435
Dianne Hackborncc278702009-09-02 23:07:23 -07001436 try {
1437 startInputInnerLocked();
1438 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001439 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001440 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001441 }
1442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001444
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001445 // ---------------------------------------------------------------------------------------
1446 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1447 // 1) it comes from the system process
1448 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1449 private boolean calledFromValidUser() {
1450 final int uid = Binder.getCallingUid();
1451 final int userId = UserHandle.getUserId(uid);
1452 if (DEBUG) {
1453 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1454 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1455 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001456 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1457 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001458 }
Kenny Guy2a764942014-04-02 13:29:20 +01001459 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001460 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001461 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001462
1463 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1464 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1465 // must not manage background users' states in any functions.
1466 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1467 // by a token.
1468 if (mContext.checkCallingOrSelfPermission(
1469 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1470 == PackageManager.PERMISSION_GRANTED) {
1471 if (DEBUG) {
1472 Slog.d(TAG, "--- Access granted because the calling process has "
1473 + "the INTERACT_ACROSS_USERS_FULL permission");
1474 }
1475 return true;
1476 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001477 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001478 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1479 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001480 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 }
1482
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001483
1484 /**
1485 * Returns true iff the caller is identified to be the current input method with the token.
1486 * @param token The window token given to the input method when it was started.
1487 * @return true if and only if non-null valid token is specified.
1488 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001489 private boolean calledWithValidToken(@Nullable IBinder token) {
1490 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1491 if (DEBUG) {
1492 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1493 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1494 }
1495 return false;
1496 }
1497 if (token == null || token != mCurToken) {
1498 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1499 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1500 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001501 return false;
1502 }
1503 return true;
1504 }
1505
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001506 private boolean bindCurrentInputMethodService(
1507 Intent service, ServiceConnection conn, int flags) {
1508 if (service == null || conn == null) {
1509 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1510 return false;
1511 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001512 return mContext.bindServiceAsUser(service, conn, flags,
1513 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001514 }
1515
satoke7c6998e2011-06-03 17:57:59 +09001516 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001518 // TODO: Make this work even for non-current users?
1519 if (!calledFromValidUser()) {
1520 return Collections.emptyList();
1521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001523 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525 }
1526
satoke7c6998e2011-06-03 17:57:59 +09001527 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001529 // TODO: Make this work even for non-current users?
1530 if (!calledFromValidUser()) {
1531 return Collections.emptyList();
1532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001534 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
1536 }
1537
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001538 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001539 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001540 * @return enabled subtypes of the specified imi
1541 */
satoke7c6998e2011-06-03 17:57:59 +09001542 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001543 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001544 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001545 // TODO: Make this work even for non-current users?
1546 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001547 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001548 }
satok67ddf9c2010-11-17 09:45:54 +09001549 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001550 final InputMethodInfo imi;
1551 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001552 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001553 } else {
1554 imi = mMethodMap.get(imiId);
1555 }
1556 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001557 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001558 }
1559 return mSettings.getEnabledInputMethodSubtypeListLocked(
1560 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001561 }
1562 }
1563
satoke7c6998e2011-06-03 17:57:59 +09001564 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 public void addClient(IInputMethodClient client,
1566 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001567 if (!calledFromValidUser()) {
1568 return;
1569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 synchronized (mMethodMap) {
1571 mClients.put(client.asBinder(), new ClientState(client,
1572 inputContext, uid, pid));
1573 }
1574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001575
satoke7c6998e2011-06-03 17:57:59 +09001576 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001578 if (!calledFromValidUser()) {
1579 return;
1580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001582 ClientState cs = mClients.remove(client.asBinder());
1583 if (cs != null) {
1584 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001585 if (mCurClient == cs) {
1586 mCurClient = null;
1587 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001588 if (mCurFocusedWindowClient == cs) {
1589 mCurFocusedWindowClient = null;
1590 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
1593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 void executeOrSendMessage(IInterface target, Message msg) {
1596 if (target.asBinder() instanceof Binder) {
1597 mCaller.sendMessage(msg);
1598 } else {
1599 handleMessage(msg);
1600 msg.recycle();
1601 }
1602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001603
Yohei Yukawa33e81792015-11-17 21:14:42 -08001604 void unbindCurrentClientLocked(
1605 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001607 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 + mCurClient.client.asBinder());
1609 if (mBoundToMethod) {
1610 mBoundToMethod = false;
1611 if (mCurMethod != null) {
1612 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1613 MSG_UNBIND_INPUT, mCurMethod));
1614 }
1615 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001616
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001617 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1618 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001619 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1620 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001623
The Android Open Source Project10592532009-03-18 17:39:46 -07001624 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
1626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 private int getImeShowFlags() {
1629 int flags = 0;
1630 if (mShowForced) {
1631 flags |= InputMethod.SHOW_FORCED
1632 | InputMethod.SHOW_EXPLICIT;
1633 } else if (mShowExplicitlyRequested) {
1634 flags |= InputMethod.SHOW_EXPLICIT;
1635 }
1636 return flags;
1637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 private int getAppShowFlags() {
1640 int flags = 0;
1641 if (mShowForced) {
1642 flags |= InputMethodManager.SHOW_FORCED;
1643 } else if (!mShowExplicitlyRequested) {
1644 flags |= InputMethodManager.SHOW_IMPLICIT;
1645 }
1646 return flags;
1647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001649 InputBindResult attachNewInputLocked(
1650 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 if (!mBoundToMethod) {
1652 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1653 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1654 mBoundToMethod = true;
1655 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001656
1657 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001658 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1659 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1660 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001661 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001662 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001665 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001666 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001667 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001669 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001670 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001672 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001673 (session.channel != null ? session.channel.dup() : null),
1674 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001676
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001677 InputBindResult startInputLocked(
1678 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001679 IInputMethodClient client, IInputContext inputContext,
1680 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001681 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 // If no method is currently selected, do nothing.
1683 if (mCurMethodId == null) {
1684 return mNoBinding;
1685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 ClientState cs = mClients.get(client.asBinder());
1688 if (cs == null) {
1689 throw new IllegalArgumentException("unknown client "
1690 + client.asBinder());
1691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001692
Yohei Yukawa74750f22016-03-22 12:54:22 -07001693 if (attribute == null) {
1694 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1695 + " uid=" + cs.uid + " pid=" + cs.pid);
1696 return null;
1697 }
1698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 try {
1700 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1701 // Check with the window manager to make sure this client actually
1702 // has a window with focus. If not, reject. This is thread safe
1703 // because if the focus changes some time before or after, the
1704 // next client receiving focus that has any interest in input will
1705 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001706 if (DEBUG) {
1707 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1708 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 return null;
1711 }
1712 } catch (RemoteException e) {
1713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001714
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001715 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001716 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001717 }
1718
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001719 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001720 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001721 @NonNull EditorInfo attribute, int controlFlags,
1722 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001723 // If no method is currently selected, do nothing.
1724 if (mCurMethodId == null) {
1725 return mNoBinding;
1726 }
1727
Yohei Yukawad57ba672015-06-08 16:39:46 -07001728 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1729 attribute.packageName)) {
1730 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1731 + " uid=" + cs.uid + " package=" + attribute.packageName);
1732 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001733 }
1734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001736 // Was the keyguard locked when switching over to the new client?
1737 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 // If the client is changing, we need to switch over to the new
1739 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001740 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001741 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001742 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743
1744 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001745 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001746 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001747 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 }
1749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 // Bump up the sequence for this client and attach it.
1752 mCurSeq++;
1753 if (mCurSeq <= 0) mCurSeq = 1;
1754 mCurClient = cs;
1755 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001756 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 // Check if the input method is changing.
1760 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1761 if (cs.curSession != null) {
1762 // Fast case: if we are already connected to the input method,
1763 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001764 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001765 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
1767 if (mHaveConnection) {
1768 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 // Return to client, and we will get back with it when
1770 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001771 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001772 return new InputBindResult(null, null, mCurId, mCurSeq,
1773 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 } else if (SystemClock.uptimeMillis()
1775 < (mLastBindTime+TIME_TO_RECONNECT)) {
1776 // In this case we have connected to the service, but
1777 // don't yet have its interface. If it hasn't been too
1778 // long since we did the connection, we'll return to
1779 // the client and wait to get the service interface so
1780 // we can report back. If it has been too long, we want
1781 // to fall through so we can try a disconnect/reconnect
1782 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001783 return new InputBindResult(null, null, mCurId, mCurSeq,
1784 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001786 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1787 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 }
1790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001791
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001792 return startInputInnerLocked();
1793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001794
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001795 InputBindResult startInputInnerLocked() {
1796 if (mCurMethodId == null) {
1797 return mNoBinding;
1798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001799
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001800 if (!mSystemReady) {
1801 // If the system is not yet ready, we shouldn't be running third
1802 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001803 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1804 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1808 if (info == null) {
1809 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001811
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001812 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1815 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001816 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1817 com.android.internal.R.string.input_method_binding_label);
1818 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1819 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001820 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001821 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1822 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 mLastBindTime = SystemClock.uptimeMillis();
1824 mHaveConnection = true;
1825 mCurId = info.getId();
1826 mCurToken = new Binder();
1827 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001828 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001829 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 } catch (RemoteException e) {
1831 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001832 return new InputBindResult(null, null, mCurId, mCurSeq,
1833 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 } else {
1835 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001836 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 + mCurIntent);
1838 }
1839 return null;
1840 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001841
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001842 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001843 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001844 IInputMethodClient client, IInputContext inputContext,
1845 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001846 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001847 if (!calledFromValidUser()) {
1848 return null;
1849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001851 if (DEBUG) {
1852 Slog.v(TAG, "startInput: reason="
1853 + InputMethodClient.getStartInputReason(startInputReason)
1854 + " client = " + client.asBinder()
1855 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001856 + " missingMethods="
1857 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001858 + " attribute=" + attribute
1859 + " controlFlags=#" + Integer.toHexString(controlFlags));
1860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 final long ident = Binder.clearCallingIdentity();
1862 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001863 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1864 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 } finally {
1866 Binder.restoreCallingIdentity(ident);
1867 }
1868 }
1869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001870
satoke7c6998e2011-06-03 17:57:59 +09001871 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 public void finishInput(IInputMethodClient client) {
1873 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001874
satoke7c6998e2011-06-03 17:57:59 +09001875 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 public void onServiceConnected(ComponentName name, IBinder service) {
1877 synchronized (mMethodMap) {
1878 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1879 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001880 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001881 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001882 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001883 return;
1884 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001885 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001886 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1887 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001889 clearClientSessionLocked(mCurClient);
1890 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
1892 }
1893 }
1894 }
1895
Jeff Brownc28867a2013-03-26 15:42:39 -07001896 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1897 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 synchronized (mMethodMap) {
1899 if (mCurMethod != null && method != null
1900 && mCurMethod.asBinder() == method.asBinder()) {
1901 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001902 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001904 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001905 InputBindResult res = attachNewInputLocked(
1906 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 if (res.method != null) {
1908 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001909 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001911 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
1913 }
1914 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001915
1916 // Session abandoned. Close its associated input channel.
1917 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001919
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001920 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001921 if (mVisibleBound) {
1922 mContext.unbindService(mVisibleConnection);
1923 mVisibleBound = false;
1924 }
1925
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001926 if (mHaveConnection) {
1927 mContext.unbindService(this);
1928 mHaveConnection = false;
1929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001930
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001931 if (mCurToken != null) {
1932 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001933 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001934 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001935 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001936 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001937 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001938 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001939 } catch (RemoteException e) {
1940 }
1941 mCurToken = null;
1942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001943
The Android Open Source Project10592532009-03-18 17:39:46 -07001944 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001945 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001947
Yohei Yukawa33e81792015-11-17 21:14:42 -08001948 void resetCurrentMethodAndClient(
1949 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001950 mCurMethodId = null;
1951 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001952 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001953 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001954
1955 void requestClientSessionLocked(ClientState cs) {
1956 if (!cs.sessionRequested) {
1957 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1958 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1959 cs.sessionRequested = true;
1960 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1961 MSG_CREATE_SESSION, mCurMethod, channels[1],
1962 new MethodCallback(this, mCurMethod, channels[0])));
1963 }
1964 }
1965
1966 void clearClientSessionLocked(ClientState cs) {
1967 finishSessionLocked(cs.curSession);
1968 cs.curSession = null;
1969 cs.sessionRequested = false;
1970 }
1971
1972 private void finishSessionLocked(SessionState sessionState) {
1973 if (sessionState != null) {
1974 if (sessionState.session != null) {
1975 try {
1976 sessionState.session.finishSession();
1977 } catch (RemoteException e) {
1978 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001979 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001980 }
1981 sessionState.session = null;
1982 }
1983 if (sessionState.channel != null) {
1984 sessionState.channel.dispose();
1985 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001986 }
1987 }
1988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001989
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001990 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 if (mCurMethod != null) {
1992 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001993 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001995
Jeff Brownc28867a2013-03-26 15:42:39 -07001996 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001997 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 mCurMethod = null;
1999 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002000 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002001 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002002 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002003 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
satoke7c6998e2011-06-03 17:57:59 +09002006 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002008 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2009 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2010 // which is irregular but can eventually happen for everyone just by continuing using the
2011 // device. Thus it is important to make sure that all the internal states are properly
2012 // refreshed when this method is called back. Running
2013 // adb install -r <APK that implements the current IME>
2014 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002016 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 + " mCurIntent=" + mCurIntent);
2018 if (mCurMethod != null && mCurIntent != null
2019 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002020 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 // We consider this to be a new bind attempt, since the system
2022 // should now try to restart the service for us.
2023 mLastBindTime = SystemClock.uptimeMillis();
2024 mShowRequested = mInputShown;
2025 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002026 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
2028 }
2029 }
2030
satokf9f01002011-05-19 21:31:50 +09002031 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002033 synchronized (mMethodMap) {
2034 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002035 return;
2036 }
2037 final long ident = Binder.clearCallingIdentity();
2038 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002040 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002041 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002042 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002045 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002046 CharSequence contentDescription = null;
2047 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002048 // Use PackageManager to load label
2049 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002050 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002051 mIPackageManager.getApplicationInfo(packageName, 0,
2052 mSettings.getCurrentUserId()));
2053 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002054 /* ignore */
2055 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002056 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002057 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002058 contentDescription != null
2059 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002060 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002063 } finally {
2064 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
2067 }
2068
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002069 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002070 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002071 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09002072 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002073 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07002074 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07002075 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
2076 // When physical keyboard is attached, we show the ime switcher (or notification if
2077 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2078 // exists in the IME switcher dialog. Might be OK to remove this condition once
2079 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2080 return true;
2081 }
Yohei Yukawa89398382016-03-29 11:37:04 -07002082 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2083 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002084 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002085
2086 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2087 final int N = imis.size();
2088 if (N > 2) return true;
2089 if (N < 1) return false;
2090 int nonAuxCount = 0;
2091 int auxCount = 0;
2092 InputMethodSubtype nonAuxSubtype = null;
2093 InputMethodSubtype auxSubtype = null;
2094 for(int i = 0; i < N; ++i) {
2095 final InputMethodInfo imi = imis.get(i);
2096 final List<InputMethodSubtype> subtypes =
2097 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2098 final int subtypeCount = subtypes.size();
2099 if (subtypeCount == 0) {
2100 ++nonAuxCount;
2101 } else {
2102 for (int j = 0; j < subtypeCount; ++j) {
2103 final InputMethodSubtype subtype = subtypes.get(j);
2104 if (!subtype.isAuxiliary()) {
2105 ++nonAuxCount;
2106 nonAuxSubtype = subtype;
2107 } else {
2108 ++auxCount;
2109 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002110 }
2111 }
satok7cfc0ed2011-06-20 21:29:36 +09002112 }
2113 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002114 if (nonAuxCount > 1 || auxCount > 1) {
2115 return true;
2116 } else if (nonAuxCount == 1 && auxCount == 1) {
2117 if (nonAuxSubtype != null && auxSubtype != null
2118 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2119 || auxSubtype.overridesImplicitlyEnabledSubtype()
2120 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2121 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2122 return false;
2123 }
2124 return true;
2125 }
2126 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002127 }
2128
John Spurlocke0980502013-10-25 11:59:29 -04002129 private boolean isKeyguardLocked() {
2130 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2131 }
2132
satokdbf29502011-08-25 15:28:23 +09002133 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002134 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002135 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2136 int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002137 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002138 return;
2139 }
2140
Yohei Yukawa69e68022017-02-13 12:04:50 -08002141 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002142 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002143 info = mStartInputMap.get(startInputToken);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002144 mImeWindowVis = vis;
2145 mBackDisposition = backDisposition;
2146 updateSystemUiLocked(token, vis, backDisposition);
2147 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002148 mWindowManagerInternal.updateInputMethodWindowStatus(token,
2149 (vis & InputMethodService.IME_VISIBLE) != 0,
Yohei Yukawabbb10e82017-02-17 20:13:52 -08002150 info != null ? info.mTargetWindow : null);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002151 }
2152
2153 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2154 synchronized (mMethodMap) {
2155 updateSystemUiLocked(token, vis, backDisposition);
2156 }
2157 }
2158
2159 // Caution! This method is called in this class. Handle multi-user carefully
2160 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2161 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002162 return;
2163 }
2164
2165 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2166 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002167 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002168 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002169 // apply policy for binder calls
2170 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2171 vis = 0;
satok06487a52010-10-29 11:37:18 +09002172 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002173 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2174 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2175 if (mStatusBar != null) {
2176 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2177 needsToShowImeSwitcher);
2178 }
2179 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2180 if (imi != null && needsToShowImeSwitcher) {
2181 // Used to load label
2182 final CharSequence title = mRes.getText(
2183 com.android.internal.R.string.select_input_method);
2184 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2185 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002186 mImeSwitcherNotification.setContentTitle(title)
2187 .setContentText(summary)
2188 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002189 try {
2190 if ((mNotificationManager != null)
2191 && !mIWindowManager.hasNavigationBar()) {
2192 if (DEBUG) {
2193 Slog.d(TAG, "--- show notification: label = " + summary);
2194 }
2195 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002196 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002197 mImeSwitcherNotification.build(), UserHandle.ALL);
2198 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002199 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002200 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002201 }
2202 } else {
2203 if (mNotificationShown && mNotificationManager != null) {
2204 if (DEBUG) {
2205 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002206 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002207 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002208 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002209 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002210 }
satok06487a52010-10-29 11:37:18 +09002211 }
2212 } finally {
2213 Binder.restoreCallingIdentity(ident);
2214 }
2215 }
2216
satoke7c6998e2011-06-03 17:57:59 +09002217 @Override
satokf9f01002011-05-19 21:31:50 +09002218 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002219 if (!calledFromValidUser()) {
2220 return;
2221 }
satokf9f01002011-05-19 21:31:50 +09002222 synchronized (mMethodMap) {
2223 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2224 for (int i = 0; i < spans.length; ++i) {
2225 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002226 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002227 mSecureSuggestionSpans.put(ss, currentImi);
2228 }
2229 }
2230 }
2231 }
2232
satoke7c6998e2011-06-03 17:57:59 +09002233 @Override
satokf9f01002011-05-19 21:31:50 +09002234 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002235 if (!calledFromValidUser()) {
2236 return false;
2237 }
satokf9f01002011-05-19 21:31:50 +09002238 synchronized (mMethodMap) {
2239 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2240 // TODO: Do not send the intent if the process of the targetImi is already dead.
2241 if (targetImi != null) {
2242 final String[] suggestions = span.getSuggestions();
2243 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002244 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002245 final Intent intent = new Intent();
2246 // Ensures that only a class in the original IME package will receive the
2247 // notification.
satok42c5a162011-05-26 16:46:14 +09002248 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002249 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2250 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2251 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2252 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002253 final long ident = Binder.clearCallingIdentity();
2254 try {
2255 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2256 } finally {
2257 Binder.restoreCallingIdentity(ident);
2258 }
satokf9f01002011-05-19 21:31:50 +09002259 return true;
2260 }
2261 }
2262 return false;
2263 }
2264
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002265 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002266 updateInputMethodsFromSettingsLocked(enabledMayChange);
2267 updateKeyboardFromSettingsLocked();
2268 }
2269
2270 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002271 if (enabledMayChange) {
2272 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2273 for (int i=0; i<enabled.size(); i++) {
2274 // We allow the user to select "disabled until used" apps, so if they
2275 // are enabling one of those here we now need to make it enabled.
2276 InputMethodInfo imm = enabled.get(i);
2277 try {
2278 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2279 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2280 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002281 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002282 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002283 if (DEBUG) {
2284 Slog.d(TAG, "Update state(" + imm.getId()
2285 + "): DISABLED_UNTIL_USED -> DEFAULT");
2286 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002287 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2288 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002289 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2290 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002291 }
2292 } catch (RemoteException e) {
2293 }
2294 }
2295 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002296 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2297 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2298 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2299 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002300 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002301 // There is no input method selected, try to choose new applicable input method.
2302 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002303 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002304 }
2305 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002307 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002309 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002310 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 }
satokf3db1af2010-11-23 13:34:33 +09002312 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002313 } else {
2314 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002315 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002317 // Here is not the perfect place to reset the switching controller. Ideally
2318 // mSwitchingController and mSettings should be able to share the same state.
2319 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2320 // the same enabled IMEs list.
2321 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002322
2323 }
2324
2325 public void updateKeyboardFromSettingsLocked() {
2326 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2327 if (mSwitchingDialog != null
2328 && mSwitchingDialogTitleView != null
2329 && mSwitchingDialog.isShowing()) {
2330 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2331 com.android.internal.R.id.hard_keyboard_switch);
2332 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002335
Yohei Yukawab097b822015-12-01 10:43:08 -08002336 private void notifyInputMethodSubtypeChanged(final int userId,
2337 @Nullable final InputMethodInfo inputMethodInfo,
2338 @Nullable final InputMethodSubtype subtype) {
2339 final InputManagerInternal inputManagerInternal =
2340 LocalServices.getService(InputManagerInternal.class);
2341 if (inputManagerInternal != null) {
2342 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2343 }
2344 }
2345
satokab751aa2010-09-14 19:17:36 +09002346 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 InputMethodInfo info = mMethodMap.get(id);
2348 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002349 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002351
satokd81e9502012-05-21 12:58:45 +09002352 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002354 final int subtypeCount = info.getSubtypeCount();
2355 if (subtypeCount <= 0) {
2356 return;
satokcd7cd292010-11-20 15:46:23 +09002357 }
satokd81e9502012-05-21 12:58:45 +09002358 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2359 final InputMethodSubtype newSubtype;
2360 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2361 newSubtype = info.getSubtypeAt(subtypeId);
2362 } else {
2363 // If subtype is null, try to find the most applicable one from
2364 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002365 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002366 }
2367 if (newSubtype == null || oldSubtype == null) {
2368 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2369 + ", new subtype = " + newSubtype);
2370 return;
2371 }
2372 if (newSubtype != oldSubtype) {
2373 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2374 if (mCurMethod != null) {
2375 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002376 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002377 mCurMethod.changeInputMethodSubtype(newSubtype);
2378 } catch (RemoteException e) {
2379 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002380 return;
satokab751aa2010-09-14 19:17:36 +09002381 }
2382 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002383 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 return;
2386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002387
satokd81e9502012-05-21 12:58:45 +09002388 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 final long ident = Binder.clearCallingIdentity();
2390 try {
satokab751aa2010-09-14 19:17:36 +09002391 // Set a subtype to this input method.
2392 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002393 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2394 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2395 // because mCurMethodId is stored as a history in
2396 // setSelectedInputMethodAndSubtypeLocked().
2397 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002399 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002401 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002403 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002405 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 } finally {
2407 Binder.restoreCallingIdentity(ident);
2408 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002409
2410 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2411 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002413
satok42c5a162011-05-26 16:46:14 +09002414 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002415 public boolean showSoftInput(IInputMethodClient client, int flags,
2416 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002417 if (!calledFromValidUser()) {
2418 return false;
2419 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002420 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 long ident = Binder.clearCallingIdentity();
2422 try {
2423 synchronized (mMethodMap) {
2424 if (mCurClient == null || client == null
2425 || mCurClient.client.asBinder() != client.asBinder()) {
2426 try {
2427 // We need to check if this is the current client with
2428 // focus in the window manager, to allow this call to
2429 // be made before input is started in it.
2430 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002431 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002432 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 }
2434 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002435 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
2437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002438
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002439 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002440 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 }
2442 } finally {
2443 Binder.restoreCallingIdentity(ident);
2444 }
2445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002446
The Android Open Source Project4df24232009-03-05 14:34:35 -08002447 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002449 if (mAccessibilityRequestingNoSoftKeyboard) {
2450 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 }
Anna Galusza9b278112016-01-04 11:37:37 -08002452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2454 mShowExplicitlyRequested = true;
2455 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002456 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2457 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002459
Dianne Hackborncc278702009-09-02 23:07:23 -07002460 if (!mSystemReady) {
2461 return false;
2462 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002463
The Android Open Source Project4df24232009-03-05 14:34:35 -08002464 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002466 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002467 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2468 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2469 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002471 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002472 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002473 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002474 | Context.BIND_TREAT_LIKE_ACTIVITY
2475 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002476 mVisibleBound = true;
2477 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002478 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002480 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 // The client has asked to have the input method shown, but
2482 // we have been sitting here too long with a connection to the
2483 // service and no interface received, so let's disconnect/connect
2484 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002485 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002487 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002489 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002490 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002491 } else {
2492 if (DEBUG) {
2493 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2494 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002497
The Android Open Source Project4df24232009-03-05 14:34:35 -08002498 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002500
satok42c5a162011-05-26 16:46:14 +09002501 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002502 public boolean hideSoftInput(IInputMethodClient client, int flags,
2503 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002504 if (!calledFromValidUser()) {
2505 return false;
2506 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002507 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 long ident = Binder.clearCallingIdentity();
2509 try {
2510 synchronized (mMethodMap) {
2511 if (mCurClient == null || client == null
2512 || mCurClient.client.asBinder() != client.asBinder()) {
2513 try {
2514 // We need to check if this is the current client with
2515 // focus in the window manager, to allow this call to
2516 // be made before input is started in it.
2517 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002518 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2519 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002520 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
2522 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002523 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 }
2525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002526
Joe Onorato8a9b2202010-02-26 18:56:32 -08002527 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002528 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 }
2530 } finally {
2531 Binder.restoreCallingIdentity(ident);
2532 }
2533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002534
The Android Open Source Project4df24232009-03-05 14:34:35 -08002535 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2537 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002538 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 -08002539 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 }
2541 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002542 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 -08002543 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002545
2546 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2547 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2548 // to be updated with the new value sent from IME process. Even in such a transient state
2549 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2550 // application process as a valid request, and have even promised such a behavior with CTS
2551 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2552 // IMMS#InputShown indicates that the software keyboard is shown.
2553 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2554 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2555 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002556 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002557 if (shouldHideSoftInput) {
2558 // The IME will report its visible state again after the following message finally
2559 // delivered to the IME process as an IPC. Hence the inconsistency between
2560 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2561 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002562 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2563 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2564 res = true;
2565 } else {
2566 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002568 if (mHaveConnection && mVisibleBound) {
2569 mContext.unbindService(mVisibleConnection);
2570 mVisibleBound = false;
2571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 mInputShown = false;
2573 mShowRequested = false;
2574 mShowExplicitlyRequested = false;
2575 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002576 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002578
satok42c5a162011-05-26 16:46:14 +09002579 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002580 public InputBindResult startInputOrWindowGainedFocus(
2581 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2582 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002583 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002584 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002585 if (windowToken != null) {
2586 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002587 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002588 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002589 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2590 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002591 }
2592 }
2593
2594 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002595 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002596 IInputMethodClient client, IBinder windowToken, int controlFlags,
2597 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002598 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2599 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002600 // Needs to check the validity before clearing calling identity
2601 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002602 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 long ident = Binder.clearCallingIdentity();
2604 try {
2605 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002606 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2607 + InputMethodClient.getStartInputReason(startInputReason)
2608 + " client=" + client.asBinder()
2609 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002610 + " missingMethods="
2611 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002612 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002613 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002614 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002615 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002616
Dianne Hackborn7663d802012-02-24 13:08:49 -08002617 ClientState cs = mClients.get(client.asBinder());
2618 if (cs == null) {
2619 throw new IllegalArgumentException("unknown client "
2620 + client.asBinder());
2621 }
2622
2623 try {
2624 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2625 // Check with the window manager to make sure this client actually
2626 // has a window with focus. If not, reject. This is thread safe
2627 // because if the focus changes some time before or after, the
2628 // next client receiving focus that has any interest in input will
2629 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002630 if (DEBUG) {
2631 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2632 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2633 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002634 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002636 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002638
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002639 if (!calledFromValidUser) {
2640 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2641 Slog.w(TAG, "If you want to interect with IME, you need "
2642 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2643 hideCurrentInputLocked(0, null);
2644 return null;
2645 }
2646
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002647 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002648 if (DEBUG) {
2649 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2650 + " attribute=" + attribute + ", token = " + windowToken);
2651 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002652 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002653 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002654 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002655 }
2656 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002657 }
2658 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002659 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002660 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002661
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002662 // Should we auto-show the IME even if the caller has not
2663 // specified what should be done with it?
2664 // We only do this automatically if the window can resize
2665 // to accommodate the IME (so what the user sees will give
2666 // them good context without input information being obscured
2667 // by the IME) or if running on a large screen where there
2668 // is more room for the target window + IME.
2669 final boolean doAutoShow =
2670 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2671 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2672 || mRes.getConfiguration().isLayoutSizeAtLeast(
2673 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002674 final boolean isTextEditor =
2675 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2676
2677 // We want to start input before showing the IME, but after closing
2678 // it. We want to do this after closing it to help the IME disappear
2679 // more quickly (not get stuck behind it initializing itself for the
2680 // new focused input, even if its window wants to hide the IME).
2681 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2684 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002685 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2687 // There is no focus view, and this window will
2688 // be behind any soft input window, so hide the
2689 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002690 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002691 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002693 } else if (isTextEditor && doAutoShow && (softInputMode &
2694 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 // There is a focus view, and we are navigating forward
2696 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002697 // We only do this automatically if the window can resize
2698 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002699 // them good context without input information being obscured
2700 // by the IME) or if running on a large screen where there
2701 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002702 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002703 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002704 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002705 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002706 didStart = true;
2707 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002708 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 }
2710 break;
2711 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2712 // Do nothing.
2713 break;
2714 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2715 if ((softInputMode &
2716 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002717 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002718 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 }
2720 break;
2721 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002722 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002723 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 break;
2725 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
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 show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002729 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002730 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002731 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002732 didStart = true;
2733 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002734 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
2736 break;
2737 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002738 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002739 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002740 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002741 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002742 didStart = true;
2743 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002744 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 break;
2746 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002747
2748 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002749 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002750 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
2753 } finally {
2754 Binder.restoreCallingIdentity(ident);
2755 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002756
2757 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002759
satok42c5a162011-05-26 16:46:14 +09002760 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002761 public void showInputMethodPickerFromClient(
2762 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002763 if (!calledFromValidUser()) {
2764 return;
2765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 synchronized (mMethodMap) {
2767 if (mCurClient == null || client == null
2768 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002769 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002770 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 }
2772
satok440aab52010-11-25 09:43:11 +09002773 // Always call subtype picker, because subtype picker is a superset of input method
2774 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002775 mHandler.sendMessage(mCaller.obtainMessageI(
2776 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002777 }
2778 }
2779
satok42c5a162011-05-26 16:46:14 +09002780 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002782 if (!calledFromValidUser()) {
2783 return;
2784 }
satok28203512010-11-24 11:06:49 +09002785 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2786 }
2787
satok42c5a162011-05-26 16:46:14 +09002788 @Override
satok28203512010-11-24 11:06:49 +09002789 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002790 if (!calledFromValidUser()) {
2791 return;
2792 }
satok28203512010-11-24 11:06:49 +09002793 synchronized (mMethodMap) {
2794 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002795 setInputMethodWithSubtypeIdLocked(token, id,
2796 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2797 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002798 } else {
2799 setInputMethod(token, id);
2800 }
2801 }
satokab751aa2010-09-14 19:17:36 +09002802 }
2803
satok42c5a162011-05-26 16:46:14 +09002804 @Override
satokb416a712010-11-25 20:42:14 +09002805 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002806 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002807 if (!calledFromValidUser()) {
2808 return;
2809 }
satokb416a712010-11-25 20:42:14 +09002810 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002811 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2812 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002813 }
2814 }
2815
satok4fc87d62011-05-20 16:13:43 +09002816 @Override
satok735cf382010-11-11 20:40:09 +09002817 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002818 if (!calledFromValidUser()) {
2819 return false;
2820 }
satok735cf382010-11-11 20:40:09 +09002821 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002822 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002823 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002824 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002825 lastImi = mMethodMap.get(lastIme.first);
2826 } else {
2827 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002828 }
satok4fc87d62011-05-20 16:13:43 +09002829 String targetLastImiId = null;
2830 int subtypeId = NOT_A_SUBTYPE_ID;
2831 if (lastIme != null && lastImi != null) {
2832 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002833 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002834 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2835 : mCurrentSubtype.hashCode();
2836 // If the last IME is the same as the current IME and the last subtype is not
2837 // defined, there is no need to switch to the last IME.
2838 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2839 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002840 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002841 }
2842 }
2843
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002844 if (TextUtils.isEmpty(targetLastImiId)
2845 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002846 // This is a safety net. If the currentSubtype can't be added to the history
2847 // and the framework couldn't find the last ime, we will make the last ime be
2848 // the most applicable enabled keyboard subtype of the system imes.
2849 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2850 if (enabled != null) {
2851 final int N = enabled.size();
2852 final String locale = mCurrentSubtype == null
2853 ? mRes.getConfiguration().locale.toString()
2854 : mCurrentSubtype.getLocale();
2855 for (int i = 0; i < N; ++i) {
2856 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002857 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002858 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002859 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2860 InputMethodUtils.getSubtypes(imi),
2861 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002862 if (keyboardSubtype != null) {
2863 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002864 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002865 imi, keyboardSubtype.hashCode());
2866 if(keyboardSubtype.getLocale().equals(locale)) {
2867 break;
2868 }
2869 }
2870 }
2871 }
2872 }
2873 }
2874
2875 if (!TextUtils.isEmpty(targetLastImiId)) {
2876 if (DEBUG) {
2877 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2878 + ", from: " + mCurMethodId + ", " + subtypeId);
2879 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002880 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002881 return true;
2882 } else {
2883 return false;
2884 }
satok735cf382010-11-11 20:40:09 +09002885 }
2886 }
2887
satoke7c6998e2011-06-03 17:57:59 +09002888 @Override
satok688bd472012-02-09 20:09:17 +09002889 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002890 if (!calledFromValidUser()) {
2891 return false;
2892 }
satok688bd472012-02-09 20:09:17 +09002893 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002894 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002895 return false;
2896 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002897 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002898 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2899 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002900 if (nextSubtype == null) {
2901 return false;
2902 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002903 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2904 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002905 return true;
2906 }
2907 }
2908
2909 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002910 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2911 if (!calledFromValidUser()) {
2912 return false;
2913 }
2914 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002915 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002916 return false;
2917 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002918 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002919 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2920 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002921 if (nextSubtype == null) {
2922 return false;
2923 }
2924 return true;
2925 }
2926 }
2927
2928 @Override
satok68f1b782011-04-11 14:26:04 +09002929 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002930 if (!calledFromValidUser()) {
2931 return null;
2932 }
satok68f1b782011-04-11 14:26:04 +09002933 synchronized (mMethodMap) {
2934 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2935 // TODO: Handle the case of the last IME with no subtypes
2936 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2937 || TextUtils.isEmpty(lastIme.second)) return null;
2938 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2939 if (lastImi == null) return null;
2940 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002941 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002942 final int lastSubtypeId =
2943 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002944 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2945 return null;
2946 }
2947 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002948 } catch (NumberFormatException e) {
2949 return null;
2950 }
2951 }
2952 }
2953
satoke7c6998e2011-06-03 17:57:59 +09002954 @Override
satokee5e77c2011-09-02 18:50:15 +09002955 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002956 if (!calledFromValidUser()) {
2957 return;
2958 }
satok91e88122011-07-18 11:11:42 +09002959 // By this IPC call, only a process which shares the same uid with the IME can add
2960 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002961 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002962 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002963 if (!mSystemReady) {
2964 return;
2965 }
satok91e88122011-07-18 11:11:42 +09002966 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002967 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002968 final String[] packageInfos;
2969 try {
2970 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2971 } catch (RemoteException e) {
2972 Slog.e(TAG, "Failed to get package infos");
2973 return;
2974 }
satok91e88122011-07-18 11:11:42 +09002975 if (packageInfos != null) {
2976 final int packageNum = packageInfos.length;
2977 for (int i = 0; i < packageNum; ++i) {
2978 if (packageInfos[i].equals(imi.getPackageName())) {
2979 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002980 final long ident = Binder.clearCallingIdentity();
2981 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002982 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002983 } finally {
2984 Binder.restoreCallingIdentity(ident);
2985 }
satokee5e77c2011-09-02 18:50:15 +09002986 return;
satok91e88122011-07-18 11:11:42 +09002987 }
2988 }
2989 }
satoke7c6998e2011-06-03 17:57:59 +09002990 }
satokee5e77c2011-09-02 18:50:15 +09002991 return;
satoke7c6998e2011-06-03 17:57:59 +09002992 }
2993
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002994 @Override
2995 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002996 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002997 }
2998
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002999 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003000 public void clearLastInputMethodWindowForTransition(IBinder token) {
3001 if (!calledFromValidUser()) {
3002 return;
3003 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003004 synchronized (mMethodMap) {
3005 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08003006 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003007 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003008 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08003009 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07003010 }
3011
3012 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003013 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003014 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003015 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003016 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003017 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003018 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
3019 if (DEBUG) {
3020 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
3021 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
3022 + " actual: " + sequenceNumber);
3023 }
3024 return;
3025 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003026 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3027 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003028 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003029 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003030 }
3031 }
3032
satok28203512010-11-24 11:06:49 +09003033 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003035 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
3036 }
3037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003039 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3040 if (token == null) {
3041 if (mContext.checkCallingOrSelfPermission(
3042 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3043 != PackageManager.PERMISSION_GRANTED) {
3044 throw new SecurityException(
3045 "Using null token requires permission "
3046 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003048 } else if (mCurToken != token) {
3049 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3050 + " token: " + token);
3051 return;
3052 }
3053
3054 final long ident = Binder.clearCallingIdentity();
3055 try {
3056 setInputMethodLocked(id, subtypeId);
3057 } finally {
3058 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 }
3060 }
3061
satok42c5a162011-05-26 16:46:14 +09003062 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003064 if (!calledFromValidUser()) {
3065 return;
3066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003068 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 return;
3070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 long ident = Binder.clearCallingIdentity();
3072 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003073 hideCurrentInputLocked(flags, null);
3074 } finally {
3075 Binder.restoreCallingIdentity(ident);
3076 }
3077 }
3078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003079
satok42c5a162011-05-26 16:46:14 +09003080 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08003081 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003082 if (!calledFromValidUser()) {
3083 return;
3084 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003085 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09003086 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003087 return;
3088 }
3089 long ident = Binder.clearCallingIdentity();
3090 try {
3091 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 } finally {
3093 Binder.restoreCallingIdentity(ident);
3094 }
3095 }
3096 }
3097
3098 void setEnabledSessionInMainThread(SessionState session) {
3099 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003100 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003102 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003103 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 } catch (RemoteException e) {
3105 }
3106 }
3107 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003108 if (mEnabledSession != null && mEnabledSession.session != null) {
3109 try {
3110 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3111 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3112 } catch (RemoteException e) {
3113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 }
3115 }
3116 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003117
satok42c5a162011-05-26 16:46:14 +09003118 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003120 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003122 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003123 final boolean showAuxSubtypes;
3124 switch (msg.arg1) {
3125 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3126 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3127 // implemented so that auxiliary subtypes will be excluded when the soft
3128 // keyboard is invisible.
3129 showAuxSubtypes = mInputShown;
3130 break;
3131 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3132 showAuxSubtypes = true;
3133 break;
3134 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3135 showAuxSubtypes = false;
3136 break;
3137 default:
3138 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3139 return false;
3140 }
3141 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003142 return true;
3143
satok47a44912010-10-06 16:03:58 +09003144 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003145 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003146 return true;
3147
3148 case MSG_SHOW_IM_CONFIG:
3149 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003150 return true;
3151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 case MSG_UNBIND_INPUT:
3155 try {
3156 ((IInputMethod)msg.obj).unbindInput();
3157 } catch (RemoteException e) {
3158 // There is nothing interesting about the method dying.
3159 }
3160 return true;
3161 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003162 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 try {
3164 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3165 } catch (RemoteException e) {
3166 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003167 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 return true;
3169 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003170 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003172 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003173 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003174 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 } catch (RemoteException e) {
3176 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003177 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 return true;
3179 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003180 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003182 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003183 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003184 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185 } catch (RemoteException e) {
3186 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003187 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003189 case MSG_HIDE_CURRENT_INPUT_METHOD:
3190 synchronized (mMethodMap) {
3191 hideCurrentInputLocked(0, null);
3192 }
3193 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003195 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003197 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3199 } catch (RemoteException e) {
3200 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003201 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003203 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003204 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003205 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003206 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003208 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003210 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003211 // Dispose the channel if the input method is not local to this process
3212 // because the remote proxy will get its own copy when unparceled.
3213 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003214 channel.dispose();
3215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003217 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003221
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003222 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003223 final int missingMethods = msg.arg1;
3224 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003225 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003226 final IBinder startInputToken = (IBinder) args.arg1;
3227 final SessionState session = (SessionState) args.arg2;
3228 final IInputContext inputContext = (IInputContext) args.arg3;
3229 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003232 session.method.startInput(startInputToken, inputContext, missingMethods,
3233 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 } catch (RemoteException e) {
3235 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003236 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003241
Yohei Yukawa33e81792015-11-17 21:14:42 -08003242 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003244 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 } catch (RemoteException e) {
3246 // There is nothing interesting about the last client dying.
3247 }
3248 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003249 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003250 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003251 IInputMethodClient client = (IInputMethodClient)args.arg1;
3252 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003254 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003256 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003257 } finally {
3258 // Dispose the channel if the input method is not local to this process
3259 // because the remote proxy will get its own copy when unparceled.
3260 if (res.channel != null && Binder.isProxy(client)) {
3261 res.channel.dispose();
3262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003264 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003266 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003267 case MSG_SET_ACTIVE:
3268 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003269 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003270 } catch (RemoteException e) {
3271 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3272 + ((ClientState)msg.obj).pid + " uid "
3273 + ((ClientState)msg.obj).uid);
3274 }
3275 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003276 case MSG_SET_INTERACTIVE:
3277 handleSetInteractive(msg.arg1 != 0);
3278 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003279 case MSG_SWITCH_IME:
3280 handleSwitchInputMethod(msg.arg1 != 0);
3281 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003282 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3283 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003284 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003285 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003286 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003287 } catch (RemoteException e) {
3288 Slog.w(TAG, "Got RemoteException sending "
3289 + "setUserActionNotificationSequenceNumber("
3290 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003291 + clientState.pid + " uid "
3292 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003293 }
3294 return true;
3295 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003296 case MSG_REPORT_FULLSCREEN_MODE: {
3297 final boolean fullscreen = msg.arg1 != 0;
3298 final ClientState clientState = (ClientState)msg.obj;
3299 try {
3300 clientState.client.reportFullscreenMode(fullscreen);
3301 } catch (RemoteException e) {
3302 Slog.w(TAG, "Got RemoteException sending "
3303 + "reportFullscreen(" + fullscreen + ") notification to pid="
3304 + clientState.pid + " uid=" + clientState.uid);
3305 }
3306 return true;
3307 }
satok01038492012-04-09 21:08:27 +09003308
3309 // --------------------------------------------------------------
3310 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003311 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003312 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003313 case MSG_SYSTEM_UNLOCK_USER:
3314 final int userId = msg.arg1;
3315 onUnlockUser(userId);
3316 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 }
3318 return false;
3319 }
3320
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003321 private void handleSetInteractive(final boolean interactive) {
3322 synchronized (mMethodMap) {
3323 mIsInteractive = interactive;
3324 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3325
3326 // Inform the current client of the change in active status
3327 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003328 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3329 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3330 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003331 }
3332 }
3333 }
3334
Yohei Yukawaae61f712015-12-09 13:00:10 -08003335 private void handleSwitchInputMethod(final boolean forwardDirection) {
3336 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003337 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003338 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003339 if (nextSubtype == null) {
3340 return;
3341 }
3342 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003343 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3344 if (newInputMethodInfo == null) {
3345 return;
3346 }
3347 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3348 newInputMethodInfo, mCurrentSubtype);
3349 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003350 if (mSubtypeSwitchedByShortCutToast == null) {
3351 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3352 Toast.LENGTH_SHORT);
3353 } else {
3354 mSubtypeSwitchedByShortCutToast.setText(toastText);
3355 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003356 mSubtypeSwitchedByShortCutToast.show();
3357 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003358 }
3359 }
3360
satokdc9ddae2011-10-06 12:22:36 +09003361 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003362 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3363 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003364 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003365 if (DEBUG) {
3366 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3367 }
satok723a27e2010-11-11 14:58:11 +09003368 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003369 return true;
3370 }
3371
3372 return false;
3373 }
3374
Yohei Yukawa94e33302016-02-12 19:37:03 -08003375 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003376 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003377 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003378 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003379 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003380 if (!mSystemReady) {
3381 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3382 return;
3383 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003384 mMethodList.clear();
3385 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003386 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003387 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003388
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003389 // Use for queryIntentServicesAsUser
3390 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003392 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3393 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3394 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003395 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003397 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3398 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003399
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003400 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003401 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 for (int i = 0; i < services.size(); ++i) {
3403 ResolveInfo ri = services.get(i);
3404 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003405 final String imeId = InputMethodInfo.computeId(ri);
3406 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3407 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 + ": it does not require the permission "
3409 + android.Manifest.permission.BIND_INPUT_METHOD);
3410 continue;
3411 }
3412
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003413 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003415 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 try {
satoke7c6998e2011-06-03 17:57:59 +09003417 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003418 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003419 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003420 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421
3422 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003423 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003425 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003426 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 }
3428 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003429
Yohei Yukawac4e44912017-02-09 19:30:22 -08003430 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3431 // negatives when the package state remains to be the same but only the component state is
3432 // changed.
3433 {
3434 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3435 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3436 // conservative, but it seems we cannot use it for now (Issue 35176630).
3437 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3438 new Intent(InputMethod.SERVICE_INTERFACE),
3439 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3440 final int N = allInputMethodServices.size();
3441 for (int i = 0; i < N; ++i) {
3442 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003443 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3444 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003445 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003446 }
3447 }
3448
Yohei Yukawa859df052016-02-17 07:56:46 -08003449 // TODO: The following code should find better place to live.
3450 if (!resetDefaultEnabledIme) {
3451 boolean enabledImeFound = false;
3452 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3453 final int N = enabledImes.size();
3454 for (int i = 0; i < N; ++i) {
3455 final InputMethodInfo imi = enabledImes.get(i);
3456 if (mMethodList.contains(imi)) {
3457 enabledImeFound = true;
3458 break;
3459 }
3460 }
3461 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003462 if (DEBUG) {
3463 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3464 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003465 resetDefaultEnabledIme = true;
3466 resetSelectedInputMethodAndSubtypeLocked("");
3467 }
3468 }
3469
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003470 if (resetDefaultEnabledIme) {
3471 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003472 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003473 final int N = defaultEnabledIme.size();
3474 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003475 final InputMethodInfo imi = defaultEnabledIme.get(i);
3476 if (DEBUG) {
3477 Slog.d(TAG, "--- enable ime = " + imi);
3478 }
3479 setInputMethodEnabledLocked(imi.getId(), true);
3480 }
3481 }
3482
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003483 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003484 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003485 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003486 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3487 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003488 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003489 }
3490 } else {
3491 // Double check that the default IME is certainly enabled.
3492 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003493 }
3494 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003495 // Here is not the perfect place to reset the switching controller. Ideally
3496 // mSwitchingController and mSettings should be able to share the same state.
3497 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3498 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003499 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003503
satok217f5482010-12-15 05:19:19 +09003504 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003505 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003506 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003507 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3508 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003509 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003510 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003511 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003512 final int userId;
3513 synchronized (mMethodMap) {
3514 userId = mSettings.getCurrentUserId();
3515 }
3516 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003517 }
3518
3519 private void showConfigureInputMethods() {
3520 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3521 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3522 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3523 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003524 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003525 }
3526
satok2c93efc2012-04-02 19:33:47 +09003527 private boolean isScreenLocked() {
3528 return mKeyguardManager != null
3529 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3530 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003531
Seigo Nonaka14e13912015-05-06 21:04:13 -07003532 private void showInputMethodMenu(boolean showAuxSubtypes) {
3533 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003536 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003537
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003538 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003539 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003540 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003541
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003542 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003543 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003544 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3545 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003546 if (immis == null || immis.size() == 0) {
3547 return;
3548 }
3549
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003550 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551
satok688bd472012-02-09 20:09:17 +09003552 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003553 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003554 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003555
satokc3690562012-01-10 20:14:43 +09003556 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003557 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003558 if (currentSubtype != null) {
3559 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003560 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3561 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003562 }
3563 }
3564
Ken Wakasa761eb372011-03-04 19:06:18 +09003565 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003566 mIms = new InputMethodInfo[N];
3567 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003568 int checkedItem = 0;
3569 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003570 final ImeSubtypeListItem item = imList.get(i);
3571 mIms[i] = item.mImi;
3572 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003573 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003574 int subtypeId = mSubtypeIds[i];
3575 if ((subtypeId == NOT_A_SUBTYPE_ID)
3576 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3577 || (subtypeId == lastInputMethodSubtypeId)) {
3578 checkedItem = i;
3579 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003582
3583 final Context settingsContext = new ContextThemeWrapper(context,
3584 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3585
3586 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003587 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3588 @Override
3589 public void onCancel(DialogInterface dialog) {
3590 hideInputMethodMenu();
3591 }
3592 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003593
3594 final Context dialogContext = mDialogBuilder.getContext();
3595 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3596 com.android.internal.R.styleable.DialogPreference,
3597 com.android.internal.R.attr.alertDialogStyle, 0);
3598 final Drawable dialogIcon = a.getDrawable(
3599 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3600 a.recycle();
3601
3602 mDialogBuilder.setIcon(dialogIcon);
3603
Yohei Yukawad34e1482016-02-11 08:03:52 -08003604 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003605 final View tv = inflater.inflate(
3606 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3607 mDialogBuilder.setCustomTitle(tv);
3608
3609 // Setup layout for a toggle switch of the hardware keyboard
3610 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003611 mSwitchingDialogTitleView
3612 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003613 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003614 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003615 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003616 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003617 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003618 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3619 @Override
3620 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003621 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003622 // Ensure that the input method dialog is dismissed when changing
3623 // the hardware keyboard state.
3624 hideInputMethodMenu();
3625 }
3626 });
3627
Alan Viverette505e3ab2014-11-24 15:22:11 -08003628 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003629 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3630 final OnClickListener choiceListener = new OnClickListener() {
3631 @Override
3632 public void onClick(final DialogInterface dialog, final int which) {
3633 synchronized (mMethodMap) {
3634 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3635 || mSubtypeIds.length <= which) {
3636 return;
satok01038492012-04-09 21:08:27 +09003637 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003638 final InputMethodInfo im = mIms[which];
3639 int subtypeId = mSubtypeIds[which];
3640 adapter.mCheckedItem = which;
3641 adapter.notifyDataSetChanged();
3642 hideInputMethodMenu();
3643 if (im != null) {
3644 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3645 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003646 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003647 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003649 }
3650 }
3651 };
3652 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003655 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003656 final Window w = mSwitchingDialog.getWindow();
3657 final WindowManager.LayoutParams attrs = w.getAttributes();
3658 w.setType(TYPE_INPUT_METHOD_DIALOG);
3659 // Use an alternate token for the dialog for that window manager can group the token
3660 // with other IME windows based on type vs. grouping based on whichever token happens
3661 // to get selected by the system later on.
3662 attrs.token = mSwitchingDialogToken;
3663 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3664 attrs.setTitle("Select input method");
3665 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003666 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 mSwitchingDialog.show();
3668 }
3669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003670
Ken Wakasa05dbb652011-08-22 15:22:43 +09003671 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3672 private final LayoutInflater mInflater;
3673 private final int mTextViewResourceId;
3674 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003675 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003676 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3677 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3678 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003679
Ken Wakasa05dbb652011-08-22 15:22:43 +09003680 mTextViewResourceId = textViewResourceId;
3681 mItemsList = itemsList;
3682 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003683 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003684 }
3685
3686 @Override
3687 public View getView(int position, View convertView, ViewGroup parent) {
3688 final View view = convertView != null ? convertView
3689 : mInflater.inflate(mTextViewResourceId, null);
3690 if (position < 0 || position >= mItemsList.size()) return view;
3691 final ImeSubtypeListItem item = mItemsList.get(position);
3692 final CharSequence imeName = item.mImeName;
3693 final CharSequence subtypeName = item.mSubtypeName;
3694 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3695 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3696 if (TextUtils.isEmpty(subtypeName)) {
3697 firstTextView.setText(imeName);
3698 secondTextView.setVisibility(View.GONE);
3699 } else {
3700 firstTextView.setText(subtypeName);
3701 secondTextView.setText(imeName);
3702 secondTextView.setVisibility(View.VISIBLE);
3703 }
3704 final RadioButton radioButton =
3705 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3706 radioButton.setChecked(position == mCheckedItem);
3707 return view;
3708 }
3709 }
3710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003712 synchronized (mMethodMap) {
3713 hideInputMethodMenuLocked();
3714 }
3715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003716
The Android Open Source Project10592532009-03-18 17:39:46 -07003717 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003718 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719
The Android Open Source Project10592532009-03-18 17:39:46 -07003720 if (mSwitchingDialog != null) {
3721 mSwitchingDialog.dismiss();
3722 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003724
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003725 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003726 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003727 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003731
satok42c5a162011-05-26 16:46:14 +09003732 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003734 // TODO: Make this work even for non-current users?
3735 if (!calledFromValidUser()) {
3736 return false;
3737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 synchronized (mMethodMap) {
3739 if (mContext.checkCallingOrSelfPermission(
3740 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3741 != PackageManager.PERMISSION_GRANTED) {
3742 throw new SecurityException(
3743 "Requires permission "
3744 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3745 }
Anna Galusza9b278112016-01-04 11:37:37 -08003746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 long ident = Binder.clearCallingIdentity();
3748 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003749 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 } finally {
3751 Binder.restoreCallingIdentity(ident);
3752 }
3753 }
3754 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003755
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003756 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3757 // Make sure this is a valid input method.
3758 InputMethodInfo imm = mMethodMap.get(id);
3759 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003760 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003761 }
3762
satokd87c2592010-09-29 11:52:06 +09003763 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3764 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003765
satokd87c2592010-09-29 11:52:06 +09003766 if (enabled) {
3767 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3768 if (pair.first.equals(id)) {
3769 // We are enabling this input method, but it is already enabled.
3770 // Nothing to do. The previous state was enabled.
3771 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003772 }
3773 }
satokd87c2592010-09-29 11:52:06 +09003774 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3775 // Previous state was disabled.
3776 return false;
3777 } else {
3778 StringBuilder builder = new StringBuilder();
3779 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3780 builder, enabledInputMethodsList, id)) {
3781 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003782 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003783 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3784 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3785 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003786 }
3787 // Previous state was enabled.
3788 return true;
3789 } else {
3790 // We are disabling the input method but it is already disabled.
3791 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003792 return false;
3793 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003794 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003795 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003796
satok723a27e2010-11-11 14:58:11 +09003797 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3798 boolean setSubtypeOnly) {
3799 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003800 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003801
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003802 mCurUserActionNotificationSequenceNumber =
3803 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3804 if (DEBUG) {
3805 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3806 + mCurUserActionNotificationSequenceNumber);
3807 }
3808
3809 if (mCurClient != null && mCurClient.client != null) {
3810 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3811 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003812 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003813 }
3814
satok723a27e2010-11-11 14:58:11 +09003815 // Set Subtype here
3816 if (imi == null || subtypeId < 0) {
3817 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003818 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003819 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003820 if (subtypeId < imi.getSubtypeCount()) {
3821 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3822 mSettings.putSelectedSubtype(subtype.hashCode());
3823 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003824 } else {
3825 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003826 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003827 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003828 }
satokab751aa2010-09-14 19:17:36 +09003829 }
satok723a27e2010-11-11 14:58:11 +09003830
Yohei Yukawa68645a62016-02-17 07:54:20 -08003831 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003832 // Set InputMethod here
3833 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3834 }
3835 }
3836
3837 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3838 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3839 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3840 // newDefaultIme is empty when there is no candidate for the selected IME.
3841 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3842 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3843 if (subtypeHashCode != null) {
3844 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003845 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003846 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003847 } catch (NumberFormatException e) {
3848 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3849 }
3850 }
3851 }
3852 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003853 }
3854
satok4e4569d2010-11-19 18:45:53 +09003855 // If there are no selected shortcuts, tries finding the most applicable ones.
3856 private Pair<InputMethodInfo, InputMethodSubtype>
3857 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3858 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3859 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003860 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003861 boolean foundInSystemIME = false;
3862
3863 // Search applicable subtype for each InputMethodInfo
3864 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003865 final String imiId = imi.getId();
3866 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3867 continue;
3868 }
satokcd7cd292010-11-20 15:46:23 +09003869 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003870 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003871 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003872 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003873 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003874 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003875 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003876 }
satokdf31ae62011-01-15 06:19:44 +09003877 // 2. Search by the system locale from enabledSubtypes.
3878 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003879 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003880 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003881 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003882 }
satoka86f5e42011-09-02 17:12:42 +09003883 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003884 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003885 final ArrayList<InputMethodSubtype> subtypesForSearch =
3886 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003887 ? InputMethodUtils.getSubtypes(imi)
3888 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003889 // 4. Search by the current subtype's locale from all subtypes.
3890 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003891 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003892 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003893 }
3894 // 5. Search by the system locale from all subtypes.
3895 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003896 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003897 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003898 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003899 }
satokcd7cd292010-11-20 15:46:23 +09003900 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003901 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003902 // The current input method is the most applicable IME.
3903 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003904 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003905 break;
satok7599a7f2010-12-22 13:45:23 +09003906 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003907 // The system input method is 2nd applicable IME.
3908 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003909 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003910 if ((imi.getServiceInfo().applicationInfo.flags
3911 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3912 foundInSystemIME = true;
3913 }
satok4e4569d2010-11-19 18:45:53 +09003914 }
3915 }
3916 }
3917 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003918 if (mostApplicableIMI != null) {
3919 Slog.w(TAG, "Most applicable shortcut input method was:"
3920 + mostApplicableIMI.getId());
3921 if (mostApplicableSubtype != null) {
3922 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3923 + "," + mostApplicableSubtype.getMode() + ","
3924 + mostApplicableSubtype.getLocale());
3925 }
3926 }
satok4e4569d2010-11-19 18:45:53 +09003927 }
satokcd7cd292010-11-20 15:46:23 +09003928 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003929 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003930 } else {
3931 return null;
3932 }
3933 }
3934
satokab751aa2010-09-14 19:17:36 +09003935 /**
3936 * @return Return the current subtype of this input method.
3937 */
satok42c5a162011-05-26 16:46:14 +09003938 @Override
satokab751aa2010-09-14 19:17:36 +09003939 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003940 // TODO: Make this work even for non-current users?
3941 if (!calledFromValidUser()) {
3942 return null;
3943 }
3944 synchronized (mMethodMap) {
3945 return getCurrentInputMethodSubtypeLocked();
3946 }
3947 }
3948
3949 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003950 if (mCurMethodId == null) {
3951 return null;
3952 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003953 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003954 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3955 if (imi == null || imi.getSubtypeCount() == 0) {
3956 return null;
satok4e4569d2010-11-19 18:45:53 +09003957 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003958 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003959 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3960 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003961 if (subtypeId == NOT_A_SUBTYPE_ID) {
3962 // If there are no selected subtypes, the framework will try to find
3963 // the most applicable subtype from explicitly or implicitly enabled
3964 // subtypes.
3965 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003966 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003967 // If there is only one explicitly or implicitly enabled subtype,
3968 // just returns it.
3969 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3970 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3971 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003972 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003973 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003974 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003975 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003976 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003977 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3978 true);
satok4e4569d2010-11-19 18:45:53 +09003979 }
satok3ef8b292010-11-23 06:06:29 +09003980 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003981 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003982 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003983 }
3984 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003985 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003986 }
3987
satok4e4569d2010-11-19 18:45:53 +09003988 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003989 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003990 @Override
satok4e4569d2010-11-19 18:45:53 +09003991 public List getShortcutInputMethodsAndSubtypes() {
3992 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003993 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003994 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003995 // If there are no selected shortcut subtypes, the framework will try to find
3996 // the most applicable subtype from all subtypes whose mode is
3997 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003998 Pair<InputMethodInfo, InputMethodSubtype> info =
3999 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004000 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09004001 if (info != null) {
satok3da92232011-01-11 22:46:30 +09004002 ret.add(info.first);
4003 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09004004 }
satok3da92232011-01-11 22:46:30 +09004005 return ret;
satokf3db1af2010-11-23 13:34:33 +09004006 }
satokf3db1af2010-11-23 13:34:33 +09004007 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
4008 ret.add(imi);
4009 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
4010 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09004011 }
4012 }
satokf3db1af2010-11-23 13:34:33 +09004013 return ret;
satok4e4569d2010-11-19 18:45:53 +09004014 }
4015 }
4016
satok42c5a162011-05-26 16:46:14 +09004017 @Override
satokb66d2872010-11-10 01:04:04 +09004018 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004019 // TODO: Make this work even for non-current users?
4020 if (!calledFromValidUser()) {
4021 return false;
4022 }
satokb66d2872010-11-10 01:04:04 +09004023 synchronized (mMethodMap) {
4024 if (subtype != null && mCurMethodId != null) {
4025 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004026 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09004027 if (subtypeId != NOT_A_SUBTYPE_ID) {
4028 setInputMethodLocked(mCurMethodId, subtypeId);
4029 return true;
4030 }
4031 }
4032 return false;
4033 }
4034 }
4035
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004036 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09004037 private static class InputMethodFileManager {
4038 private static final String SYSTEM_PATH = "system";
4039 private static final String INPUT_METHOD_PATH = "inputmethod";
4040 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
4041 private static final String NODE_SUBTYPES = "subtypes";
4042 private static final String NODE_SUBTYPE = "subtype";
4043 private static final String NODE_IMI = "imi";
4044 private static final String ATTR_ID = "id";
4045 private static final String ATTR_LABEL = "label";
4046 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07004047 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09004048 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004049 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09004050 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
4051 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
4052 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004053 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09004054 private final AtomicFile mAdditionalInputMethodSubtypeFile;
4055 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004056 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004057 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004058 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09004059 if (methodMap == null) {
4060 throw new NullPointerException("methodMap is null");
4061 }
4062 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07004063 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09004064 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
4065 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09004066 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07004067 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09004068 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
4069 }
4070 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
4071 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
4072 if (!subtypeFile.exists()) {
4073 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004074 writeAdditionalInputMethodSubtypes(
4075 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09004076 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004077 readAdditionalInputMethodSubtypes(
4078 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09004079 }
4080 }
4081
4082 private void deleteAllInputMethodSubtypes(String imiId) {
4083 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004084 mAdditionalSubtypesMap.remove(imiId);
4085 writeAdditionalInputMethodSubtypes(
4086 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004087 }
4088 }
4089
4090 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09004091 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09004092 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004093 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004094 final int N = additionalSubtypes.length;
4095 for (int i = 0; i < N; ++i) {
4096 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004097 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004098 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004099 } else {
4100 Slog.w(TAG, "Duplicated subtype definition found: "
4101 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004102 }
4103 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004104 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4105 writeAdditionalInputMethodSubtypes(
4106 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004107 }
4108 }
4109
4110 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4111 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004112 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004113 }
4114 }
4115
4116 private static void writeAdditionalInputMethodSubtypes(
4117 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4118 HashMap<String, InputMethodInfo> methodMap) {
4119 // Safety net for the case that this function is called before methodMap is set.
4120 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4121 FileOutputStream fos = null;
4122 try {
4123 fos = subtypesFile.startWrite();
4124 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004125 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004126 out.startDocument(null, true);
4127 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4128 out.startTag(null, NODE_SUBTYPES);
4129 for (String imiId : allSubtypes.keySet()) {
4130 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4131 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4132 continue;
4133 }
4134 out.startTag(null, NODE_IMI);
4135 out.attribute(null, ATTR_ID, imiId);
4136 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4137 final int N = subtypesList.size();
4138 for (int i = 0; i < N; ++i) {
4139 final InputMethodSubtype subtype = subtypesList.get(i);
4140 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004141 if (subtype.hasSubtypeId()) {
4142 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4143 String.valueOf(subtype.getSubtypeId()));
4144 }
satoke7c6998e2011-06-03 17:57:59 +09004145 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4146 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4147 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004148 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4149 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004150 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4151 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4152 out.attribute(null, ATTR_IS_AUXILIARY,
4153 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004154 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4155 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004156 out.endTag(null, NODE_SUBTYPE);
4157 }
4158 out.endTag(null, NODE_IMI);
4159 }
4160 out.endTag(null, NODE_SUBTYPES);
4161 out.endDocument();
4162 subtypesFile.finishWrite(fos);
4163 } catch (java.io.IOException e) {
4164 Slog.w(TAG, "Error writing subtypes", e);
4165 if (fos != null) {
4166 subtypesFile.failWrite(fos);
4167 }
4168 }
4169 }
4170
4171 private static void readAdditionalInputMethodSubtypes(
4172 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4173 if (allSubtypes == null || subtypesFile == null) return;
4174 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004175 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004176 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004177 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004178 int type = parser.getEventType();
4179 // Skip parsing until START_TAG
4180 while ((type = parser.next()) != XmlPullParser.START_TAG
4181 && type != XmlPullParser.END_DOCUMENT) {}
4182 String firstNodeName = parser.getName();
4183 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4184 throw new XmlPullParserException("Xml doesn't start with subtypes");
4185 }
4186 final int depth =parser.getDepth();
4187 String currentImiId = null;
4188 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4189 while (((type = parser.next()) != XmlPullParser.END_TAG
4190 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4191 if (type != XmlPullParser.START_TAG)
4192 continue;
4193 final String nodeName = parser.getName();
4194 if (NODE_IMI.equals(nodeName)) {
4195 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4196 if (TextUtils.isEmpty(currentImiId)) {
4197 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4198 continue;
4199 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004200 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004201 allSubtypes.put(currentImiId, tempSubtypesArray);
4202 } else if (NODE_SUBTYPE.equals(nodeName)) {
4203 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4204 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4205 continue;
4206 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004207 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004208 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004209 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004210 parser.getAttributeValue(null, ATTR_LABEL));
4211 final String imeSubtypeLocale =
4212 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004213 final String languageTag =
4214 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004215 final String imeSubtypeMode =
4216 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4217 final String imeSubtypeExtraValue =
4218 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004219 final boolean isAuxiliary = "1".equals(String.valueOf(
4220 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004221 final boolean isAsciiCapable = "1".equals(String.valueOf(
4222 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004223 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004224 .setSubtypeNameResId(label)
4225 .setSubtypeIconResId(icon)
4226 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004227 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004228 .setSubtypeMode(imeSubtypeMode)
4229 .setSubtypeExtraValue(imeSubtypeExtraValue)
4230 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004231 .setIsAsciiCapable(isAsciiCapable);
4232 final String subtypeIdString =
4233 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4234 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004235 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004236 }
4237 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004238 }
4239 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004240 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4241 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004242 return;
satoke7c6998e2011-06-03 17:57:59 +09004243 }
4244 }
4245 }
4246
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004247 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4248 @NonNull
4249 private final Handler mHandler;
4250
4251 LocalServiceImpl(@NonNull final Handler handler) {
4252 mHandler = handler;
4253 }
4254
4255 @Override
4256 public void setInteractive(boolean interactive) {
4257 // Do everything in handler so as not to block the caller.
4258 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4259 interactive ? 1 : 0, 0));
4260 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004261
4262 @Override
4263 public void switchInputMethod(boolean forwardDirection) {
4264 // Do everything in handler so as not to block the caller.
4265 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4266 forwardDirection ? 1 : 0, 0));
4267 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004268
4269 @Override
4270 public void hideCurrentInputMethod() {
4271 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4272 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4273 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004274 }
4275
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004276 private static String imeWindowStatusToString(final int imeWindowVis) {
4277 final StringBuilder sb = new StringBuilder();
4278 boolean first = true;
4279 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4280 sb.append("Active");
4281 first = false;
4282 }
4283 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4284 if (!first) {
4285 sb.append("|");
4286 }
4287 sb.append("Visible");
4288 }
4289 return sb.toString();
4290 }
4291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004293 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4294 @Nullable Uri contentUri, @Nullable String packageName) {
4295 if (!calledFromValidUser()) {
4296 return null;
4297 }
4298
4299 if (token == null) {
4300 throw new NullPointerException("token");
4301 }
4302 if (packageName == null) {
4303 throw new NullPointerException("packageName");
4304 }
4305 if (contentUri == null) {
4306 throw new NullPointerException("contentUri");
4307 }
4308 final String contentUriScheme = contentUri.getScheme();
4309 if (!"content".equals(contentUriScheme)) {
4310 throw new InvalidParameterException("contentUri must have content scheme");
4311 }
4312
4313 synchronized (mMethodMap) {
4314 final int uid = Binder.getCallingUid();
4315 if (mCurMethodId == null) {
4316 return null;
4317 }
4318 if (mCurToken != token) {
4319 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4320 + " token=" + token);
4321 return null;
4322 }
4323 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4324 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4325 // nature of our system. Let's compare it with our internal record.
4326 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4327 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4328 + mCurAttribute.packageName + " packageName=" + packageName);
4329 return null;
4330 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004331 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004332 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004333 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004334 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004335 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4336 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4337 imeUserId);
4338 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4339 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4340 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4341 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4342 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4343 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4344 // actually allowed to "uid", which is guaranteed to be the IME's one.
4345 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4346 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004347 }
4348 }
4349
4350 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004351 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4352 if (!calledFromValidUser()) {
4353 return;
4354 }
4355 synchronized (mMethodMap) {
4356 if (!calledWithValidToken(token)) {
4357 return;
4358 }
4359 if (mCurClient != null && mCurClient.client != null) {
4360 mInFullscreenMode = fullscreen;
4361 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4362 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4363 }
4364 }
4365 }
4366
4367 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4369 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4370 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4373 + Binder.getCallingPid()
4374 + ", uid=" + Binder.getCallingUid());
4375 return;
4376 }
4377
4378 IInputMethod method;
4379 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004380 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 synchronized (mMethodMap) {
4385 p.println("Current Input Method Manager state:");
4386 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004387 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 for (int i=0; i<N; i++) {
4389 InputMethodInfo info = mMethodList.get(i);
4390 p.println(" InputMethod #" + i + ":");
4391 info.dump(p, " ");
4392 }
4393 p.println(" Clients:");
4394 for (ClientState ci : mClients.values()) {
4395 p.println(" Client " + ci + ":");
4396 p.println(" client=" + ci.client);
4397 p.println(" inputContext=" + ci.inputContext);
4398 p.println(" sessionRequested=" + ci.sessionRequested);
4399 p.println(" curSession=" + ci.curSession);
4400 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004401 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004403 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004404 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4405 + " softInputMode=" +
4406 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4407 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004408 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4410 + " mBoundToMethod=" + mBoundToMethod);
4411 p.println(" mCurToken=" + mCurToken);
4412 p.println(" mCurIntent=" + mCurIntent);
4413 method = mCurMethod;
4414 p.println(" mCurMethod=" + mCurMethod);
4415 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004416 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 p.println(" mShowRequested=" + mShowRequested
4418 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4419 + " mShowForced=" + mShowForced
4420 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004421 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004422 p.println(" mCurUserActionNotificationSequenceNumber="
4423 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004424 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004425 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004426 p.println(" mSwitchingController:");
4427 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004428 p.println(" mSettings:");
4429 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004430
4431 p.println(" mStartInputHistory:");
4432 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004434
Jeff Brownb88102f2010-09-08 11:49:43 -07004435 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 pw.flush();
4438 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004439 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4440 } catch (IOException | RemoteException e) {
4441 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004443 } else {
4444 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004446
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004447 if (focusedWindowClient != null && client != focusedWindowClient) {
4448 p.println(" ");
4449 p.println("Warning: Current input method client doesn't match the last focused. "
4450 + "window.");
4451 p.println("Dumping input method client in the last focused window just in case.");
4452 p.println(" ");
4453 pw.flush();
4454 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004455 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4456 } catch (IOException | RemoteException e) {
4457 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004458 }
4459 }
4460
Jeff Brownb88102f2010-09-08 11:49:43 -07004461 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 pw.flush();
4464 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004465 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4466 } catch (IOException | RemoteException e) {
4467 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004469 } else {
4470 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 }
4472 }
4473}