blob: dc987fac97e5555b2f7425288529cee168aa29c9 [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;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800152import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900154import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800155import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156import java.util.HashMap;
157import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800158import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800159import java.util.WeakHashMap;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800160import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
162/**
163 * This class provides a system service that manages input methods.
164 */
165public class InputMethodManagerService extends IInputMethodManager.Stub
166 implements ServiceConnection, Handler.Callback {
167 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700168 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700169 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700171 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
172 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
173 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 static final int MSG_UNBIND_INPUT = 1000;
176 static final int MSG_BIND_INPUT = 1010;
177 static final int MSG_SHOW_SOFT_INPUT = 1020;
178 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700179 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 static final int MSG_ATTACH_TOKEN = 1040;
181 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
Yohei Yukawa33e81792015-11-17 21:14:42 -0800185 static final int MSG_UNBIND_CLIENT = 3000;
186 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700187 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700188 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900189 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800190 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800191 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
satok01038492012-04-09 21:08:27 +0900193 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
194
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700195 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
196
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700197 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800198
satokf9f01002011-05-19 21:31:50 +0900199 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
200
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900201 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900202 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900203
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700204 @Retention(SOURCE)
205 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
206 private @interface HardKeyboardBehavior {
207 int WIRELESS_AFFORDANCE = 0;
208 int WIRED_AFFORDANCE = 1;
209 }
satok4e4569d2010-11-19 18:45:53 +0900210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800212 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900214 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700217 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700219 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900220 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900221 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900222 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800223 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900225 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 // All known input methods. mMethodMap also serves as the global
228 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700229 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
230 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900231 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700232 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900233 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
Yohei Yukawae0733062017-02-09 22:49:35 -0800235 /**
236 * Tracks how many times {@link #mMethodMap} was updated.
237 */
238 @GuardedBy("mMethodMap")
239 private int mMethodMapUpdateCount = 0;
240
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700241 // Used to bring IME service up to visible adjustment while it is being shown.
242 final ServiceConnection mVisibleConnection = new ServiceConnection() {
243 @Override public void onServiceConnected(ComponentName name, IBinder service) {
244 }
245
246 @Override public void onServiceDisconnected(ComponentName name) {
247 }
248 };
249 boolean mVisibleBound = false;
250
satok7cfc0ed2011-06-20 21:29:36 +0900251 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700252 private NotificationManager mNotificationManager;
253 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700254 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400255 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700256 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900257 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900258 private boolean mNotificationShown;
259
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900260 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 final ClientState client;
262 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700263
264 IInputMethodSession session;
265 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 @Override
268 public String toString() {
269 return "SessionState{uid " + client.uid + " pid " + client.pid
270 + " method " + Integer.toHexString(
271 System.identityHashCode(method))
272 + " session " + Integer.toHexString(
273 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700274 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 + "}";
276 }
277
278 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700279 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 client = _client;
281 method = _method;
282 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700283 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
Jeff Brownc28867a2013-03-26 15:42:39 -0700287 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 final IInputMethodClient client;
289 final IInputContext inputContext;
290 final int uid;
291 final int pid;
292 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 boolean sessionRequested;
295 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 @Override
298 public String toString() {
299 return "ClientState{" + Integer.toHexString(
300 System.identityHashCode(this)) + " uid " + uid
301 + " pid " + pid + "}";
302 }
303
304 ClientState(IInputMethodClient _client, IInputContext _inputContext,
305 int _uid, int _pid) {
306 client = _client;
307 inputContext = _inputContext;
308 uid = _uid;
309 pid = _pid;
310 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
311 }
312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700314 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700317 * Set once the system is ready to run third party code.
318 */
319 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700321 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700322 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
323 * method. This is to be synchronized with the secure settings keyed with
324 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
325 *
326 * <p>This can be transiently {@code null} when the system is re-initializing input method
327 * settings, e.g., the system locale is just changed.</p>
328 *
329 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
330 * {@link InputMethodManagerService}.</p>
331 *
332 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700334 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * The current binding sequence number, incremented every time there is
339 * a new bind performed.
340 */
341 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * The client that is currently bound to an input method.
345 */
346 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800349 * The last window token that we confirmed to be focused. This is always updated upon reports
350 * from the input method client. If the window state is already changed before the report is
351 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700352 */
353 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700355 /**
Yohei Yukawa22a89232017-02-12 16:38:59 -0800356 * {@link WindowManager.LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
357 *
358 * @see #mCurFocusedWindow
359 */
360 int mCurFocusedWindowSoftInputMode;
361
362 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800363 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
364 */
365 ClientState mCurFocusedWindowClient;
366
367 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 * The input context last provided by the current client.
369 */
370 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700373 * The missing method flags for the input context last provided by the current client.
374 *
375 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
376 */
377 int mCurInputContextMissingMethods;
378
379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 * The attributes last provided by the current client.
381 */
382 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700385 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700387 *
388 * <p>This can be {@code null} when no input method is connected.</p>
389 *
390 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700392 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 /**
satokab751aa2010-09-14 19:17:36 +0900396 * The current subtype of the current input method.
397 */
398 private InputMethodSubtype mCurrentSubtype;
399
satok4e4569d2010-11-19 18:45:53 +0900400 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900401 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700402 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900403
John Spurlocke0980502013-10-25 11:59:29 -0400404 // Was the keyguard locked when this client became current?
405 private boolean mCurClientInKeyguard;
406
satokab751aa2010-09-14 19:17:36 +0900407 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * Set to true if our ServiceConnection is currently actively bound to
409 * a service (whether or not we have gotten its IBinder back yet).
410 */
411 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * Set if the client has asked for the input method to be shown.
415 */
416 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 /**
419 * Set if we were explicitly told to show the input method.
420 */
421 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 /**
424 * Set if we were forced to be shown.
425 */
426 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 /**
429 * Set if we last told the input method to show itself.
430 */
431 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800434 * {@code true} if the current input method is in fullscreen mode.
435 */
436 boolean mInFullscreenMode;
437
438 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 * The Intent used to connect to the current input method.
440 */
441 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 /**
444 * The token we have made for the currently active input method, to
445 * identify it in the future.
446 */
447 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 /**
450 * If non-null, this is the input method service we are currently connected
451 * to.
452 */
453 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 /**
456 * Time that we last initiated a bind to the input method, to determine
457 * if we should try to disconnect and reconnect to it.
458 */
459 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 /**
462 * Have we called mCurMethod.bindInput()?
463 */
464 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 /**
467 * Currently enabled session. Only touched by service thread, not
468 * protected by a lock.
469 */
470 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700473 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700475 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800476
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900477 int mCurUserActionNotificationSequenceNumber = 0;
478
Joe Onorato857fd9b2011-01-27 15:08:35 -0800479 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900480
481 /**
482 * A set of status bits regarding the active IME.
483 *
484 * <p>This value is a combination of following two bits:</p>
485 * <dl>
486 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
487 * <dd>
488 * If this bit is ON, connected IME is ready to accept touch/key events.
489 * </dd>
490 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
491 * <dd>
492 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
493 * </dd>
494 * </dl>
495 * <em>Do not update this value outside of setImeWindowStatus.</em>
496 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800497 int mImeWindowVis;
498
Ken Wakasa05dbb652011-08-22 15:22:43 +0900499 private AlertDialog.Builder mDialogBuilder;
500 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700501 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900502 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700503 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900504 private InputMethodInfo[] mIms;
505 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800506 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700507 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800508 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900509 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
510 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500511 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700512 @HardKeyboardBehavior
513 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800515 /**
516 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
517 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
518 * will not affect those tasks that are already posted.
519 *
520 * <p>Posting {@link #MSG_START_INPUT} message basically means that
521 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
522 * back in the current IME process shortly, which will also affect what the current IME starts
523 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
524 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
525 * logical input session between the client application and the current IME.</p>
526 *
527 * <p>Be careful to not keep strong references to this object forever, which can prevent
528 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
529 * </p>
530 */
531 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800532 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
533
534 final int mSequenceNumber;
535 final long mTimestamp;
536 final long mWallTime;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800537 @NonNull
538 final IBinder mImeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800539 @NonNull
540 final String mImeId;
541 // @InputMethodClient.StartInputReason
542 final int mStartInputReason;
543 final boolean mRestarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800544 @Nullable
545 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800546 @NonNull
547 final EditorInfo mEditorInfo;
548 final int mTargetWindowSoftInputMode;
549 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800550
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800551 StartInputInfo(@NonNull IBinder imeToken, @NonNull String imeId,
552 /* @InputMethodClient.StartInputReason */ int startInputReason, boolean restarting,
553 @Nullable IBinder targetWindow, @NonNull EditorInfo editorInfo,
554 int targetWindowSoftInputMode, int clientBindSequenceNumber) {
555 mSequenceNumber = sSequenceNumber.getAndIncrement();
556 mTimestamp = SystemClock.uptimeMillis();
557 mWallTime = System.currentTimeMillis();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800558 mImeToken = imeToken;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800559 mImeId = imeId;
560 mStartInputReason = startInputReason;
561 mRestarting = restarting;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800562 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800563 mEditorInfo = editorInfo;
564 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
565 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800566 }
567 }
568
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800569 @GuardedBy("mMethodMap")
570 private final WeakHashMap<IBinder, StartInputInfo> mStartInputMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800571
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800572 /**
573 * A ring buffer to store the history of {@link StartInputInfo}.
574 */
575 private static final class StartInputHistory {
576 /**
577 * Entry size for non low-RAM devices.
578 *
579 * <p>TODO: Consider to follow what other system services have been doing to manage
580 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
581 */
582 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
583
584 /**
585 * Entry size for non low-RAM devices.
586 *
587 * <p>TODO: Consider to follow what other system services have been doing to manage
588 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
589 */
590 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
591
592 private static int getEntrySize() {
593 if (ActivityManager.isLowRamDeviceStatic()) {
594 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
595 } else {
596 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
597 }
598 }
599
600 /**
601 * Backing store for the ring bugger.
602 */
603 private final Entry[] mEntries = new Entry[getEntrySize()];
604
605 /**
606 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
607 * write.
608 */
609 private int mNextIndex = 0;
610
611 /**
612 * Recyclable entry to store the information in {@link StartInputInfo}.
613 */
614 private static final class Entry {
615 int mSequenceNumber;
616 long mTimestamp;
617 long mWallTime;
618 @NonNull
619 String mImeTokenString;
620 @NonNull
621 String mImeId;
622 /* @InputMethodClient.StartInputReason */
623 int mStartInputReason;
624 boolean mRestarting;
625 @NonNull
626 String mTargetWindowString;
627 @NonNull
628 EditorInfo mEditorInfo;
629 int mTargetWindowSoftInputMode;
630 int mClientBindSequenceNumber;
631
632 Entry(@NonNull StartInputInfo original) {
633 set(original);
634 }
635
636 void set(@NonNull StartInputInfo original) {
637 mSequenceNumber = original.mSequenceNumber;
638 mTimestamp = original.mTimestamp;
639 mWallTime = original.mWallTime;
640 // Intentionally convert to String so as not to keep a strong reference to a Binder
641 // object.
642 mImeTokenString = String.valueOf(original.mImeToken);
643 mImeId = original.mImeId;
644 mStartInputReason = original.mStartInputReason;
645 mRestarting = original.mRestarting;
646 // Intentionally convert to String so as not to keep a strong reference to a Binder
647 // object.
648 mTargetWindowString = String.valueOf(original.mTargetWindow);
649 mEditorInfo = original.mEditorInfo;
650 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
651 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
652 }
653 }
654
655 /**
656 * Add a new entry and discard the oldest entry as needed.
657 * @param info {@lin StartInputInfo} to be added.
658 */
659 void addEntry(@NonNull StartInputInfo info) {
660 final int index = mNextIndex;
661 if (mEntries[index] == null) {
662 mEntries[index] = new Entry(info);
663 } else {
664 mEntries[index].set(info);
665 }
666 mNextIndex = (mNextIndex + 1) % mEntries.length;
667 }
668
669 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
670 final SimpleDateFormat dataFormat =
671 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
672
673 for (int i = 0; i < mEntries.length; ++i) {
674 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
675 if (entry == null) {
676 continue;
677 }
678 pw.print(prefix);
679 pw.println("StartInput #" + entry.mSequenceNumber + ":");
680
681 pw.print(prefix);
682 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
683 + " (timestamp=" + entry.mTimestamp + ")"
684 + " reason="
685 + InputMethodClient.getStartInputReason(entry.mStartInputReason)
686 + " restarting=" + entry.mRestarting);
687
688 pw.print(prefix);
689 pw.println(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
690
691 pw.print(prefix);
692 pw.println(" targetWin=" + entry.mTargetWindowString
693 + " [" + entry.mEditorInfo.packageName + "]"
694 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
695
696 pw.print(prefix);
697 pw.println(" softInputMode=" + InputMethodClient.softInputModeToString(
698 entry.mTargetWindowSoftInputMode));
699
700 pw.print(prefix);
701 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
702 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
703 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
704 + " fieldName=" + entry.mEditorInfo.fieldName
705 + " actionId=" + entry.mEditorInfo.actionId
706 + " actionLabel=" + entry.mEditorInfo.actionLabel);
707 }
708 }
709 }
710
711 @GuardedBy("mMethodMap")
712 @NonNull
713 private final StartInputHistory mStartInputHistory = new StartInputHistory();
714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700716 int mUserId;
717 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700718 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800719 String mLastEnabled = "";
720
Yohei Yukawa81482972015-06-04 00:58:59 -0700721 /**
722 * <em>This constructor must be called within the lock.</em>
723 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 SettingsObserver(Handler handler) {
725 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700726 }
727
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800728 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700729 if (mRegistered && mUserId == userId) {
730 return;
731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700733 if (mRegistered) {
734 mContext.getContentResolver().unregisterContentObserver(this);
735 mRegistered = false;
736 }
737 if (mUserId != userId) {
738 mLastEnabled = "";
739 mUserId = userId;
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700742 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900743 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700744 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900745 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700746 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700747 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700748 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800749 resolver.registerContentObserver(Settings.Secure.getUriFor(
750 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700751 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800753
Michael Wright7b5a96b2014-08-09 19:28:42 -0700754 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800755 final Uri showImeUri = Settings.Secure.getUriFor(
756 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
757 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
758 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700760 if (showImeUri.equals(uri)) {
761 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800762 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
763 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
764 mContext.getContentResolver(),
765 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
766 0, mUserId) == 1;
767 if (mAccessibilityRequestingNoSoftKeyboard) {
768 final boolean showRequested = mShowRequested;
769 hideCurrentInputLocked(0, null);
770 mShowRequested = showRequested;
771 } else if (mShowRequested) {
772 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
773 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700774 } else {
775 boolean enabledChanged = false;
776 String newEnabled = mSettings.getEnabledInputMethodsStr();
777 if (!mLastEnabled.equals(newEnabled)) {
778 mLastEnabled = newEnabled;
779 enabledChanged = true;
780 }
781 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700785
786 @Override
787 public String toString() {
788 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
789 + " mLastEnabled=" + mLastEnabled + "}";
790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800792
Yohei Yukawa79247822017-01-23 15:26:15 -0800793 class ImmsBroadcastReceiver extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900794 @Override
795 public void onReceive(Context context, Intent intent) {
796 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700797 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900798 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700799 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900800 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800801 } else if (Intent.ACTION_USER_ADDED.equals(action)
802 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100803 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800804 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700805 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
806 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
807 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
808 final String prevValue = intent.getStringExtra(
809 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
810 final String newValue = intent.getStringExtra(
811 Intent.EXTRA_SETTING_NEW_VALUE);
812 restoreEnabledInputMethods(mContext, prevValue, newValue);
813 }
Yohei Yukawa79247822017-01-23 15:26:15 -0800814 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800815 onActionLocaleChanged();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900816 } else {
817 Slog.w(TAG, "Unexpected intent " + intent);
818 }
819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800821
Yohei Yukawa0d7aff82017-02-10 00:40:51 -0800822 /**
823 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
824 *
825 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
826 * the users. We should ignore this event if this is about any background user's locale.</p>
827 *
828 * <p>Caution: This method must not be called when system is not ready.</p>
829 */
830 void onActionLocaleChanged() {
831 synchronized (mMethodMap) {
832 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
833 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
834 return;
835 }
836 buildInputMethodListLocked(true);
837 // If the locale is changed, needs to reset the default ime
838 resetDefaultImeLocked(mContext);
839 updateFromSettingsLocked(true);
840 mLastSystemLocales = possibleNewLocale;
841 }
842 }
843
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700844 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
845 // does not attempt to validate on the fly with any installed device policy, so must only
846 // be run in the context of initial device setup.
847 //
848 // TODO: Move this method to InputMethodUtils with adding unit tests.
849 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
850 if (DEBUG_RESTORE) {
851 Slog.i(TAG, "Restoring enabled input methods:");
852 Slog.i(TAG, "prev=" + prevValue);
853 Slog.i(TAG, " new=" + newValue);
854 }
855 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900856 ArrayMap<String, ArraySet<String>> prevMap =
857 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
858 ArrayMap<String, ArraySet<String>> newMap =
859 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700860
861 // Merge the restored ime+subtype enabled states into the live state
862 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
863 final String imeId = entry.getKey();
864 ArraySet<String> prevSubtypes = prevMap.get(imeId);
865 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700866 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700867 prevMap.put(imeId, prevSubtypes);
868 }
869 prevSubtypes.addAll(entry.getValue());
870 }
871
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700872 final String mergedImesAndSubtypesString =
873 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700874 if (DEBUG_RESTORE) {
875 Slog.i(TAG, "Merged IME string:");
876 Slog.i(TAG, " " + mergedImesAndSubtypesString);
877 }
878 Settings.Secure.putString(context.getContentResolver(),
879 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
880 }
881
Yohei Yukawac4e44912017-02-09 19:30:22 -0800882 final class MyPackageMonitor extends PackageMonitor {
883 /**
884 * Set of packages to be monitored.
885 *
886 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
887 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
888 * dynamically unless the entire package is updated, which also always triggers package
889 * rescanning.</p>
890 */
891 @GuardedBy("mMethodMap")
892 private ArraySet<String> mPackagesToMonitorComponentChange = new ArraySet<>();
893
894 @GuardedBy("mMethodMap")
895 void clearPackagesToMonitorComponentChangeLocked() {
896 mPackagesToMonitorComponentChange.clear();
897 }
898
899 @GuardedBy("mMethodMap")
900 final void addPackageToMonitorComponentChangeLocked(@NonNull String packageName) {
901 mPackagesToMonitorComponentChange.add(packageName);
902 }
903
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900904 private boolean isChangingPackagesOfCurrentUser() {
905 final int userId = getChangingUserId();
906 final boolean retval = userId == mSettings.getCurrentUserId();
907 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900908 if (!retval) {
909 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
910 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900911 }
912 return retval;
913 }
914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800916 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900917 if (!isChangingPackagesOfCurrentUser()) {
918 return false;
919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900921 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 final int N = mMethodList.size();
923 if (curInputMethodId != null) {
924 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800925 InputMethodInfo imi = mMethodList.get(i);
926 if (imi.getId().equals(curInputMethodId)) {
927 for (String pkg : packages) {
928 if (imi.getPackageName().equals(pkg)) {
929 if (!doit) {
930 return true;
931 }
satok723a27e2010-11-11 14:58:11 +0900932 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800933 chooseNewDefaultIMELocked();
934 return true;
935 }
936 }
937 }
938 }
939 }
940 }
941 return false;
942 }
943
944 @Override
Yohei Yukawac4e44912017-02-09 19:30:22 -0800945 public boolean onPackageChanged(String packageName, int uid, String[] components) {
946 // If this package is in the watch list, we want to check it.
947 synchronized (mMethodMap) {
948 return mPackagesToMonitorComponentChange.contains(packageName);
949 }
950 }
951
952 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800953 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 if (!isChangingPackagesOfCurrentUser()) {
955 return;
956 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800957 synchronized (mMethodMap) {
958 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900959 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800960 final int N = mMethodList.size();
961 if (curInputMethodId != null) {
962 for (int i=0; i<N; i++) {
963 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900964 final String imiId = imi.getId();
965 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800966 curIm = imi;
967 }
satoke7c6998e2011-06-03 17:57:59 +0900968
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800969 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900970 if (isPackageModified(imi.getPackageName())) {
971 mFileManager.deleteAllInputMethodSubtypes(imiId);
972 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800973 if (change == PACKAGE_TEMPORARY_CHANGE
974 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800975 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800976 + imi.getComponent());
977 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 }
979 }
980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
Yohei Yukawa94e33302016-02-12 19:37:03 -0800982 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800985
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800986 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800987 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800988 if (change == PACKAGE_TEMPORARY_CHANGE
989 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800990 ServiceInfo si = null;
991 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900992 si = mIPackageManager.getServiceInfo(
993 curIm.getComponent(), 0, mSettings.getCurrentUserId());
994 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800995 }
996 if (si == null) {
997 // Uh oh, current input method is no longer around!
998 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800999 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001000 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001001 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001002 changed = true;
1003 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001004 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001005 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001006 }
1007 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001008 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001009 }
satokab751aa2010-09-14 19:17:36 +09001010
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001011 if (curIm == null) {
1012 // We currently don't have a default input method... is
1013 // one now available?
1014 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001015 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1016 // Even if the current input method is still available, mCurrentSubtype could
1017 // be obsolete when the package is modified in practice.
1018 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001020
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001021 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001022 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
1024 }
1025 }
1026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
Jeff Brownc28867a2013-03-26 15:42:39 -07001028 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001029 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001030 private final IInputMethod mMethod;
1031 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001032
Jeff Brownc28867a2013-03-26 15:42:39 -07001033 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1034 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001035 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001036 mMethod = method;
1037 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001039
satoke7c6998e2011-06-03 17:57:59 +09001040 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001041 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001042 long ident = Binder.clearCallingIdentity();
1043 try {
1044 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1045 } finally {
1046 Binder.restoreCallingIdentity(ident);
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
satok01038492012-04-09 21:08:27 +09001051 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001052 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001053 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001054 public void onHardKeyboardStatusChange(boolean available) {
1055 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1056 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001057 }
1058
Michael Wright7b5a96b2014-08-09 19:28:42 -07001059 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001060 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001061 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001062 }
1063 synchronized(mMethodMap) {
1064 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1065 && mSwitchingDialog.isShowing()) {
1066 mSwitchingDialogTitleView.findViewById(
1067 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1068 available ? View.VISIBLE : View.GONE);
1069 }
1070 }
1071 }
1072 }
1073
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001074 public static final class Lifecycle extends SystemService {
1075 private InputMethodManagerService mService;
1076
1077 public Lifecycle(Context context) {
1078 super(context);
1079 mService = new InputMethodManagerService(context);
1080 }
1081
1082 @Override
1083 public void onStart() {
1084 LocalServices.addService(InputMethodManagerInternal.class,
1085 new LocalServiceImpl(mService.mHandler));
1086 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1087 }
1088
1089 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001090 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001091 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001092 // TODO: Dispatch this to a worker thread as needed.
1093 mService.onSwitchUser(userHandle);
1094 }
1095
1096 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001097 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001098 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001099 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001100 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1101 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1102 .getService(Context.STATUS_BAR_SERVICE);
1103 mService.systemRunning(statusBarService);
1104 }
1105 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001106
1107 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001108 public void onUnlockUser(final @UserIdInt int userHandle) {
1109 // Called on ActivityManager thread.
1110 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001111 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001112 }
1113 }
1114
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001115 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001116 synchronized(mMethodMap) {
1117 final int currentUserId = mSettings.getCurrentUserId();
1118 if (DEBUG) {
1119 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1120 }
1121 if (userId != currentUserId) {
1122 return;
1123 }
1124 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001125 if (mSystemReady) {
1126 // We need to rebuild IMEs.
1127 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1128 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1129 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001130 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001131 }
1132
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001133 void onSwitchUser(@UserIdInt int userId) {
1134 synchronized (mMethodMap) {
1135 switchUserLocked(userId);
1136 }
1137 }
1138
Seigo Nonaka7309b122015-08-17 18:34:13 -07001139 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001140 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001142 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001144 // Note: SettingsObserver doesn't register observers in its constructor.
1145 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 mIWindowManager = IWindowManager.Stub.asInterface(
1147 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001148 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -08001149 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001150 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 public void executeMessage(Message msg) {
1152 handleMessage(msg);
1153 }
Mita Yuned218c72012-12-06 17:18:25 -08001154 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001155 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001156 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +09001157 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001158 mHasFeature = context.getPackageManager().hasSystemFeature(
1159 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001160 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001161 mHardKeyboardBehavior = mContext.getResources().getInteger(
1162 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +09001163
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001164 Bundle extras = new Bundle();
1165 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
1166 mImeSwitcherNotification = new Notification.Builder(mContext)
1167 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1168 .setWhen(0)
1169 .setOngoing(true)
1170 .addExtras(extras)
1171 .setCategory(Notification.CATEGORY_SYSTEM)
1172 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -04001173
satok7cfc0ed2011-06-20 21:29:36 +09001174 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +09001175 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001176
1177 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001178
satok7cfc0ed2011-06-20 21:29:36 +09001179 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001180 int userId = 0;
1181 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001182 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001183 } catch (RemoteException e) {
1184 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1185 }
satok913a8922010-08-26 21:53:41 +09001186
satokd87c2592010-09-29 11:52:06 +09001187 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001188 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -08001189 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001190
Kenny Guy2a764942014-04-02 13:29:20 +01001191 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001192 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001193 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1194 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001195 }
1196
satok5b927c432012-05-01 20:09:34 +09001197 private void resetDefaultImeLocked(Context context) {
1198 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001199 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +09001200 return;
1201 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001202 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001203 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001204 if (suitableImes.isEmpty()) {
1205 Slog.i(TAG, "No default found");
1206 return;
satok5b927c432012-05-01 20:09:34 +09001207 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001208 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001209 if (DEBUG) {
1210 Slog.i(TAG, "Default found, using " + defIm.getId());
1211 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001212 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001213 }
1214
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001215 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001216 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1217 + " currentUserId=" + mSettings.getCurrentUserId());
1218
Yohei Yukawa81482972015-06-04 00:58:59 -07001219 // ContentObserver should be registered again when the user is changed
1220 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001221
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001222 // If the system is not ready or the device is not yed unlocked by the user, then we use
1223 // copy-on-write settings.
1224 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001225 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001226 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001227 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001228 // InputMethodFileManager should be reset when the user is changed
1229 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001230 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001231
1232 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1233 + " defaultImiId=" + defaultImiId);
1234
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001235 // For secondary users, the list of enabled IMEs may not have been updated since the
1236 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1237 // not be empty even if the IME has been uninstalled by the primary user.
1238 // Even in such cases, IMMS works fine because it will find the most applicable
1239 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001240 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001241 mLastSystemLocales = mRes.getConfiguration().getLocales();
1242
1243 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1244 if (mSystemReady) {
1245 hideCurrentInputLocked(0, null);
1246 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER);
1247 buildInputMethodListLocked(initialUserSwitch);
1248 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1249 // This is the first time of the user switch and
1250 // set the current ime to the proper one.
1251 resetDefaultImeLocked(mContext);
1252 }
1253 updateFromSettingsLocked(true);
1254 try {
1255 startInputInnerLocked();
1256 } catch (RuntimeException e) {
1257 Slog.w(TAG, "Unexpected exception", e);
1258 }
1259 }
1260
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001261 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001262 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1263 mSettings.getEnabledInputMethodListLocked(), newUserId,
1264 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001265 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001266
1267 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1268 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001269 }
1270
Kenny Guy2a764942014-04-02 13:29:20 +01001271 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001272 mSettings.setCurrentProfileIds(
1273 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001274 }
1275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 @Override
1277 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1278 throws RemoteException {
1279 try {
1280 return super.onTransact(code, data, reply, flags);
1281 } catch (RuntimeException e) {
1282 // The input method manager only throws security exceptions, so let's
1283 // log all others.
1284 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001285 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287 throw e;
1288 }
1289 }
1290
Svetoslav Ganova0027152013-06-25 14:59:53 -07001291 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001292 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001293 if (DEBUG) {
1294 Slog.d(TAG, "--- systemReady");
1295 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001296 if (!mSystemReady) {
1297 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001298 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001299 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001300 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001301 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001302 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1303 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001304 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001305 if (mStatusBar != null) {
1306 mStatusBar.setIconVisibility(mSlotIme, false);
1307 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001308 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001309 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1310 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001311 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001312 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001313 mHardKeyboardListener);
1314 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001315
1316 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1317 mSettingsObserver.registerContentObserverLocked(currentUserId);
1318
1319 final IntentFilter broadcastFilter = new IntentFilter();
1320 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1321 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
1322 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
1323 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
1324 broadcastFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
1325 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
1326
1327 buildInputMethodListLocked(true /* resetDefaultEnabledIme */);
1328 resetDefaultImeLocked(mContext);
1329 updateFromSettingsLocked(true);
1330 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1331 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1332 mContext.getBasePackageName());
1333
Dianne Hackborncc278702009-09-02 23:07:23 -07001334 try {
1335 startInputInnerLocked();
1336 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001337 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001338 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001339 }
1340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001343 // ---------------------------------------------------------------------------------------
1344 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1345 // 1) it comes from the system process
1346 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1347 private boolean calledFromValidUser() {
1348 final int uid = Binder.getCallingUid();
1349 final int userId = UserHandle.getUserId(uid);
1350 if (DEBUG) {
1351 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1352 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1353 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001354 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1355 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001356 }
Kenny Guy2a764942014-04-02 13:29:20 +01001357 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001358 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001359 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001360
1361 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1362 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1363 // must not manage background users' states in any functions.
1364 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1365 // by a token.
1366 if (mContext.checkCallingOrSelfPermission(
1367 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1368 == PackageManager.PERMISSION_GRANTED) {
1369 if (DEBUG) {
1370 Slog.d(TAG, "--- Access granted because the calling process has "
1371 + "the INTERACT_ACROSS_USERS_FULL permission");
1372 }
1373 return true;
1374 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001375 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001376 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1377 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001378 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001379 }
1380
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001381
1382 /**
1383 * Returns true iff the caller is identified to be the current input method with the token.
1384 * @param token The window token given to the input method when it was started.
1385 * @return true if and only if non-null valid token is specified.
1386 */
Yohei Yukawad0332832017-02-01 13:59:43 -08001387 private boolean calledWithValidToken(@Nullable IBinder token) {
1388 if (token == null && Binder.getCallingPid() == Process.myPid()) {
1389 if (DEBUG) {
1390 // TODO(b/34851776): Basically it's the caller's fault if we reach here.
1391 Slog.d(TAG, "Bug 34851776 is detected callers=" + Debug.getCallers(10));
1392 }
1393 return false;
1394 }
1395 if (token == null || token != mCurToken) {
1396 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
1397 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1398 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001399 return false;
1400 }
1401 return true;
1402 }
1403
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001404 private boolean bindCurrentInputMethodService(
1405 Intent service, ServiceConnection conn, int flags) {
1406 if (service == null || conn == null) {
1407 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1408 return false;
1409 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001410 return mContext.bindServiceAsUser(service, conn, flags,
1411 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001412 }
1413
satoke7c6998e2011-06-03 17:57:59 +09001414 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001416 // TODO: Make this work even for non-current users?
1417 if (!calledFromValidUser()) {
1418 return Collections.emptyList();
1419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001421 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423 }
1424
satoke7c6998e2011-06-03 17:57:59 +09001425 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001427 // TODO: Make this work even for non-current users?
1428 if (!calledFromValidUser()) {
1429 return Collections.emptyList();
1430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001432 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 }
1434 }
1435
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001436 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001437 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001438 * @return enabled subtypes of the specified imi
1439 */
satoke7c6998e2011-06-03 17:57:59 +09001440 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001441 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001442 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001443 // TODO: Make this work even for non-current users?
1444 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001445 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001446 }
satok67ddf9c2010-11-17 09:45:54 +09001447 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001448 final InputMethodInfo imi;
1449 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001450 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001451 } else {
1452 imi = mMethodMap.get(imiId);
1453 }
1454 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001455 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001456 }
1457 return mSettings.getEnabledInputMethodSubtypeListLocked(
1458 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001459 }
1460 }
1461
satoke7c6998e2011-06-03 17:57:59 +09001462 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 public void addClient(IInputMethodClient client,
1464 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001465 if (!calledFromValidUser()) {
1466 return;
1467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 synchronized (mMethodMap) {
1469 mClients.put(client.asBinder(), new ClientState(client,
1470 inputContext, uid, pid));
1471 }
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
satoke7c6998e2011-06-03 17:57:59 +09001474 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001476 if (!calledFromValidUser()) {
1477 return;
1478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001480 ClientState cs = mClients.remove(client.asBinder());
1481 if (cs != null) {
1482 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001483 if (mCurClient == cs) {
1484 mCurClient = null;
1485 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001486 if (mCurFocusedWindowClient == cs) {
1487 mCurFocusedWindowClient = null;
1488 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 void executeOrSendMessage(IInterface target, Message msg) {
1494 if (target.asBinder() instanceof Binder) {
1495 mCaller.sendMessage(msg);
1496 } else {
1497 handleMessage(msg);
1498 msg.recycle();
1499 }
1500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001501
Yohei Yukawa33e81792015-11-17 21:14:42 -08001502 void unbindCurrentClientLocked(
1503 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001505 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 + mCurClient.client.asBinder());
1507 if (mBoundToMethod) {
1508 mBoundToMethod = false;
1509 if (mCurMethod != null) {
1510 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1511 MSG_UNBIND_INPUT, mCurMethod));
1512 }
1513 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001514
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001515 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1516 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001517 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1518 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
The Android Open Source Project10592532009-03-18 17:39:46 -07001522 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 private int getImeShowFlags() {
1527 int flags = 0;
1528 if (mShowForced) {
1529 flags |= InputMethod.SHOW_FORCED
1530 | InputMethod.SHOW_EXPLICIT;
1531 } else if (mShowExplicitlyRequested) {
1532 flags |= InputMethod.SHOW_EXPLICIT;
1533 }
1534 return flags;
1535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 private int getAppShowFlags() {
1538 int flags = 0;
1539 if (mShowForced) {
1540 flags |= InputMethodManager.SHOW_FORCED;
1541 } else if (!mShowExplicitlyRequested) {
1542 flags |= InputMethodManager.SHOW_IMPLICIT;
1543 }
1544 return flags;
1545 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001546
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001547 InputBindResult attachNewInputLocked(
1548 /* @InputMethodClient.StartInputReason */ final int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 if (!mBoundToMethod) {
1550 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1551 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1552 mBoundToMethod = true;
1553 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001554
1555 final Binder startInputToken = new Binder();
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001556 final StartInputInfo info = new StartInputInfo(mCurToken, mCurId, startInputReason,
1557 !initial, mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode,
1558 mCurSeq);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001559 mStartInputMap.put(startInputToken, info);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001560 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001563 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08001564 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08001565 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001567 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001568 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001570 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001571 (session.channel != null ? session.channel.dup() : null),
1572 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001574
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001575 InputBindResult startInputLocked(
1576 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001577 IInputMethodClient client, IInputContext inputContext,
1578 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001579 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 // If no method is currently selected, do nothing.
1581 if (mCurMethodId == null) {
1582 return mNoBinding;
1583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 ClientState cs = mClients.get(client.asBinder());
1586 if (cs == null) {
1587 throw new IllegalArgumentException("unknown client "
1588 + client.asBinder());
1589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001590
Yohei Yukawa74750f22016-03-22 12:54:22 -07001591 if (attribute == null) {
1592 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1593 + " uid=" + cs.uid + " pid=" + cs.pid);
1594 return null;
1595 }
1596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 try {
1598 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1599 // Check with the window manager to make sure this client actually
1600 // has a window with focus. If not, reject. This is thread safe
1601 // because if the focus changes some time before or after, the
1602 // next client receiving focus that has any interest in input will
1603 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08001604 if (DEBUG) {
1605 Slog.w(TAG, "Starting input on non-focused client " + cs.client
1606 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 return null;
1609 }
1610 } catch (RemoteException e) {
1611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001612
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001613 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001614 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001615 }
1616
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001617 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001618 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001619 @NonNull EditorInfo attribute, int controlFlags,
1620 /* @InputMethodClient.StartInputReason */ final int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001621 // If no method is currently selected, do nothing.
1622 if (mCurMethodId == null) {
1623 return mNoBinding;
1624 }
1625
Yohei Yukawad57ba672015-06-08 16:39:46 -07001626 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1627 attribute.packageName)) {
1628 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1629 + " uid=" + cs.uid + " package=" + attribute.packageName);
1630 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001631 }
1632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001634 // Was the keyguard locked when switching over to the new client?
1635 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 // If the client is changing, we need to switch over to the new
1637 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001638 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001639 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001640 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641
1642 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001643 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001644 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001645 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 // Bump up the sequence for this client and attach it.
1650 mCurSeq++;
1651 if (mCurSeq <= 0) mCurSeq = 1;
1652 mCurClient = cs;
1653 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001654 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 // Check if the input method is changing.
1658 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1659 if (cs.curSession != null) {
1660 // Fast case: if we are already connected to the input method,
1661 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001662 return attachNewInputLocked(startInputReason,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001663 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
1665 if (mHaveConnection) {
1666 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 // Return to client, and we will get back with it when
1668 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001669 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001670 return new InputBindResult(null, null, mCurId, mCurSeq,
1671 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 } else if (SystemClock.uptimeMillis()
1673 < (mLastBindTime+TIME_TO_RECONNECT)) {
1674 // In this case we have connected to the service, but
1675 // don't yet have its interface. If it hasn't been too
1676 // long since we did the connection, we'll return to
1677 // the client and wait to get the service interface so
1678 // we can report back. If it has been too long, we want
1679 // to fall through so we can try a disconnect/reconnect
1680 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001681 return new InputBindResult(null, null, mCurId, mCurSeq,
1682 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001684 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1685 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687 }
1688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001689
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001690 return startInputInnerLocked();
1691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001692
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001693 InputBindResult startInputInnerLocked() {
1694 if (mCurMethodId == null) {
1695 return mNoBinding;
1696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001697
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001698 if (!mSystemReady) {
1699 // If the system is not yet ready, we shouldn't be running third
1700 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001701 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1702 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1706 if (info == null) {
1707 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001710 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1713 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001714 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1715 com.android.internal.R.string.input_method_binding_label);
1716 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1717 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001718 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001719 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1720 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 mLastBindTime = SystemClock.uptimeMillis();
1722 mHaveConnection = true;
1723 mCurId = info.getId();
1724 mCurToken = new Binder();
1725 try {
Yohei Yukawad0332832017-02-01 13:59:43 -08001726 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001727 mIWindowManager.addWindowToken(mCurToken, TYPE_INPUT_METHOD, DEFAULT_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 } catch (RemoteException e) {
1729 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001730 return new InputBindResult(null, null, mCurId, mCurSeq,
1731 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 } else {
1733 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001734 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 + mCurIntent);
1736 }
1737 return null;
1738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001740 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001741 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001742 IInputMethodClient client, IInputContext inputContext,
1743 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001744 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001745 if (!calledFromValidUser()) {
1746 return null;
1747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001749 if (DEBUG) {
1750 Slog.v(TAG, "startInput: reason="
1751 + InputMethodClient.getStartInputReason(startInputReason)
1752 + " client = " + client.asBinder()
1753 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001754 + " missingMethods="
1755 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001756 + " attribute=" + attribute
1757 + " controlFlags=#" + Integer.toHexString(controlFlags));
1758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 final long ident = Binder.clearCallingIdentity();
1760 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001761 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1762 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 } finally {
1764 Binder.restoreCallingIdentity(ident);
1765 }
1766 }
1767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001768
satoke7c6998e2011-06-03 17:57:59 +09001769 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 public void finishInput(IInputMethodClient client) {
1771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001772
satoke7c6998e2011-06-03 17:57:59 +09001773 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 public void onServiceConnected(ComponentName name, IBinder service) {
1775 synchronized (mMethodMap) {
1776 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1777 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001778 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001779 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001780 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001781 return;
1782 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001783 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001784 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1785 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001787 clearClientSessionLocked(mCurClient);
1788 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790 }
1791 }
1792 }
1793
Jeff Brownc28867a2013-03-26 15:42:39 -07001794 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1795 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 synchronized (mMethodMap) {
1797 if (mCurMethod != null && method != null
1798 && mCurMethod.asBinder() == method.asBinder()) {
1799 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001800 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001802 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08001803 InputBindResult res = attachNewInputLocked(
1804 InputMethodClient.START_INPUT_REASON_SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (res.method != null) {
1806 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001807 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001809 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 }
1811 }
1812 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001813
1814 // Session abandoned. Close its associated input channel.
1815 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001817
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001818 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001819 if (mVisibleBound) {
1820 mContext.unbindService(mVisibleConnection);
1821 mVisibleBound = false;
1822 }
1823
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001824 if (mHaveConnection) {
1825 mContext.unbindService(this);
1826 mHaveConnection = false;
1827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001828
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001829 if (mCurToken != null) {
1830 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001831 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001832 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001833 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001834 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001835 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001836 mIWindowManager.removeWindowToken(mCurToken, DEFAULT_DISPLAY);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001837 } catch (RemoteException e) {
1838 }
1839 mCurToken = null;
1840 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001841
The Android Open Source Project10592532009-03-18 17:39:46 -07001842 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001843 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001844 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001845
Yohei Yukawa33e81792015-11-17 21:14:42 -08001846 void resetCurrentMethodAndClient(
1847 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001848 mCurMethodId = null;
1849 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001850 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001851 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001852
1853 void requestClientSessionLocked(ClientState cs) {
1854 if (!cs.sessionRequested) {
1855 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1856 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1857 cs.sessionRequested = true;
1858 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1859 MSG_CREATE_SESSION, mCurMethod, channels[1],
1860 new MethodCallback(this, mCurMethod, channels[0])));
1861 }
1862 }
1863
1864 void clearClientSessionLocked(ClientState cs) {
1865 finishSessionLocked(cs.curSession);
1866 cs.curSession = null;
1867 cs.sessionRequested = false;
1868 }
1869
1870 private void finishSessionLocked(SessionState sessionState) {
1871 if (sessionState != null) {
1872 if (sessionState.session != null) {
1873 try {
1874 sessionState.session.finishSession();
1875 } catch (RemoteException e) {
1876 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001877 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001878 }
1879 sessionState.session = null;
1880 }
1881 if (sessionState.channel != null) {
1882 sessionState.channel.dispose();
1883 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001884 }
1885 }
1886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001887
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001888 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 if (mCurMethod != null) {
1890 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001891 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001893
Jeff Brownc28867a2013-03-26 15:42:39 -07001894 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001895 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 mCurMethod = null;
1897 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001898 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001899 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001900 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08001901 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001903
satoke7c6998e2011-06-03 17:57:59 +09001904 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001906 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
1907 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
1908 // which is irregular but can eventually happen for everyone just by continuing using the
1909 // device. Thus it is important to make sure that all the internal states are properly
1910 // refreshed when this method is called back. Running
1911 // adb install -r <APK that implements the current IME>
1912 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001914 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 + " mCurIntent=" + mCurIntent);
1916 if (mCurMethod != null && mCurIntent != null
1917 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001918 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 // We consider this to be a new bind attempt, since the system
1920 // should now try to restart the service for us.
1921 mLastBindTime = SystemClock.uptimeMillis();
1922 mShowRequested = mInputShown;
1923 mInputShown = false;
Yohei Yukawa817d5f72017-01-04 20:15:02 -08001924 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926 }
1927 }
1928
satokf9f01002011-05-19 21:31:50 +09001929 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001931 synchronized (mMethodMap) {
1932 if (!calledWithValidToken(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001933 return;
1934 }
1935 final long ident = Binder.clearCallingIdentity();
1936 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001938 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001939 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001940 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001943 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001944 CharSequence contentDescription = null;
1945 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001946 // Use PackageManager to load label
1947 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001948 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001949 mIPackageManager.getApplicationInfo(packageName, 0,
1950 mSettings.getCurrentUserId()));
1951 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001952 /* ignore */
1953 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001954 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001955 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001956 contentDescription != null
1957 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001958 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08001961 } finally {
1962 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
1965 }
1966
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001967 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001968 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001969 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001970 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001971 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001972 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001973 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1974 // When physical keyboard is attached, we show the ime switcher (or notification if
1975 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1976 // exists in the IME switcher dialog. Might be OK to remove this condition once
1977 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1978 return true;
1979 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001980 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1981 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001982 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001983
1984 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1985 final int N = imis.size();
1986 if (N > 2) return true;
1987 if (N < 1) return false;
1988 int nonAuxCount = 0;
1989 int auxCount = 0;
1990 InputMethodSubtype nonAuxSubtype = null;
1991 InputMethodSubtype auxSubtype = null;
1992 for(int i = 0; i < N; ++i) {
1993 final InputMethodInfo imi = imis.get(i);
1994 final List<InputMethodSubtype> subtypes =
1995 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1996 final int subtypeCount = subtypes.size();
1997 if (subtypeCount == 0) {
1998 ++nonAuxCount;
1999 } else {
2000 for (int j = 0; j < subtypeCount; ++j) {
2001 final InputMethodSubtype subtype = subtypes.get(j);
2002 if (!subtype.isAuxiliary()) {
2003 ++nonAuxCount;
2004 nonAuxSubtype = subtype;
2005 } else {
2006 ++auxCount;
2007 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002008 }
2009 }
satok7cfc0ed2011-06-20 21:29:36 +09002010 }
2011 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002012 if (nonAuxCount > 1 || auxCount > 1) {
2013 return true;
2014 } else if (nonAuxCount == 1 && auxCount == 1) {
2015 if (nonAuxSubtype != null && auxSubtype != null
2016 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2017 || auxSubtype.overridesImplicitlyEnabledSubtype()
2018 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2019 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2020 return false;
2021 }
2022 return true;
2023 }
2024 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002025 }
2026
John Spurlocke0980502013-10-25 11:59:29 -04002027 private boolean isKeyguardLocked() {
2028 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2029 }
2030
satokdbf29502011-08-25 15:28:23 +09002031 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09002032 @Override
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002033 public void setImeWindowStatus(IBinder token, IBinder startInputToken, int vis,
2034 int backDisposition) {
2035 if (startInputToken == null) {
2036 throw new InvalidParameterException("startInputToken cannot be null");
2037 }
2038
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002039 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002040 return;
2041 }
2042
Yohei Yukawa69e68022017-02-13 12:04:50 -08002043 final StartInputInfo info;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002044 synchronized (mMethodMap) {
Yohei Yukawa69e68022017-02-13 12:04:50 -08002045 info = mStartInputMap.get(startInputToken);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002046 if (info == null) {
2047 throw new InvalidParameterException("Unknown startInputToken=" + startInputToken);
2048 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002049 mImeWindowVis = vis;
2050 mBackDisposition = backDisposition;
2051 updateSystemUiLocked(token, vis, backDisposition);
2052 }
Yohei Yukawa69e68022017-02-13 12:04:50 -08002053 mWindowManagerInternal.updateInputMethodWindowStatus(info.mImeToken,
2054 (vis & InputMethodService.IME_VISIBLE) != 0, info.mTargetWindow);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002055 }
2056
2057 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
2058 synchronized (mMethodMap) {
2059 updateSystemUiLocked(token, vis, backDisposition);
2060 }
2061 }
2062
2063 // Caution! This method is called in this class. Handle multi-user carefully
2064 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
2065 if (!calledWithValidToken(token)) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002066 return;
2067 }
2068
2069 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2070 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002071 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002072 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002073 // apply policy for binder calls
2074 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2075 vis = 0;
satok06487a52010-10-29 11:37:18 +09002076 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002077 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2078 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2079 if (mStatusBar != null) {
2080 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
2081 needsToShowImeSwitcher);
2082 }
2083 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2084 if (imi != null && needsToShowImeSwitcher) {
2085 // Used to load label
2086 final CharSequence title = mRes.getText(
2087 com.android.internal.R.string.select_input_method);
2088 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2089 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002090 mImeSwitcherNotification.setContentTitle(title)
2091 .setContentText(summary)
2092 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002093 try {
2094 if ((mNotificationManager != null)
2095 && !mIWindowManager.hasNavigationBar()) {
2096 if (DEBUG) {
2097 Slog.d(TAG, "--- show notification: label = " + summary);
2098 }
2099 mNotificationManager.notifyAsUser(null,
2100 com.android.internal.R.string.select_input_method,
2101 mImeSwitcherNotification.build(), UserHandle.ALL);
2102 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002103 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002104 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002105 }
2106 } else {
2107 if (mNotificationShown && mNotificationManager != null) {
2108 if (DEBUG) {
2109 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002110 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002111 mNotificationManager.cancelAsUser(null,
2112 com.android.internal.R.string.select_input_method, UserHandle.ALL);
2113 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002114 }
satok06487a52010-10-29 11:37:18 +09002115 }
2116 } finally {
2117 Binder.restoreCallingIdentity(ident);
2118 }
2119 }
2120
satoke7c6998e2011-06-03 17:57:59 +09002121 @Override
satokf9f01002011-05-19 21:31:50 +09002122 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002123 if (!calledFromValidUser()) {
2124 return;
2125 }
satokf9f01002011-05-19 21:31:50 +09002126 synchronized (mMethodMap) {
2127 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2128 for (int i = 0; i < spans.length; ++i) {
2129 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09002130 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09002131 mSecureSuggestionSpans.put(ss, currentImi);
2132 }
2133 }
2134 }
2135 }
2136
satoke7c6998e2011-06-03 17:57:59 +09002137 @Override
satokf9f01002011-05-19 21:31:50 +09002138 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002139 if (!calledFromValidUser()) {
2140 return false;
2141 }
satokf9f01002011-05-19 21:31:50 +09002142 synchronized (mMethodMap) {
2143 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
2144 // TODO: Do not send the intent if the process of the targetImi is already dead.
2145 if (targetImi != null) {
2146 final String[] suggestions = span.getSuggestions();
2147 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09002148 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09002149 final Intent intent = new Intent();
2150 // Ensures that only a class in the original IME package will receive the
2151 // notification.
satok42c5a162011-05-26 16:46:14 +09002152 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09002153 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
2154 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
2155 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
2156 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07002157 final long ident = Binder.clearCallingIdentity();
2158 try {
2159 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
2160 } finally {
2161 Binder.restoreCallingIdentity(ident);
2162 }
satokf9f01002011-05-19 21:31:50 +09002163 return true;
2164 }
2165 }
2166 return false;
2167 }
2168
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002169 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002170 updateInputMethodsFromSettingsLocked(enabledMayChange);
2171 updateKeyboardFromSettingsLocked();
2172 }
2173
2174 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002175 if (enabledMayChange) {
2176 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2177 for (int i=0; i<enabled.size(); i++) {
2178 // We allow the user to select "disabled until used" apps, so if they
2179 // are enabling one of those here we now need to make it enabled.
2180 InputMethodInfo imm = enabled.get(i);
2181 try {
2182 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2183 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2184 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002185 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002186 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002187 if (DEBUG) {
2188 Slog.d(TAG, "Update state(" + imm.getId()
2189 + "): DISABLED_UNTIL_USED -> DEFAULT");
2190 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002191 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2192 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002193 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2194 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002195 }
2196 } catch (RemoteException e) {
2197 }
2198 }
2199 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002200 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2201 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2202 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2203 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002204 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002205 // There is no input method selected, try to choose new applicable input method.
2206 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002207 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002208 }
2209 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002211 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002213 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08002214 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
satokf3db1af2010-11-23 13:34:33 +09002216 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002217 } else {
2218 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08002219 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002221 // Here is not the perfect place to reset the switching controller. Ideally
2222 // mSwitchingController and mSettings should be able to share the same state.
2223 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2224 // the same enabled IMEs list.
2225 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002226
2227 }
2228
2229 public void updateKeyboardFromSettingsLocked() {
2230 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2231 if (mSwitchingDialog != null
2232 && mSwitchingDialogTitleView != null
2233 && mSwitchingDialog.isShowing()) {
2234 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2235 com.android.internal.R.id.hard_keyboard_switch);
2236 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002239
Yohei Yukawab097b822015-12-01 10:43:08 -08002240 private void notifyInputMethodSubtypeChanged(final int userId,
2241 @Nullable final InputMethodInfo inputMethodInfo,
2242 @Nullable final InputMethodSubtype subtype) {
2243 final InputManagerInternal inputManagerInternal =
2244 LocalServices.getService(InputManagerInternal.class);
2245 if (inputManagerInternal != null) {
2246 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
2247 }
2248 }
2249
satokab751aa2010-09-14 19:17:36 +09002250 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 InputMethodInfo info = mMethodMap.get(id);
2252 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002253 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002255
satokd81e9502012-05-21 12:58:45 +09002256 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002258 final int subtypeCount = info.getSubtypeCount();
2259 if (subtypeCount <= 0) {
2260 return;
satokcd7cd292010-11-20 15:46:23 +09002261 }
satokd81e9502012-05-21 12:58:45 +09002262 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2263 final InputMethodSubtype newSubtype;
2264 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2265 newSubtype = info.getSubtypeAt(subtypeId);
2266 } else {
2267 // If subtype is null, try to find the most applicable one from
2268 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002269 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002270 }
2271 if (newSubtype == null || oldSubtype == null) {
2272 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2273 + ", new subtype = " + newSubtype);
2274 return;
2275 }
2276 if (newSubtype != oldSubtype) {
2277 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2278 if (mCurMethod != null) {
2279 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002280 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002281 mCurMethod.changeInputMethodSubtype(newSubtype);
2282 } catch (RemoteException e) {
2283 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002284 return;
satokab751aa2010-09-14 19:17:36 +09002285 }
2286 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002287 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 return;
2290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002291
satokd81e9502012-05-21 12:58:45 +09002292 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 final long ident = Binder.clearCallingIdentity();
2294 try {
satokab751aa2010-09-14 19:17:36 +09002295 // Set a subtype to this input method.
2296 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002297 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2298 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2299 // because mCurMethodId is stored as a history in
2300 // setSelectedInputMethodAndSubtypeLocked().
2301 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07002303 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002305 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002307 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002309 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 } finally {
2311 Binder.restoreCallingIdentity(ident);
2312 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002313
2314 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2315 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002317
satok42c5a162011-05-26 16:46:14 +09002318 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002319 public boolean showSoftInput(IInputMethodClient client, int flags,
2320 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002321 if (!calledFromValidUser()) {
2322 return false;
2323 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002324 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 long ident = Binder.clearCallingIdentity();
2326 try {
2327 synchronized (mMethodMap) {
2328 if (mCurClient == null || client == null
2329 || mCurClient.client.asBinder() != client.asBinder()) {
2330 try {
2331 // We need to check if this is the current client with
2332 // focus in the window manager, to allow this call to
2333 // be made before input is started in it.
2334 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002335 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002336 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 }
2338 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002339 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 }
2341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002342
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002343 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002344 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 }
2346 } finally {
2347 Binder.restoreCallingIdentity(ident);
2348 }
2349 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002350
The Android Open Source Project4df24232009-03-05 14:34:35 -08002351 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002353 if (mAccessibilityRequestingNoSoftKeyboard) {
2354 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 }
Anna Galusza9b278112016-01-04 11:37:37 -08002356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2358 mShowExplicitlyRequested = true;
2359 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002360 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2361 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002363
Dianne Hackborncc278702009-09-02 23:07:23 -07002364 if (!mSystemReady) {
2365 return false;
2366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
The Android Open Source Project4df24232009-03-05 14:34:35 -08002368 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002370 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002371 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2372 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2373 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002375 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002376 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002377 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002378 | Context.BIND_TREAT_LIKE_ACTIVITY
2379 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002380 mVisibleBound = true;
2381 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002382 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002384 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 // The client has asked to have the input method shown, but
2386 // we have been sitting here too long with a connection to the
2387 // service and no interface received, so let's disconnect/connect
2388 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002389 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002391 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002393 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002394 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002395 } else {
2396 if (DEBUG) {
2397 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2398 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002401
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
The Android Open Source Project4df24232009-03-05 14:34:35 -08002406 public boolean hideSoftInput(IInputMethodClient client, int flags,
2407 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002408 if (!calledFromValidUser()) {
2409 return false;
2410 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002411 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 long ident = Binder.clearCallingIdentity();
2413 try {
2414 synchronized (mMethodMap) {
2415 if (mCurClient == null || client == null
2416 || mCurClient.client.asBinder() != client.asBinder()) {
2417 try {
2418 // We need to check if this is the current client with
2419 // focus in the window manager, to allow this call to
2420 // be made before input is started in it.
2421 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002422 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2423 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002424 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
2426 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002427 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 }
2429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002430
Joe Onorato8a9b2202010-02-26 18:56:32 -08002431 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002432 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 }
2434 } finally {
2435 Binder.restoreCallingIdentity(ident);
2436 }
2437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002438
The Android Open Source Project4df24232009-03-05 14:34:35 -08002439 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2441 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002442 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 -08002443 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
2445 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002446 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 -08002447 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002449
2450 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2451 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2452 // to be updated with the new value sent from IME process. Even in such a transient state
2453 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2454 // application process as a valid request, and have even promised such a behavior with CTS
2455 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2456 // IMMS#InputShown indicates that the software keyboard is shown.
2457 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2458 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2459 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002460 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002461 if (shouldHideSoftInput) {
2462 // The IME will report its visible state again after the following message finally
2463 // delivered to the IME process as an IPC. Hence the inconsistency between
2464 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2465 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002466 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2467 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2468 res = true;
2469 } else {
2470 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002472 if (mHaveConnection && mVisibleBound) {
2473 mContext.unbindService(mVisibleConnection);
2474 mVisibleBound = false;
2475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 mInputShown = false;
2477 mShowRequested = false;
2478 mShowExplicitlyRequested = false;
2479 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002480 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002482
satok42c5a162011-05-26 16:46:14 +09002483 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002484 public InputBindResult startInputOrWindowGainedFocus(
2485 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2486 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002487 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002488 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002489 if (windowToken != null) {
2490 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002491 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002492 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002493 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2494 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002495 }
2496 }
2497
2498 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002499 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa22dac1c2017-02-12 16:54:16 -08002500 IInputMethodClient client, IBinder windowToken, int controlFlags,
2501 /* @android.view.WindowManager.LayoutParams.SoftInputModeFlags */ int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002502 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2503 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002504 // Needs to check the validity before clearing calling identity
2505 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002506 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 long ident = Binder.clearCallingIdentity();
2508 try {
2509 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002510 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2511 + InputMethodClient.getStartInputReason(startInputReason)
2512 + " client=" + client.asBinder()
2513 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002514 + " missingMethods="
2515 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002516 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002517 + " controlFlags=#" + Integer.toHexString(controlFlags)
Yohei Yukawa22a89232017-02-12 16:38:59 -08002518 + " softInputMode=" + InputMethodClient.softInputModeToString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002519 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002520
Dianne Hackborn7663d802012-02-24 13:08:49 -08002521 ClientState cs = mClients.get(client.asBinder());
2522 if (cs == null) {
2523 throw new IllegalArgumentException("unknown client "
2524 + client.asBinder());
2525 }
2526
2527 try {
2528 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2529 // Check with the window manager to make sure this client actually
2530 // has a window with focus. If not, reject. This is thread safe
2531 // because if the focus changes some time before or after, the
2532 // next client receiving focus that has any interest in input will
2533 // be calling through here after that change happens.
Yohei Yukawad0332832017-02-01 13:59:43 -08002534 if (DEBUG) {
2535 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2536 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2537 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002538 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002540 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002542
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002543 if (!calledFromValidUser) {
2544 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2545 Slog.w(TAG, "If you want to interect with IME, you need "
2546 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2547 hideCurrentInputLocked(0, null);
2548 return null;
2549 }
2550
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002551 if (mCurFocusedWindow == windowToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08002552 if (DEBUG) {
2553 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
2554 + " attribute=" + attribute + ", token = " + windowToken);
2555 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002556 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002557 return startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002558 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002559 }
2560 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002561 }
2562 mCurFocusedWindow = windowToken;
Yohei Yukawa22a89232017-02-12 16:38:59 -08002563 mCurFocusedWindowSoftInputMode = softInputMode;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002564 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002565
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002566 // Should we auto-show the IME even if the caller has not
2567 // specified what should be done with it?
2568 // We only do this automatically if the window can resize
2569 // to accommodate the IME (so what the user sees will give
2570 // them good context without input information being obscured
2571 // by the IME) or if running on a large screen where there
2572 // is more room for the target window + IME.
2573 final boolean doAutoShow =
2574 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2575 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2576 || mRes.getConfiguration().isLayoutSizeAtLeast(
2577 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002578 final boolean isTextEditor =
2579 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2580
2581 // We want to start input before showing the IME, but after closing
2582 // it. We want to do this after closing it to help the IME disappear
2583 // more quickly (not get stuck behind it initializing itself for the
2584 // new focused input, even if its window wants to hide the IME).
2585 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2588 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002589 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2591 // There is no focus view, and this window will
2592 // be behind any soft input window, so hide the
2593 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002594 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002595 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002597 } else if (isTextEditor && doAutoShow && (softInputMode &
2598 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 // There is a focus view, and we are navigating forward
2600 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002601 // We only do this automatically if the window can resize
2602 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002603 // them good context without input information being obscured
2604 // by the IME) or if running on a large screen where there
2605 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002606 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002607 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002608 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002609 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002610 didStart = true;
2611 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002612 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 }
2614 break;
2615 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2616 // Do nothing.
2617 break;
2618 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2619 if ((softInputMode &
2620 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002621 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002622 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 }
2624 break;
2625 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002626 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002627 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 break;
2629 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2630 if ((softInputMode &
2631 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002632 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002633 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002634 res = startInputUncheckedLocked(cs, inputContext,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002635 missingMethods, attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002636 didStart = true;
2637 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002638 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 }
2640 break;
2641 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002642 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002643 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002644 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002645 attribute, controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002646 didStart = true;
2647 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002648 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 break;
2650 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002651
2652 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002653 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002654 controlFlags, startInputReason);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 }
2657 } finally {
2658 Binder.restoreCallingIdentity(ident);
2659 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002660
2661 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002663
satok42c5a162011-05-26 16:46:14 +09002664 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002665 public void showInputMethodPickerFromClient(
2666 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002667 if (!calledFromValidUser()) {
2668 return;
2669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 synchronized (mMethodMap) {
2671 if (mCurClient == null || client == null
2672 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002673 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002674 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 }
2676
satok440aab52010-11-25 09:43:11 +09002677 // Always call subtype picker, because subtype picker is a superset of input method
2678 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002679 mHandler.sendMessage(mCaller.obtainMessageI(
2680 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002681 }
2682 }
2683
satok42c5a162011-05-26 16:46:14 +09002684 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002686 if (!calledFromValidUser()) {
2687 return;
2688 }
satok28203512010-11-24 11:06:49 +09002689 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2690 }
2691
satok42c5a162011-05-26 16:46:14 +09002692 @Override
satok28203512010-11-24 11:06:49 +09002693 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002694 if (!calledFromValidUser()) {
2695 return;
2696 }
satok28203512010-11-24 11:06:49 +09002697 synchronized (mMethodMap) {
2698 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002699 setInputMethodWithSubtypeIdLocked(token, id,
2700 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2701 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002702 } else {
2703 setInputMethod(token, id);
2704 }
2705 }
satokab751aa2010-09-14 19:17:36 +09002706 }
2707
satok42c5a162011-05-26 16:46:14 +09002708 @Override
satokb416a712010-11-25 20:42:14 +09002709 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002710 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002711 if (!calledFromValidUser()) {
2712 return;
2713 }
satokb416a712010-11-25 20:42:14 +09002714 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002715 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2716 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002717 }
2718 }
2719
satok4fc87d62011-05-20 16:13:43 +09002720 @Override
satok735cf382010-11-11 20:40:09 +09002721 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002722 if (!calledFromValidUser()) {
2723 return false;
2724 }
satok735cf382010-11-11 20:40:09 +09002725 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002726 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002727 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002728 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002729 lastImi = mMethodMap.get(lastIme.first);
2730 } else {
2731 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002732 }
satok4fc87d62011-05-20 16:13:43 +09002733 String targetLastImiId = null;
2734 int subtypeId = NOT_A_SUBTYPE_ID;
2735 if (lastIme != null && lastImi != null) {
2736 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002737 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002738 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2739 : mCurrentSubtype.hashCode();
2740 // If the last IME is the same as the current IME and the last subtype is not
2741 // defined, there is no need to switch to the last IME.
2742 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2743 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002744 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002745 }
2746 }
2747
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002748 if (TextUtils.isEmpty(targetLastImiId)
2749 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002750 // This is a safety net. If the currentSubtype can't be added to the history
2751 // and the framework couldn't find the last ime, we will make the last ime be
2752 // the most applicable enabled keyboard subtype of the system imes.
2753 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2754 if (enabled != null) {
2755 final int N = enabled.size();
2756 final String locale = mCurrentSubtype == null
2757 ? mRes.getConfiguration().locale.toString()
2758 : mCurrentSubtype.getLocale();
2759 for (int i = 0; i < N; ++i) {
2760 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002761 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002762 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002763 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2764 InputMethodUtils.getSubtypes(imi),
2765 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002766 if (keyboardSubtype != null) {
2767 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002768 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002769 imi, keyboardSubtype.hashCode());
2770 if(keyboardSubtype.getLocale().equals(locale)) {
2771 break;
2772 }
2773 }
2774 }
2775 }
2776 }
2777 }
2778
2779 if (!TextUtils.isEmpty(targetLastImiId)) {
2780 if (DEBUG) {
2781 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2782 + ", from: " + mCurMethodId + ", " + subtypeId);
2783 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002784 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002785 return true;
2786 } else {
2787 return false;
2788 }
satok735cf382010-11-11 20:40:09 +09002789 }
2790 }
2791
satoke7c6998e2011-06-03 17:57:59 +09002792 @Override
satok688bd472012-02-09 20:09:17 +09002793 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002794 if (!calledFromValidUser()) {
2795 return false;
2796 }
satok688bd472012-02-09 20:09:17 +09002797 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002798 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002799 return false;
2800 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002801 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002802 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2803 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002804 if (nextSubtype == null) {
2805 return false;
2806 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002807 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2808 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002809 return true;
2810 }
2811 }
2812
2813 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002814 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2815 if (!calledFromValidUser()) {
2816 return false;
2817 }
2818 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002819 if (!calledWithValidToken(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002820 return false;
2821 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002822 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002823 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2824 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002825 if (nextSubtype == null) {
2826 return false;
2827 }
2828 return true;
2829 }
2830 }
2831
2832 @Override
satok68f1b782011-04-11 14:26:04 +09002833 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002834 if (!calledFromValidUser()) {
2835 return null;
2836 }
satok68f1b782011-04-11 14:26:04 +09002837 synchronized (mMethodMap) {
2838 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2839 // TODO: Handle the case of the last IME with no subtypes
2840 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2841 || TextUtils.isEmpty(lastIme.second)) return null;
2842 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2843 if (lastImi == null) return null;
2844 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002845 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002846 final int lastSubtypeId =
2847 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002848 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2849 return null;
2850 }
2851 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002852 } catch (NumberFormatException e) {
2853 return null;
2854 }
2855 }
2856 }
2857
satoke7c6998e2011-06-03 17:57:59 +09002858 @Override
satokee5e77c2011-09-02 18:50:15 +09002859 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002860 if (!calledFromValidUser()) {
2861 return;
2862 }
satok91e88122011-07-18 11:11:42 +09002863 // By this IPC call, only a process which shares the same uid with the IME can add
2864 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002865 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002866 synchronized (mMethodMap) {
Yohei Yukawa79247822017-01-23 15:26:15 -08002867 if (!mSystemReady) {
2868 return;
2869 }
satok91e88122011-07-18 11:11:42 +09002870 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002871 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002872 final String[] packageInfos;
2873 try {
2874 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2875 } catch (RemoteException e) {
2876 Slog.e(TAG, "Failed to get package infos");
2877 return;
2878 }
satok91e88122011-07-18 11:11:42 +09002879 if (packageInfos != null) {
2880 final int packageNum = packageInfos.length;
2881 for (int i = 0; i < packageNum; ++i) {
2882 if (packageInfos[i].equals(imi.getPackageName())) {
2883 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002884 final long ident = Binder.clearCallingIdentity();
2885 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002886 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002887 } finally {
2888 Binder.restoreCallingIdentity(ident);
2889 }
satokee5e77c2011-09-02 18:50:15 +09002890 return;
satok91e88122011-07-18 11:11:42 +09002891 }
2892 }
2893 }
satoke7c6998e2011-06-03 17:57:59 +09002894 }
satokee5e77c2011-09-02 18:50:15 +09002895 return;
satoke7c6998e2011-06-03 17:57:59 +09002896 }
2897
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002898 @Override
2899 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002900 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002901 }
2902
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002903 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002904 public void clearLastInputMethodWindowForTransition(IBinder token) {
2905 if (!calledFromValidUser()) {
2906 return;
2907 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002908 synchronized (mMethodMap) {
2909 if (!calledWithValidToken(token)) {
Yohei Yukawafa49c002017-01-31 19:15:00 -08002910 return;
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002911 }
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002912 }
Yohei Yukawafa49c002017-01-31 19:15:00 -08002913 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002914 }
2915
2916 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002917 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002918 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002919 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002920 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002921 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002922 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2923 if (DEBUG) {
2924 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2925 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2926 + " actual: " + sequenceNumber);
2927 }
2928 return;
2929 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002930 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2931 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002932 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002933 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002934 }
2935 }
2936
satok28203512010-11-24 11:06:49 +09002937 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002939 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2940 }
2941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002943 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2944 if (token == null) {
2945 if (mContext.checkCallingOrSelfPermission(
2946 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2947 != PackageManager.PERMISSION_GRANTED) {
2948 throw new SecurityException(
2949 "Using null token requires permission "
2950 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002952 } else if (mCurToken != token) {
2953 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2954 + " token: " + token);
2955 return;
2956 }
2957
2958 final long ident = Binder.clearCallingIdentity();
2959 try {
2960 setInputMethodLocked(id, subtypeId);
2961 } finally {
2962 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 }
2964 }
2965
satok42c5a162011-05-26 16:46:14 +09002966 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002968 if (!calledFromValidUser()) {
2969 return;
2970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002972 if (!calledWithValidToken(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 return;
2974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 long ident = Binder.clearCallingIdentity();
2976 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002977 hideCurrentInputLocked(flags, null);
2978 } finally {
2979 Binder.restoreCallingIdentity(ident);
2980 }
2981 }
2982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002983
satok42c5a162011-05-26 16:46:14 +09002984 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002985 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002986 if (!calledFromValidUser()) {
2987 return;
2988 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002989 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002990 if (!calledWithValidToken(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002991 return;
2992 }
2993 long ident = Binder.clearCallingIdentity();
2994 try {
2995 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 } finally {
2997 Binder.restoreCallingIdentity(ident);
2998 }
2999 }
3000 }
3001
3002 void setEnabledSessionInMainThread(SessionState session) {
3003 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003004 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003006 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003007 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 } catch (RemoteException e) {
3009 }
3010 }
3011 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09003012 if (mEnabledSession != null && mEnabledSession.session != null) {
3013 try {
3014 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
3015 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
3016 } catch (RemoteException e) {
3017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 }
3019 }
3020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003021
satok42c5a162011-05-26 16:46:14 +09003022 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003024 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09003026 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07003027 final boolean showAuxSubtypes;
3028 switch (msg.arg1) {
3029 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
3030 // This is undocumented so far, but IMM#showInputMethodPicker() has been
3031 // implemented so that auxiliary subtypes will be excluded when the soft
3032 // keyboard is invisible.
3033 showAuxSubtypes = mInputShown;
3034 break;
3035 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
3036 showAuxSubtypes = true;
3037 break;
3038 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
3039 showAuxSubtypes = false;
3040 break;
3041 default:
3042 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
3043 return false;
3044 }
3045 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09003046 return true;
3047
satok47a44912010-10-06 16:03:58 +09003048 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08003049 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09003050 return true;
3051
3052 case MSG_SHOW_IM_CONFIG:
3053 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09003054 return true;
3055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 case MSG_UNBIND_INPUT:
3059 try {
3060 ((IInputMethod)msg.obj).unbindInput();
3061 } catch (RemoteException e) {
3062 // There is nothing interesting about the method dying.
3063 }
3064 return true;
3065 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003066 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 try {
3068 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
3069 } catch (RemoteException e) {
3070 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003071 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 return true;
3073 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003074 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003076 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07003077 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003078 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 } catch (RemoteException e) {
3080 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003081 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 return true;
3083 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003084 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003086 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07003087 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07003088 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 } catch (RemoteException e) {
3090 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003091 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003093 case MSG_HIDE_CURRENT_INPUT_METHOD:
3094 synchronized (mMethodMap) {
3095 hideCurrentInputLocked(0, null);
3096 }
3097 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003099 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003101 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
3103 } catch (RemoteException e) {
3104 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003105 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003107 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003108 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003109 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07003110 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003112 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003114 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07003115 // Dispose the channel if the input method is not local to this process
3116 // because the remote proxy will get its own copy when unparceled.
3117 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07003118 channel.dispose();
3119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003121 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07003123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003125
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003126 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08003127 final int missingMethods = msg.arg1;
3128 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003129 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003130 final IBinder startInputToken = (IBinder) args.arg1;
3131 final SessionState session = (SessionState) args.arg2;
3132 final IInputContext inputContext = (IInputContext) args.arg3;
3133 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08003136 session.method.startInput(startInputToken, inputContext, missingMethods,
3137 editorInfo, restarting);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 } catch (RemoteException e) {
3139 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003140 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07003142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003145
Yohei Yukawa33e81792015-11-17 21:14:42 -08003146 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08003148 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 } catch (RemoteException e) {
3150 // There is nothing interesting about the last client dying.
3151 }
3152 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08003153 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003154 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07003155 IInputMethodClient client = (IInputMethodClient)args.arg1;
3156 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07003158 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003160 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07003161 } finally {
3162 // Dispose the channel if the input method is not local to this process
3163 // because the remote proxy will get its own copy when unparceled.
3164 if (res.channel != null && Binder.isProxy(client)) {
3165 res.channel.dispose();
3166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003168 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07003170 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07003171 case MSG_SET_ACTIVE:
3172 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003173 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07003174 } catch (RemoteException e) {
3175 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
3176 + ((ClientState)msg.obj).pid + " uid "
3177 + ((ClientState)msg.obj).uid);
3178 }
3179 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003180 case MSG_SET_INTERACTIVE:
3181 handleSetInteractive(msg.arg1 != 0);
3182 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08003183 case MSG_SWITCH_IME:
3184 handleSwitchInputMethod(msg.arg1 != 0);
3185 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003186 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
3187 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07003188 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003189 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07003190 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003191 } catch (RemoteException e) {
3192 Slog.w(TAG, "Got RemoteException sending "
3193 + "setUserActionNotificationSequenceNumber("
3194 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07003195 + clientState.pid + " uid "
3196 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003197 }
3198 return true;
3199 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003200 case MSG_REPORT_FULLSCREEN_MODE: {
3201 final boolean fullscreen = msg.arg1 != 0;
3202 final ClientState clientState = (ClientState)msg.obj;
3203 try {
3204 clientState.client.reportFullscreenMode(fullscreen);
3205 } catch (RemoteException e) {
3206 Slog.w(TAG, "Got RemoteException sending "
3207 + "reportFullscreen(" + fullscreen + ") notification to pid="
3208 + clientState.pid + " uid=" + clientState.uid);
3209 }
3210 return true;
3211 }
satok01038492012-04-09 21:08:27 +09003212
3213 // --------------------------------------------------------------
3214 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07003215 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09003216 return true;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07003217 case MSG_SYSTEM_UNLOCK_USER:
3218 final int userId = msg.arg1;
3219 onUnlockUser(userId);
3220 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 }
3222 return false;
3223 }
3224
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003225 private void handleSetInteractive(final boolean interactive) {
3226 synchronized (mMethodMap) {
3227 mIsInteractive = interactive;
3228 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
3229
3230 // Inform the current client of the change in active status
3231 if (mCurClient != null && mCurClient.client != null) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08003232 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
3233 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mInFullscreenMode ? 1 : 0,
3234 mCurClient));
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003235 }
3236 }
3237 }
3238
Yohei Yukawaae61f712015-12-09 13:00:10 -08003239 private void handleSwitchInputMethod(final boolean forwardDirection) {
3240 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08003241 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07003242 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08003243 if (nextSubtype == null) {
3244 return;
3245 }
3246 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003247 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
3248 if (newInputMethodInfo == null) {
3249 return;
3250 }
3251 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3252 newInputMethodInfo, mCurrentSubtype);
3253 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003254 if (mSubtypeSwitchedByShortCutToast == null) {
3255 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3256 Toast.LENGTH_SHORT);
3257 } else {
3258 mSubtypeSwitchedByShortCutToast.setText(toastText);
3259 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003260 mSubtypeSwitchedByShortCutToast.show();
3261 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003262 }
3263 }
3264
satokdc9ddae2011-10-06 12:22:36 +09003265 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003266 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3267 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003268 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003269 if (DEBUG) {
3270 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3271 }
satok723a27e2010-11-11 14:58:11 +09003272 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003273 return true;
3274 }
3275
3276 return false;
3277 }
3278
Yohei Yukawa94e33302016-02-12 19:37:03 -08003279 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003280 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003281 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003282 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003283 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003284 if (!mSystemReady) {
3285 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
3286 return;
3287 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003288 mMethodList.clear();
3289 mMethodMap.clear();
Yohei Yukawae0733062017-02-09 22:49:35 -08003290 mMethodMapUpdateCount++;
Yohei Yukawac4e44912017-02-09 19:30:22 -08003291 mMyPackageMonitor.clearPackagesToMonitorComponentChangeLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003292
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003293 // Use for queryIntentServicesAsUser
3294 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003296 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3297 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3298 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003299 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003301 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3302 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003303
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003304 final HashMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
satoke7c6998e2011-06-03 17:57:59 +09003305 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 for (int i = 0; i < services.size(); ++i) {
3307 ResolveInfo ri = services.get(i);
3308 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003309 final String imeId = InputMethodInfo.computeId(ri);
3310 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3311 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 + ": it does not require the permission "
3313 + android.Manifest.permission.BIND_INPUT_METHOD);
3314 continue;
3315 }
3316
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003317 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003319 final List<InputMethodSubtype> additionalSubtypes = additionalSubtypeMap.get(imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 try {
satoke7c6998e2011-06-03 17:57:59 +09003321 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003322 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003323 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003324 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325
3326 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003327 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09003329 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08003330 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 }
3332 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003333
Yohei Yukawac4e44912017-02-09 19:30:22 -08003334 // Construct the set of possible IME packages for onPackageChanged() to avoid false
3335 // negatives when the package state remains to be the same but only the component state is
3336 // changed.
3337 {
3338 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
3339 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
3340 // conservative, but it seems we cannot use it for now (Issue 35176630).
3341 final List<ResolveInfo> allInputMethodServices = pm.queryIntentServicesAsUser(
3342 new Intent(InputMethod.SERVICE_INTERFACE),
3343 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
3344 final int N = allInputMethodServices.size();
3345 for (int i = 0; i < N; ++i) {
3346 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
3347 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
3348 continue;
3349 }
3350 mMyPackageMonitor.addPackageToMonitorComponentChangeLocked(si.packageName);
3351 }
3352 }
3353
Yohei Yukawa859df052016-02-17 07:56:46 -08003354 // TODO: The following code should find better place to live.
3355 if (!resetDefaultEnabledIme) {
3356 boolean enabledImeFound = false;
3357 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3358 final int N = enabledImes.size();
3359 for (int i = 0; i < N; ++i) {
3360 final InputMethodInfo imi = enabledImes.get(i);
3361 if (mMethodList.contains(imi)) {
3362 enabledImeFound = true;
3363 break;
3364 }
3365 }
3366 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08003367 if (DEBUG) {
3368 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3369 }
Yohei Yukawa859df052016-02-17 07:56:46 -08003370 resetDefaultEnabledIme = true;
3371 resetSelectedInputMethodAndSubtypeLocked("");
3372 }
3373 }
3374
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003375 if (resetDefaultEnabledIme) {
3376 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08003377 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003378 final int N = defaultEnabledIme.size();
3379 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003380 final InputMethodInfo imi = defaultEnabledIme.get(i);
3381 if (DEBUG) {
3382 Slog.d(TAG, "--- enable ime = " + imi);
3383 }
3384 setInputMethodEnabledLocked(imi.getId(), true);
3385 }
3386 }
3387
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003388 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003389 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003390 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003391 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3392 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003393 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003394 }
3395 } else {
3396 // Double check that the default IME is certainly enabled.
3397 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003398 }
3399 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003400 // Here is not the perfect place to reset the switching controller. Ideally
3401 // mSwitchingController and mSettings should be able to share the same state.
3402 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3403 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003404 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003408
satok217f5482010-12-15 05:19:19 +09003409 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003410 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003411 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003412 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3413 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003414 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003415 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003416 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003417 final int userId;
3418 synchronized (mMethodMap) {
3419 userId = mSettings.getCurrentUserId();
3420 }
3421 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003422 }
3423
3424 private void showConfigureInputMethods() {
3425 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3426 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3427 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3428 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003429 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003430 }
3431
satok2c93efc2012-04-02 19:33:47 +09003432 private boolean isScreenLocked() {
3433 return mKeyguardManager != null
3434 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3435 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003436
Seigo Nonaka14e13912015-05-06 21:04:13 -07003437 private void showInputMethodMenu(boolean showAuxSubtypes) {
3438 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003441 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003442
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003443 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003444 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003445 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003446
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003447 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003448 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003449 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3450 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003451 if (immis == null || immis.size() == 0) {
3452 return;
3453 }
3454
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003455 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456
satok688bd472012-02-09 20:09:17 +09003457 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003458 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003459 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003460
satokc3690562012-01-10 20:14:43 +09003461 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003462 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003463 if (currentSubtype != null) {
3464 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003465 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3466 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003467 }
3468 }
3469
Ken Wakasa761eb372011-03-04 19:06:18 +09003470 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003471 mIms = new InputMethodInfo[N];
3472 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003473 int checkedItem = 0;
3474 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003475 final ImeSubtypeListItem item = imList.get(i);
3476 mIms[i] = item.mImi;
3477 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003478 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003479 int subtypeId = mSubtypeIds[i];
3480 if ((subtypeId == NOT_A_SUBTYPE_ID)
3481 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3482 || (subtypeId == lastInputMethodSubtypeId)) {
3483 checkedItem = i;
3484 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003487
3488 final Context settingsContext = new ContextThemeWrapper(context,
3489 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3490
3491 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003492 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3493 @Override
3494 public void onCancel(DialogInterface dialog) {
3495 hideInputMethodMenu();
3496 }
3497 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003498
3499 final Context dialogContext = mDialogBuilder.getContext();
3500 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3501 com.android.internal.R.styleable.DialogPreference,
3502 com.android.internal.R.attr.alertDialogStyle, 0);
3503 final Drawable dialogIcon = a.getDrawable(
3504 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3505 a.recycle();
3506
3507 mDialogBuilder.setIcon(dialogIcon);
3508
Yohei Yukawad34e1482016-02-11 08:03:52 -08003509 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003510 final View tv = inflater.inflate(
3511 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3512 mDialogBuilder.setCustomTitle(tv);
3513
3514 // Setup layout for a toggle switch of the hardware keyboard
3515 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003516 mSwitchingDialogTitleView
3517 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003518 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003519 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003520 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003521 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003522 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003523 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3524 @Override
3525 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003526 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003527 // Ensure that the input method dialog is dismissed when changing
3528 // the hardware keyboard state.
3529 hideInputMethodMenu();
3530 }
3531 });
3532
Alan Viverette505e3ab2014-11-24 15:22:11 -08003533 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003534 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3535 final OnClickListener choiceListener = new OnClickListener() {
3536 @Override
3537 public void onClick(final DialogInterface dialog, final int which) {
3538 synchronized (mMethodMap) {
3539 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3540 || mSubtypeIds.length <= which) {
3541 return;
satok01038492012-04-09 21:08:27 +09003542 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003543 final InputMethodInfo im = mIms[which];
3544 int subtypeId = mSubtypeIds[which];
3545 adapter.mCheckedItem = which;
3546 adapter.notifyDataSetChanged();
3547 hideInputMethodMenu();
3548 if (im != null) {
3549 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3550 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003551 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003552 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003554 }
3555 }
3556 };
3557 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003560 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003561 final Window w = mSwitchingDialog.getWindow();
3562 final WindowManager.LayoutParams attrs = w.getAttributes();
3563 w.setType(TYPE_INPUT_METHOD_DIALOG);
3564 // Use an alternate token for the dialog for that window manager can group the token
3565 // with other IME windows based on type vs. grouping based on whichever token happens
3566 // to get selected by the system later on.
3567 attrs.token = mSwitchingDialogToken;
3568 attrs.privateFlags |= PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
3569 attrs.setTitle("Select input method");
3570 w.setAttributes(attrs);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003571 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 mSwitchingDialog.show();
3573 }
3574 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003575
Ken Wakasa05dbb652011-08-22 15:22:43 +09003576 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3577 private final LayoutInflater mInflater;
3578 private final int mTextViewResourceId;
3579 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003580 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003581 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3582 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3583 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003584
Ken Wakasa05dbb652011-08-22 15:22:43 +09003585 mTextViewResourceId = textViewResourceId;
3586 mItemsList = itemsList;
3587 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003588 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003589 }
3590
3591 @Override
3592 public View getView(int position, View convertView, ViewGroup parent) {
3593 final View view = convertView != null ? convertView
3594 : mInflater.inflate(mTextViewResourceId, null);
3595 if (position < 0 || position >= mItemsList.size()) return view;
3596 final ImeSubtypeListItem item = mItemsList.get(position);
3597 final CharSequence imeName = item.mImeName;
3598 final CharSequence subtypeName = item.mSubtypeName;
3599 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3600 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3601 if (TextUtils.isEmpty(subtypeName)) {
3602 firstTextView.setText(imeName);
3603 secondTextView.setVisibility(View.GONE);
3604 } else {
3605 firstTextView.setText(subtypeName);
3606 secondTextView.setText(imeName);
3607 secondTextView.setVisibility(View.VISIBLE);
3608 }
3609 final RadioButton radioButton =
3610 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3611 radioButton.setChecked(position == mCheckedItem);
3612 return view;
3613 }
3614 }
3615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003617 synchronized (mMethodMap) {
3618 hideInputMethodMenuLocked();
3619 }
3620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003621
The Android Open Source Project10592532009-03-18 17:39:46 -07003622 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003623 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624
The Android Open Source Project10592532009-03-18 17:39:46 -07003625 if (mSwitchingDialog != null) {
3626 mSwitchingDialog.dismiss();
3627 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003629
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003630 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003631 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003632 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003636
satok42c5a162011-05-26 16:46:14 +09003637 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003639 // TODO: Make this work even for non-current users?
3640 if (!calledFromValidUser()) {
3641 return false;
3642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 synchronized (mMethodMap) {
3644 if (mContext.checkCallingOrSelfPermission(
3645 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3646 != PackageManager.PERMISSION_GRANTED) {
3647 throw new SecurityException(
3648 "Requires permission "
3649 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3650 }
Anna Galusza9b278112016-01-04 11:37:37 -08003651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 long ident = Binder.clearCallingIdentity();
3653 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003654 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 } finally {
3656 Binder.restoreCallingIdentity(ident);
3657 }
3658 }
3659 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003660
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003661 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3662 // Make sure this is a valid input method.
3663 InputMethodInfo imm = mMethodMap.get(id);
3664 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003665 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003666 }
3667
satokd87c2592010-09-29 11:52:06 +09003668 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3669 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003670
satokd87c2592010-09-29 11:52:06 +09003671 if (enabled) {
3672 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3673 if (pair.first.equals(id)) {
3674 // We are enabling this input method, but it is already enabled.
3675 // Nothing to do. The previous state was enabled.
3676 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003677 }
3678 }
satokd87c2592010-09-29 11:52:06 +09003679 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3680 // Previous state was disabled.
3681 return false;
3682 } else {
3683 StringBuilder builder = new StringBuilder();
3684 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3685 builder, enabledInputMethodsList, id)) {
3686 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003687 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003688 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3689 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3690 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003691 }
3692 // Previous state was enabled.
3693 return true;
3694 } else {
3695 // We are disabling the input method but it is already disabled.
3696 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003697 return false;
3698 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003699 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003700 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003701
satok723a27e2010-11-11 14:58:11 +09003702 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3703 boolean setSubtypeOnly) {
3704 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003705 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003706
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003707 mCurUserActionNotificationSequenceNumber =
3708 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3709 if (DEBUG) {
3710 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3711 + mCurUserActionNotificationSequenceNumber);
3712 }
3713
3714 if (mCurClient != null && mCurClient.client != null) {
3715 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3716 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003717 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003718 }
3719
satok723a27e2010-11-11 14:58:11 +09003720 // Set Subtype here
3721 if (imi == null || subtypeId < 0) {
3722 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003723 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003724 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003725 if (subtypeId < imi.getSubtypeCount()) {
3726 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3727 mSettings.putSelectedSubtype(subtype.hashCode());
3728 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003729 } else {
3730 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003731 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003732 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003733 }
satokab751aa2010-09-14 19:17:36 +09003734 }
satok723a27e2010-11-11 14:58:11 +09003735
Yohei Yukawa68645a62016-02-17 07:54:20 -08003736 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003737 // Set InputMethod here
3738 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3739 }
3740 }
3741
3742 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3743 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3744 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3745 // newDefaultIme is empty when there is no candidate for the selected IME.
3746 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3747 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3748 if (subtypeHashCode != null) {
3749 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003750 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003751 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003752 } catch (NumberFormatException e) {
3753 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3754 }
3755 }
3756 }
3757 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003758 }
3759
satok4e4569d2010-11-19 18:45:53 +09003760 // If there are no selected shortcuts, tries finding the most applicable ones.
3761 private Pair<InputMethodInfo, InputMethodSubtype>
3762 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3763 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3764 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003765 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003766 boolean foundInSystemIME = false;
3767
3768 // Search applicable subtype for each InputMethodInfo
3769 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003770 final String imiId = imi.getId();
3771 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3772 continue;
3773 }
satokcd7cd292010-11-20 15:46:23 +09003774 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003775 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003776 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003777 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003778 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003779 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003780 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003781 }
satokdf31ae62011-01-15 06:19:44 +09003782 // 2. Search by the system locale from enabledSubtypes.
3783 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003784 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003785 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003786 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003787 }
satoka86f5e42011-09-02 17:12:42 +09003788 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003789 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003790 final ArrayList<InputMethodSubtype> subtypesForSearch =
3791 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003792 ? InputMethodUtils.getSubtypes(imi)
3793 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003794 // 4. Search by the current subtype's locale from all subtypes.
3795 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003796 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003797 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003798 }
3799 // 5. Search by the system locale from all subtypes.
3800 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003801 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003802 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003803 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003804 }
satokcd7cd292010-11-20 15:46:23 +09003805 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003806 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003807 // The current input method is the most applicable IME.
3808 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003809 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003810 break;
satok7599a7f2010-12-22 13:45:23 +09003811 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003812 // The system input method is 2nd applicable IME.
3813 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003814 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003815 if ((imi.getServiceInfo().applicationInfo.flags
3816 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3817 foundInSystemIME = true;
3818 }
satok4e4569d2010-11-19 18:45:53 +09003819 }
3820 }
3821 }
3822 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003823 if (mostApplicableIMI != null) {
3824 Slog.w(TAG, "Most applicable shortcut input method was:"
3825 + mostApplicableIMI.getId());
3826 if (mostApplicableSubtype != null) {
3827 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3828 + "," + mostApplicableSubtype.getMode() + ","
3829 + mostApplicableSubtype.getLocale());
3830 }
3831 }
satok4e4569d2010-11-19 18:45:53 +09003832 }
satokcd7cd292010-11-20 15:46:23 +09003833 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003834 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003835 } else {
3836 return null;
3837 }
3838 }
3839
satokab751aa2010-09-14 19:17:36 +09003840 /**
3841 * @return Return the current subtype of this input method.
3842 */
satok42c5a162011-05-26 16:46:14 +09003843 @Override
satokab751aa2010-09-14 19:17:36 +09003844 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003845 // TODO: Make this work even for non-current users?
3846 if (!calledFromValidUser()) {
3847 return null;
3848 }
3849 synchronized (mMethodMap) {
3850 return getCurrentInputMethodSubtypeLocked();
3851 }
3852 }
3853
3854 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003855 if (mCurMethodId == null) {
3856 return null;
3857 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003858 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003859 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3860 if (imi == null || imi.getSubtypeCount() == 0) {
3861 return null;
satok4e4569d2010-11-19 18:45:53 +09003862 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003863 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003864 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3865 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003866 if (subtypeId == NOT_A_SUBTYPE_ID) {
3867 // If there are no selected subtypes, the framework will try to find
3868 // the most applicable subtype from explicitly or implicitly enabled
3869 // subtypes.
3870 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003871 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003872 // If there is only one explicitly or implicitly enabled subtype,
3873 // just returns it.
3874 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3875 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3876 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003877 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003878 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003879 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003880 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003881 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003882 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3883 true);
satok4e4569d2010-11-19 18:45:53 +09003884 }
satok3ef8b292010-11-23 06:06:29 +09003885 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003886 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003887 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003888 }
3889 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003890 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003891 }
3892
satok4e4569d2010-11-19 18:45:53 +09003893 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003894 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003895 @Override
satok4e4569d2010-11-19 18:45:53 +09003896 public List getShortcutInputMethodsAndSubtypes() {
3897 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003898 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003899 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003900 // If there are no selected shortcut subtypes, the framework will try to find
3901 // the most applicable subtype from all subtypes whose mode is
3902 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003903 Pair<InputMethodInfo, InputMethodSubtype> info =
3904 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003905 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003906 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003907 ret.add(info.first);
3908 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003909 }
satok3da92232011-01-11 22:46:30 +09003910 return ret;
satokf3db1af2010-11-23 13:34:33 +09003911 }
satokf3db1af2010-11-23 13:34:33 +09003912 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3913 ret.add(imi);
3914 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3915 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003916 }
3917 }
satokf3db1af2010-11-23 13:34:33 +09003918 return ret;
satok4e4569d2010-11-19 18:45:53 +09003919 }
3920 }
3921
satok42c5a162011-05-26 16:46:14 +09003922 @Override
satokb66d2872010-11-10 01:04:04 +09003923 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003924 // TODO: Make this work even for non-current users?
3925 if (!calledFromValidUser()) {
3926 return false;
3927 }
satokb66d2872010-11-10 01:04:04 +09003928 synchronized (mMethodMap) {
3929 if (subtype != null && mCurMethodId != null) {
3930 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003931 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003932 if (subtypeId != NOT_A_SUBTYPE_ID) {
3933 setInputMethodLocked(mCurMethodId, subtypeId);
3934 return true;
3935 }
3936 }
3937 return false;
3938 }
3939 }
3940
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003941 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003942 private static class InputMethodFileManager {
3943 private static final String SYSTEM_PATH = "system";
3944 private static final String INPUT_METHOD_PATH = "inputmethod";
3945 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3946 private static final String NODE_SUBTYPES = "subtypes";
3947 private static final String NODE_SUBTYPE = "subtype";
3948 private static final String NODE_IMI = "imi";
3949 private static final String ATTR_ID = "id";
3950 private static final String ATTR_LABEL = "label";
3951 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003952 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003953 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003954 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003955 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3956 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3957 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003958 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003959 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3960 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003961 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003962 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003963 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003964 if (methodMap == null) {
3965 throw new NullPointerException("methodMap is null");
3966 }
3967 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003968 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003969 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3970 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003971 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003972 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003973 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3974 }
3975 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3976 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3977 if (!subtypeFile.exists()) {
3978 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003979 writeAdditionalInputMethodSubtypes(
3980 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003981 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003982 readAdditionalInputMethodSubtypes(
3983 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003984 }
3985 }
3986
3987 private void deleteAllInputMethodSubtypes(String imiId) {
3988 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003989 mAdditionalSubtypesMap.remove(imiId);
3990 writeAdditionalInputMethodSubtypes(
3991 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003992 }
3993 }
3994
3995 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003996 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003997 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003998 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003999 final int N = additionalSubtypes.length;
4000 for (int i = 0; i < N; ++i) {
4001 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09004002 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09004003 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004004 } else {
4005 Slog.w(TAG, "Duplicated subtype definition found: "
4006 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09004007 }
4008 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004009 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
4010 writeAdditionalInputMethodSubtypes(
4011 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09004012 }
4013 }
4014
4015 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
4016 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09004017 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09004018 }
4019 }
4020
4021 private static void writeAdditionalInputMethodSubtypes(
4022 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
4023 HashMap<String, InputMethodInfo> methodMap) {
4024 // Safety net for the case that this function is called before methodMap is set.
4025 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
4026 FileOutputStream fos = null;
4027 try {
4028 fos = subtypesFile.startWrite();
4029 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004030 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004031 out.startDocument(null, true);
4032 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
4033 out.startTag(null, NODE_SUBTYPES);
4034 for (String imiId : allSubtypes.keySet()) {
4035 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
4036 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
4037 continue;
4038 }
4039 out.startTag(null, NODE_IMI);
4040 out.attribute(null, ATTR_ID, imiId);
4041 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
4042 final int N = subtypesList.size();
4043 for (int i = 0; i < N; ++i) {
4044 final InputMethodSubtype subtype = subtypesList.get(i);
4045 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07004046 if (subtype.hasSubtypeId()) {
4047 out.attribute(null, ATTR_IME_SUBTYPE_ID,
4048 String.valueOf(subtype.getSubtypeId()));
4049 }
satoke7c6998e2011-06-03 17:57:59 +09004050 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
4051 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
4052 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004053 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
4054 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09004055 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
4056 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
4057 out.attribute(null, ATTR_IS_AUXILIARY,
4058 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004059 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
4060 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09004061 out.endTag(null, NODE_SUBTYPE);
4062 }
4063 out.endTag(null, NODE_IMI);
4064 }
4065 out.endTag(null, NODE_SUBTYPES);
4066 out.endDocument();
4067 subtypesFile.finishWrite(fos);
4068 } catch (java.io.IOException e) {
4069 Slog.w(TAG, "Error writing subtypes", e);
4070 if (fos != null) {
4071 subtypesFile.failWrite(fos);
4072 }
4073 }
4074 }
4075
4076 private static void readAdditionalInputMethodSubtypes(
4077 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4078 if (allSubtypes == null || subtypesFile == null) return;
4079 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07004080 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09004081 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01004082 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09004083 int type = parser.getEventType();
4084 // Skip parsing until START_TAG
4085 while ((type = parser.next()) != XmlPullParser.START_TAG
4086 && type != XmlPullParser.END_DOCUMENT) {}
4087 String firstNodeName = parser.getName();
4088 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4089 throw new XmlPullParserException("Xml doesn't start with subtypes");
4090 }
4091 final int depth =parser.getDepth();
4092 String currentImiId = null;
4093 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4094 while (((type = parser.next()) != XmlPullParser.END_TAG
4095 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4096 if (type != XmlPullParser.START_TAG)
4097 continue;
4098 final String nodeName = parser.getName();
4099 if (NODE_IMI.equals(nodeName)) {
4100 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4101 if (TextUtils.isEmpty(currentImiId)) {
4102 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4103 continue;
4104 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07004105 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09004106 allSubtypes.put(currentImiId, tempSubtypesArray);
4107 } else if (NODE_SUBTYPE.equals(nodeName)) {
4108 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4109 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4110 continue;
4111 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004112 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004113 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004114 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09004115 parser.getAttributeValue(null, ATTR_LABEL));
4116 final String imeSubtypeLocale =
4117 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004118 final String languageTag =
4119 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09004120 final String imeSubtypeMode =
4121 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4122 final String imeSubtypeExtraValue =
4123 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004124 final boolean isAuxiliary = "1".equals(String.valueOf(
4125 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08004126 final boolean isAsciiCapable = "1".equals(String.valueOf(
4127 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004128 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004129 .setSubtypeNameResId(label)
4130 .setSubtypeIconResId(icon)
4131 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08004132 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09004133 .setSubtypeMode(imeSubtypeMode)
4134 .setSubtypeExtraValue(imeSubtypeExtraValue)
4135 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07004136 .setIsAsciiCapable(isAsciiCapable);
4137 final String subtypeIdString =
4138 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
4139 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004140 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07004141 }
4142 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09004143 }
4144 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07004145 } catch (XmlPullParserException | IOException | NumberFormatException e) {
4146 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09004147 return;
satoke7c6998e2011-06-03 17:57:59 +09004148 }
4149 }
4150 }
4151
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004152 private static final class LocalServiceImpl implements InputMethodManagerInternal {
4153 @NonNull
4154 private final Handler mHandler;
4155
4156 LocalServiceImpl(@NonNull final Handler handler) {
4157 mHandler = handler;
4158 }
4159
4160 @Override
4161 public void setInteractive(boolean interactive) {
4162 // Do everything in handler so as not to block the caller.
4163 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
4164 interactive ? 1 : 0, 0));
4165 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08004166
4167 @Override
4168 public void switchInputMethod(boolean forwardDirection) {
4169 // Do everything in handler so as not to block the caller.
4170 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
4171 forwardDirection ? 1 : 0, 0));
4172 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004173
4174 @Override
4175 public void hideCurrentInputMethod() {
4176 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
4177 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
4178 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004179 }
4180
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004181 private static String imeWindowStatusToString(final int imeWindowVis) {
4182 final StringBuilder sb = new StringBuilder();
4183 boolean first = true;
4184 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
4185 sb.append("Active");
4186 first = false;
4187 }
4188 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
4189 if (!first) {
4190 sb.append("|");
4191 }
4192 sb.append("Visible");
4193 }
4194 return sb.toString();
4195 }
4196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 @Override
Yohei Yukawa25e08132016-06-22 16:31:41 -07004198 public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
4199 @Nullable Uri contentUri, @Nullable String packageName) {
4200 if (!calledFromValidUser()) {
4201 return null;
4202 }
4203
4204 if (token == null) {
4205 throw new NullPointerException("token");
4206 }
4207 if (packageName == null) {
4208 throw new NullPointerException("packageName");
4209 }
4210 if (contentUri == null) {
4211 throw new NullPointerException("contentUri");
4212 }
4213 final String contentUriScheme = contentUri.getScheme();
4214 if (!"content".equals(contentUriScheme)) {
4215 throw new InvalidParameterException("contentUri must have content scheme");
4216 }
4217
4218 synchronized (mMethodMap) {
4219 final int uid = Binder.getCallingUid();
4220 if (mCurMethodId == null) {
4221 return null;
4222 }
4223 if (mCurToken != token) {
4224 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4225 + " token=" + token);
4226 return null;
4227 }
4228 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4229 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4230 // nature of our system. Let's compare it with our internal record.
4231 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4232 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4233 + mCurAttribute.packageName + " packageName=" + packageName);
4234 return null;
4235 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004236 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004237 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004238 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004239 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004240 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4241 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4242 imeUserId);
4243 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4244 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4245 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4246 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4247 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4248 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4249 // actually allowed to "uid", which is guaranteed to be the IME's one.
4250 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4251 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004252 }
4253 }
4254
4255 @Override
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004256 public void reportFullscreenMode(IBinder token, boolean fullscreen) {
4257 if (!calledFromValidUser()) {
4258 return;
4259 }
4260 synchronized (mMethodMap) {
4261 if (!calledWithValidToken(token)) {
4262 return;
4263 }
4264 if (mCurClient != null && mCurClient.client != null) {
4265 mInFullscreenMode = fullscreen;
4266 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4267 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4268 }
4269 }
4270 }
4271
4272 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4274 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4275 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4278 + Binder.getCallingPid()
4279 + ", uid=" + Binder.getCallingUid());
4280 return;
4281 }
4282
4283 IInputMethod method;
4284 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004285 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 synchronized (mMethodMap) {
4290 p.println("Current Input Method Manager state:");
4291 int N = mMethodList.size();
Yohei Yukawae0733062017-02-09 22:49:35 -08004292 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 for (int i=0; i<N; i++) {
4294 InputMethodInfo info = mMethodList.get(i);
4295 p.println(" InputMethod #" + i + ":");
4296 info.dump(p, " ");
4297 }
4298 p.println(" Clients:");
4299 for (ClientState ci : mClients.values()) {
4300 p.println(" Client " + ci + ":");
4301 p.println(" client=" + ci.client);
4302 p.println(" inputContext=" + ci.inputContext);
4303 p.println(" sessionRequested=" + ci.sessionRequested);
4304 p.println(" curSession=" + ci.curSession);
4305 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004306 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004308 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08004309 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
4310 + " softInputMode=" +
4311 InputMethodClient.softInputModeToString(mCurFocusedWindowSoftInputMode)
4312 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004313 focusedWindowClient = mCurFocusedWindowClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4315 + " mBoundToMethod=" + mBoundToMethod);
4316 p.println(" mCurToken=" + mCurToken);
4317 p.println(" mCurIntent=" + mCurIntent);
4318 method = mCurMethod;
4319 p.println(" mCurMethod=" + mCurMethod);
4320 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07004321 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 p.println(" mShowRequested=" + mShowRequested
4323 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4324 + " mShowForced=" + mShowForced
4325 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004326 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09004327 p.println(" mCurUserActionNotificationSequenceNumber="
4328 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004329 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07004330 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07004331 p.println(" mSwitchingController:");
4332 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004333 p.println(" mSettings:");
4334 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08004335
4336 p.println(" mStartInputHistory:");
4337 mStartInputHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004339
Jeff Brownb88102f2010-09-08 11:49:43 -07004340 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 pw.flush();
4343 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004344 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
4345 } catch (IOException | RemoteException e) {
4346 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004348 } else {
4349 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004351
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004352 if (focusedWindowClient != null && client != focusedWindowClient) {
4353 p.println(" ");
4354 p.println("Warning: Current input method client doesn't match the last focused. "
4355 + "window.");
4356 p.println("Dumping input method client in the last focused window just in case.");
4357 p.println(" ");
4358 pw.flush();
4359 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004360 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
4361 } catch (IOException | RemoteException e) {
4362 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08004363 }
4364 }
4365
Jeff Brownb88102f2010-09-08 11:49:43 -07004366 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 pw.flush();
4369 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07004370 TransferPipe.dumpAsync(method.asBinder(), fd, args);
4371 } catch (IOException | RemoteException e) {
4372 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004374 } else {
4375 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
4377 }
4378}