blob: 27c49523a659931b6595c508de650888265b9704 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070032import com.android.internal.os.SomeArgs;
Jeff Sharkey850c83e2016-11-09 12:25:44 -070033import com.android.internal.os.TransferPipe;
satoke7c6998e2011-06-03 17:57:59 +090034import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import com.android.internal.view.IInputContext;
36import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import com.android.internal.view.IInputMethodClient;
38import com.android.internal.view.IInputMethodManager;
39import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070040import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080042import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080043import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
satoke7c6998e2011-06-03 17:57:59 +090045import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090047import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070049import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070050import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070051import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080052import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080053import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070054import android.app.ActivityManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070056import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090057import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090058import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090059import android.app.Notification;
60import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070061import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090062import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080064import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.ContentResolver;
66import android.content.Context;
67import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090069import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090071import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070073import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090074import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.content.pm.PackageManager;
76import android.content.pm.ResolveInfo;
77import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070078import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.content.res.Resources;
80import android.content.res.TypedArray;
81import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080082import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080083import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080084import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070085import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040087import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070088import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090089import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.Handler;
91import android.os.IBinder;
92import android.os.IInterface;
93import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070094import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070096import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080098import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.os.ServiceManager;
100import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900101import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -0800102import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.provider.Settings;
104import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +0900105import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700106import android.util.ArrayMap;
107import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -0700108import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900110import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900111import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.util.PrintWriterPrinter;
113import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900114import android.util.Slog;
115import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900116import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700118import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900119import android.view.LayoutInflater;
120import android.view.View;
121import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700122import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700124import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900125import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800127import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700128import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import android.view.inputmethod.InputMethod;
130import android.view.inputmethod.InputMethodInfo;
131import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700132import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900133import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900134import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900135import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900136import android.widget.CompoundButton;
137import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900138import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900139import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900140import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700141import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
satoke7c6998e2011-06-03 17:57:59 +0900143import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900145import java.io.FileInputStream;
146import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147import java.io.IOException;
148import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700149import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100150import java.nio.charset.StandardCharsets;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700151import java.security.InvalidParameterException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900153import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154import java.util.HashMap;
155import java.util.List;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800156import java.util.WeakHashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158/**
159 * This class provides a system service that manages input methods.
160 */
161public class InputMethodManagerService extends IInputMethodManager.Stub
162 implements ServiceConnection, Handler.Callback {
163 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700164 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700165 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700167 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
168 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
169 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 static final int MSG_UNBIND_INPUT = 1000;
172 static final int MSG_BIND_INPUT = 1010;
173 static final int MSG_SHOW_SOFT_INPUT = 1020;
174 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700175 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final int MSG_ATTACH_TOKEN = 1040;
177 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
Yohei Yukawa33e81792015-11-17 21:14:42 -0800181 static final int MSG_UNBIND_CLIENT = 3000;
182 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700183 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700184 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900185 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800186 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800187 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800188
satok01038492012-04-09 21:08:27 +0900189 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
190
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700191 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
192
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700193 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
satokf9f01002011-05-19 21:31:50 +0900195 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
196
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900197 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900198 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900199
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700200 @Retention(SOURCE)
201 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
202 private @interface HardKeyboardBehavior {
203 int WIRELESS_AFFORDANCE = 0;
204 int WIRED_AFFORDANCE = 1;
205 }
satok4e4569d2010-11-19 18:45:53 +0900206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800208 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900210 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700213 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700215 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900216 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900217 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900218 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800219 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900221 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 // All known input methods. mMethodMap also serves as the global
224 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700225 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
226 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900227 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700228 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900229 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
Yohei Yukawae0733062017-02-09 22:49:35 -0800231 /**
232 * Tracks how many times {@link #mMethodMap} was updated.
233 */
234 @GuardedBy("mMethodMap")
235 private int mMethodMapUpdateCount = 0;
236
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700237 // Used to bring IME service up to visible adjustment while it is being shown.
238 final ServiceConnection mVisibleConnection = new ServiceConnection() {
239 @Override public void onServiceConnected(ComponentName name, IBinder service) {
240 }
241
242 @Override public void onServiceDisconnected(ComponentName name) {
243 }
244 };
245 boolean mVisibleBound = false;
246
satok7cfc0ed2011-06-20 21:29:36 +0900247 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700248 private NotificationManager mNotificationManager;
249 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700250 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400251 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700252 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900253 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900254 private boolean mNotificationShown;
255
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900256 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 final ClientState client;
258 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700259
260 IInputMethodSession session;
261 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 @Override
264 public String toString() {
265 return "SessionState{uid " + client.uid + " pid " + client.pid
266 + " method " + Integer.toHexString(
267 System.identityHashCode(method))
268 + " session " + Integer.toHexString(
269 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700270 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 + "}";
272 }
273
274 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700275 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 client = _client;
277 method = _method;
278 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700279 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
Jeff Brownc28867a2013-03-26 15:42:39 -0700283 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 final IInputMethodClient client;
285 final IInputContext inputContext;
286 final int uid;
287 final int pid;
288 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 boolean sessionRequested;
291 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 @Override
294 public String toString() {
295 return "ClientState{" + Integer.toHexString(
296 System.identityHashCode(this)) + " uid " + uid
297 + " pid " + pid + "}";
298 }
299
300 ClientState(IInputMethodClient _client, IInputContext _inputContext,
301 int _uid, int _pid) {
302 client = _client;
303 inputContext = _inputContext;
304 uid = _uid;
305 pid = _pid;
306 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
307 }
308 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700310 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700313 * Set once the system is ready to run third party code.
314 */
315 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700317 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700318 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
319 * method. This is to be synchronized with the secure settings keyed with
320 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
321 *
322 * <p>This can be transiently {@code null} when the system is re-initializing input method
323 * settings, e.g., the system locale is just changed.</p>
324 *
325 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
326 * {@link InputMethodManagerService}.</p>
327 *
328 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700330 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * The current binding sequence number, incremented every time there is
335 * a new bind performed.
336 */
337 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * The client that is currently bound to an input method.
341 */
342 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800345 * The last window token that we confirmed to be focused. This is always updated upon reports
346 * from the input method client. If the window state is already changed before the report is
347 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700348 */
349 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700351 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800352 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
353 *
354 * @see #mCurFocusedWindow
355 */
356 int mCurFocusedWindowSoftInputMode;
357
358 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800359 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
360 */
361 ClientState mCurFocusedWindowClient;
362
363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * The input context last provided by the current client.
365 */
366 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700369 * The missing method flags for the input context last provided by the current client.
370 *
371 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
372 */
373 int mCurInputContextMissingMethods;
374
375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * The attributes last provided by the current client.
377 */
378 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700381 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700383 *
384 * <p>This can be {@code null} when no input method is connected.</p>
385 *
386 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700388 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
satokab751aa2010-09-14 19:17:36 +0900392 * The current subtype of the current input method.
393 */
394 private InputMethodSubtype mCurrentSubtype;
395
satok4e4569d2010-11-19 18:45:53 +0900396 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900397 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700398 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900399
John Spurlocke0980502013-10-25 11:59:29 -0400400 // Was the keyguard locked when this client became current?
401 private boolean mCurClientInKeyguard;
402
satokab751aa2010-09-14 19:17:36 +0900403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 * Set to true if our ServiceConnection is currently actively bound to
405 * a service (whether or not we have gotten its IBinder back yet).
406 */
407 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 /**
410 * Set if the client has asked for the input method to be shown.
411 */
412 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Set if we were explicitly told to show the input method.
416 */
417 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * Set if we were forced to be shown.
421 */
422 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
425 * Set if we last told the input method to show itself.
426 */
427 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800430 * {@code true} if the current input method is in fullscreen mode.
431 */
432 boolean mInFullscreenMode;
433
434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 * The Intent used to connect to the current input method.
436 */
437 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 /**
440 * The token we have made for the currently active input method, to
441 * identify it in the future.
442 */
443 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 /**
446 * If non-null, this is the input method service we are currently connected
447 * to.
448 */
449 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 /**
452 * Time that we last initiated a bind to the input method, to determine
453 * if we should try to disconnect and reconnect to it.
454 */
455 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 /**
458 * Have we called mCurMethod.bindInput()?
459 */
460 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 /**
463 * Currently enabled session. Only touched by service thread, not
464 * protected by a lock.
465 */
466 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700469 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700471 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900473 int mCurUserActionNotificationSequenceNumber = 0;
474
Joe Onorato857fd9b2011-01-27 15:08:35 -0800475 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900476
477 /**
478 * A set of status bits regarding the active IME.
479 *
480 * <p>This value is a combination of following two bits:</p>
481 * <dl>
482 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
483 * <dd>
484 * If this bit is ON, connected IME is ready to accept touch/key events.
485 * </dd>
486 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
487 * <dd>
488 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
489 * </dd>
490 * </dl>
491 * <em>Do not update this value outside of setImeWindowStatus.</em>
492 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800493 int mImeWindowVis;
494
Ken Wakasa05dbb652011-08-22 15:22:43 +0900495 private AlertDialog.Builder mDialogBuilder;
496 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700497 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900498 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700499 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900500 private InputMethodInfo[] mIms;
501 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800502 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700503 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800504 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900505 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
506 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500507 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700508 @HardKeyboardBehavior
509 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800510
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800511 /**
512 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
513 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
514 * will not affect those tasks that are already posted.
515 *
516 * <p>Posting {@link #MSG_START_INPUT} message basically means that
517 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
518 * back in the current IME process shortly, which will also affect what the current IME starts
519 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
520 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
521 * logical input session between the client application and the current IME.</p>
522 *
523 * <p>Be careful to not keep strong references to this object forever, which can prevent
524 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
525 * </p>
526 */
527 private static class StartInputInfo {
528 @NonNull
529 final IBinder mImeToken;
530 @Nullable
531 final IBinder mTargetWindow;
532
533 StartInputInfo(@NonNull IBinder imeToken, @Nullable IBinder targetWindow) {
534 mImeToken = imeToken;
535 mTargetWindow = targetWindow;
536 }
537 }
538
539 private WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700542 int mUserId;
543 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700544 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800545 String mLastEnabled = "";
546
Yohei Yukawa81482972015-06-04 00:58:59 -0700547 /**
548 * <em>This constructor must be called within the lock.</em>
549 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 SettingsObserver(Handler handler) {
551 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700552 }
553
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800554 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700555 if (mRegistered && mUserId == userId) {
556 return;
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700559 if (mRegistered) {
560 mContext.getContentResolver().unregisterContentObserver(this);
561 mRegistered = false;
562 }
563 if (mUserId != userId) {
564 mLastEnabled = "";
565 mUserId = userId;
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700568 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900569 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700570 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900571 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700572 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700573 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700574 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800575 resolver.registerContentObserver(Settings.Secure.getUriFor(
576 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700577 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
Michael Wright7b5a96b2014-08-09 19:28:42 -0700580 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800581 final Uri showImeUri = Settings.Secure.getUriFor(
582 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
583 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
584 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700586 if (showImeUri.equals(uri)) {
587 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800588 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
589 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
590 mContext.getContentResolver(),
591 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
592 0, mUserId) == 1;
593 if (mAccessibilityRequestingNoSoftKeyboard) {
594 final boolean showRequested = mShowRequested;
595 hideCurrentInputLocked(0, null);
596 mShowRequested = showRequested;
597 } else if (mShowRequested) {
598 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
599 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700600 } else {
601 boolean enabledChanged = false;
602 String newEnabled = mSettings.getEnabledInputMethodsStr();
603 if (!mLastEnabled.equals(newEnabled)) {
604 mLastEnabled = newEnabled;
605 enabledChanged = true;
606 }
607 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
610 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700611
612 @Override
613 public String toString() {
614 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
615 + " mLastEnabled=" + mLastEnabled + "}";
616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800618
Yohei Yukawa79247822017-01-23 15:26:15 -0800619 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900620 @Override
621 public void onReceive(Context context, Intent intent) {
622 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700623 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900624 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700625 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900626 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800627 } else if (Intent.ACTION_USER_ADDED.equals(action)
628 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100629 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800630 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700631 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
632 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
633 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
634 final String prevValue = intent.getStringExtra(
635 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
636 final String newValue = intent.getStringExtra(
637 Intent.EXTRA_SETTING_NEW_VALUE);
638 restoreEnabledInputMethods(mContext, prevValue, newValue);
639 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800640 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800641 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900642 } else {
643 Slog.w(TAG, "Unexpected intent " + intent);
644 }
645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800647
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800648 /**
649 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
650 *
651 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
652 * the users. We should ignore this event if this is about any background user's locale.</p>
653 *
654 * <p>Caution: This method must not be called when system is not ready.</p>
655 */
656 void onActionLocaleChanged() {
657 synchronized (mMethodMap) {
658 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
659 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
660 return;
661 }
662 buildInputMethodListLocked(true);
663 // If the locale is changed, needs to reset the default ime
664 resetDefaultImeLocked(mContext);
665 updateFromSettingsLocked(true);
666 mLastSystemLocales = possibleNewLocale;
667 }
668 }
669
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700670 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
671 // does not attempt to validate on the fly with any installed device policy, so must only
672 // be run in the context of initial device setup.
673 //
674 // TODO: Move this method to InputMethodUtils with adding unit tests.
675 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
676 if (DEBUG_RESTORE) {
677 Slog.i(TAG, "Restoring enabled input methods:");
678 Slog.i(TAG, "prev=" + prevValue);
679 Slog.i(TAG, " new=" + newValue);
680 }
681 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900682 ArrayMap<String, ArraySet<String>> prevMap =
683 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
684 ArrayMap<String, ArraySet<String>> newMap =
685 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700686
687 // Merge the restored ime+subtype enabled states into the live state
688 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
689 final String imeId = entry.getKey();
690 ArraySet<String> prevSubtypes = prevMap.get(imeId);
691 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700692 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700693 prevMap.put(imeId, prevSubtypes);
694 }
695 prevSubtypes.addAll(entry.getValue());
696 }
697
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700698 final String mergedImesAndSubtypesString =
699 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700700 if (DEBUG_RESTORE) {
701 Slog.i(TAG, "Merged IME string:");
702 Slog.i(TAG, " " + mergedImesAndSubtypesString);
703 }
704 Settings.Secure.putString(context.getContentResolver(),
705 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
706 }
707
Yohei Yukawac4e44912017-02-09 19:30:22 -0800708 final class MyPackageMonitor extends PackageMonitor {
709 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800710 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -0800711 *
712 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
713 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
714 * dynamically unless the entire package is updated, which also always triggers package
715 * rescanning.</p>
716 */
717 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800718 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
719
720 /**
721 * Packages that are appeared, disappeared, or modified for whatever reason.
722 *
723 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
724 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
725 * duplicate elements for our use case.</p>
726 *
727 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
728 * which should be bound to {@link #getRegisteredHandler()}.</p>
729 */
730 private final ArrayList<String> mChangedPackages = new ArrayList<>();
731
732 /**
733 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
734 *
735 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
736 * which should be bound to {@link #getRegisteredHandler()}.</p>
737 */
738 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -0800739
740 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800741 void clearKnownImePackageNamesLocked() {
742 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -0800743 }
744
745 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800746 final void addKnownImePackageNameLocked(@NonNull String packageName) {
747 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800748 }
749
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800750 @GuardedBy("mMethodMap")
751 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900752 final int userId = getChangingUserId();
753 final boolean retval = userId == mSettings.getCurrentUserId();
754 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900755 if (!retval) {
756 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
757 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900758 }
759 return retval;
760 }
761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800763 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800765 if (!isChangingPackagesOfCurrentUserLocked()) {
766 return false;
767 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900768 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 final int N = mMethodList.size();
770 if (curInputMethodId != null) {
771 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800772 InputMethodInfo imi = mMethodList.get(i);
773 if (imi.getId().equals(curInputMethodId)) {
774 for (String pkg : packages) {
775 if (imi.getPackageName().equals(pkg)) {
776 if (!doit) {
777 return true;
778 }
satok723a27e2010-11-11 14:58:11 +0900779 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800780 chooseNewDefaultIMELocked();
781 return true;
782 }
783 }
784 }
785 }
786 }
787 }
788 return false;
789 }
790
791 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800792 public void onBeginPackageChanges() {
793 clearPackageChangeState();
794 }
795
796 @Override
797 public void onPackageAppeared(String packageName, int reason) {
798 if (!mImePackageAppeared) {
799 final PackageManager pm = mContext.getPackageManager();
800 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
801 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
802 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
803 // No need to lock this because we access it only on getRegisteredHandler().
804 if (!services.isEmpty()) {
805 mImePackageAppeared = true;
806 }
807 }
808 // No need to lock this because we access it only on getRegisteredHandler().
809 mChangedPackages.add(packageName);
810 }
811
812 @Override
813 public void onPackageDisappeared(String packageName, int reason) {
814 // No need to lock this because we access it only on getRegisteredHandler().
815 mChangedPackages.add(packageName);
816 }
817
818 @Override
819 public void onPackageModified(String packageName) {
820 // No need to lock this because we access it only on getRegisteredHandler().
821 mChangedPackages.add(packageName);
822 }
823
824 @Override
825 public void onPackagesSuspended(String[] packages) {
826 // No need to lock this because we access it only on getRegisteredHandler().
827 for (String packageName : packages) {
828 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -0800829 }
830 }
831
832 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800833 public void onPackagesUnsuspended(String[] packages) {
834 // No need to lock this because we access it only on getRegisteredHandler().
835 for (String packageName : packages) {
836 mChangedPackages.add(packageName);
837 }
838 }
839
840 @Override
841 public void onFinishPackageChanges() {
842 onFinishPackageChangesInternal();
843 clearPackageChangeState();
844 }
845
846 private void clearPackageChangeState() {
847 // No need to lock them because we access these fields only on getRegisteredHandler().
848 mChangedPackages.clear();
849 mImePackageAppeared = false;
850 }
851
852 private boolean shouldRebuildInputMethodListLocked() {
853 // This method is guaranteed to be called only by getRegisteredHandler().
854
855 // If there is any new package that contains at least one IME, then rebuilt the list
856 // of IMEs.
857 if (mImePackageAppeared) {
858 return true;
859 }
860
861 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
862 // TODO: Consider to create a utility method to do the following test. List.retainAll()
863 // is an option, but it may still do some extra operations that we do not need here.
864 final int N = mChangedPackages.size();
865 for (int i = 0; i < N; ++i) {
866 final String packageName = mChangedPackages.get(i);
867 if (mKnownImePackageNames.contains(packageName)) {
868 return true;
869 }
870 }
871 return false;
872 }
873
874 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800875 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -0800876 if (!isChangingPackagesOfCurrentUserLocked()) {
877 return;
878 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -0800879 if (!shouldRebuildInputMethodListLocked()) {
880 return;
881 }
882
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800883 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900884 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800885 final int N = mMethodList.size();
886 if (curInputMethodId != null) {
887 for (int i=0; i<N; i++) {
888 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900889 final String imiId = imi.getId();
890 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800891 curIm = imi;
892 }
satoke7c6998e2011-06-03 17:57:59 +0900893
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800894 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900895 if (isPackageModified(imi.getPackageName())) {
896 mFileManager.deleteAllInputMethodSubtypes(imiId);
897 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800898 if (change == PACKAGE_TEMPORARY_CHANGE
899 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800900 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800901 + imi.getComponent());
902 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904 }
905 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906
Yohei Yukawa94e33302016-02-12 19:37:03 -0800907 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800910
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800911 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800912 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800913 if (change == PACKAGE_TEMPORARY_CHANGE
914 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800915 ServiceInfo si = null;
916 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900917 si = mIPackageManager.getServiceInfo(
918 curIm.getComponent(), 0, mSettings.getCurrentUserId());
919 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800920 }
921 if (si == null) {
922 // Uh oh, current input method is no longer around!
923 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800924 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900925 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800926 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800927 changed = true;
928 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800929 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900930 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800931 }
932 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800933 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800934 }
satokab751aa2010-09-14 19:17:36 +0900935
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800936 if (curIm == null) {
937 // We currently don't have a default input method... is
938 // one now available?
939 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700940 } else if (!changed && isPackageModified(curIm.getPackageName())) {
941 // Even if the current input method is still available, mCurrentSubtype could
942 // be obsolete when the package is modified in practice.
943 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800944 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800945
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800946 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800947 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949 }
950 }
951 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800952
Jeff Brownc28867a2013-03-26 15:42:39 -0700953 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900954 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700955 private final IInputMethod mMethod;
956 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800957
Jeff Brownc28867a2013-03-26 15:42:39 -0700958 MethodCallback(InputMethodManagerService imms, IInputMethod method,
959 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900960 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700961 mMethod = method;
962 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800964
satoke7c6998e2011-06-03 17:57:59 +0900965 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700966 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700967 long ident = Binder.clearCallingIdentity();
968 try {
969 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
970 } finally {
971 Binder.restoreCallingIdentity(ident);
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975
satok01038492012-04-09 21:08:27 +0900976 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700977 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900978 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700979 public void onHardKeyboardStatusChange(boolean available) {
980 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
981 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900982 }
983
Michael Wright7b5a96b2014-08-09 19:28:42 -0700984 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900985 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700986 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900987 }
988 synchronized(mMethodMap) {
989 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
990 && mSwitchingDialog.isShowing()) {
991 mSwitchingDialogTitleView.findViewById(
992 com.android.internal.R.id.hard_keyboard_section).setVisibility(
993 available ? View.VISIBLE : View.GONE);
994 }
995 }
996 }
997 }
998
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800999 public static final class Lifecycle extends SystemService {
1000 private InputMethodManagerService mService;
1001
1002 public Lifecycle(Context context) {
1003 super(context);
1004 mService = new InputMethodManagerService(context);
1005 }
1006
1007 @Override
1008 public void onStart() {
1009 LocalServices.addService(InputMethodManagerInternal.class,
1010 new LocalServiceImpl(mService.mHandler));
1011 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1012 }
1013
1014 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001015 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001016 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001017 // TODO: Dispatch this to a worker thread as needed.
1018 mService.onSwitchUser(userHandle);
1019 }
1020
1021 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001022 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001023 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001024 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001025 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1026 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1027 .getService(Context.STATUS_BAR_SERVICE);
1028 mService.systemRunning(statusBarService);
1029 }
1030 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001031
1032 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001033 public void onUnlockUser(final @UserIdInt int userHandle) {
1034 // Called on ActivityManager thread.
1035 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001036 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001037 }
1038 }
1039
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001040 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001041 synchronized(mMethodMap) {
1042 final int currentUserId = mSettings.getCurrentUserId();
1043 if (DEBUG) {
1044 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1045 }
1046 if (userId != currentUserId) {
1047 return;
1048 }
1049 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001050 if (mSystemReady) {
1051 // We need to rebuild IMEs.
1052 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1053 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1054 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001055 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001056 }
1057
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001058 void onSwitchUser(@UserIdInt int userId) {
1059 synchronized (mMethodMap) {
1060 switchUserLocked(userId);
1061 }
1062 }
1063
Seigo Nonaka7309b122015-08-17 18:34:13 -07001064 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001065 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001067 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001069 // Note: SettingsObserver doesn't register observers in its constructor.
1070 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 mIWindowManager = IWindowManager.Stub.asInterface(
1072 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001073 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001074 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001075 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 public void executeMessage(Message msg) {
1077 handleMessage(msg);
1078 }
Mita Yuned218c72012-12-06 17:18:25 -08001079 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001080 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001081 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001082 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001083 mHasFeature = context.getPackageManager().hasSystemFeature(
1084 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001085 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001086 mHardKeyboardBehavior = mContext.getResources().getInteger(
1087 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001088
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001089 Bundle extras = new Bundle();
1090 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
1091 mImeSwitcherNotification = new Notification.Builder(mContext)
1092 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1093 .setWhen(0)
1094 .setOngoing(true)
1095 .addExtras(extras)
1096 .setCategory(Notification.CATEGORY_SYSTEM)
1097 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001098
satok7cfc0ed2011-06-20 21:29:36 +09001099 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001100 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001101
1102 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001103
satok7cfc0ed2011-06-20 21:29:36 +09001104 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001105 int userId = 0;
1106 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001107 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001108 } catch (RemoteException e) {
1109 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1110 }
satok913a8922010-08-26 21:53:41 +09001111
satokd87c2592010-09-29 11:52:06 +09001112 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001113 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001114 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001115
Kenny Guy2a764942014-04-02 13:29:20 +01001116 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001117 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001118 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1119 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001120 }
1121
satok5b927c432012-05-01 20:09:34 +09001122 private void resetDefaultImeLocked(Context context) {
1123 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001124 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001125 return;
1126 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001127 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001128 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001129 if (suitableImes.isEmpty()) {
1130 Slog.i(TAG, "No default found");
1131 return;
satok5b927c432012-05-01 20:09:34 +09001132 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001133 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001134 if (DEBUG) {
1135 Slog.i(TAG, "Default found, using " + defIm.getId());
1136 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001137 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001138 }
1139
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001140 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001141 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1142 + " currentUserId=" + mSettings.getCurrentUserId());
1143
Yohei Yukawa81482972015-06-04 00:58:59 -07001144 // ContentObserver should be registered again when the user is changed
1145 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001146
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001147 // If the system is not ready or the device is not yed unlocked by the user, then we use
1148 // copy-on-write settings.
1149 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001150 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001151 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001152 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001153 // InputMethodFileManager should be reset when the user is changed
1154 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001155 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001156
1157 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1158 + " defaultImiId=" + defaultImiId);
1159
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001160 // For secondary users, the list of enabled IMEs may not have been updated since the
1161 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1162 // not be empty even if the IME has been uninstalled by the primary user.
1163 // Even in such cases, IMMS works fine because it will find the most applicable
1164 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001165 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001166 mLastSystemLocales = mRes.getConfiguration().getLocales();
1167
1168 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1169 if (mSystemReady) {
1170 hideCurrentInputLocked(0, null);
1171 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1172 buildInputMethodListLocked(initialUserSwitch);
1173 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1174 // This is the first time of the user switch and
1175 // set the current ime to the proper one.
1176 resetDefaultImeLocked(mContext);
1177 }
1178 updateFromSettingsLocked(true);
1179 try {
1180 startInputInnerLocked();
1181 } catch (RuntimeException e) {
1182 Slog.w(TAG, "Unexpected exception", e);
1183 }
1184 }
1185
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001186 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001187 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1188 mSettings.getEnabledInputMethodListLocked(), newUserId,
1189 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001190 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001191
1192 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1193 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001194 }
1195
Kenny Guy2a764942014-04-02 13:29:20 +01001196 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001197 mSettings.setCurrentProfileIds(
1198 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001199 }
1200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 @Override
1202 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1203 throws RemoteException {
1204 try {
1205 return super.onTransact(code, data, reply, flags);
1206 } catch (RuntimeException e) {
1207 // The input method manager only throws security exceptions, so let's
1208 // log all others.
1209 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001210 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212 throw e;
1213 }
1214 }
1215
Svetoslav Ganova0027152013-06-25 14:59:53 -07001216 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001217 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001218 if (DEBUG) {
1219 Slog.d(TAG, "--- systemReady");
1220 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001221 if (!mSystemReady) {
1222 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001223 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001224 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001225 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001226 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001227 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1228 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001229 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001230 if (mStatusBar != null) {
1231 mStatusBar.setIconVisibility(mSlotIme, false);
1232 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001233 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001234 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1235 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001236 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001237 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001238 mHardKeyboardListener);
1239 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001240
1241 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1242 mSettingsObserver.registerContentObserverLocked(currentUserId);
1243
1244 final IntentFilter broadcastFilter = new IntentFilter();
1245 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1246 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1247 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1248 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1249 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1250 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1251
1252 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1253 resetDefaultImeLocked(mContext);
1254 updateFromSettingsLocked(true);
1255 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1256 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1257 mContext.getBasePackageName());
1258
Dianne Hackborncc278702009-09-02 23:07:23 -07001259 try {
1260 startInputInnerLocked();
1261 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001262 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001263 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001264 }
1265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001267
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001268 // ---------------------------------------------------------------------------------------
1269 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1270 // 1) it comes from the system process
1271 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1272 private boolean calledFromValidUser() {
1273 final int uid = Binder.getCallingUid();
1274 final int userId = UserHandle.getUserId(uid);
1275 if (DEBUG) {
1276 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1277 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1278 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001279 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1280 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001281 }
Kenny Guy2a764942014-04-02 13:29:20 +01001282 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001283 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001284 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001285
1286 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1287 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1288 // must not manage background users' states in any functions.
1289 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1290 // by a token.
1291 if (mContext.checkCallingOrSelfPermission(
1292 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1293 == PackageManager.PERMISSION_GRANTED) {
1294 if (DEBUG) {
1295 Slog.d(TAG, "--- Access granted because the calling process has "
1296 + "the INTERACT_ACROSS_USERS_FULL permission");
1297 }
1298 return true;
1299 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001300 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001301 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1302 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001303 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001304 }
1305
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001306
1307 /**
1308 * Returns true iff the caller is identified to be the current input method with the token.
1309 * @param token The window token given to the input method when it was started.
1310 * @return true if and only if non-null valid token is specified.
1311 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001312 private boolean calledWithValidToken(@Nullable IBinder token) {
1313 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1314 if (DEBUG) {
1315 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1316 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1317 }
1318 return false;
1319 }
1320 if (token == null || token != mCurToken) {
1321 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1322 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1323 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001324 return false;
1325 }
1326 return true;
1327 }
1328
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001329 private boolean bindCurrentInputMethodService(
1330 Intent service, ServiceConnection conn, int flags) {
1331 if (service == null || conn == null) {
1332 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1333 return false;
1334 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001335 return mContext.bindServiceAsUser(service, conn, flags,
1336 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001337 }
1338
satoke7c6998e2011-06-03 17:57:59 +09001339 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001341 // TODO: Make this work even for non-current users?
1342 if (!calledFromValidUser()) {
1343 return Collections.emptyList();
1344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001346 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348 }
1349
satoke7c6998e2011-06-03 17:57:59 +09001350 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001352 // TODO: Make this work even for non-current users?
1353 if (!calledFromValidUser()) {
1354 return Collections.emptyList();
1355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001357 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 }
1360
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001361 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001362 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001363 * @return enabled subtypes of the specified imi
1364 */
satoke7c6998e2011-06-03 17:57:59 +09001365 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001366 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001367 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001368 // TODO: Make this work even for non-current users?
1369 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001370 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001371 }
satok67ddf9c2010-11-17 09:45:54 +09001372 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001373 final InputMethodInfo imi;
1374 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001375 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001376 } else {
1377 imi = mMethodMap.get(imiId);
1378 }
1379 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001380 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001381 }
1382 return mSettings.getEnabledInputMethodSubtypeListLocked(
1383 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001384 }
1385 }
1386
satoke7c6998e2011-06-03 17:57:59 +09001387 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 public void addClient(IInputMethodClient client,
1389 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001390 if (!calledFromValidUser()) {
1391 return;
1392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 synchronized (mMethodMap) {
1394 mClients.put(client.asBinder(), new ClientState(client,
1395 inputContext, uid, pid));
1396 }
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
satoke7c6998e2011-06-03 17:57:59 +09001399 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001401 if (!calledFromValidUser()) {
1402 return;
1403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001405 ClientState cs = mClients.remove(client.asBinder());
1406 if (cs != null) {
1407 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001408 if (mCurClient == cs) {
1409 mCurClient = null;
1410 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001411 if (mCurFocusedWindowClient == cs) {
1412 mCurFocusedWindowClient = null;
1413 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 void executeOrSendMessage(IInterface target, Message msg) {
1419 if (target.asBinder() instanceof Binder) {
1420 mCaller.sendMessage(msg);
1421 } else {
1422 handleMessage(msg);
1423 msg.recycle();
1424 }
1425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
Yohei Yukawa33e81792015-11-17 21:14:42 -08001427 void unbindCurrentClientLocked(
1428 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001430 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 + mCurClient.client.asBinder());
1432 if (mBoundToMethod) {
1433 mBoundToMethod = false;
1434 if (mCurMethod != null) {
1435 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1436 MSG_UNBIND_INPUT, mCurMethod));
1437 }
1438 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001439
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001440 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1441 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001442 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1443 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
The Android Open Source Project10592532009-03-18 17:39:46 -07001447 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 }
1449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 private int getImeShowFlags() {
1452 int flags = 0;
1453 if (mShowForced) {
1454 flags |= InputMethod.SHOW_FORCED
1455 | InputMethod.SHOW_EXPLICIT;
1456 } else if (mShowExplicitlyRequested) {
1457 flags |= InputMethod.SHOW_EXPLICIT;
1458 }
1459 return flags;
1460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 private int getAppShowFlags() {
1463 int flags = 0;
1464 if (mShowForced) {
1465 flags |= InputMethodManager.SHOW_FORCED;
1466 } else if (!mShowExplicitlyRequested) {
1467 flags |= InputMethodManager.SHOW_IMPLICIT;
1468 }
1469 return flags;
1470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001472 InputBindResult attachNewInputLocked(
1473 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 if (!mBoundToMethod) {
1475 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1476 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1477 mBoundToMethod = true;
1478 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001479
1480 final Binder startInputToken = new Binder();
1481 final StartInputInfo info = new StartInputInfo(mCurToken, mCurFocusedWindow);
1482 mStartInputMap.put(startInputToken, info);
1483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001485 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001486 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001487 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001489 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001490 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001492 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001493 (session.channel != null ? session.channel.dup() : null),
1494 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001497 InputBindResult startInputLocked(
1498 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001499 IInputMethodClient client, IInputContext inputContext,
1500 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001501 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 // If no method is currently selected, do nothing.
1503 if (mCurMethodId == null) {
1504 return mNoBinding;
1505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 ClientState cs = mClients.get(client.asBinder());
1508 if (cs == null) {
1509 throw new IllegalArgumentException("unknown client "
1510 + client.asBinder());
1511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001512
Yohei Yukawa74750f22016-03-22 12:54:22 -07001513 if (attribute == null) {
1514 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1515 + " uid=" + cs.uid + " pid=" + cs.pid);
1516 return null;
1517 }
1518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 try {
1520 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1521 // Check with the window manager to make sure this client actually
1522 // has a window with focus. If not, reject. This is thread safe
1523 // because if the focus changes some time before or after, the
1524 // next client receiving focus that has any interest in input will
1525 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001526 if (DEBUG) {
1527 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1528 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 return null;
1531 }
1532 } catch (RemoteException e) {
1533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001534
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001535 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001536 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001537 }
1538
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001539 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001540 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001541 @NonNull EditorInfo attribute, int controlFlags,
1542 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001543 // If no method is currently selected, do nothing.
1544 if (mCurMethodId == null) {
1545 return mNoBinding;
1546 }
1547
Yohei Yukawad57ba672015-06-08 16:39:46 -07001548 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1549 attribute.packageName)) {
1550 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1551 + " uid=" + cs.uid + " package=" + attribute.packageName);
1552 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001553 }
1554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001556 // Was the keyguard locked when switching over to the new client?
1557 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 // If the client is changing, we need to switch over to the new
1559 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001560 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001561 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001562 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563
1564 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001565 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001566 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001567 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 // Bump up the sequence for this client and attach it.
1572 mCurSeq++;
1573 if (mCurSeq <= 0) mCurSeq = 1;
1574 mCurClient = cs;
1575 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001576 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 // Check if the input method is changing.
1580 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1581 if (cs.curSession != null) {
1582 // Fast case: if we are already connected to the input method,
1583 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001584 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001585 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 }
1587 if (mHaveConnection) {
1588 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 // Return to client, and we will get back with it when
1590 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001591 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001592 return new InputBindResult(null, null, mCurId, mCurSeq,
1593 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 } else if (SystemClock.uptimeMillis()
1595 < (mLastBindTime+TIME_TO_RECONNECT)) {
1596 // In this case we have connected to the service, but
1597 // don't yet have its interface. If it hasn't been too
1598 // long since we did the connection, we'll return to
1599 // the client and wait to get the service interface so
1600 // we can report back. If it has been too long, we want
1601 // to fall through so we can try a disconnect/reconnect
1602 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001603 return new InputBindResult(null, null, mCurId, mCurSeq,
1604 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001606 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1607 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 }
1609 }
1610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001611
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001612 return startInputInnerLocked();
1613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001615 InputBindResult startInputInnerLocked() {
1616 if (mCurMethodId == null) {
1617 return mNoBinding;
1618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001619
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001620 if (!mSystemReady) {
1621 // If the system is not yet ready, we shouldn't be running third
1622 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001623 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1624 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001625 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1628 if (info == null) {
1629 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001632 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1635 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001636 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1637 com.android.internal.R.string.input_method_binding_label);
1638 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1639 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001640 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001641 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1642 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 mLastBindTime = SystemClock.uptimeMillis();
1644 mHaveConnection = true;
1645 mCurId = info.getId();
1646 mCurToken = new Binder();
1647 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001648 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001649 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 } catch (RemoteException e) {
1651 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001652 return new InputBindResult(null, null, mCurId, mCurSeq,
1653 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 } else {
1655 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001656 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 + mCurIntent);
1658 }
1659 return null;
1660 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001661
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001662 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001663 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001664 IInputMethodClient client, IInputContext inputContext,
1665 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001666 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001667 if (!calledFromValidUser()) {
1668 return null;
1669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001671 if (DEBUG) {
1672 Slog.v(TAG, "startInput: reason="
1673 + InputMethodClient.getStartInputReason(startInputReason)
1674 + " client = " + client.asBinder()
1675 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001676 + " missingMethods="
1677 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001678 + " attribute=" + attribute
1679 + " controlFlags=#" + Integer.toHexString(controlFlags));
1680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 final long ident = Binder.clearCallingIdentity();
1682 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001683 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1684 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 } finally {
1686 Binder.restoreCallingIdentity(ident);
1687 }
1688 }
1689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001690
satoke7c6998e2011-06-03 17:57:59 +09001691 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 public void finishInput(IInputMethodClient client) {
1693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001694
satoke7c6998e2011-06-03 17:57:59 +09001695 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 public void onServiceConnected(ComponentName name, IBinder service) {
1697 synchronized (mMethodMap) {
1698 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1699 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001700 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001701 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001702 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001703 return;
1704 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001705 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001706 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1707 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001709 clearClientSessionLocked(mCurClient);
1710 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
1712 }
1713 }
1714 }
1715
Jeff Brownc28867a2013-03-26 15:42:39 -07001716 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1717 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 synchronized (mMethodMap) {
1719 if (mCurMethod != null && method != null
1720 && mCurMethod.asBinder() == method.asBinder()) {
1721 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001722 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001724 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001725 InputBindResult res = attachNewInputLocked(
1726 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 if (res.method != null) {
1728 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001729 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001731 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 }
1733 }
1734 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001735
1736 // Session abandoned. Close its associated input channel.
1737 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001740 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001741 if (mVisibleBound) {
1742 mContext.unbindService(mVisibleConnection);
1743 mVisibleBound = false;
1744 }
1745
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001746 if (mHaveConnection) {
1747 mContext.unbindService(this);
1748 mHaveConnection = false;
1749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001750
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001751 if (mCurToken != null) {
1752 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001753 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001754 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001755 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001756 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001757 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001758 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001759 } catch (RemoteException e) {
1760 }
1761 mCurToken = null;
1762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001763
The Android Open Source Project10592532009-03-18 17:39:46 -07001764 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001765 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001767
Yohei Yukawa33e81792015-11-17 21:14:42 -08001768 void resetCurrentMethodAndClient(
1769 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001770 mCurMethodId = null;
1771 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001772 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001773 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001774
1775 void requestClientSessionLocked(ClientState cs) {
1776 if (!cs.sessionRequested) {
1777 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1778 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1779 cs.sessionRequested = true;
1780 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1781 MSG_CREATE_SESSION, mCurMethod, channels[1],
1782 new MethodCallback(this, mCurMethod, channels[0])));
1783 }
1784 }
1785
1786 void clearClientSessionLocked(ClientState cs) {
1787 finishSessionLocked(cs.curSession);
1788 cs.curSession = null;
1789 cs.sessionRequested = false;
1790 }
1791
1792 private void finishSessionLocked(SessionState sessionState) {
1793 if (sessionState != null) {
1794 if (sessionState.session != null) {
1795 try {
1796 sessionState.session.finishSession();
1797 } catch (RemoteException e) {
1798 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001799 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001800 }
1801 sessionState.session = null;
1802 }
1803 if (sessionState.channel != null) {
1804 sessionState.channel.dispose();
1805 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001806 }
1807 }
1808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001809
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001810 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 if (mCurMethod != null) {
1812 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001813 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001815
Jeff Brownc28867a2013-03-26 15:42:39 -07001816 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001817 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 mCurMethod = null;
1819 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001820 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001821 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001822 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001823 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001825
satoke7c6998e2011-06-03 17:57:59 +09001826 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001828 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1829 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1830 // which is irregular but can eventually happen for everyone just by continuing using the
1831 // device. Thus it is important to make sure that all the internal states are properly
1832 // refreshed when this method is called back. Running
1833 // adb install -r <APK that implements the current IME>
1834 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001836 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 + " mCurIntent=" + mCurIntent);
1838 if (mCurMethod != null && mCurIntent != null
1839 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001840 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 // We consider this to be a new bind attempt, since the system
1842 // should now try to restart the service for us.
1843 mLastBindTime = SystemClock.uptimeMillis();
1844 mShowRequested = mInputShown;
1845 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001846 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
1848 }
1849 }
1850
satokf9f01002011-05-19 21:31:50 +09001851 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001853 synchronized (mMethodMap) {
1854 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001855 return;
1856 }
1857 final long ident = Binder.clearCallingIdentity();
1858 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001860 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001861 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001862 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001865 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001866 CharSequence contentDescription = null;
1867 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001868 // Use PackageManager to load label
1869 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001870 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001871 mIPackageManager.getApplicationInfo(packageName, 0,
1872 mSettings.getCurrentUserId()));
1873 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001874 /* ignore */
1875 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001876 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001877 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001878 contentDescription != null
1879 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001880 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001883 } finally {
1884 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
1887 }
1888
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001889 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001890 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001891 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001892 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001893 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001894 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001895 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1896 // When physical keyboard is attached, we show the ime switcher (or notification if
1897 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1898 // exists in the IME switcher dialog. Might be OK to remove this condition once
1899 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1900 return true;
1901 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001902 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1903 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001904 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001905
1906 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1907 final int N = imis.size();
1908 if (N > 2) return true;
1909 if (N < 1) return false;
1910 int nonAuxCount = 0;
1911 int auxCount = 0;
1912 InputMethodSubtype nonAuxSubtype = null;
1913 InputMethodSubtype auxSubtype = null;
1914 for(int i = 0; i < N; ++i) {
1915 final InputMethodInfo imi = imis.get(i);
1916 final List<InputMethodSubtype> subtypes =
1917 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1918 final int subtypeCount = subtypes.size();
1919 if (subtypeCount == 0) {
1920 ++nonAuxCount;
1921 } else {
1922 for (int j = 0; j < subtypeCount; ++j) {
1923 final InputMethodSubtype subtype = subtypes.get(j);
1924 if (!subtype.isAuxiliary()) {
1925 ++nonAuxCount;
1926 nonAuxSubtype = subtype;
1927 } else {
1928 ++auxCount;
1929 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001930 }
1931 }
satok7cfc0ed2011-06-20 21:29:36 +09001932 }
1933 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001934 if (nonAuxCount > 1 || auxCount > 1) {
1935 return true;
1936 } else if (nonAuxCount == 1 && auxCount == 1) {
1937 if (nonAuxSubtype != null && auxSubtype != null
1938 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1939 || auxSubtype.overridesImplicitlyEnabledSubtype()
1940 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1941 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1942 return false;
1943 }
1944 return true;
1945 }
1946 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001947 }
1948
John Spurlocke0980502013-10-25 11:59:29 -04001949 private boolean isKeyguardLocked() {
1950 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1951 }
1952
satokdbf29502011-08-25 15:28:23 +09001953 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001954 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001955 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
1956 int backDisposition) {
1957 if (startInputToken == null) {
1958 throw new InvalidParameterException("startInputToken cannot be null");
1959 }
1960
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001961 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001962 return;
1963 }
1964
Yohei Yukawa69e68022017-02-13 12:04:50 -08001965 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001966 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08001967 info = mStartInputMap.get(startInputToken);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001968 if (info == null) {
1969 throw new InvalidParameterException("Unknown startInputToken=" + startInputToken);
1970 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001971 mImeWindowVis = vis;
1972 mBackDisposition = backDisposition;
1973 updateSystemUiLocked(token, vis, backDisposition);
1974 }
Yohei Yukawa69e68022017-02-13 12:04:50 -08001975 mWindowManagerInternal.updateInputMethodWindowStatus(info.mImeToken,
1976 (vis & InputMethodService.IME_VISIBLE) != 0, info.mTargetWindow);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001977 }
1978
1979 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1980 synchronized (mMethodMap) {
1981 updateSystemUiLocked(token, vis, backDisposition);
1982 }
1983 }
1984
1985 // Caution! This method is called in this class. Handle multi-user carefully
1986 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1987 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001988 return;
1989 }
1990
1991 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1992 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001993 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001994 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001995 // apply policy for binder calls
1996 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1997 vis = 0;
satok06487a52010-10-29 11:37:18 +09001998 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001999 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2000 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2001 if (mStatusBar != null) {
2002 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2003 needsToShowImeSwitcher);
2004 }
2005 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2006 if (imi != null && needsToShowImeSwitcher) {
2007 // Used to load label
2008 final CharSequence title = mRes.getText(
2009 com.android.internal.R.string.select_input_method);
2010 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2011 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002012 mImeSwitcherNotification.setContentTitle(title)
2013 .setContentText(summary)
2014 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002015 try {
2016 if ((mNotificationManager != null)
2017 && !mIWindowManager.hasNavigationBar()) {
2018 if (DEBUG) {
2019 Slog.d(TAG, "--- show notification: label = " + summary);
2020 }
2021 mNotificationManager.notifyAsUser(null,
2022 com.android.internal.R.string.select_input_method,
2023 mImeSwitcherNotification.build(), UserHandle.ALL);
2024 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002025 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002026 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002027 }
2028 } else {
2029 if (mNotificationShown && mNotificationManager != null) {
2030 if (DEBUG) {
2031 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002032 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002033 mNotificationManager.cancelAsUser(null,
2034 com.android.internal.R.string.select_input_method, UserHandle.ALL);
2035 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002036 }
satok06487a52010-10-29 11:37:18 +09002037 }
2038 } finally {
2039 Binder.restoreCallingIdentity(ident);
2040 }
2041 }
2042
satoke7c6998e2011-06-03 17:57:59 +09002043 @Override
satokf9f01002011-05-19 21:31:50 +09002044 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002045 if (!calledFromValidUser()) {
2046 return;
2047 }
satokf9f01002011-05-19 21:31:50 +09002048 synchronized (mMethodMap) {
2049 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2050 for (int i = 0; i < spans.length; ++i) {
2051 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002052 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002053 mSecureSuggestionSpans.put(ss, currentImi);
2054 }
2055 }
2056 }
2057 }
2058
satoke7c6998e2011-06-03 17:57:59 +09002059 @Override
satokf9f01002011-05-19 21:31:50 +09002060 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002061 if (!calledFromValidUser()) {
2062 return false;
2063 }
satokf9f01002011-05-19 21:31:50 +09002064 synchronized (mMethodMap) {
2065 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2066 // TODO: Do not send the intent if the process of the targetImi is already dead.
2067 if (targetImi != null) {
2068 final String[] suggestions = span.getSuggestions();
2069 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002070 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002071 final Intent intent = new Intent();
2072 // Ensures that only a class in the original IME package will receive the
2073 // notification.
satok42c5a162011-05-26 16:46:14 +09002074 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002075 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2076 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2077 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2078 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002079 final long ident = Binder.clearCallingIdentity();
2080 try {
2081 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2082 } finally {
2083 Binder.restoreCallingIdentity(ident);
2084 }
satokf9f01002011-05-19 21:31:50 +09002085 return true;
2086 }
2087 }
2088 return false;
2089 }
2090
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002091 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002092 updateInputMethodsFromSettingsLocked(enabledMayChange);
2093 updateKeyboardFromSettingsLocked();
2094 }
2095
2096 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002097 if (enabledMayChange) {
2098 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2099 for (int i=0; i<enabled.size(); i++) {
2100 // We allow the user to select "disabled until used" apps, so if they
2101 // are enabling one of those here we now need to make it enabled.
2102 InputMethodInfo imm = enabled.get(i);
2103 try {
2104 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2105 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2106 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002107 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002108 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002109 if (DEBUG) {
2110 Slog.d(TAG, "Update state(" + imm.getId()
2111 + "): DISABLED_UNTIL_USED -> DEFAULT");
2112 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002113 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2114 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002115 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2116 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002117 }
2118 } catch (RemoteException e) {
2119 }
2120 }
2121 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002122 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2123 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2124 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2125 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002126 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002127 // There is no input method selected, try to choose new applicable input method.
2128 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002129 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002130 }
2131 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002133 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002135 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002136 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 }
satokf3db1af2010-11-23 13:34:33 +09002138 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002139 } else {
2140 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002141 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002143 // Here is not the perfect place to reset the switching controller. Ideally
2144 // mSwitchingController and mSettings should be able to share the same state.
2145 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2146 // the same enabled IMEs list.
2147 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002148
2149 }
2150
2151 public void updateKeyboardFromSettingsLocked() {
2152 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2153 if (mSwitchingDialog != null
2154 && mSwitchingDialogTitleView != null
2155 && mSwitchingDialog.isShowing()) {
2156 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2157 com.android.internal.R.id.hard_keyboard_switch);
2158 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002161
Yohei Yukawab097b822015-12-01 10:43:08 -08002162 private void notifyInputMethodSubtypeChanged(final int userId,
2163 @Nullable final InputMethodInfo inputMethodInfo,
2164 @Nullable final InputMethodSubtype subtype) {
2165 final InputManagerInternal inputManagerInternal =
2166 LocalServices.getService(InputManagerInternal.class);
2167 if (inputManagerInternal != null) {
2168 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2169 }
2170 }
2171
satokab751aa2010-09-14 19:17:36 +09002172 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 InputMethodInfo info = mMethodMap.get(id);
2174 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002175 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002177
satokd81e9502012-05-21 12:58:45 +09002178 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002180 final int subtypeCount = info.getSubtypeCount();
2181 if (subtypeCount <= 0) {
2182 return;
satokcd7cd292010-11-20 15:46:23 +09002183 }
satokd81e9502012-05-21 12:58:45 +09002184 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2185 final InputMethodSubtype newSubtype;
2186 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2187 newSubtype = info.getSubtypeAt(subtypeId);
2188 } else {
2189 // If subtype is null, try to find the most applicable one from
2190 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002191 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002192 }
2193 if (newSubtype == null || oldSubtype == null) {
2194 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2195 + ", new subtype = " + newSubtype);
2196 return;
2197 }
2198 if (newSubtype != oldSubtype) {
2199 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2200 if (mCurMethod != null) {
2201 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002202 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002203 mCurMethod.changeInputMethodSubtype(newSubtype);
2204 } catch (RemoteException e) {
2205 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002206 return;
satokab751aa2010-09-14 19:17:36 +09002207 }
2208 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002209 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 return;
2212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002213
satokd81e9502012-05-21 12:58:45 +09002214 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 final long ident = Binder.clearCallingIdentity();
2216 try {
satokab751aa2010-09-14 19:17:36 +09002217 // Set a subtype to this input method.
2218 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002219 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2220 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2221 // because mCurMethodId is stored as a history in
2222 // setSelectedInputMethodAndSubtypeLocked().
2223 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002225 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002227 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002229 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002231 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 } finally {
2233 Binder.restoreCallingIdentity(ident);
2234 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002235
2236 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2237 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002239
satok42c5a162011-05-26 16:46:14 +09002240 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002241 public boolean showSoftInput(IInputMethodClient client, int flags,
2242 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002243 if (!calledFromValidUser()) {
2244 return false;
2245 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002246 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 long ident = Binder.clearCallingIdentity();
2248 try {
2249 synchronized (mMethodMap) {
2250 if (mCurClient == null || client == null
2251 || mCurClient.client.asBinder() != client.asBinder()) {
2252 try {
2253 // We need to check if this is the current client with
2254 // focus in the window manager, to allow this call to
2255 // be made before input is started in it.
2256 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002257 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002258 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 }
2260 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002261 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 }
2263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002264
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002265 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002266 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
2268 } finally {
2269 Binder.restoreCallingIdentity(ident);
2270 }
2271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002272
The Android Open Source Project4df24232009-03-05 14:34:35 -08002273 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002275 if (mAccessibilityRequestingNoSoftKeyboard) {
2276 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 }
Anna Galusza9b278112016-01-04 11:37:37 -08002278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2280 mShowExplicitlyRequested = true;
2281 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002282 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2283 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002285
Dianne Hackborncc278702009-09-02 23:07:23 -07002286 if (!mSystemReady) {
2287 return false;
2288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
The Android Open Source Project4df24232009-03-05 14:34:35 -08002290 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002292 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002293 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2294 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2295 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002297 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002298 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002299 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002300 | Context.BIND_TREAT_LIKE_ACTIVITY
2301 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002302 mVisibleBound = true;
2303 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002304 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002306 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 // The client has asked to have the input method shown, but
2308 // we have been sitting here too long with a connection to the
2309 // service and no interface received, so let's disconnect/connect
2310 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002311 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002313 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002315 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002316 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002317 } else {
2318 if (DEBUG) {
2319 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2320 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002323
The Android Open Source Project4df24232009-03-05 14:34:35 -08002324 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002326
satok42c5a162011-05-26 16:46:14 +09002327 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002328 public boolean hideSoftInput(IInputMethodClient client, int flags,
2329 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002330 if (!calledFromValidUser()) {
2331 return false;
2332 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002333 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 long ident = Binder.clearCallingIdentity();
2335 try {
2336 synchronized (mMethodMap) {
2337 if (mCurClient == null || client == null
2338 || mCurClient.client.asBinder() != client.asBinder()) {
2339 try {
2340 // We need to check if this is the current client with
2341 // focus in the window manager, to allow this call to
2342 // be made before input is started in it.
2343 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002344 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2345 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002346 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 }
2348 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002349 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
2351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002352
Joe Onorato8a9b2202010-02-26 18:56:32 -08002353 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002354 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
2356 } finally {
2357 Binder.restoreCallingIdentity(ident);
2358 }
2359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002360
The Android Open Source Project4df24232009-03-05 14:34:35 -08002361 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2363 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002364 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 -08002365 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 }
2367 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002368 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 -08002369 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002371
2372 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2373 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2374 // to be updated with the new value sent from IME process. Even in such a transient state
2375 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2376 // application process as a valid request, and have even promised such a behavior with CTS
2377 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2378 // IMMS#InputShown indicates that the software keyboard is shown.
2379 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2380 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2381 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002382 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002383 if (shouldHideSoftInput) {
2384 // The IME will report its visible state again after the following message finally
2385 // delivered to the IME process as an IPC. Hence the inconsistency between
2386 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2387 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002388 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2389 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2390 res = true;
2391 } else {
2392 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002394 if (mHaveConnection && mVisibleBound) {
2395 mContext.unbindService(mVisibleConnection);
2396 mVisibleBound = false;
2397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 mInputShown = false;
2399 mShowRequested = false;
2400 mShowExplicitlyRequested = false;
2401 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002402 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002404
satok42c5a162011-05-26 16:46:14 +09002405 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002406 public InputBindResult startInputOrWindowGainedFocus(
2407 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2408 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002409 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002410 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002411 if (windowToken != null) {
2412 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002413 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002414 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002415 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2416 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002417 }
2418 }
2419
2420 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002421 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002422 IInputMethodClient client, IBinder windowToken, int controlFlags,
2423 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002424 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2425 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002426 // Needs to check the validity before clearing calling identity
2427 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002428 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 long ident = Binder.clearCallingIdentity();
2430 try {
2431 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002432 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2433 + InputMethodClient.getStartInputReason(startInputReason)
2434 + " client=" + client.asBinder()
2435 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002436 + " missingMethods="
2437 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002438 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002439 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002440 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002441 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002442
Dianne Hackborn7663d802012-02-24 13:08:49 -08002443 ClientState cs = mClients.get(client.asBinder());
2444 if (cs == null) {
2445 throw new IllegalArgumentException("unknown client "
2446 + client.asBinder());
2447 }
2448
2449 try {
2450 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2451 // Check with the window manager to make sure this client actually
2452 // has a window with focus. If not, reject. This is thread safe
2453 // because if the focus changes some time before or after, the
2454 // next client receiving focus that has any interest in input will
2455 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002456 if (DEBUG) {
2457 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2458 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2459 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002460 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002462 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002464
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002465 if (!calledFromValidUser) {
2466 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2467 Slog.w(TAG, "If you want to interect with IME, you need "
2468 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2469 hideCurrentInputLocked(0, null);
2470 return null;
2471 }
2472
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002473 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002474 if (DEBUG) {
2475 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2476 + " attribute=" + attribute + ", token = " + windowToken);
2477 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002478 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002479 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002480 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002481 }
2482 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002483 }
2484 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002485 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002486 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002487
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002488 // Should we auto-show the IME even if the caller has not
2489 // specified what should be done with it?
2490 // We only do this automatically if the window can resize
2491 // to accommodate the IME (so what the user sees will give
2492 // them good context without input information being obscured
2493 // by the IME) or if running on a large screen where there
2494 // is more room for the target window + IME.
2495 final boolean doAutoShow =
2496 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2497 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2498 || mRes.getConfiguration().isLayoutSizeAtLeast(
2499 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002500 final boolean isTextEditor =
2501 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2502
2503 // We want to start input before showing the IME, but after closing
2504 // it. We want to do this after closing it to help the IME disappear
2505 // more quickly (not get stuck behind it initializing itself for the
2506 // new focused input, even if its window wants to hide the IME).
2507 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2510 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002511 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2513 // There is no focus view, and this window will
2514 // be behind any soft input window, so hide the
2515 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002516 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002517 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002519 } else if (isTextEditor && doAutoShow && (softInputMode &
2520 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 // There is a focus view, and we are navigating forward
2522 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002523 // We only do this automatically if the window can resize
2524 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002525 // them good context without input information being obscured
2526 // by the IME) or if running on a large screen where there
2527 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002528 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002529 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002530 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002531 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002532 didStart = true;
2533 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002534 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 }
2536 break;
2537 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2538 // Do nothing.
2539 break;
2540 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2541 if ((softInputMode &
2542 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002543 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002544 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 }
2546 break;
2547 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002548 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002549 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 break;
2551 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2552 if ((softInputMode &
2553 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002554 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002555 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002556 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002557 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002558 didStart = true;
2559 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002560 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 }
2562 break;
2563 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002564 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002565 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002566 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002567 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002568 didStart = true;
2569 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002570 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 break;
2572 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002573
2574 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002575 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002576 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579 } finally {
2580 Binder.restoreCallingIdentity(ident);
2581 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002582
2583 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002585
satok42c5a162011-05-26 16:46:14 +09002586 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002587 public void showInputMethodPickerFromClient(
2588 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002589 if (!calledFromValidUser()) {
2590 return;
2591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 synchronized (mMethodMap) {
2593 if (mCurClient == null || client == null
2594 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002595 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002596 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 }
2598
satok440aab52010-11-25 09:43:11 +09002599 // Always call subtype picker, because subtype picker is a superset of input method
2600 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002601 mHandler.sendMessage(mCaller.obtainMessageI(
2602 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002603 }
2604 }
2605
satok42c5a162011-05-26 16:46:14 +09002606 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002608 if (!calledFromValidUser()) {
2609 return;
2610 }
satok28203512010-11-24 11:06:49 +09002611 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2612 }
2613
satok42c5a162011-05-26 16:46:14 +09002614 @Override
satok28203512010-11-24 11:06:49 +09002615 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002616 if (!calledFromValidUser()) {
2617 return;
2618 }
satok28203512010-11-24 11:06:49 +09002619 synchronized (mMethodMap) {
2620 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002621 setInputMethodWithSubtypeIdLocked(token, id,
2622 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2623 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002624 } else {
2625 setInputMethod(token, id);
2626 }
2627 }
satokab751aa2010-09-14 19:17:36 +09002628 }
2629
satok42c5a162011-05-26 16:46:14 +09002630 @Override
satokb416a712010-11-25 20:42:14 +09002631 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002632 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002633 if (!calledFromValidUser()) {
2634 return;
2635 }
satokb416a712010-11-25 20:42:14 +09002636 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002637 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2638 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002639 }
2640 }
2641
satok4fc87d62011-05-20 16:13:43 +09002642 @Override
satok735cf382010-11-11 20:40:09 +09002643 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002644 if (!calledFromValidUser()) {
2645 return false;
2646 }
satok735cf382010-11-11 20:40:09 +09002647 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002648 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002649 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002650 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002651 lastImi = mMethodMap.get(lastIme.first);
2652 } else {
2653 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002654 }
satok4fc87d62011-05-20 16:13:43 +09002655 String targetLastImiId = null;
2656 int subtypeId = NOT_A_SUBTYPE_ID;
2657 if (lastIme != null && lastImi != null) {
2658 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002659 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002660 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2661 : mCurrentSubtype.hashCode();
2662 // If the last IME is the same as the current IME and the last subtype is not
2663 // defined, there is no need to switch to the last IME.
2664 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2665 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002666 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002667 }
2668 }
2669
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002670 if (TextUtils.isEmpty(targetLastImiId)
2671 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002672 // This is a safety net. If the currentSubtype can't be added to the history
2673 // and the framework couldn't find the last ime, we will make the last ime be
2674 // the most applicable enabled keyboard subtype of the system imes.
2675 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2676 if (enabled != null) {
2677 final int N = enabled.size();
2678 final String locale = mCurrentSubtype == null
2679 ? mRes.getConfiguration().locale.toString()
2680 : mCurrentSubtype.getLocale();
2681 for (int i = 0; i < N; ++i) {
2682 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002683 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002684 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002685 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2686 InputMethodUtils.getSubtypes(imi),
2687 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002688 if (keyboardSubtype != null) {
2689 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002690 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002691 imi, keyboardSubtype.hashCode());
2692 if(keyboardSubtype.getLocale().equals(locale)) {
2693 break;
2694 }
2695 }
2696 }
2697 }
2698 }
2699 }
2700
2701 if (!TextUtils.isEmpty(targetLastImiId)) {
2702 if (DEBUG) {
2703 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2704 + ", from: " + mCurMethodId + ", " + subtypeId);
2705 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002706 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002707 return true;
2708 } else {
2709 return false;
2710 }
satok735cf382010-11-11 20:40:09 +09002711 }
2712 }
2713
satoke7c6998e2011-06-03 17:57:59 +09002714 @Override
satok688bd472012-02-09 20:09:17 +09002715 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002716 if (!calledFromValidUser()) {
2717 return false;
2718 }
satok688bd472012-02-09 20:09:17 +09002719 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002720 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002721 return false;
2722 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002723 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002724 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2725 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002726 if (nextSubtype == null) {
2727 return false;
2728 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002729 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2730 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002731 return true;
2732 }
2733 }
2734
2735 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002736 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2737 if (!calledFromValidUser()) {
2738 return false;
2739 }
2740 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002741 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002742 return false;
2743 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002744 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002745 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2746 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002747 if (nextSubtype == null) {
2748 return false;
2749 }
2750 return true;
2751 }
2752 }
2753
2754 @Override
satok68f1b782011-04-11 14:26:04 +09002755 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002756 if (!calledFromValidUser()) {
2757 return null;
2758 }
satok68f1b782011-04-11 14:26:04 +09002759 synchronized (mMethodMap) {
2760 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2761 // TODO: Handle the case of the last IME with no subtypes
2762 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2763 || TextUtils.isEmpty(lastIme.second)) return null;
2764 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2765 if (lastImi == null) return null;
2766 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002767 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002768 final int lastSubtypeId =
2769 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002770 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2771 return null;
2772 }
2773 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002774 } catch (NumberFormatException e) {
2775 return null;
2776 }
2777 }
2778 }
2779
satoke7c6998e2011-06-03 17:57:59 +09002780 @Override
satokee5e77c2011-09-02 18:50:15 +09002781 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002782 if (!calledFromValidUser()) {
2783 return;
2784 }
satok91e88122011-07-18 11:11:42 +09002785 // By this IPC call, only a process which shares the same uid with the IME can add
2786 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002787 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002788 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002789 if (!mSystemReady) {
2790 return;
2791 }
satok91e88122011-07-18 11:11:42 +09002792 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002793 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002794 final String[] packageInfos;
2795 try {
2796 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2797 } catch (RemoteException e) {
2798 Slog.e(TAG, "Failed to get package infos");
2799 return;
2800 }
satok91e88122011-07-18 11:11:42 +09002801 if (packageInfos != null) {
2802 final int packageNum = packageInfos.length;
2803 for (int i = 0; i < packageNum; ++i) {
2804 if (packageInfos[i].equals(imi.getPackageName())) {
2805 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002806 final long ident = Binder.clearCallingIdentity();
2807 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002808 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002809 } finally {
2810 Binder.restoreCallingIdentity(ident);
2811 }
satokee5e77c2011-09-02 18:50:15 +09002812 return;
satok91e88122011-07-18 11:11:42 +09002813 }
2814 }
2815 }
satoke7c6998e2011-06-03 17:57:59 +09002816 }
satokee5e77c2011-09-02 18:50:15 +09002817 return;
satoke7c6998e2011-06-03 17:57:59 +09002818 }
2819
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002820 @Override
2821 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002822 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002823 }
2824
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002825 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002826 public void clearLastInputMethodWindowForTransition(IBinder token) {
2827 if (!calledFromValidUser()) {
2828 return;
2829 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002830 synchronized (mMethodMap) {
2831 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002832 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002833 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002834 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002835 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002836 }
2837
2838 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002839 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002840 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002841 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002842 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002843 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002844 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2845 if (DEBUG) {
2846 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2847 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2848 + " actual: " + sequenceNumber);
2849 }
2850 return;
2851 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002852 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2853 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002854 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002855 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002856 }
2857 }
2858
satok28203512010-11-24 11:06:49 +09002859 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002861 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2862 }
2863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002865 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2866 if (token == null) {
2867 if (mContext.checkCallingOrSelfPermission(
2868 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2869 != PackageManager.PERMISSION_GRANTED) {
2870 throw new SecurityException(
2871 "Using null token requires permission "
2872 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002874 } else if (mCurToken != token) {
2875 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2876 + " token: " + token);
2877 return;
2878 }
2879
2880 final long ident = Binder.clearCallingIdentity();
2881 try {
2882 setInputMethodLocked(id, subtypeId);
2883 } finally {
2884 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 }
2886 }
2887
satok42c5a162011-05-26 16:46:14 +09002888 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002890 if (!calledFromValidUser()) {
2891 return;
2892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002894 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 return;
2896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 long ident = Binder.clearCallingIdentity();
2898 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002899 hideCurrentInputLocked(flags, null);
2900 } finally {
2901 Binder.restoreCallingIdentity(ident);
2902 }
2903 }
2904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002905
satok42c5a162011-05-26 16:46:14 +09002906 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002907 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002908 if (!calledFromValidUser()) {
2909 return;
2910 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002911 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002912 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002913 return;
2914 }
2915 long ident = Binder.clearCallingIdentity();
2916 try {
2917 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 } finally {
2919 Binder.restoreCallingIdentity(ident);
2920 }
2921 }
2922 }
2923
2924 void setEnabledSessionInMainThread(SessionState session) {
2925 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002926 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002928 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002929 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 } catch (RemoteException e) {
2931 }
2932 }
2933 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002934 if (mEnabledSession != null && mEnabledSession.session != null) {
2935 try {
2936 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2937 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2938 } catch (RemoteException e) {
2939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
2941 }
2942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002943
satok42c5a162011-05-26 16:46:14 +09002944 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002946 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002948 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002949 final boolean showAuxSubtypes;
2950 switch (msg.arg1) {
2951 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2952 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2953 // implemented so that auxiliary subtypes will be excluded when the soft
2954 // keyboard is invisible.
2955 showAuxSubtypes = mInputShown;
2956 break;
2957 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2958 showAuxSubtypes = true;
2959 break;
2960 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2961 showAuxSubtypes = false;
2962 break;
2963 default:
2964 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2965 return false;
2966 }
2967 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002968 return true;
2969
satok47a44912010-10-06 16:03:58 +09002970 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002971 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002972 return true;
2973
2974 case MSG_SHOW_IM_CONFIG:
2975 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002976 return true;
2977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 case MSG_UNBIND_INPUT:
2981 try {
2982 ((IInputMethod)msg.obj).unbindInput();
2983 } catch (RemoteException e) {
2984 // There is nothing interesting about the method dying.
2985 }
2986 return true;
2987 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002988 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 try {
2990 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2991 } catch (RemoteException e) {
2992 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002993 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 return true;
2995 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002996 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002998 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002999 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003000 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 } catch (RemoteException e) {
3002 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003003 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 return true;
3005 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003006 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003008 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003009 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003010 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 } catch (RemoteException e) {
3012 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003013 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003015 case MSG_HIDE_CURRENT_INPUT_METHOD:
3016 synchronized (mMethodMap) {
3017 hideCurrentInputLocked(0, null);
3018 }
3019 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003021 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003023 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3025 } catch (RemoteException e) {
3026 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003027 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003029 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003030 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003031 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003032 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003034 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003036 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003037 // Dispose the channel if the input method is not local to this process
3038 // because the remote proxy will get its own copy when unparceled.
3039 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003040 channel.dispose();
3041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003043 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003047
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003048 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003049 final int missingMethods = msg.arg1;
3050 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003051 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003052 final IBinder startInputToken = (IBinder) args.arg1;
3053 final SessionState session = (SessionState) args.arg2;
3054 final IInputContext inputContext = (IInputContext) args.arg3;
3055 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003058 session.method.startInput(startInputToken, inputContext, missingMethods,
3059 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 } catch (RemoteException e) {
3061 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003062 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003067
Yohei Yukawa33e81792015-11-17 21:14:42 -08003068 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003070 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 } catch (RemoteException e) {
3072 // There is nothing interesting about the last client dying.
3073 }
3074 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003075 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003076 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003077 IInputMethodClient client = (IInputMethodClient)args.arg1;
3078 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003080 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003082 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003083 } finally {
3084 // Dispose the channel if the input method is not local to this process
3085 // because the remote proxy will get its own copy when unparceled.
3086 if (res.channel != null && Binder.isProxy(client)) {
3087 res.channel.dispose();
3088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003090 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003092 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003093 case MSG_SET_ACTIVE:
3094 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003095 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003096 } catch (RemoteException e) {
3097 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3098 + ((ClientState)msg.obj).pid + " uid "
3099 + ((ClientState)msg.obj).uid);
3100 }
3101 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003102 case MSG_SET_INTERACTIVE:
3103 handleSetInteractive(msg.arg1 != 0);
3104 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003105 case MSG_SWITCH_IME:
3106 handleSwitchInputMethod(msg.arg1 != 0);
3107 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003108 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3109 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003110 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003111 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003112 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003113 } catch (RemoteException e) {
3114 Slog.w(TAG, "Got RemoteException sending "
3115 + "setUserActionNotificationSequenceNumber("
3116 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003117 + clientState.pid + " uid "
3118 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003119 }
3120 return true;
3121 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003122 case MSG_REPORT_FULLSCREEN_MODE: {
3123 final boolean fullscreen = msg.arg1 != 0;
3124 final ClientState clientState = (ClientState)msg.obj;
3125 try {
3126 clientState.client.reportFullscreenMode(fullscreen);
3127 } catch (RemoteException e) {
3128 Slog.w(TAG, "Got RemoteException sending "
3129 + "reportFullscreen(" + fullscreen + ") notification to pid="
3130 + clientState.pid + " uid=" + clientState.uid);
3131 }
3132 return true;
3133 }
satok01038492012-04-09 21:08:27 +09003134
3135 // --------------------------------------------------------------
3136 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003137 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003138 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003139 case MSG_SYSTEM_UNLOCK_USER:
3140 final int userId = msg.arg1;
3141 onUnlockUser(userId);
3142 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 }
3144 return false;
3145 }
3146
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003147 private void handleSetInteractive(final boolean interactive) {
3148 synchronized (mMethodMap) {
3149 mIsInteractive = interactive;
3150 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3151
3152 // Inform the current client of the change in active status
3153 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003154 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3155 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3156 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003157 }
3158 }
3159 }
3160
Yohei Yukawaae61f712015-12-09 13:00:10 -08003161 private void handleSwitchInputMethod(final boolean forwardDirection) {
3162 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003163 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003164 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003165 if (nextSubtype == null) {
3166 return;
3167 }
3168 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003169 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3170 if (newInputMethodInfo == null) {
3171 return;
3172 }
3173 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3174 newInputMethodInfo, mCurrentSubtype);
3175 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003176 if (mSubtypeSwitchedByShortCutToast == null) {
3177 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3178 Toast.LENGTH_SHORT);
3179 } else {
3180 mSubtypeSwitchedByShortCutToast.setText(toastText);
3181 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003182 mSubtypeSwitchedByShortCutToast.show();
3183 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003184 }
3185 }
3186
satokdc9ddae2011-10-06 12:22:36 +09003187 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003188 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3189 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003190 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003191 if (DEBUG) {
3192 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3193 }
satok723a27e2010-11-11 14:58:11 +09003194 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003195 return true;
3196 }
3197
3198 return false;
3199 }
3200
Yohei Yukawa94e33302016-02-12 19:37:03 -08003201 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003202 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003203 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003204 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003205 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003206 if (!mSystemReady) {
3207 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3208 return;
3209 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003210 mMethodList.clear();
3211 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003212 mMethodMapUpdateCount++;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003213 mMyPackageMonitor.clearKnownImePackageNamesLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003214
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003215 // Use for queryIntentServicesAsUser
3216 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003218 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3219 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3220 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003221 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003223 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3224 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003225
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003226 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003227 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 for (int i = 0; i < services.size(); ++i) {
3229 ResolveInfo ri = services.get(i);
3230 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003231 final String imeId = InputMethodInfo.computeId(ri);
3232 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3233 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 + ": it does not require the permission "
3235 + android.Manifest.permission.BIND_INPUT_METHOD);
3236 continue;
3237 }
3238
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003239 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003241 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 try {
satoke7c6998e2011-06-03 17:57:59 +09003243 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003244 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003245 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003246 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247
3248 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003249 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003251 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003252 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 }
3254 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003255
Yohei Yukawac4e44912017-02-09 19:30:22 -08003256 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3257 // negatives when the package state remains to be the same but only the component state is
3258 // changed.
3259 {
3260 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3261 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3262 // conservative, but it seems we cannot use it for now (Issue 35176630).
3263 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3264 new Intent(InputMethod.SERVICE_INTERFACE),
3265 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3266 final int N = allInputMethodServices.size();
3267 for (int i = 0; i < N; ++i) {
3268 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08003269 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3270 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08003271 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08003272 }
3273 }
3274
Yohei Yukawa859df052016-02-17 07:56:46 -08003275 // TODO: The following code should find better place to live.
3276 if (!resetDefaultEnabledIme) {
3277 boolean enabledImeFound = false;
3278 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3279 final int N = enabledImes.size();
3280 for (int i = 0; i < N; ++i) {
3281 final InputMethodInfo imi = enabledImes.get(i);
3282 if (mMethodList.contains(imi)) {
3283 enabledImeFound = true;
3284 break;
3285 }
3286 }
3287 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003288 if (DEBUG) {
3289 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3290 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003291 resetDefaultEnabledIme = true;
3292 resetSelectedInputMethodAndSubtypeLocked("");
3293 }
3294 }
3295
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003296 if (resetDefaultEnabledIme) {
3297 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003298 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003299 final int N = defaultEnabledIme.size();
3300 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003301 final InputMethodInfo imi = defaultEnabledIme.get(i);
3302 if (DEBUG) {
3303 Slog.d(TAG, "--- enable ime = " + imi);
3304 }
3305 setInputMethodEnabledLocked(imi.getId(), true);
3306 }
3307 }
3308
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003309 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003310 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003311 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003312 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3313 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003314 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003315 }
3316 } else {
3317 // Double check that the default IME is certainly enabled.
3318 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003319 }
3320 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003321 // Here is not the perfect place to reset the switching controller. Ideally
3322 // mSwitchingController and mSettings should be able to share the same state.
3323 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3324 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003325 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003329
satok217f5482010-12-15 05:19:19 +09003330 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003331 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003332 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003333 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3334 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003335 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003336 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003337 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003338 final int userId;
3339 synchronized (mMethodMap) {
3340 userId = mSettings.getCurrentUserId();
3341 }
3342 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003343 }
3344
3345 private void showConfigureInputMethods() {
3346 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3347 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3348 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3349 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003350 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003351 }
3352
satok2c93efc2012-04-02 19:33:47 +09003353 private boolean isScreenLocked() {
3354 return mKeyguardManager != null
3355 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3356 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003357
Seigo Nonaka14e13912015-05-06 21:04:13 -07003358 private void showInputMethodMenu(boolean showAuxSubtypes) {
3359 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003362 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003363
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003364 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003365 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003366 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003367
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003368 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003369 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003370 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3371 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003372 if (immis == null || immis.size() == 0) {
3373 return;
3374 }
3375
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003376 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377
satok688bd472012-02-09 20:09:17 +09003378 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003379 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003380 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003381
satokc3690562012-01-10 20:14:43 +09003382 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003383 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003384 if (currentSubtype != null) {
3385 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003386 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3387 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003388 }
3389 }
3390
Ken Wakasa761eb372011-03-04 19:06:18 +09003391 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003392 mIms = new InputMethodInfo[N];
3393 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003394 int checkedItem = 0;
3395 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003396 final ImeSubtypeListItem item = imList.get(i);
3397 mIms[i] = item.mImi;
3398 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003399 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003400 int subtypeId = mSubtypeIds[i];
3401 if ((subtypeId == NOT_A_SUBTYPE_ID)
3402 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3403 || (subtypeId == lastInputMethodSubtypeId)) {
3404 checkedItem = i;
3405 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003408
3409 final Context settingsContext = new ContextThemeWrapper(context,
3410 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3411
3412 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003413 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3414 @Override
3415 public void onCancel(DialogInterface dialog) {
3416 hideInputMethodMenu();
3417 }
3418 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003419
3420 final Context dialogContext = mDialogBuilder.getContext();
3421 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3422 com.android.internal.R.styleable.DialogPreference,
3423 com.android.internal.R.attr.alertDialogStyle, 0);
3424 final Drawable dialogIcon = a.getDrawable(
3425 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3426 a.recycle();
3427
3428 mDialogBuilder.setIcon(dialogIcon);
3429
Yohei Yukawad34e1482016-02-11 08:03:52 -08003430 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003431 final View tv = inflater.inflate(
3432 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3433 mDialogBuilder.setCustomTitle(tv);
3434
3435 // Setup layout for a toggle switch of the hardware keyboard
3436 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003437 mSwitchingDialogTitleView
3438 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003439 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003440 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003441 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003442 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003443 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003444 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3445 @Override
3446 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003447 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003448 // Ensure that the input method dialog is dismissed when changing
3449 // the hardware keyboard state.
3450 hideInputMethodMenu();
3451 }
3452 });
3453
Alan Viverette505e3ab2014-11-24 15:22:11 -08003454 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003455 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3456 final OnClickListener choiceListener = new OnClickListener() {
3457 @Override
3458 public void onClick(final DialogInterface dialog, final int which) {
3459 synchronized (mMethodMap) {
3460 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3461 || mSubtypeIds.length <= which) {
3462 return;
satok01038492012-04-09 21:08:27 +09003463 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003464 final InputMethodInfo im = mIms[which];
3465 int subtypeId = mSubtypeIds[which];
3466 adapter.mCheckedItem = which;
3467 adapter.notifyDataSetChanged();
3468 hideInputMethodMenu();
3469 if (im != null) {
3470 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3471 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003472 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003473 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003475 }
3476 }
3477 };
3478 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003481 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003482 final Window w = mSwitchingDialog.getWindow();
3483 final WindowManager.LayoutParams attrs = w.getAttributes();
3484 w.setType(TYPE_INPUT_METHOD_DIALOG);
3485 // Use an alternate token for the dialog for that window manager can group the token
3486 // with other IME windows based on type vs. grouping based on whichever token happens
3487 // to get selected by the system later on.
3488 attrs.token = mSwitchingDialogToken;
3489 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3490 attrs.setTitle("Select input method");
3491 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003492 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 mSwitchingDialog.show();
3494 }
3495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003496
Ken Wakasa05dbb652011-08-22 15:22:43 +09003497 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3498 private final LayoutInflater mInflater;
3499 private final int mTextViewResourceId;
3500 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003501 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003502 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3503 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3504 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003505
Ken Wakasa05dbb652011-08-22 15:22:43 +09003506 mTextViewResourceId = textViewResourceId;
3507 mItemsList = itemsList;
3508 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003509 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003510 }
3511
3512 @Override
3513 public View getView(int position, View convertView, ViewGroup parent) {
3514 final View view = convertView != null ? convertView
3515 : mInflater.inflate(mTextViewResourceId, null);
3516 if (position < 0 || position >= mItemsList.size()) return view;
3517 final ImeSubtypeListItem item = mItemsList.get(position);
3518 final CharSequence imeName = item.mImeName;
3519 final CharSequence subtypeName = item.mSubtypeName;
3520 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3521 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3522 if (TextUtils.isEmpty(subtypeName)) {
3523 firstTextView.setText(imeName);
3524 secondTextView.setVisibility(View.GONE);
3525 } else {
3526 firstTextView.setText(subtypeName);
3527 secondTextView.setText(imeName);
3528 secondTextView.setVisibility(View.VISIBLE);
3529 }
3530 final RadioButton radioButton =
3531 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3532 radioButton.setChecked(position == mCheckedItem);
3533 return view;
3534 }
3535 }
3536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003538 synchronized (mMethodMap) {
3539 hideInputMethodMenuLocked();
3540 }
3541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003542
The Android Open Source Project10592532009-03-18 17:39:46 -07003543 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003544 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545
The Android Open Source Project10592532009-03-18 17:39:46 -07003546 if (mSwitchingDialog != null) {
3547 mSwitchingDialog.dismiss();
3548 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003550
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003551 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003552 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003553 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003557
satok42c5a162011-05-26 16:46:14 +09003558 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003560 // TODO: Make this work even for non-current users?
3561 if (!calledFromValidUser()) {
3562 return false;
3563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 synchronized (mMethodMap) {
3565 if (mContext.checkCallingOrSelfPermission(
3566 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3567 != PackageManager.PERMISSION_GRANTED) {
3568 throw new SecurityException(
3569 "Requires permission "
3570 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3571 }
Anna Galusza9b278112016-01-04 11:37:37 -08003572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 long ident = Binder.clearCallingIdentity();
3574 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003575 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 } finally {
3577 Binder.restoreCallingIdentity(ident);
3578 }
3579 }
3580 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003581
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003582 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3583 // Make sure this is a valid input method.
3584 InputMethodInfo imm = mMethodMap.get(id);
3585 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003586 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003587 }
3588
satokd87c2592010-09-29 11:52:06 +09003589 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3590 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003591
satokd87c2592010-09-29 11:52:06 +09003592 if (enabled) {
3593 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3594 if (pair.first.equals(id)) {
3595 // We are enabling this input method, but it is already enabled.
3596 // Nothing to do. The previous state was enabled.
3597 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003598 }
3599 }
satokd87c2592010-09-29 11:52:06 +09003600 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3601 // Previous state was disabled.
3602 return false;
3603 } else {
3604 StringBuilder builder = new StringBuilder();
3605 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3606 builder, enabledInputMethodsList, id)) {
3607 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003608 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003609 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3610 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3611 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003612 }
3613 // Previous state was enabled.
3614 return true;
3615 } else {
3616 // We are disabling the input method but it is already disabled.
3617 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003618 return false;
3619 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003620 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003621 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003622
satok723a27e2010-11-11 14:58:11 +09003623 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3624 boolean setSubtypeOnly) {
3625 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003626 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003627
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003628 mCurUserActionNotificationSequenceNumber =
3629 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3630 if (DEBUG) {
3631 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3632 + mCurUserActionNotificationSequenceNumber);
3633 }
3634
3635 if (mCurClient != null && mCurClient.client != null) {
3636 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3637 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003638 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003639 }
3640
satok723a27e2010-11-11 14:58:11 +09003641 // Set Subtype here
3642 if (imi == null || subtypeId < 0) {
3643 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003644 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003645 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003646 if (subtypeId < imi.getSubtypeCount()) {
3647 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3648 mSettings.putSelectedSubtype(subtype.hashCode());
3649 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003650 } else {
3651 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003652 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003653 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003654 }
satokab751aa2010-09-14 19:17:36 +09003655 }
satok723a27e2010-11-11 14:58:11 +09003656
Yohei Yukawa68645a62016-02-17 07:54:20 -08003657 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003658 // Set InputMethod here
3659 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3660 }
3661 }
3662
3663 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3664 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3665 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3666 // newDefaultIme is empty when there is no candidate for the selected IME.
3667 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3668 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3669 if (subtypeHashCode != null) {
3670 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003671 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003672 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003673 } catch (NumberFormatException e) {
3674 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3675 }
3676 }
3677 }
3678 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003679 }
3680
satok4e4569d2010-11-19 18:45:53 +09003681 // If there are no selected shortcuts, tries finding the most applicable ones.
3682 private Pair<InputMethodInfo, InputMethodSubtype>
3683 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3684 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3685 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003686 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003687 boolean foundInSystemIME = false;
3688
3689 // Search applicable subtype for each InputMethodInfo
3690 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003691 final String imiId = imi.getId();
3692 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3693 continue;
3694 }
satokcd7cd292010-11-20 15:46:23 +09003695 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003696 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003697 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003698 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003699 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003700 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003701 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003702 }
satokdf31ae62011-01-15 06:19:44 +09003703 // 2. Search by the system locale from enabledSubtypes.
3704 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003705 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003706 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003707 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003708 }
satoka86f5e42011-09-02 17:12:42 +09003709 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003710 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003711 final ArrayList<InputMethodSubtype> subtypesForSearch =
3712 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003713 ? InputMethodUtils.getSubtypes(imi)
3714 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003715 // 4. Search by the current subtype's locale from all subtypes.
3716 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003717 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003718 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003719 }
3720 // 5. Search by the system locale from all subtypes.
3721 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003722 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003723 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003724 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003725 }
satokcd7cd292010-11-20 15:46:23 +09003726 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003727 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003728 // The current input method is the most applicable IME.
3729 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003730 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003731 break;
satok7599a7f2010-12-22 13:45:23 +09003732 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003733 // The system input method is 2nd applicable IME.
3734 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003735 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003736 if ((imi.getServiceInfo().applicationInfo.flags
3737 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3738 foundInSystemIME = true;
3739 }
satok4e4569d2010-11-19 18:45:53 +09003740 }
3741 }
3742 }
3743 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003744 if (mostApplicableIMI != null) {
3745 Slog.w(TAG, "Most applicable shortcut input method was:"
3746 + mostApplicableIMI.getId());
3747 if (mostApplicableSubtype != null) {
3748 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3749 + "," + mostApplicableSubtype.getMode() + ","
3750 + mostApplicableSubtype.getLocale());
3751 }
3752 }
satok4e4569d2010-11-19 18:45:53 +09003753 }
satokcd7cd292010-11-20 15:46:23 +09003754 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003755 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003756 } else {
3757 return null;
3758 }
3759 }
3760
satokab751aa2010-09-14 19:17:36 +09003761 /**
3762 * @return Return the current subtype of this input method.
3763 */
satok42c5a162011-05-26 16:46:14 +09003764 @Override
satokab751aa2010-09-14 19:17:36 +09003765 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003766 // TODO: Make this work even for non-current users?
3767 if (!calledFromValidUser()) {
3768 return null;
3769 }
3770 synchronized (mMethodMap) {
3771 return getCurrentInputMethodSubtypeLocked();
3772 }
3773 }
3774
3775 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003776 if (mCurMethodId == null) {
3777 return null;
3778 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003779 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003780 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3781 if (imi == null || imi.getSubtypeCount() == 0) {
3782 return null;
satok4e4569d2010-11-19 18:45:53 +09003783 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003784 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003785 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3786 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003787 if (subtypeId == NOT_A_SUBTYPE_ID) {
3788 // If there are no selected subtypes, the framework will try to find
3789 // the most applicable subtype from explicitly or implicitly enabled
3790 // subtypes.
3791 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003792 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003793 // If there is only one explicitly or implicitly enabled subtype,
3794 // just returns it.
3795 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3796 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3797 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003798 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003799 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003800 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003801 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003802 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003803 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3804 true);
satok4e4569d2010-11-19 18:45:53 +09003805 }
satok3ef8b292010-11-23 06:06:29 +09003806 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003807 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003808 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003809 }
3810 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003811 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003812 }
3813
satok4e4569d2010-11-19 18:45:53 +09003814 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003815 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003816 @Override
satok4e4569d2010-11-19 18:45:53 +09003817 public List getShortcutInputMethodsAndSubtypes() {
3818 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003819 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003820 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003821 // If there are no selected shortcut subtypes, the framework will try to find
3822 // the most applicable subtype from all subtypes whose mode is
3823 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003824 Pair<InputMethodInfo, InputMethodSubtype> info =
3825 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003826 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003827 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003828 ret.add(info.first);
3829 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003830 }
satok3da92232011-01-11 22:46:30 +09003831 return ret;
satokf3db1af2010-11-23 13:34:33 +09003832 }
satokf3db1af2010-11-23 13:34:33 +09003833 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3834 ret.add(imi);
3835 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3836 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003837 }
3838 }
satokf3db1af2010-11-23 13:34:33 +09003839 return ret;
satok4e4569d2010-11-19 18:45:53 +09003840 }
3841 }
3842
satok42c5a162011-05-26 16:46:14 +09003843 @Override
satokb66d2872010-11-10 01:04:04 +09003844 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003845 // TODO: Make this work even for non-current users?
3846 if (!calledFromValidUser()) {
3847 return false;
3848 }
satokb66d2872010-11-10 01:04:04 +09003849 synchronized (mMethodMap) {
3850 if (subtype != null && mCurMethodId != null) {
3851 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003852 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003853 if (subtypeId != NOT_A_SUBTYPE_ID) {
3854 setInputMethodLocked(mCurMethodId, subtypeId);
3855 return true;
3856 }
3857 }
3858 return false;
3859 }
3860 }
3861
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003862 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003863 private static class InputMethodFileManager {
3864 private static final String SYSTEM_PATH = "system";
3865 private static final String INPUT_METHOD_PATH = "inputmethod";
3866 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3867 private static final String NODE_SUBTYPES = "subtypes";
3868 private static final String NODE_SUBTYPE = "subtype";
3869 private static final String NODE_IMI = "imi";
3870 private static final String ATTR_ID = "id";
3871 private static final String ATTR_LABEL = "label";
3872 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003873 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003874 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003875 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003876 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3877 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3878 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003879 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003880 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3881 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003882 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003883 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003884 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003885 if (methodMap == null) {
3886 throw new NullPointerException("methodMap is null");
3887 }
3888 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003889 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003890 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3891 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003892 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003893 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003894 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3895 }
3896 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3897 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3898 if (!subtypeFile.exists()) {
3899 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003900 writeAdditionalInputMethodSubtypes(
3901 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003902 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003903 readAdditionalInputMethodSubtypes(
3904 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003905 }
3906 }
3907
3908 private void deleteAllInputMethodSubtypes(String imiId) {
3909 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003910 mAdditionalSubtypesMap.remove(imiId);
3911 writeAdditionalInputMethodSubtypes(
3912 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003913 }
3914 }
3915
3916 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003917 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003918 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003919 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003920 final int N = additionalSubtypes.length;
3921 for (int i = 0; i < N; ++i) {
3922 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003923 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003924 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003925 } else {
3926 Slog.w(TAG, "Duplicated subtype definition found: "
3927 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003928 }
3929 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003930 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3931 writeAdditionalInputMethodSubtypes(
3932 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003933 }
3934 }
3935
3936 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3937 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003938 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003939 }
3940 }
3941
3942 private static void writeAdditionalInputMethodSubtypes(
3943 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3944 HashMap<String, InputMethodInfo> methodMap) {
3945 // Safety net for the case that this function is called before methodMap is set.
3946 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3947 FileOutputStream fos = null;
3948 try {
3949 fos = subtypesFile.startWrite();
3950 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003951 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003952 out.startDocument(null, true);
3953 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3954 out.startTag(null, NODE_SUBTYPES);
3955 for (String imiId : allSubtypes.keySet()) {
3956 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3957 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3958 continue;
3959 }
3960 out.startTag(null, NODE_IMI);
3961 out.attribute(null, ATTR_ID, imiId);
3962 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3963 final int N = subtypesList.size();
3964 for (int i = 0; i < N; ++i) {
3965 final InputMethodSubtype subtype = subtypesList.get(i);
3966 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003967 if (subtype.hasSubtypeId()) {
3968 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3969 String.valueOf(subtype.getSubtypeId()));
3970 }
satoke7c6998e2011-06-03 17:57:59 +09003971 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3972 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3973 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003974 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3975 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003976 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3977 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3978 out.attribute(null, ATTR_IS_AUXILIARY,
3979 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003980 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3981 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003982 out.endTag(null, NODE_SUBTYPE);
3983 }
3984 out.endTag(null, NODE_IMI);
3985 }
3986 out.endTag(null, NODE_SUBTYPES);
3987 out.endDocument();
3988 subtypesFile.finishWrite(fos);
3989 } catch (java.io.IOException e) {
3990 Slog.w(TAG, "Error writing subtypes", e);
3991 if (fos != null) {
3992 subtypesFile.failWrite(fos);
3993 }
3994 }
3995 }
3996
3997 private static void readAdditionalInputMethodSubtypes(
3998 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3999 if (allSubtypes == null || subtypesFile == null) return;
4000 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004001 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004002 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004003 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004004 int type = parser.getEventType();
4005 // Skip parsing until START_TAG
4006 while ((type = parser.next()) != XmlPullParser.START_TAG
4007 && type != XmlPullParser.END_DOCUMENT) {}
4008 String firstNodeName = parser.getName();
4009 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4010 throw new XmlPullParserException("Xml doesn't start with subtypes");
4011 }
4012 final int depth =parser.getDepth();
4013 String currentImiId = null;
4014 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4015 while (((type = parser.next()) != XmlPullParser.END_TAG
4016 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4017 if (type != XmlPullParser.START_TAG)
4018 continue;
4019 final String nodeName = parser.getName();
4020 if (NODE_IMI.equals(nodeName)) {
4021 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4022 if (TextUtils.isEmpty(currentImiId)) {
4023 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4024 continue;
4025 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004026 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004027 allSubtypes.put(currentImiId, tempSubtypesArray);
4028 } else if (NODE_SUBTYPE.equals(nodeName)) {
4029 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4030 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4031 continue;
4032 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004033 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004034 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004035 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004036 parser.getAttributeValue(null, ATTR_LABEL));
4037 final String imeSubtypeLocale =
4038 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004039 final String languageTag =
4040 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004041 final String imeSubtypeMode =
4042 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4043 final String imeSubtypeExtraValue =
4044 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004045 final boolean isAuxiliary = "1".equals(String.valueOf(
4046 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004047 final boolean isAsciiCapable = "1".equals(String.valueOf(
4048 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004049 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004050 .setSubtypeNameResId(label)
4051 .setSubtypeIconResId(icon)
4052 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004053 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004054 .setSubtypeMode(imeSubtypeMode)
4055 .setSubtypeExtraValue(imeSubtypeExtraValue)
4056 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004057 .setIsAsciiCapable(isAsciiCapable);
4058 final String subtypeIdString =
4059 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4060 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004061 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004062 }
4063 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004064 }
4065 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004066 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4067 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004068 return;
satoke7c6998e2011-06-03 17:57:59 +09004069 }
4070 }
4071 }
4072
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004073 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4074 @NonNull
4075 private final Handler mHandler;
4076
4077 LocalServiceImpl(@NonNull final Handler handler) {
4078 mHandler = handler;
4079 }
4080
4081 @Override
4082 public void setInteractive(boolean interactive) {
4083 // Do everything in handler so as not to block the caller.
4084 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4085 interactive ? 1 : 0, 0));
4086 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004087
4088 @Override
4089 public void switchInputMethod(boolean forwardDirection) {
4090 // Do everything in handler so as not to block the caller.
4091 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4092 forwardDirection ? 1 : 0, 0));
4093 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004094
4095 @Override
4096 public void hideCurrentInputMethod() {
4097 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4098 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4099 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004100 }
4101
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004102 private static String imeWindowStatusToString(final int imeWindowVis) {
4103 final StringBuilder sb = new StringBuilder();
4104 boolean first = true;
4105 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4106 sb.append("Active");
4107 first = false;
4108 }
4109 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4110 if (!first) {
4111 sb.append("|");
4112 }
4113 sb.append("Visible");
4114 }
4115 return sb.toString();
4116 }
4117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004119 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4120 @Nullable Uri contentUri, @Nullable String packageName) {
4121 if (!calledFromValidUser()) {
4122 return null;
4123 }
4124
4125 if (token == null) {
4126 throw new NullPointerException("token");
4127 }
4128 if (packageName == null) {
4129 throw new NullPointerException("packageName");
4130 }
4131 if (contentUri == null) {
4132 throw new NullPointerException("contentUri");
4133 }
4134 final String contentUriScheme = contentUri.getScheme();
4135 if (!"content".equals(contentUriScheme)) {
4136 throw new InvalidParameterException("contentUri must have content scheme");
4137 }
4138
4139 synchronized (mMethodMap) {
4140 final int uid = Binder.getCallingUid();
4141 if (mCurMethodId == null) {
4142 return null;
4143 }
4144 if (mCurToken != token) {
4145 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4146 + " token=" + token);
4147 return null;
4148 }
4149 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4150 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4151 // nature of our system. Let's compare it with our internal record.
4152 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4153 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4154 + mCurAttribute.packageName + " packageName=" + packageName);
4155 return null;
4156 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004157 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004158 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004159 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004160 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004161 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4162 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4163 imeUserId);
4164 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4165 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4166 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4167 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4168 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4169 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4170 // actually allowed to "uid", which is guaranteed to be the IME's one.
4171 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4172 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004173 }
4174 }
4175
4176 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004177 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4178 if (!calledFromValidUser()) {
4179 return;
4180 }
4181 synchronized (mMethodMap) {
4182 if (!calledWithValidToken(token)) {
4183 return;
4184 }
4185 if (mCurClient != null && mCurClient.client != null) {
4186 mInFullscreenMode = fullscreen;
4187 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4188 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4189 }
4190 }
4191 }
4192
4193 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4195 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4196 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4199 + Binder.getCallingPid()
4200 + ", uid=" + Binder.getCallingUid());
4201 return;
4202 }
4203
4204 IInputMethod method;
4205 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004206 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 synchronized (mMethodMap) {
4211 p.println("Current Input Method Manager state:");
4212 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004213 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 for (int i=0; i<N; i++) {
4215 InputMethodInfo info = mMethodList.get(i);
4216 p.println(" InputMethod #" + i + ":");
4217 info.dump(p, " ");
4218 }
4219 p.println(" Clients:");
4220 for (ClientState ci : mClients.values()) {
4221 p.println(" Client " + ci + ":");
4222 p.println(" client=" + ci.client);
4223 p.println(" inputContext=" + ci.inputContext);
4224 p.println(" sessionRequested=" + ci.sessionRequested);
4225 p.println(" curSession=" + ci.curSession);
4226 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004227 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004229 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004230 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4231 + " softInputMode=" +
4232 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4233 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004234 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4236 + " mBoundToMethod=" + mBoundToMethod);
4237 p.println(" mCurToken=" + mCurToken);
4238 p.println(" mCurIntent=" + mCurIntent);
4239 method = mCurMethod;
4240 p.println(" mCurMethod=" + mCurMethod);
4241 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004242 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 p.println(" mShowRequested=" + mShowRequested
4244 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4245 + " mShowForced=" + mShowForced
4246 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004247 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004248 p.println(" mCurUserActionNotificationSequenceNumber="
4249 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004250 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004251 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004252 p.println(" mSwitchingController:");
4253 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004254 p.println(" mSettings:");
4255 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004257
Jeff Brownb88102f2010-09-08 11:49:43 -07004258 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 pw.flush();
4261 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004262 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4263 } catch (IOException | RemoteException e) {
4264 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004266 } else {
4267 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004269
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004270 if (focusedWindowClient != null && client != focusedWindowClient) {
4271 p.println(" ");
4272 p.println("Warning: Current input method client doesn't match the last focused. "
4273 + "window.");
4274 p.println("Dumping input method client in the last focused window just in case.");
4275 p.println(" ");
4276 pw.flush();
4277 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004278 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4279 } catch (IOException | RemoteException e) {
4280 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004281 }
4282 }
4283
Jeff Brownb88102f2010-09-08 11:49:43 -07004284 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 pw.flush();
4287 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004288 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4289 } catch (IOException | RemoteException e) {
4290 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004292 } else {
4293 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 }
4295 }
4296}