blob: 77b3111fa98b56e32a68c0b3567b443023c984d7 [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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputMethodClient;
29import com.android.internal.view.IInputMethodManager;
30import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070031import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080033import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080034import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070040import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070041import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080042import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.ActivityManagerNative;
44import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070045import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090046import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090047import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090048import android.app.Notification;
49import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070050import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ComponentName;
53import android.content.ContentResolver;
54import android.content.Context;
55import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090057import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090059import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070061import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090062import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080066import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070067import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.res.Resources;
69import android.content.res.TypedArray;
70import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080071import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080072import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080073import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070074import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040076import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070077import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090078import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Handler;
80import android.os.IBinder;
81import android.os.IInterface;
82import android.os.Message;
83import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070084import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080086import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.ServiceManager;
88import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090089import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080090import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.provider.Settings;
92import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090093import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070094import android.util.ArrayMap;
95import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070096import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.EventLog;
Yohei Yukawae985c242016-02-24 18:27:04 -080098import android.util.LocaleList;
satokf9f01002011-05-19 21:31:50 +090099import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900100import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.PrintWriterPrinter;
102import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900103import android.util.Slog;
104import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900105import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700107import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.view.LayoutInflater;
109import android.view.View;
110import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700112import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900113import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700115import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.inputmethod.InputMethod;
117import android.view.inputmethod.InputMethodInfo;
118import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700119import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900120import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900121import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900122import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900123import android.widget.CompoundButton;
124import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900126import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900127import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900131import java.io.FileInputStream;
132import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.io.IOException;
134import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100135import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900137import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import java.util.HashMap;
139import java.util.List;
140
141/**
142 * This class provides a system service that manages input methods.
143 */
144public class InputMethodManagerService extends IInputMethodManager.Stub
145 implements ServiceConnection, Handler.Callback {
146 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700147 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700148 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700150 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
151 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
152 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final int MSG_UNBIND_INPUT = 1000;
155 static final int MSG_BIND_INPUT = 1010;
156 static final int MSG_SHOW_SOFT_INPUT = 1020;
157 static final int MSG_HIDE_SOFT_INPUT = 1030;
158 static final int MSG_ATTACH_TOKEN = 1040;
159 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 static final int MSG_START_INPUT = 2000;
162 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
Yohei Yukawa33e81792015-11-17 21:14:42 -0800164 static final int MSG_UNBIND_CLIENT = 3000;
165 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700166 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700167 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900168 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800169 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
satok01038492012-04-09 21:08:27 +0900171 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
172
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700173 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
satokf9f01002011-05-19 21:31:50 +0900175 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
176
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900178 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900179
satok4e4569d2010-11-19 18:45:53 +0900180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800182 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900184 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700187 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700189 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900190 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900191 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900192 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800193 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900195 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 // All known input methods. mMethodMap also serves as the global
198 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700199 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
200 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900201 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700202 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900203 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700205 // Used to bring IME service up to visible adjustment while it is being shown.
206 final ServiceConnection mVisibleConnection = new ServiceConnection() {
207 @Override public void onServiceConnected(ComponentName name, IBinder service) {
208 }
209
210 @Override public void onServiceDisconnected(ComponentName name) {
211 }
212 };
213 boolean mVisibleBound = false;
214
satok7cfc0ed2011-06-20 21:29:36 +0900215 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700216 private NotificationManager mNotificationManager;
217 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700218 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400219 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700220 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900221 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900222 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900223 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900224
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900225 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 final ClientState client;
227 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700228
229 IInputMethodSession session;
230 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 @Override
233 public String toString() {
234 return "SessionState{uid " + client.uid + " pid " + client.pid
235 + " method " + Integer.toHexString(
236 System.identityHashCode(method))
237 + " session " + Integer.toHexString(
238 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700239 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 + "}";
241 }
242
243 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700244 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 client = _client;
246 method = _method;
247 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700248 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
Jeff Brownc28867a2013-03-26 15:42:39 -0700252 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final IInputMethodClient client;
254 final IInputContext inputContext;
255 final int uid;
256 final int pid;
257 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 boolean sessionRequested;
260 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 @Override
263 public String toString() {
264 return "ClientState{" + Integer.toHexString(
265 System.identityHashCode(this)) + " uid " + uid
266 + " pid " + pid + "}";
267 }
268
269 ClientState(IInputMethodClient _client, IInputContext _inputContext,
270 int _uid, int _pid) {
271 client = _client;
272 inputContext = _inputContext;
273 uid = _uid;
274 pid = _pid;
275 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
276 }
277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700279 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700282 * Set once the system is ready to run third party code.
283 */
284 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700286 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700287 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
288 * method. This is to be synchronized with the secure settings keyed with
289 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
290 *
291 * <p>This can be transiently {@code null} when the system is re-initializing input method
292 * settings, e.g., the system locale is just changed.</p>
293 *
294 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
295 * {@link InputMethodManagerService}.</p>
296 *
297 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700299 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
303 * The current binding sequence number, incremented every time there is
304 * a new bind performed.
305 */
306 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
309 * The client that is currently bound to an input method.
310 */
311 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800314 * The last window token that we confirmed to be focused. This is always updated upon reports
315 * from the input method client. If the window state is already changed before the report is
316 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700317 */
318 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700320 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800321 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
322 */
323 ClientState mCurFocusedWindowClient;
324
325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 * The input context last provided by the current client.
327 */
328 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700331 * The missing method flags for the input context last provided by the current client.
332 *
333 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
334 */
335 int mCurInputContextMissingMethods;
336
337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 * The attributes last provided by the current client.
339 */
340 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700343 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700345 *
346 * <p>This can be {@code null} when no input method is connected.</p>
347 *
348 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700350 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
satokab751aa2010-09-14 19:17:36 +0900354 * The current subtype of the current input method.
355 */
356 private InputMethodSubtype mCurrentSubtype;
357
satok4e4569d2010-11-19 18:45:53 +0900358 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900359 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700360 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900361
John Spurlocke0980502013-10-25 11:59:29 -0400362 // Was the keyguard locked when this client became current?
363 private boolean mCurClientInKeyguard;
364
satokab751aa2010-09-14 19:17:36 +0900365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * Set to true if our ServiceConnection is currently actively bound to
367 * a service (whether or not we have gotten its IBinder back yet).
368 */
369 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * Set if the client has asked for the input method to be shown.
373 */
374 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * Set if we were explicitly told to show the input method.
378 */
379 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * Set if we were forced to be shown.
383 */
384 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * Set if we last told the input method to show itself.
388 */
389 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
392 * The Intent used to connect to the current input method.
393 */
394 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
397 * The token we have made for the currently active input method, to
398 * identify it in the future.
399 */
400 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * If non-null, this is the input method service we are currently connected
404 * to.
405 */
406 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Time that we last initiated a bind to the input method, to determine
410 * if we should try to disconnect and reconnect to it.
411 */
412 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Have we called mCurMethod.bindInput()?
416 */
417 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * Currently enabled session. Only touched by service thread, not
421 * protected by a lock.
422 */
423 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700426 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700428 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900430 int mCurUserActionNotificationSequenceNumber = 0;
431
Joe Onorato857fd9b2011-01-27 15:08:35 -0800432 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900433
434 /**
435 * A set of status bits regarding the active IME.
436 *
437 * <p>This value is a combination of following two bits:</p>
438 * <dl>
439 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
440 * <dd>
441 * If this bit is ON, connected IME is ready to accept touch/key events.
442 * </dd>
443 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
444 * <dd>
445 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
446 * </dd>
447 * </dl>
448 * <em>Do not update this value outside of setImeWindowStatus.</em>
449 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800450 int mImeWindowVis;
451
Ken Wakasa05dbb652011-08-22 15:22:43 +0900452 private AlertDialog.Builder mDialogBuilder;
453 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900454 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900455 private InputMethodInfo[] mIms;
456 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800457 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700458 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800459 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900460 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
461 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500462 private final String mSlotIme;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700465 int mUserId;
466 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700467 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800468 String mLastEnabled = "";
469
Yohei Yukawa81482972015-06-04 00:58:59 -0700470 /**
471 * <em>This constructor must be called within the lock.</em>
472 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 SettingsObserver(Handler handler) {
474 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700475 }
476
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800477 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700478 if (mRegistered && mUserId == userId) {
479 return;
480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700482 if (mRegistered) {
483 mContext.getContentResolver().unregisterContentObserver(this);
484 mRegistered = false;
485 }
486 if (mUserId != userId) {
487 mLastEnabled = "";
488 mUserId = userId;
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900492 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700493 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900494 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700495 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700496 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700497 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800498 resolver.registerContentObserver(Settings.Secure.getUriFor(
499 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700500 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
Michael Wright7b5a96b2014-08-09 19:28:42 -0700503 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800504 final Uri showImeUri = Settings.Secure.getUriFor(
505 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
506 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
507 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700509 if (showImeUri.equals(uri)) {
510 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800511 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
512 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
513 mContext.getContentResolver(),
514 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
515 0, mUserId) == 1;
516 if (mAccessibilityRequestingNoSoftKeyboard) {
517 final boolean showRequested = mShowRequested;
518 hideCurrentInputLocked(0, null);
519 mShowRequested = showRequested;
520 } else if (mShowRequested) {
521 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
522 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700523 } else {
524 boolean enabledChanged = false;
525 String newEnabled = mSettings.getEnabledInputMethodsStr();
526 if (!mLastEnabled.equals(newEnabled)) {
527 mLastEnabled = newEnabled;
528 enabledChanged = true;
529 }
530 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700534
535 @Override
536 public String toString() {
537 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
538 + " mLastEnabled=" + mLastEnabled + "}";
539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900542 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900543 @Override
544 public void onReceive(Context context, Intent intent) {
545 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700546 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900547 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700548 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900549 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800550 } else if (Intent.ACTION_USER_ADDED.equals(action)
551 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100552 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800553 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700554 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
555 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
556 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
557 final String prevValue = intent.getStringExtra(
558 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
559 final String newValue = intent.getStringExtra(
560 Intent.EXTRA_SETTING_NEW_VALUE);
561 restoreEnabledInputMethods(mContext, prevValue, newValue);
562 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900563 } else {
564 Slog.w(TAG, "Unexpected intent " + intent);
565 }
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700569 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
570 // does not attempt to validate on the fly with any installed device policy, so must only
571 // be run in the context of initial device setup.
572 //
573 // TODO: Move this method to InputMethodUtils with adding unit tests.
574 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
575 if (DEBUG_RESTORE) {
576 Slog.i(TAG, "Restoring enabled input methods:");
577 Slog.i(TAG, "prev=" + prevValue);
578 Slog.i(TAG, " new=" + newValue);
579 }
580 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900581 ArrayMap<String, ArraySet<String>> prevMap =
582 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
583 ArrayMap<String, ArraySet<String>> newMap =
584 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700585
586 // Merge the restored ime+subtype enabled states into the live state
587 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
588 final String imeId = entry.getKey();
589 ArraySet<String> prevSubtypes = prevMap.get(imeId);
590 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700591 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700592 prevMap.put(imeId, prevSubtypes);
593 }
594 prevSubtypes.addAll(entry.getValue());
595 }
596
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700597 final String mergedImesAndSubtypesString =
598 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700599 if (DEBUG_RESTORE) {
600 Slog.i(TAG, "Merged IME string:");
601 Slog.i(TAG, " " + mergedImesAndSubtypesString);
602 }
603 Settings.Secure.putString(context.getContentResolver(),
604 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
605 }
606
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800607 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900608 private boolean isChangingPackagesOfCurrentUser() {
609 final int userId = getChangingUserId();
610 final boolean retval = userId == mSettings.getCurrentUserId();
611 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900612 if (!retval) {
613 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
614 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900615 }
616 return retval;
617 }
618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 if (!isChangingPackagesOfCurrentUser()) {
622 return false;
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900625 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 final int N = mMethodList.size();
627 if (curInputMethodId != null) {
628 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 InputMethodInfo imi = mMethodList.get(i);
630 if (imi.getId().equals(curInputMethodId)) {
631 for (String pkg : packages) {
632 if (imi.getPackageName().equals(pkg)) {
633 if (!doit) {
634 return true;
635 }
satok723a27e2010-11-11 14:58:11 +0900636 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800637 chooseNewDefaultIMELocked();
638 return true;
639 }
640 }
641 }
642 }
643 }
644 }
645 return false;
646 }
647
648 @Override
649 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900650 if (!isChangingPackagesOfCurrentUser()) {
651 return;
652 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800653 synchronized (mMethodMap) {
654 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 final int N = mMethodList.size();
657 if (curInputMethodId != null) {
658 for (int i=0; i<N; i++) {
659 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900660 final String imiId = imi.getId();
661 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800662 curIm = imi;
663 }
satoke7c6998e2011-06-03 17:57:59 +0900664
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800665 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900666 if (isPackageModified(imi.getPackageName())) {
667 mFileManager.deleteAllInputMethodSubtypes(imiId);
668 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800669 if (change == PACKAGE_TEMPORARY_CHANGE
670 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800671 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 + imi.getComponent());
673 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 }
675 }
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
Yohei Yukawa94e33302016-02-12 19:37:03 -0800678 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800682 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800683 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800684 if (change == PACKAGE_TEMPORARY_CHANGE
685 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800686 ServiceInfo si = null;
687 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900688 si = mIPackageManager.getServiceInfo(
689 curIm.getComponent(), 0, mSettings.getCurrentUserId());
690 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800691 }
692 if (si == null) {
693 // Uh oh, current input method is no longer around!
694 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800695 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900696 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800697 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800698 changed = true;
699 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900701 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800702 }
703 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800704 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800705 }
satokab751aa2010-09-14 19:17:36 +0900706
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800707 if (curIm == null) {
708 // We currently don't have a default input method... is
709 // one now available?
710 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700711 } else if (!changed && isPackageModified(curIm.getPackageName())) {
712 // Even if the current input method is still available, mCurrentSubtype could
713 // be obsolete when the package is modified in practice.
714 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800717 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800718 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720 }
721 }
722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
Jeff Brownc28867a2013-03-26 15:42:39 -0700724 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900725 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700726 private final IInputMethod mMethod;
727 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800728
Jeff Brownc28867a2013-03-26 15:42:39 -0700729 MethodCallback(InputMethodManagerService imms, IInputMethod method,
730 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900731 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700732 mMethod = method;
733 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
satoke7c6998e2011-06-03 17:57:59 +0900736 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700737 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700738 long ident = Binder.clearCallingIdentity();
739 try {
740 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
741 } finally {
742 Binder.restoreCallingIdentity(ident);
743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
satok01038492012-04-09 21:08:27 +0900747 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700748 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900749 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700750 public void onHardKeyboardStatusChange(boolean available) {
751 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
752 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900753 }
754
Michael Wright7b5a96b2014-08-09 19:28:42 -0700755 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900756 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700757 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900758 }
759 synchronized(mMethodMap) {
760 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
761 && mSwitchingDialog.isShowing()) {
762 mSwitchingDialogTitleView.findViewById(
763 com.android.internal.R.id.hard_keyboard_section).setVisibility(
764 available ? View.VISIBLE : View.GONE);
765 }
766 }
767 }
768 }
769
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800770 public static final class Lifecycle extends SystemService {
771 private InputMethodManagerService mService;
772
773 public Lifecycle(Context context) {
774 super(context);
775 mService = new InputMethodManagerService(context);
776 }
777
778 @Override
779 public void onStart() {
780 LocalServices.addService(InputMethodManagerInternal.class,
781 new LocalServiceImpl(mService.mHandler));
782 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
783 }
784
785 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800786 public void onSwitchUser(@UserIdInt int userHandle) {
787 // Called on the system server's main looper thread.
788 // TODO: Dispatch this to a worker thread as needed.
789 mService.onSwitchUser(userHandle);
790 }
791
792 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800793 public void onBootPhase(int phase) {
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800794 // Called on the system server's main looper thread.
795 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800796 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
797 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
798 .getService(Context.STATUS_BAR_SERVICE);
799 mService.systemRunning(statusBarService);
800 }
801 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800802
803 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800804 public void onUnlockUser(@UserIdInt int userHandle) {
805 // Called on the system server's main looper thread.
806 // TODO: Dispatch this to a worker thread as needed.
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800807 mService.onUnlockUser(userHandle);
808 }
809 }
810
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800811 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800812 synchronized(mMethodMap) {
813 final int currentUserId = mSettings.getCurrentUserId();
814 if (DEBUG) {
815 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
816 }
817 if (userId != currentUserId) {
818 return;
819 }
820 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
821 // We need to rebuild IMEs.
822 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
823 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
824 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800825 }
826
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800827 void onSwitchUser(@UserIdInt int userId) {
828 synchronized (mMethodMap) {
829 switchUserLocked(userId);
830 }
831 }
832
Seigo Nonaka7309b122015-08-17 18:34:13 -0700833 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900834 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800836 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700838 // Note: SettingsObserver doesn't register observers in its constructor.
839 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 mIWindowManager = IWindowManager.Stub.asInterface(
841 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700842 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800843 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900844 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 public void executeMessage(Message msg) {
846 handleMessage(msg);
847 }
Mita Yuned218c72012-12-06 17:18:25 -0800848 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800849 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800850 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900851 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700852 mHasFeature = context.getPackageManager().hasSystemFeature(
853 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500854 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
satok7cfc0ed2011-06-20 21:29:36 +0900855
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400856 Bundle extras = new Bundle();
857 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
858 mImeSwitcherNotification = new Notification.Builder(mContext)
859 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
860 .setWhen(0)
861 .setOngoing(true)
862 .addExtras(extras)
863 .setCategory(Notification.CATEGORY_SYSTEM)
864 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400865
satok7cfc0ed2011-06-20 21:29:36 +0900866 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900867 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900868
869 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900870
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900871 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900872 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800873 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
874 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700875 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900876 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800877
satok7cfc0ed2011-06-20 21:29:36 +0900878 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900879 int userId = 0;
880 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900881 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
882 } catch (RemoteException e) {
883 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
884 }
satok81f8b7c2012-12-04 20:42:56 +0900885 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900886
satokd87c2592010-09-29 11:52:06 +0900887 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900888 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800889 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700890
Kenny Guy2a764942014-04-02 13:29:20 +0100891 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900892 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900893 synchronized (mMethodMap) {
894 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
895 mSettings, context);
896 }
satok0a1bcf42012-05-16 19:26:31 +0900897
898 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900899 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900900 if (DEBUG) {
901 Slog.d(TAG, "Initial default ime = " + defaultImiId);
902 }
satok0a1bcf42012-05-16 19:26:31 +0900903 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
904
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900905 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800906 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900907 }
satokd87c2592010-09-29 11:52:06 +0900908 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
satok0a1bcf42012-05-16 19:26:31 +0900910 if (!mImeSelectedOnBoot) {
911 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900912 synchronized (mMethodMap) {
913 resetDefaultImeLocked(context);
914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900917 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700918 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900919 updateFromSettingsLocked(true);
920 }
satok5b927c432012-05-01 20:09:34 +0900921
922 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
923 // according to the new system locale.
924 final IntentFilter filter = new IntentFilter();
925 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
926 mContext.registerReceiver(
927 new BroadcastReceiver() {
928 @Override
929 public void onReceive(Context context, Intent intent) {
930 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900931 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900932 }
933 }
934 }, filter);
935 }
936
satok5b927c432012-05-01 20:09:34 +0900937 private void resetDefaultImeLocked(Context context) {
938 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800939 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900940 return;
941 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800942 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
943 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
944 if (suitableImes.isEmpty()) {
945 Slog.i(TAG, "No default found");
946 return;
satok5b927c432012-05-01 20:09:34 +0900947 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800948 final InputMethodInfo defIm = suitableImes.get(0);
949 Slog.i(TAG, "Default found, using " + defIm.getId());
950 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900951 }
952
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900953 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
954 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 if (!mSystemReady) {
956 // not system ready
957 return;
958 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800959 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900960 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800961 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 if (!updateOnlyWhenLocaleChanged) {
963 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800964 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 }
966 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800967 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800969 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 if (!updateOnlyWhenLocaleChanged) {
971 final String selectedImiId = mSettings.getSelectedInputMethod();
972 if (TextUtils.isEmpty(selectedImiId)) {
973 // This is the first time of the user switch and
974 // set the current ime to the proper one.
975 resetDefaultImeLocked(mContext);
976 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900977 } else {
978 // If the locale is changed, needs to reset the default ime
979 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800981 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800982 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 if (!updateOnlyWhenLocaleChanged) {
984 try {
985 startInputInnerLocked();
986 } catch (RuntimeException e) {
987 Slog.w(TAG, "Unexpected exception", e);
988 }
989 }
990 }
991 }
992
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900993 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900994 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
995 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 }
997
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900998 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700999 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1000 + " currentUserId=" + mSettings.getCurrentUserId());
1001
Yohei Yukawa81482972015-06-04 00:58:59 -07001002 // ContentObserver should be registered again when the user is changed
1003 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001004
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001005 // If the system is not ready or the device is not yed unlocked by the user, then we use
1006 // copy-on-write settings.
1007 final boolean useCopyOnWriteSettings =
1008 !mSystemReady || !mUserManager.isUserUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001009 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001010 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001011 // InputMethodFileManager should be reset when the user is changed
1012 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001013 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001014
1015 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1016 + " defaultImiId=" + defaultImiId);
1017
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001018 // For secondary users, the list of enabled IMEs may not have been updated since the
1019 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1020 // not be empty even if the IME has been uninstalled by the primary user.
1021 // Even in such cases, IMMS works fine because it will find the most applicable
1022 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001023 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001024 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001025 initialUserSwitch /* needsToResetDefaultIme */);
1026 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001027 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1028 mSettings.getEnabledInputMethodListLocked(), newUserId,
1029 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001030 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001031
1032 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1033 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001034 }
1035
Kenny Guy2a764942014-04-02 13:29:20 +01001036 void updateCurrentProfileIds() {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001037 List<UserInfo> profiles = mUserManager.getProfiles(mSettings.getCurrentUserId());
Kenny Guy2a764942014-04-02 13:29:20 +01001038 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1039 for (int i = 0; i < currentProfileIds.length; i++) {
1040 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001041 }
Kenny Guy2a764942014-04-02 13:29:20 +01001042 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001043 }
1044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 @Override
1046 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1047 throws RemoteException {
1048 try {
1049 return super.onTransact(code, data, reply, flags);
1050 } catch (RuntimeException e) {
1051 // The input method manager only throws security exceptions, so let's
1052 // log all others.
1053 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001054 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
1056 throw e;
1057 }
1058 }
1059
Svetoslav Ganova0027152013-06-25 14:59:53 -07001060 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001061 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001062 if (DEBUG) {
1063 Slog.d(TAG, "--- systemReady");
1064 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001065 if (!mSystemReady) {
1066 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001067 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001068 mSettings.switchCurrentUser(currentUserId,
1069 !mUserManager.isUserUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001070 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1071 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001072 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001073 if (mStatusBar != null) {
1074 mStatusBar.setIconVisibility(mSlotIme, false);
1075 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001076 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001077 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1078 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001079 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001080 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001081 mHardKeyboardListener);
1082 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001083 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001084 if (!mImeSelectedOnBoot) {
1085 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001086 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001087 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1088 mSettings.getEnabledInputMethodListLocked(),
1089 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001090 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001091 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001092 try {
1093 startInputInnerLocked();
1094 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001096 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001097 }
1098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001101 // ---------------------------------------------------------------------------------------
1102 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1103 // 1) it comes from the system process
1104 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1105 private boolean calledFromValidUser() {
1106 final int uid = Binder.getCallingUid();
1107 final int userId = UserHandle.getUserId(uid);
1108 if (DEBUG) {
1109 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1110 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1111 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001112 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1113 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001114 }
Kenny Guy2a764942014-04-02 13:29:20 +01001115 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001116 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001118
1119 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1120 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1121 // must not manage background users' states in any functions.
1122 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1123 // by a token.
1124 if (mContext.checkCallingOrSelfPermission(
1125 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1126 == PackageManager.PERMISSION_GRANTED) {
1127 if (DEBUG) {
1128 Slog.d(TAG, "--- Access granted because the calling process has "
1129 + "the INTERACT_ACROSS_USERS_FULL permission");
1130 }
1131 return true;
1132 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001133 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1134 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001135 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001136 }
1137
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001138
1139 /**
1140 * Returns true iff the caller is identified to be the current input method with the token.
1141 * @param token The window token given to the input method when it was started.
1142 * @return true if and only if non-null valid token is specified.
1143 */
1144 private boolean calledWithValidToken(IBinder token) {
1145 if (token == null || mCurToken != token) {
1146 return false;
1147 }
1148 return true;
1149 }
1150
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001151 private boolean bindCurrentInputMethodService(
1152 Intent service, ServiceConnection conn, int flags) {
1153 if (service == null || conn == null) {
1154 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1155 return false;
1156 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001157 return mContext.bindServiceAsUser(service, conn, flags,
1158 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001159 }
1160
satoke7c6998e2011-06-03 17:57:59 +09001161 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001163 // TODO: Make this work even for non-current users?
1164 if (!calledFromValidUser()) {
1165 return Collections.emptyList();
1166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001168 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 }
1171
satoke7c6998e2011-06-03 17:57:59 +09001172 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001174 // TODO: Make this work even for non-current users?
1175 if (!calledFromValidUser()) {
1176 return Collections.emptyList();
1177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001179 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 }
1182
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001183 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001184 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001185 * @return enabled subtypes of the specified imi
1186 */
satoke7c6998e2011-06-03 17:57:59 +09001187 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001188 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001189 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001190 // TODO: Make this work even for non-current users?
1191 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001192 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001193 }
satok67ddf9c2010-11-17 09:45:54 +09001194 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001195 final InputMethodInfo imi;
1196 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001197 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001198 } else {
1199 imi = mMethodMap.get(imiId);
1200 }
1201 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001202 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001203 }
1204 return mSettings.getEnabledInputMethodSubtypeListLocked(
1205 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001206 }
1207 }
1208
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public void addClient(IInputMethodClient client,
1211 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001212 if (!calledFromValidUser()) {
1213 return;
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 synchronized (mMethodMap) {
1216 mClients.put(client.asBinder(), new ClientState(client,
1217 inputContext, uid, pid));
1218 }
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
satoke7c6998e2011-06-03 17:57:59 +09001221 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001223 if (!calledFromValidUser()) {
1224 return;
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001227 ClientState cs = mClients.remove(client.asBinder());
1228 if (cs != null) {
1229 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001230 if (mCurClient == cs) {
1231 mCurClient = null;
1232 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001233 if (mCurFocusedWindowClient == cs) {
1234 mCurFocusedWindowClient = null;
1235 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 void executeOrSendMessage(IInterface target, Message msg) {
1241 if (target.asBinder() instanceof Binder) {
1242 mCaller.sendMessage(msg);
1243 } else {
1244 handleMessage(msg);
1245 msg.recycle();
1246 }
1247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001248
Yohei Yukawa33e81792015-11-17 21:14:42 -08001249 void unbindCurrentClientLocked(
1250 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001252 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 + mCurClient.client.asBinder());
1254 if (mBoundToMethod) {
1255 mBoundToMethod = false;
1256 if (mCurMethod != null) {
1257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1258 MSG_UNBIND_INPUT, mCurMethod));
1259 }
1260 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001261
1262 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1263 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001264 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1265 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001268
The Android Open Source Project10592532009-03-18 17:39:46 -07001269 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 private int getImeShowFlags() {
1274 int flags = 0;
1275 if (mShowForced) {
1276 flags |= InputMethod.SHOW_FORCED
1277 | InputMethod.SHOW_EXPLICIT;
1278 } else if (mShowExplicitlyRequested) {
1279 flags |= InputMethod.SHOW_EXPLICIT;
1280 }
1281 return flags;
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 private int getAppShowFlags() {
1285 int flags = 0;
1286 if (mShowForced) {
1287 flags |= InputMethodManager.SHOW_FORCED;
1288 } else if (!mShowExplicitlyRequested) {
1289 flags |= InputMethodManager.SHOW_IMPLICIT;
1290 }
1291 return flags;
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
Dianne Hackborn7663d802012-02-24 13:08:49 -08001294 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 if (!mBoundToMethod) {
1296 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1297 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1298 mBoundToMethod = true;
1299 }
1300 final SessionState session = mCurClient.curSession;
1301 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001302 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1303 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1304 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001306 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1307 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1308 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001311 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001312 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001314 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001315 (session.channel != null ? session.channel.dup() : null),
1316 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001319 InputBindResult startInputLocked(
1320 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001321 IInputMethodClient client, IInputContext inputContext,
1322 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001323 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 // If no method is currently selected, do nothing.
1325 if (mCurMethodId == null) {
1326 return mNoBinding;
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 ClientState cs = mClients.get(client.asBinder());
1330 if (cs == null) {
1331 throw new IllegalArgumentException("unknown client "
1332 + client.asBinder());
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Yohei Yukawa74750f22016-03-22 12:54:22 -07001335 if (attribute == null) {
1336 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1337 + " uid=" + cs.uid + " pid=" + cs.pid);
1338 return null;
1339 }
1340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 try {
1342 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1343 // Check with the window manager to make sure this client actually
1344 // has a window with focus. If not, reject. This is thread safe
1345 // because if the focus changes some time before or after, the
1346 // next client receiving focus that has any interest in input will
1347 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001348 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1350 return null;
1351 }
1352 } catch (RemoteException e) {
1353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001354
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001355 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1356 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001357 }
1358
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001359 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001360 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001361 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001362 // If no method is currently selected, do nothing.
1363 if (mCurMethodId == null) {
1364 return mNoBinding;
1365 }
1366
Yohei Yukawad57ba672015-06-08 16:39:46 -07001367 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1368 attribute.packageName)) {
1369 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1370 + " uid=" + cs.uid + " package=" + attribute.packageName);
1371 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001372 }
1373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001375 // Was the keyguard locked when switching over to the new client?
1376 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 // If the client is changing, we need to switch over to the new
1378 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001379 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001380 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001381 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382
1383 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001384 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001385 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001386 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 // Bump up the sequence for this client and attach it.
1391 mCurSeq++;
1392 if (mCurSeq <= 0) mCurSeq = 1;
1393 mCurClient = cs;
1394 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001395 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 // Check if the input method is changing.
1399 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1400 if (cs.curSession != null) {
1401 // Fast case: if we are already connected to the input method,
1402 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001403 return attachNewInputLocked(
1404 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 if (mHaveConnection) {
1407 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 // Return to client, and we will get back with it when
1409 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001410 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001411 return new InputBindResult(null, null, mCurId, mCurSeq,
1412 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 } else if (SystemClock.uptimeMillis()
1414 < (mLastBindTime+TIME_TO_RECONNECT)) {
1415 // In this case we have connected to the service, but
1416 // don't yet have its interface. If it hasn't been too
1417 // long since we did the connection, we'll return to
1418 // the client and wait to get the service interface so
1419 // we can report back. If it has been too long, we want
1420 // to fall through so we can try a disconnect/reconnect
1421 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001422 return new InputBindResult(null, null, mCurId, mCurSeq,
1423 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1426 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428 }
1429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001431 return startInputInnerLocked();
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001434 InputBindResult startInputInnerLocked() {
1435 if (mCurMethodId == null) {
1436 return mNoBinding;
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001439 if (!mSystemReady) {
1440 // If the system is not yet ready, we shouldn't be running third
1441 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001442 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1443 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1447 if (info == null) {
1448 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001451 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1454 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001455 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1456 com.android.internal.R.string.input_method_binding_label);
1457 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1458 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001459 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001460 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1461 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 mLastBindTime = SystemClock.uptimeMillis();
1463 mHaveConnection = true;
1464 mCurId = info.getId();
1465 mCurToken = new Binder();
1466 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001467 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 mIWindowManager.addWindowToken(mCurToken,
1469 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1470 } catch (RemoteException e) {
1471 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001472 return new InputBindResult(null, null, mCurId, mCurSeq,
1473 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 } else {
1475 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001476 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 + mCurIntent);
1478 }
1479 return null;
1480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001481
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001482 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001483 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001484 IInputMethodClient client, IInputContext inputContext,
1485 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001486 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001487 if (!calledFromValidUser()) {
1488 return null;
1489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001491 if (DEBUG) {
1492 Slog.v(TAG, "startInput: reason="
1493 + InputMethodClient.getStartInputReason(startInputReason)
1494 + " client = " + client.asBinder()
1495 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001496 + " missingMethods="
1497 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001498 + " attribute=" + attribute
1499 + " controlFlags=#" + Integer.toHexString(controlFlags));
1500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 final long ident = Binder.clearCallingIdentity();
1502 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001503 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1504 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 } finally {
1506 Binder.restoreCallingIdentity(ident);
1507 }
1508 }
1509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
satoke7c6998e2011-06-03 17:57:59 +09001511 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 public void finishInput(IInputMethodClient client) {
1513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
satoke7c6998e2011-06-03 17:57:59 +09001515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 public void onServiceConnected(ComponentName name, IBinder service) {
1517 synchronized (mMethodMap) {
1518 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1519 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001520 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001522 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001523 return;
1524 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001525 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001526 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1527 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001529 clearClientSessionLocked(mCurClient);
1530 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
1532 }
1533 }
1534 }
1535
Jeff Brownc28867a2013-03-26 15:42:39 -07001536 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1537 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 synchronized (mMethodMap) {
1539 if (mCurMethod != null && method != null
1540 && mCurMethod.asBinder() == method.asBinder()) {
1541 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001542 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001544 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001545 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 if (res.method != null) {
1547 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001548 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001550 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552 }
1553 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001554
1555 // Session abandoned. Close its associated input channel.
1556 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001558
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001559 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001560 if (mVisibleBound) {
1561 mContext.unbindService(mVisibleConnection);
1562 mVisibleBound = false;
1563 }
1564
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001565 if (mHaveConnection) {
1566 mContext.unbindService(this);
1567 mHaveConnection = false;
1568 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001569
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001570 if (mCurToken != null) {
1571 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001572 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001573 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001574 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001575 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001576 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001577 mIWindowManager.removeWindowToken(mCurToken);
1578 } catch (RemoteException e) {
1579 }
1580 mCurToken = null;
1581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
The Android Open Source Project10592532009-03-18 17:39:46 -07001583 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001586
Yohei Yukawa33e81792015-11-17 21:14:42 -08001587 void resetCurrentMethodAndClient(
1588 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001589 mCurMethodId = null;
1590 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001591 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001592 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001593
1594 void requestClientSessionLocked(ClientState cs) {
1595 if (!cs.sessionRequested) {
1596 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1597 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1598 cs.sessionRequested = true;
1599 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1600 MSG_CREATE_SESSION, mCurMethod, channels[1],
1601 new MethodCallback(this, mCurMethod, channels[0])));
1602 }
1603 }
1604
1605 void clearClientSessionLocked(ClientState cs) {
1606 finishSessionLocked(cs.curSession);
1607 cs.curSession = null;
1608 cs.sessionRequested = false;
1609 }
1610
1611 private void finishSessionLocked(SessionState sessionState) {
1612 if (sessionState != null) {
1613 if (sessionState.session != null) {
1614 try {
1615 sessionState.session.finishSession();
1616 } catch (RemoteException e) {
1617 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001618 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001619 }
1620 sessionState.session = null;
1621 }
1622 if (sessionState.channel != null) {
1623 sessionState.channel.dispose();
1624 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001625 }
1626 }
1627 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001628
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001629 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 if (mCurMethod != null) {
1631 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001632 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001634
Jeff Brownc28867a2013-03-26 15:42:39 -07001635 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001636 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 mCurMethod = null;
1638 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001639 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001640 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001643
satoke7c6998e2011-06-03 17:57:59 +09001644 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 public void onServiceDisconnected(ComponentName name) {
1646 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001647 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 + " mCurIntent=" + mCurIntent);
1649 if (mCurMethod != null && mCurIntent != null
1650 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001651 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 // We consider this to be a new bind attempt, since the system
1653 // should now try to restart the service for us.
1654 mLastBindTime = SystemClock.uptimeMillis();
1655 mShowRequested = mInputShown;
1656 mInputShown = false;
1657 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001658 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1659 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1660 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662 }
1663 }
1664 }
1665
satokf9f01002011-05-19 21:31:50 +09001666 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1668 long ident = Binder.clearCallingIdentity();
1669 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001671 if (!calledWithValidToken(token)) {
1672 final int uid = Binder.getCallingUid();
1673 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1674 + " token:" + token);
1675 return;
1676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001678 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001679 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001680 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001683 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001684 CharSequence contentDescription = null;
1685 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001686 // Use PackageManager to load label
1687 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001688 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001689 mIPackageManager.getApplicationInfo(packageName, 0,
1690 mSettings.getCurrentUserId()));
1691 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001692 /* ignore */
1693 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001694 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001695 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001696 contentDescription != null
1697 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001698 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701 }
1702 } finally {
1703 Binder.restoreCallingIdentity(ident);
1704 }
1705 }
1706
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001707 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001708 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001709 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001710 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001711 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001712 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001713 // When physical keyboard is attached, we show the ime switcher (or notification if
1714 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1715 // exists in the IME switcher dialog. Might be OK to remove this condition once
1716 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1717 return true;
Yohei Yukawa89398382016-03-29 11:37:04 -07001718 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1719 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001720 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001721
1722 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1723 final int N = imis.size();
1724 if (N > 2) return true;
1725 if (N < 1) return false;
1726 int nonAuxCount = 0;
1727 int auxCount = 0;
1728 InputMethodSubtype nonAuxSubtype = null;
1729 InputMethodSubtype auxSubtype = null;
1730 for(int i = 0; i < N; ++i) {
1731 final InputMethodInfo imi = imis.get(i);
1732 final List<InputMethodSubtype> subtypes =
1733 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1734 final int subtypeCount = subtypes.size();
1735 if (subtypeCount == 0) {
1736 ++nonAuxCount;
1737 } else {
1738 for (int j = 0; j < subtypeCount; ++j) {
1739 final InputMethodSubtype subtype = subtypes.get(j);
1740 if (!subtype.isAuxiliary()) {
1741 ++nonAuxCount;
1742 nonAuxSubtype = subtype;
1743 } else {
1744 ++auxCount;
1745 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001746 }
1747 }
satok7cfc0ed2011-06-20 21:29:36 +09001748 }
1749 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001750 if (nonAuxCount > 1 || auxCount > 1) {
1751 return true;
1752 } else if (nonAuxCount == 1 && auxCount == 1) {
1753 if (nonAuxSubtype != null && auxSubtype != null
1754 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1755 || auxSubtype.overridesImplicitlyEnabledSubtype()
1756 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1757 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1758 return false;
1759 }
1760 return true;
1761 }
1762 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001763 }
1764
John Spurlocke0980502013-10-25 11:59:29 -04001765 private boolean isKeyguardLocked() {
1766 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1767 }
1768
satokdbf29502011-08-25 15:28:23 +09001769 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001770 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001771 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001772 if (!calledWithValidToken(token)) {
1773 final int uid = Binder.getCallingUid();
1774 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1775 + " token:" + token);
1776 return;
1777 }
1778
1779 synchronized (mMethodMap) {
1780 mImeWindowVis = vis;
1781 mBackDisposition = backDisposition;
1782 updateSystemUiLocked(token, vis, backDisposition);
1783 }
1784 }
1785
1786 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1787 synchronized (mMethodMap) {
1788 updateSystemUiLocked(token, vis, backDisposition);
1789 }
1790 }
1791
1792 // Caution! This method is called in this class. Handle multi-user carefully
1793 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1794 if (!calledWithValidToken(token)) {
1795 final int uid = Binder.getCallingUid();
1796 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1797 + " token:" + token);
1798 return;
1799 }
1800
1801 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1802 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001803 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001804 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001805 // apply policy for binder calls
1806 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1807 vis = 0;
satok06487a52010-10-29 11:37:18 +09001808 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001809 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1810 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1811 if (mStatusBar != null) {
1812 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1813 needsToShowImeSwitcher);
1814 }
1815 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1816 if (imi != null && needsToShowImeSwitcher) {
1817 // Used to load label
1818 final CharSequence title = mRes.getText(
1819 com.android.internal.R.string.select_input_method);
1820 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1821 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001822 mImeSwitcherNotification.setContentTitle(title)
1823 .setContentText(summary)
1824 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001825 try {
1826 if ((mNotificationManager != null)
1827 && !mIWindowManager.hasNavigationBar()) {
1828 if (DEBUG) {
1829 Slog.d(TAG, "--- show notification: label = " + summary);
1830 }
1831 mNotificationManager.notifyAsUser(null,
1832 com.android.internal.R.string.select_input_method,
1833 mImeSwitcherNotification.build(), UserHandle.ALL);
1834 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001835 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001836 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001837 }
1838 } else {
1839 if (mNotificationShown && mNotificationManager != null) {
1840 if (DEBUG) {
1841 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001842 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001843 mNotificationManager.cancelAsUser(null,
1844 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1845 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001846 }
satok06487a52010-10-29 11:37:18 +09001847 }
1848 } finally {
1849 Binder.restoreCallingIdentity(ident);
1850 }
1851 }
1852
satoke7c6998e2011-06-03 17:57:59 +09001853 @Override
satokf9f01002011-05-19 21:31:50 +09001854 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001855 if (!calledFromValidUser()) {
1856 return;
1857 }
satokf9f01002011-05-19 21:31:50 +09001858 synchronized (mMethodMap) {
1859 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1860 for (int i = 0; i < spans.length; ++i) {
1861 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001862 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001863 mSecureSuggestionSpans.put(ss, currentImi);
1864 }
1865 }
1866 }
1867 }
1868
satoke7c6998e2011-06-03 17:57:59 +09001869 @Override
satokf9f01002011-05-19 21:31:50 +09001870 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001871 if (!calledFromValidUser()) {
1872 return false;
1873 }
satokf9f01002011-05-19 21:31:50 +09001874 synchronized (mMethodMap) {
1875 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1876 // TODO: Do not send the intent if the process of the targetImi is already dead.
1877 if (targetImi != null) {
1878 final String[] suggestions = span.getSuggestions();
1879 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001880 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001881 final Intent intent = new Intent();
1882 // Ensures that only a class in the original IME package will receive the
1883 // notification.
satok42c5a162011-05-26 16:46:14 +09001884 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001885 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1886 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1887 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1888 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001889 final long ident = Binder.clearCallingIdentity();
1890 try {
1891 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1892 } finally {
1893 Binder.restoreCallingIdentity(ident);
1894 }
satokf9f01002011-05-19 21:31:50 +09001895 return true;
1896 }
1897 }
1898 return false;
1899 }
1900
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001901 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001902 updateInputMethodsFromSettingsLocked(enabledMayChange);
1903 updateKeyboardFromSettingsLocked();
1904 }
1905
1906 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001907 if (enabledMayChange) {
1908 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1909 for (int i=0; i<enabled.size(); i++) {
1910 // We allow the user to select "disabled until used" apps, so if they
1911 // are enabling one of those here we now need to make it enabled.
1912 InputMethodInfo imm = enabled.get(i);
1913 try {
1914 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1915 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1916 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001917 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001918 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001919 if (DEBUG) {
1920 Slog.d(TAG, "Update state(" + imm.getId()
1921 + "): DISABLED_UNTIL_USED -> DEFAULT");
1922 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001923 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1924 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001925 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1926 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001927 }
1928 } catch (RemoteException e) {
1929 }
1930 }
1931 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001932 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1933 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1934 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1935 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001936 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001937 // There is no input method selected, try to choose new applicable input method.
1938 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001939 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001940 }
1941 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001943 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001945 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001946 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
satokf3db1af2010-11-23 13:34:33 +09001948 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001949 } else {
1950 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001951 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001953 // Here is not the perfect place to reset the switching controller. Ideally
1954 // mSwitchingController and mSettings should be able to share the same state.
1955 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1956 // the same enabled IMEs list.
1957 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001958
1959 }
1960
1961 public void updateKeyboardFromSettingsLocked() {
1962 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1963 if (mSwitchingDialog != null
1964 && mSwitchingDialogTitleView != null
1965 && mSwitchingDialog.isShowing()) {
1966 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1967 com.android.internal.R.id.hard_keyboard_switch);
1968 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001971
Yohei Yukawab097b822015-12-01 10:43:08 -08001972 private void notifyInputMethodSubtypeChanged(final int userId,
1973 @Nullable final InputMethodInfo inputMethodInfo,
1974 @Nullable final InputMethodSubtype subtype) {
1975 final InputManagerInternal inputManagerInternal =
1976 LocalServices.getService(InputManagerInternal.class);
1977 if (inputManagerInternal != null) {
1978 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1979 }
1980 }
1981
satokab751aa2010-09-14 19:17:36 +09001982 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 InputMethodInfo info = mMethodMap.get(id);
1984 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001985 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001987
satokd81e9502012-05-21 12:58:45 +09001988 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001990 final int subtypeCount = info.getSubtypeCount();
1991 if (subtypeCount <= 0) {
1992 return;
satokcd7cd292010-11-20 15:46:23 +09001993 }
satokd81e9502012-05-21 12:58:45 +09001994 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1995 final InputMethodSubtype newSubtype;
1996 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1997 newSubtype = info.getSubtypeAt(subtypeId);
1998 } else {
1999 // If subtype is null, try to find the most applicable one from
2000 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002001 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002002 }
2003 if (newSubtype == null || oldSubtype == null) {
2004 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2005 + ", new subtype = " + newSubtype);
2006 return;
2007 }
2008 if (newSubtype != oldSubtype) {
2009 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2010 if (mCurMethod != null) {
2011 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002012 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002013 mCurMethod.changeInputMethodSubtype(newSubtype);
2014 } catch (RemoteException e) {
2015 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002016 return;
satokab751aa2010-09-14 19:17:36 +09002017 }
2018 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002019 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 return;
2022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002023
satokd81e9502012-05-21 12:58:45 +09002024 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 final long ident = Binder.clearCallingIdentity();
2026 try {
satokab751aa2010-09-14 19:17:36 +09002027 // Set a subtype to this input method.
2028 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002029 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2030 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2031 // because mCurMethodId is stored as a history in
2032 // setSelectedInputMethodAndSubtypeLocked().
2033 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034
2035 if (ActivityManagerNative.isSystemReady()) {
2036 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002037 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002039 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002041 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 } finally {
2043 Binder.restoreCallingIdentity(ident);
2044 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002045
2046 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2047 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002049
satok42c5a162011-05-26 16:46:14 +09002050 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002051 public boolean showSoftInput(IInputMethodClient client, int flags,
2052 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002053 if (!calledFromValidUser()) {
2054 return false;
2055 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002056 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 long ident = Binder.clearCallingIdentity();
2058 try {
2059 synchronized (mMethodMap) {
2060 if (mCurClient == null || client == null
2061 || mCurClient.client.asBinder() != client.asBinder()) {
2062 try {
2063 // We need to check if this is the current client with
2064 // focus in the window manager, to allow this call to
2065 // be made before input is started in it.
2066 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002067 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002068 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002071 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
2073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002074
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002075 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002076 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
2078 } finally {
2079 Binder.restoreCallingIdentity(ident);
2080 }
2081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002082
The Android Open Source Project4df24232009-03-05 14:34:35 -08002083 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002085 if (mAccessibilityRequestingNoSoftKeyboard) {
2086 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
Anna Galusza9b278112016-01-04 11:37:37 -08002088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2090 mShowExplicitlyRequested = true;
2091 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002092 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2093 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002095
Dianne Hackborncc278702009-09-02 23:07:23 -07002096 if (!mSystemReady) {
2097 return false;
2098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002099
The Android Open Source Project4df24232009-03-05 14:34:35 -08002100 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002102 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002103 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2104 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2105 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002107 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002108 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002109 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002110 | Context.BIND_TREAT_LIKE_ACTIVITY
2111 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002112 mVisibleBound = true;
2113 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002114 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002116 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 // The client has asked to have the input method shown, but
2118 // we have been sitting here too long with a connection to the
2119 // service and no interface received, so let's disconnect/connect
2120 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002121 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002123 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002125 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002126 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002127 } else {
2128 if (DEBUG) {
2129 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2130 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002133
The Android Open Source Project4df24232009-03-05 14:34:35 -08002134 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002136
satok42c5a162011-05-26 16:46:14 +09002137 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002138 public boolean hideSoftInput(IInputMethodClient client, int flags,
2139 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002140 if (!calledFromValidUser()) {
2141 return false;
2142 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002143 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 long ident = Binder.clearCallingIdentity();
2145 try {
2146 synchronized (mMethodMap) {
2147 if (mCurClient == null || client == null
2148 || mCurClient.client.asBinder() != client.asBinder()) {
2149 try {
2150 // We need to check if this is the current client with
2151 // focus in the window manager, to allow this call to
2152 // be made before input is started in it.
2153 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002154 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2155 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002156 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002159 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
2161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002162
Joe Onorato8a9b2202010-02-26 18:56:32 -08002163 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002164 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 }
2166 } finally {
2167 Binder.restoreCallingIdentity(ident);
2168 }
2169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002170
The Android Open Source Project4df24232009-03-05 14:34:35 -08002171 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2173 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002174 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 -08002175 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002178 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 -08002179 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002181
2182 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2183 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2184 // to be updated with the new value sent from IME process. Even in such a transient state
2185 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2186 // application process as a valid request, and have even promised such a behavior with CTS
2187 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2188 // IMMS#InputShown indicates that the software keyboard is shown.
2189 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2190 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2191 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002192 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002193 if (shouldHideSoftInput) {
2194 // The IME will report its visible state again after the following message finally
2195 // delivered to the IME process as an IPC. Hence the inconsistency between
2196 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2197 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002198 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2199 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2200 res = true;
2201 } else {
2202 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002204 if (mHaveConnection && mVisibleBound) {
2205 mContext.unbindService(mVisibleConnection);
2206 mVisibleBound = false;
2207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 mInputShown = false;
2209 mShowRequested = false;
2210 mShowExplicitlyRequested = false;
2211 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002212 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002214
satok42c5a162011-05-26 16:46:14 +09002215 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002216 public InputBindResult startInputOrWindowGainedFocus(
2217 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2218 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002219 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002220 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002221 if (windowToken != null) {
2222 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002223 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002224 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002225 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2226 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002227 }
2228 }
2229
2230 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002231 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2232 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002233 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2234 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002235 // Needs to check the validity before clearing calling identity
2236 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002237 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 long ident = Binder.clearCallingIdentity();
2239 try {
2240 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002241 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2242 + InputMethodClient.getStartInputReason(startInputReason)
2243 + " client=" + client.asBinder()
2244 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002245 + " missingMethods="
2246 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002247 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002248 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002250 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002251
Dianne Hackborn7663d802012-02-24 13:08:49 -08002252 ClientState cs = mClients.get(client.asBinder());
2253 if (cs == null) {
2254 throw new IllegalArgumentException("unknown client "
2255 + client.asBinder());
2256 }
2257
2258 try {
2259 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2260 // Check with the window manager to make sure this client actually
2261 // has a window with focus. If not, reject. This is thread safe
2262 // because if the focus changes some time before or after, the
2263 // next client receiving focus that has any interest in input will
2264 // be calling through here after that change happens.
2265 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2266 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2267 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002269 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002271
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002272 if (!calledFromValidUser) {
2273 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2274 Slog.w(TAG, "If you want to interect with IME, you need "
2275 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2276 hideCurrentInputLocked(0, null);
2277 return null;
2278 }
2279
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002280 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002281 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002282 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002284 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2285 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002286 }
2287 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002288 }
2289 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002290 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002291
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002292 // Should we auto-show the IME even if the caller has not
2293 // specified what should be done with it?
2294 // We only do this automatically if the window can resize
2295 // to accommodate the IME (so what the user sees will give
2296 // them good context without input information being obscured
2297 // by the IME) or if running on a large screen where there
2298 // is more room for the target window + IME.
2299 final boolean doAutoShow =
2300 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2301 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2302 || mRes.getConfiguration().isLayoutSizeAtLeast(
2303 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002304 final boolean isTextEditor =
2305 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2306
2307 // We want to start input before showing the IME, but after closing
2308 // it. We want to do this after closing it to help the IME disappear
2309 // more quickly (not get stuck behind it initializing itself for the
2310 // new focused input, even if its window wants to hide the IME).
2311 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2314 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002315 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2317 // There is no focus view, and this window will
2318 // be behind any soft input window, so hide the
2319 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002320 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002321 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002323 } else if (isTextEditor && doAutoShow && (softInputMode &
2324 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 // There is a focus view, and we are navigating forward
2326 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002327 // We only do this automatically if the window can resize
2328 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002329 // them good context without input information being obscured
2330 // by the IME) or if running on a large screen where there
2331 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002332 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002333 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002334 res = startInputUncheckedLocked(cs, inputContext,
2335 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002336 didStart = true;
2337 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002338 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 }
2340 break;
2341 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2342 // Do nothing.
2343 break;
2344 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2345 if ((softInputMode &
2346 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002347 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002348 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
2350 break;
2351 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002352 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002353 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 break;
2355 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2356 if ((softInputMode &
2357 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002358 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002359 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002360 res = startInputUncheckedLocked(cs, inputContext,
2361 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002362 didStart = true;
2363 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002364 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
2366 break;
2367 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002368 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002369 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002370 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2371 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002372 didStart = true;
2373 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002374 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 break;
2376 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002377
2378 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002379 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002380 controlFlags);
2381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
2383 } finally {
2384 Binder.restoreCallingIdentity(ident);
2385 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002386
2387 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002389
satok42c5a162011-05-26 16:46:14 +09002390 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002391 public void showInputMethodPickerFromClient(
2392 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002393 if (!calledFromValidUser()) {
2394 return;
2395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 synchronized (mMethodMap) {
2397 if (mCurClient == null || client == null
2398 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002399 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002400 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 }
2402
satok440aab52010-11-25 09:43:11 +09002403 // Always call subtype picker, because subtype picker is a superset of input method
2404 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002405 mHandler.sendMessage(mCaller.obtainMessageI(
2406 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002407 }
2408 }
2409
satok42c5a162011-05-26 16:46:14 +09002410 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002412 if (!calledFromValidUser()) {
2413 return;
2414 }
satok28203512010-11-24 11:06:49 +09002415 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2416 }
2417
satok42c5a162011-05-26 16:46:14 +09002418 @Override
satok28203512010-11-24 11:06:49 +09002419 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002420 if (!calledFromValidUser()) {
2421 return;
2422 }
satok28203512010-11-24 11:06:49 +09002423 synchronized (mMethodMap) {
2424 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002425 setInputMethodWithSubtypeIdLocked(token, id,
2426 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2427 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002428 } else {
2429 setInputMethod(token, id);
2430 }
2431 }
satokab751aa2010-09-14 19:17:36 +09002432 }
2433
satok42c5a162011-05-26 16:46:14 +09002434 @Override
satokb416a712010-11-25 20:42:14 +09002435 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002436 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002437 if (!calledFromValidUser()) {
2438 return;
2439 }
satokb416a712010-11-25 20:42:14 +09002440 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002441 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2442 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002443 }
2444 }
2445
satok4fc87d62011-05-20 16:13:43 +09002446 @Override
satok735cf382010-11-11 20:40:09 +09002447 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002448 if (!calledFromValidUser()) {
2449 return false;
2450 }
satok735cf382010-11-11 20:40:09 +09002451 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002452 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002453 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002454 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002455 lastImi = mMethodMap.get(lastIme.first);
2456 } else {
2457 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002458 }
satok4fc87d62011-05-20 16:13:43 +09002459 String targetLastImiId = null;
2460 int subtypeId = NOT_A_SUBTYPE_ID;
2461 if (lastIme != null && lastImi != null) {
2462 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2463 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2464 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2465 : mCurrentSubtype.hashCode();
2466 // If the last IME is the same as the current IME and the last subtype is not
2467 // defined, there is no need to switch to the last IME.
2468 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2469 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002470 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002471 }
2472 }
2473
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002474 if (TextUtils.isEmpty(targetLastImiId)
2475 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002476 // This is a safety net. If the currentSubtype can't be added to the history
2477 // and the framework couldn't find the last ime, we will make the last ime be
2478 // the most applicable enabled keyboard subtype of the system imes.
2479 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2480 if (enabled != null) {
2481 final int N = enabled.size();
2482 final String locale = mCurrentSubtype == null
2483 ? mRes.getConfiguration().locale.toString()
2484 : mCurrentSubtype.getLocale();
2485 for (int i = 0; i < N; ++i) {
2486 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002487 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002488 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002489 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2490 InputMethodUtils.getSubtypes(imi),
2491 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002492 if (keyboardSubtype != null) {
2493 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002494 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002495 imi, keyboardSubtype.hashCode());
2496 if(keyboardSubtype.getLocale().equals(locale)) {
2497 break;
2498 }
2499 }
2500 }
2501 }
2502 }
2503 }
2504
2505 if (!TextUtils.isEmpty(targetLastImiId)) {
2506 if (DEBUG) {
2507 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2508 + ", from: " + mCurMethodId + ", " + subtypeId);
2509 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002510 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002511 return true;
2512 } else {
2513 return false;
2514 }
satok735cf382010-11-11 20:40:09 +09002515 }
2516 }
2517
satoke7c6998e2011-06-03 17:57:59 +09002518 @Override
satok688bd472012-02-09 20:09:17 +09002519 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002520 if (!calledFromValidUser()) {
2521 return false;
2522 }
satok688bd472012-02-09 20:09:17 +09002523 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002524 if (!calledWithValidToken(token)) {
2525 final int uid = Binder.getCallingUid();
2526 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2527 + " token:" + token);
2528 return false;
2529 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002530 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002531 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2532 if (nextSubtype == null) {
2533 return false;
2534 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002535 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2536 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002537 return true;
2538 }
2539 }
2540
2541 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002542 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2543 if (!calledFromValidUser()) {
2544 return false;
2545 }
2546 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002547 if (!calledWithValidToken(token)) {
2548 final int uid = Binder.getCallingUid();
2549 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2550 + "token. uid:" + uid + " token:" + token);
2551 return false;
2552 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002553 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002554 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2555 if (nextSubtype == null) {
2556 return false;
2557 }
2558 return true;
2559 }
2560 }
2561
2562 @Override
satok68f1b782011-04-11 14:26:04 +09002563 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002564 if (!calledFromValidUser()) {
2565 return null;
2566 }
satok68f1b782011-04-11 14:26:04 +09002567 synchronized (mMethodMap) {
2568 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2569 // TODO: Handle the case of the last IME with no subtypes
2570 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2571 || TextUtils.isEmpty(lastIme.second)) return null;
2572 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2573 if (lastImi == null) return null;
2574 try {
2575 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002576 final int lastSubtypeId =
2577 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002578 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2579 return null;
2580 }
2581 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002582 } catch (NumberFormatException e) {
2583 return null;
2584 }
2585 }
2586 }
2587
satoke7c6998e2011-06-03 17:57:59 +09002588 @Override
satokee5e77c2011-09-02 18:50:15 +09002589 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002590 if (!calledFromValidUser()) {
2591 return;
2592 }
satok91e88122011-07-18 11:11:42 +09002593 // By this IPC call, only a process which shares the same uid with the IME can add
2594 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002595 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002596 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002597 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002598 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002599 final String[] packageInfos;
2600 try {
2601 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2602 } catch (RemoteException e) {
2603 Slog.e(TAG, "Failed to get package infos");
2604 return;
2605 }
satok91e88122011-07-18 11:11:42 +09002606 if (packageInfos != null) {
2607 final int packageNum = packageInfos.length;
2608 for (int i = 0; i < packageNum; ++i) {
2609 if (packageInfos[i].equals(imi.getPackageName())) {
2610 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002611 final long ident = Binder.clearCallingIdentity();
2612 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002613 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002614 } finally {
2615 Binder.restoreCallingIdentity(ident);
2616 }
satokee5e77c2011-09-02 18:50:15 +09002617 return;
satok91e88122011-07-18 11:11:42 +09002618 }
2619 }
2620 }
satoke7c6998e2011-06-03 17:57:59 +09002621 }
satokee5e77c2011-09-02 18:50:15 +09002622 return;
satoke7c6998e2011-06-03 17:57:59 +09002623 }
2624
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002625 @Override
2626 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002627 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002628 }
2629
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002630 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002631 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002632 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002633 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002634 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002635 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002636 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2637 if (DEBUG) {
2638 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2639 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2640 + " actual: " + sequenceNumber);
2641 }
2642 return;
2643 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002644 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2645 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002646 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002647 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002648 }
2649 }
2650
satok28203512010-11-24 11:06:49 +09002651 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002653 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2654 }
2655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002657 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2658 if (token == null) {
2659 if (mContext.checkCallingOrSelfPermission(
2660 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2661 != PackageManager.PERMISSION_GRANTED) {
2662 throw new SecurityException(
2663 "Using null token requires permission "
2664 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002666 } else if (mCurToken != token) {
2667 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2668 + " token: " + token);
2669 return;
2670 }
2671
2672 final long ident = Binder.clearCallingIdentity();
2673 try {
2674 setInputMethodLocked(id, subtypeId);
2675 } finally {
2676 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 }
2678 }
2679
satok42c5a162011-05-26 16:46:14 +09002680 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002682 if (!calledFromValidUser()) {
2683 return;
2684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002686 if (!calledWithValidToken(token)) {
2687 final int uid = Binder.getCallingUid();
2688 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2689 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 return;
2691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 long ident = Binder.clearCallingIdentity();
2693 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002694 hideCurrentInputLocked(flags, null);
2695 } finally {
2696 Binder.restoreCallingIdentity(ident);
2697 }
2698 }
2699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002700
satok42c5a162011-05-26 16:46:14 +09002701 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002702 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002703 if (!calledFromValidUser()) {
2704 return;
2705 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002706 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002707 if (!calledWithValidToken(token)) {
2708 final int uid = Binder.getCallingUid();
2709 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2710 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002711 return;
2712 }
2713 long ident = Binder.clearCallingIdentity();
2714 try {
2715 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 } finally {
2717 Binder.restoreCallingIdentity(ident);
2718 }
2719 }
2720 }
2721
2722 void setEnabledSessionInMainThread(SessionState session) {
2723 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002724 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002726 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002727 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 } catch (RemoteException e) {
2729 }
2730 }
2731 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002732 if (mEnabledSession != null && mEnabledSession.session != null) {
2733 try {
2734 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2735 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2736 } catch (RemoteException e) {
2737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 }
2739 }
2740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002741
satok42c5a162011-05-26 16:46:14 +09002742 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002744 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002746 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002747 final boolean showAuxSubtypes;
2748 switch (msg.arg1) {
2749 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2750 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2751 // implemented so that auxiliary subtypes will be excluded when the soft
2752 // keyboard is invisible.
2753 showAuxSubtypes = mInputShown;
2754 break;
2755 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2756 showAuxSubtypes = true;
2757 break;
2758 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2759 showAuxSubtypes = false;
2760 break;
2761 default:
2762 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2763 return false;
2764 }
2765 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002766 return true;
2767
satok47a44912010-10-06 16:03:58 +09002768 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002769 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002770 return true;
2771
2772 case MSG_SHOW_IM_CONFIG:
2773 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002774 return true;
2775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 case MSG_UNBIND_INPUT:
2779 try {
2780 ((IInputMethod)msg.obj).unbindInput();
2781 } catch (RemoteException e) {
2782 // There is nothing interesting about the method dying.
2783 }
2784 return true;
2785 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002786 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 try {
2788 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2789 } catch (RemoteException e) {
2790 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002791 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 return true;
2793 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002794 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002796 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002797 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002798 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 } catch (RemoteException e) {
2800 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002801 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 return true;
2803 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002804 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002806 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002807 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002808 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 } catch (RemoteException e) {
2810 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002811 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 return true;
2813 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002814 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002816 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2818 } catch (RemoteException e) {
2819 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002820 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002822 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002823 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002824 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002825 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002827 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002829 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002830 // Dispose the channel if the input method is not local to this process
2831 // because the remote proxy will get its own copy when unparceled.
2832 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002833 channel.dispose();
2834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002836 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002840
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002841 case MSG_START_INPUT: {
2842 int missingMethods = msg.arg1;
2843 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002845 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002847 session.method.startInput((IInputContext) args.arg2, missingMethods,
2848 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 } catch (RemoteException e) {
2850 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002851 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002853 }
2854 case MSG_RESTART_INPUT: {
2855 int missingMethods = msg.arg1;
2856 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002858 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002860 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2861 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 } catch (RemoteException e) {
2863 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002864 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002866 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002869
Yohei Yukawa33e81792015-11-17 21:14:42 -08002870 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002872 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 } catch (RemoteException e) {
2874 // There is nothing interesting about the last client dying.
2875 }
2876 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002877 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002878 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002879 IInputMethodClient client = (IInputMethodClient)args.arg1;
2880 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002882 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002884 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002885 } finally {
2886 // Dispose the channel if the input method is not local to this process
2887 // because the remote proxy will get its own copy when unparceled.
2888 if (res.channel != null && Binder.isProxy(client)) {
2889 res.channel.dispose();
2890 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002892 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002894 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002895 case MSG_SET_ACTIVE:
2896 try {
2897 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2898 } catch (RemoteException e) {
2899 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2900 + ((ClientState)msg.obj).pid + " uid "
2901 + ((ClientState)msg.obj).uid);
2902 }
2903 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002904 case MSG_SET_INTERACTIVE:
2905 handleSetInteractive(msg.arg1 != 0);
2906 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002907 case MSG_SWITCH_IME:
2908 handleSwitchInputMethod(msg.arg1 != 0);
2909 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002910 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2911 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002912 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002913 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002914 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002915 } catch (RemoteException e) {
2916 Slog.w(TAG, "Got RemoteException sending "
2917 + "setUserActionNotificationSequenceNumber("
2918 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002919 + clientState.pid + " uid "
2920 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002921 }
2922 return true;
2923 }
satok01038492012-04-09 21:08:27 +09002924
2925 // --------------------------------------------------------------
2926 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002927 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002928 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 }
2930 return false;
2931 }
2932
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002933 private void handleSetInteractive(final boolean interactive) {
2934 synchronized (mMethodMap) {
2935 mIsInteractive = interactive;
2936 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2937
2938 // Inform the current client of the change in active status
2939 if (mCurClient != null && mCurClient.client != null) {
2940 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2941 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2942 }
2943 }
2944 }
2945
Yohei Yukawaae61f712015-12-09 13:00:10 -08002946 private void handleSwitchInputMethod(final boolean forwardDirection) {
2947 synchronized (mMethodMap) {
2948 // TODO: Support forwardDirection.
2949 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2950 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2951 if (nextSubtype == null) {
2952 return;
2953 }
2954 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2955 }
2956 }
2957
satokdc9ddae2011-10-06 12:22:36 +09002958 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002959 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2960 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002961 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002962 if (DEBUG) {
2963 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2964 }
satok723a27e2010-11-11 14:58:11 +09002965 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002966 return true;
2967 }
2968
2969 return false;
2970 }
2971
Yohei Yukawa94e33302016-02-12 19:37:03 -08002972 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002973 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002974 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002975 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002976 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08002977 mMethodList.clear();
2978 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002979
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002980 // Use for queryIntentServicesAsUser
2981 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982
Yohei Yukawaed4952a2016-02-17 07:57:25 -08002983 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
2984 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
2985 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002986 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002988 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2989 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002990
satoke7c6998e2011-06-03 17:57:59 +09002991 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2992 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 for (int i = 0; i < services.size(); ++i) {
2994 ResolveInfo ri = services.get(i);
2995 ServiceInfo si = ri.serviceInfo;
2996 ComponentName compName = new ComponentName(si.packageName, si.name);
2997 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2998 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002999 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 + ": it does not require the permission "
3001 + android.Manifest.permission.BIND_INPUT_METHOD);
3002 continue;
3003 }
3004
Joe Onorato8a9b2202010-02-26 18:56:32 -08003005 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006
3007 try {
satoke7c6998e2011-06-03 17:57:59 +09003008 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003009 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003010 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003011 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012
3013 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003014 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003016 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003017 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 }
3019 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003020
Yohei Yukawa859df052016-02-17 07:56:46 -08003021 // TODO: The following code should find better place to live.
3022 if (!resetDefaultEnabledIme) {
3023 boolean enabledImeFound = false;
3024 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3025 final int N = enabledImes.size();
3026 for (int i = 0; i < N; ++i) {
3027 final InputMethodInfo imi = enabledImes.get(i);
3028 if (mMethodList.contains(imi)) {
3029 enabledImeFound = true;
3030 break;
3031 }
3032 }
3033 if (!enabledImeFound) {
3034 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3035 resetDefaultEnabledIme = true;
3036 resetSelectedInputMethodAndSubtypeLocked("");
3037 }
3038 }
3039
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003040 if (resetDefaultEnabledIme) {
3041 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003042 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003043 final int N = defaultEnabledIme.size();
3044 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003045 final InputMethodInfo imi = defaultEnabledIme.get(i);
3046 if (DEBUG) {
3047 Slog.d(TAG, "--- enable ime = " + imi);
3048 }
3049 setInputMethodEnabledLocked(imi.getId(), true);
3050 }
3051 }
3052
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003053 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003054 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003055 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003056 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3057 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003058 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003059 }
3060 } else {
3061 // Double check that the default IME is certainly enabled.
3062 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003063 }
3064 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003065 // Here is not the perfect place to reset the switching controller. Ideally
3066 // mSwitchingController and mSettings should be able to share the same state.
3067 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3068 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003069 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003073
satok217f5482010-12-15 05:19:19 +09003074 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003075 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003076 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003077 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3078 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003079 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003080 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003081 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003082 final int userId;
3083 synchronized (mMethodMap) {
3084 userId = mSettings.getCurrentUserId();
3085 }
3086 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003087 }
3088
3089 private void showConfigureInputMethods() {
3090 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3091 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3092 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3093 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003094 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003095 }
3096
satok2c93efc2012-04-02 19:33:47 +09003097 private boolean isScreenLocked() {
3098 return mKeyguardManager != null
3099 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3100 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003101
Seigo Nonaka14e13912015-05-06 21:04:13 -07003102 private void showInputMethodMenu(boolean showAuxSubtypes) {
3103 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003106 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003107
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003108 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003109 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003110 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003111
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003112 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003113 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003114 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3115 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003116 if (immis == null || immis.size() == 0) {
3117 return;
3118 }
3119
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003120 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121
satok688bd472012-02-09 20:09:17 +09003122 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003123 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003124 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003125
satokc3690562012-01-10 20:14:43 +09003126 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003127 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003128 if (currentSubtype != null) {
3129 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003130 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3131 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003132 }
3133 }
3134
Ken Wakasa761eb372011-03-04 19:06:18 +09003135 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003136 mIms = new InputMethodInfo[N];
3137 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003138 int checkedItem = 0;
3139 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003140 final ImeSubtypeListItem item = imList.get(i);
3141 mIms[i] = item.mImi;
3142 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003143 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003144 int subtypeId = mSubtypeIds[i];
3145 if ((subtypeId == NOT_A_SUBTYPE_ID)
3146 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3147 || (subtypeId == lastInputMethodSubtypeId)) {
3148 checkedItem = i;
3149 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003152
3153 final Context settingsContext = new ContextThemeWrapper(context,
3154 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3155
3156 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003157 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3158 @Override
3159 public void onCancel(DialogInterface dialog) {
3160 hideInputMethodMenu();
3161 }
3162 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003163
3164 final Context dialogContext = mDialogBuilder.getContext();
3165 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3166 com.android.internal.R.styleable.DialogPreference,
3167 com.android.internal.R.attr.alertDialogStyle, 0);
3168 final Drawable dialogIcon = a.getDrawable(
3169 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3170 a.recycle();
3171
3172 mDialogBuilder.setIcon(dialogIcon);
3173
Yohei Yukawad34e1482016-02-11 08:03:52 -08003174 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003175 final View tv = inflater.inflate(
3176 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3177 mDialogBuilder.setCustomTitle(tv);
3178
3179 // Setup layout for a toggle switch of the hardware keyboard
3180 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003181 mSwitchingDialogTitleView
3182 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003183 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003184 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003185 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003186 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003187 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003188 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3189 @Override
3190 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003191 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003192 // Ensure that the input method dialog is dismissed when changing
3193 // the hardware keyboard state.
3194 hideInputMethodMenu();
3195 }
3196 });
3197
Alan Viverette505e3ab2014-11-24 15:22:11 -08003198 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003199 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3200 final OnClickListener choiceListener = new OnClickListener() {
3201 @Override
3202 public void onClick(final DialogInterface dialog, final int which) {
3203 synchronized (mMethodMap) {
3204 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3205 || mSubtypeIds.length <= which) {
3206 return;
satok01038492012-04-09 21:08:27 +09003207 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003208 final InputMethodInfo im = mIms[which];
3209 int subtypeId = mSubtypeIds[which];
3210 adapter.mCheckedItem = which;
3211 adapter.notifyDataSetChanged();
3212 hideInputMethodMenu();
3213 if (im != null) {
3214 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3215 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003216 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003217 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003219 }
3220 }
3221 };
3222 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003224 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003225 final OnClickListener positiveListener = new OnClickListener() {
3226 @Override
3227 public void onClick(DialogInterface dialog, int whichButton) {
3228 showConfigureInputMethods();
3229 }
3230 };
satok82beadf2010-12-27 19:03:06 +09003231 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003232 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003235 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 mSwitchingDialog.getWindow().setType(
3237 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003238 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3239 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003240 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003241 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 mSwitchingDialog.show();
3243 }
3244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003245
Ken Wakasa05dbb652011-08-22 15:22:43 +09003246 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3247 private final LayoutInflater mInflater;
3248 private final int mTextViewResourceId;
3249 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003250 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003251 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3252 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3253 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003254
Ken Wakasa05dbb652011-08-22 15:22:43 +09003255 mTextViewResourceId = textViewResourceId;
3256 mItemsList = itemsList;
3257 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003258 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003259 }
3260
3261 @Override
3262 public View getView(int position, View convertView, ViewGroup parent) {
3263 final View view = convertView != null ? convertView
3264 : mInflater.inflate(mTextViewResourceId, null);
3265 if (position < 0 || position >= mItemsList.size()) return view;
3266 final ImeSubtypeListItem item = mItemsList.get(position);
3267 final CharSequence imeName = item.mImeName;
3268 final CharSequence subtypeName = item.mSubtypeName;
3269 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3270 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3271 if (TextUtils.isEmpty(subtypeName)) {
3272 firstTextView.setText(imeName);
3273 secondTextView.setVisibility(View.GONE);
3274 } else {
3275 firstTextView.setText(subtypeName);
3276 secondTextView.setText(imeName);
3277 secondTextView.setVisibility(View.VISIBLE);
3278 }
3279 final RadioButton radioButton =
3280 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3281 radioButton.setChecked(position == mCheckedItem);
3282 return view;
3283 }
3284 }
3285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003287 synchronized (mMethodMap) {
3288 hideInputMethodMenuLocked();
3289 }
3290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003291
The Android Open Source Project10592532009-03-18 17:39:46 -07003292 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003293 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294
The Android Open Source Project10592532009-03-18 17:39:46 -07003295 if (mSwitchingDialog != null) {
3296 mSwitchingDialog.dismiss();
3297 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003299
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003300 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003301 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003302 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003306
satok42c5a162011-05-26 16:46:14 +09003307 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003309 // TODO: Make this work even for non-current users?
3310 if (!calledFromValidUser()) {
3311 return false;
3312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 synchronized (mMethodMap) {
3314 if (mContext.checkCallingOrSelfPermission(
3315 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3316 != PackageManager.PERMISSION_GRANTED) {
3317 throw new SecurityException(
3318 "Requires permission "
3319 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3320 }
Anna Galusza9b278112016-01-04 11:37:37 -08003321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 long ident = Binder.clearCallingIdentity();
3323 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003324 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 } finally {
3326 Binder.restoreCallingIdentity(ident);
3327 }
3328 }
3329 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003330
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003331 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3332 // Make sure this is a valid input method.
3333 InputMethodInfo imm = mMethodMap.get(id);
3334 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003335 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003336 }
3337
satokd87c2592010-09-29 11:52:06 +09003338 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3339 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003340
satokd87c2592010-09-29 11:52:06 +09003341 if (enabled) {
3342 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3343 if (pair.first.equals(id)) {
3344 // We are enabling this input method, but it is already enabled.
3345 // Nothing to do. The previous state was enabled.
3346 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003347 }
3348 }
satokd87c2592010-09-29 11:52:06 +09003349 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3350 // Previous state was disabled.
3351 return false;
3352 } else {
3353 StringBuilder builder = new StringBuilder();
3354 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3355 builder, enabledInputMethodsList, id)) {
3356 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003357 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003358 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3359 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3360 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003361 }
3362 // Previous state was enabled.
3363 return true;
3364 } else {
3365 // We are disabling the input method but it is already disabled.
3366 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003367 return false;
3368 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003369 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003370 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003371
satok723a27e2010-11-11 14:58:11 +09003372 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3373 boolean setSubtypeOnly) {
3374 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003375 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003376
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003377 mCurUserActionNotificationSequenceNumber =
3378 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3379 if (DEBUG) {
3380 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3381 + mCurUserActionNotificationSequenceNumber);
3382 }
3383
3384 if (mCurClient != null && mCurClient.client != null) {
3385 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3386 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003387 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003388 }
3389
satok723a27e2010-11-11 14:58:11 +09003390 // Set Subtype here
3391 if (imi == null || subtypeId < 0) {
3392 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003393 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003394 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003395 if (subtypeId < imi.getSubtypeCount()) {
3396 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3397 mSettings.putSelectedSubtype(subtype.hashCode());
3398 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003399 } else {
3400 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003401 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003402 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003403 }
satokab751aa2010-09-14 19:17:36 +09003404 }
satok723a27e2010-11-11 14:58:11 +09003405
Yohei Yukawa68645a62016-02-17 07:54:20 -08003406 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003407 // Set InputMethod here
3408 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3409 }
3410 }
3411
3412 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3413 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3414 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3415 // newDefaultIme is empty when there is no candidate for the selected IME.
3416 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3417 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3418 if (subtypeHashCode != null) {
3419 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003420 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003421 imi, Integer.valueOf(subtypeHashCode));
3422 } catch (NumberFormatException e) {
3423 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3424 }
3425 }
3426 }
3427 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003428 }
3429
satok4e4569d2010-11-19 18:45:53 +09003430 // If there are no selected shortcuts, tries finding the most applicable ones.
3431 private Pair<InputMethodInfo, InputMethodSubtype>
3432 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3433 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3434 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003435 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003436 boolean foundInSystemIME = false;
3437
3438 // Search applicable subtype for each InputMethodInfo
3439 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003440 final String imiId = imi.getId();
3441 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3442 continue;
3443 }
satokcd7cd292010-11-20 15:46:23 +09003444 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003445 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003446 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003447 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003448 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003449 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003450 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003451 }
satokdf31ae62011-01-15 06:19:44 +09003452 // 2. Search by the system locale from enabledSubtypes.
3453 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003454 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003455 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003456 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003457 }
satoka86f5e42011-09-02 17:12:42 +09003458 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003459 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003460 final ArrayList<InputMethodSubtype> subtypesForSearch =
3461 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003462 ? InputMethodUtils.getSubtypes(imi)
3463 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003464 // 4. Search by the current subtype's locale from all subtypes.
3465 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003466 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003467 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003468 }
3469 // 5. Search by the system locale from all subtypes.
3470 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003471 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003472 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003473 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003474 }
satokcd7cd292010-11-20 15:46:23 +09003475 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003476 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003477 // The current input method is the most applicable IME.
3478 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003479 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003480 break;
satok7599a7f2010-12-22 13:45:23 +09003481 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003482 // The system input method is 2nd applicable IME.
3483 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003484 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003485 if ((imi.getServiceInfo().applicationInfo.flags
3486 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3487 foundInSystemIME = true;
3488 }
satok4e4569d2010-11-19 18:45:53 +09003489 }
3490 }
3491 }
3492 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003493 if (mostApplicableIMI != null) {
3494 Slog.w(TAG, "Most applicable shortcut input method was:"
3495 + mostApplicableIMI.getId());
3496 if (mostApplicableSubtype != null) {
3497 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3498 + "," + mostApplicableSubtype.getMode() + ","
3499 + mostApplicableSubtype.getLocale());
3500 }
3501 }
satok4e4569d2010-11-19 18:45:53 +09003502 }
satokcd7cd292010-11-20 15:46:23 +09003503 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003504 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003505 } else {
3506 return null;
3507 }
3508 }
3509
satokab751aa2010-09-14 19:17:36 +09003510 /**
3511 * @return Return the current subtype of this input method.
3512 */
satok42c5a162011-05-26 16:46:14 +09003513 @Override
satokab751aa2010-09-14 19:17:36 +09003514 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003515 // TODO: Make this work even for non-current users?
3516 if (!calledFromValidUser()) {
3517 return null;
3518 }
3519 synchronized (mMethodMap) {
3520 return getCurrentInputMethodSubtypeLocked();
3521 }
3522 }
3523
3524 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003525 if (mCurMethodId == null) {
3526 return null;
3527 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003528 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003529 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3530 if (imi == null || imi.getSubtypeCount() == 0) {
3531 return null;
satok4e4569d2010-11-19 18:45:53 +09003532 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003533 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003534 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3535 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003536 if (subtypeId == NOT_A_SUBTYPE_ID) {
3537 // If there are no selected subtypes, the framework will try to find
3538 // the most applicable subtype from explicitly or implicitly enabled
3539 // subtypes.
3540 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003541 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003542 // If there is only one explicitly or implicitly enabled subtype,
3543 // just returns it.
3544 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3545 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3546 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003547 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003548 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003549 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003550 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003551 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003552 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3553 true);
satok4e4569d2010-11-19 18:45:53 +09003554 }
satok3ef8b292010-11-23 06:06:29 +09003555 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003556 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003557 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003558 }
3559 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003560 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003561 }
3562
satok4e4569d2010-11-19 18:45:53 +09003563 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003564 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003565 @Override
satok4e4569d2010-11-19 18:45:53 +09003566 public List getShortcutInputMethodsAndSubtypes() {
3567 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003568 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003569 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003570 // If there are no selected shortcut subtypes, the framework will try to find
3571 // the most applicable subtype from all subtypes whose mode is
3572 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003573 Pair<InputMethodInfo, InputMethodSubtype> info =
3574 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003575 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003576 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003577 ret.add(info.first);
3578 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003579 }
satok3da92232011-01-11 22:46:30 +09003580 return ret;
satokf3db1af2010-11-23 13:34:33 +09003581 }
satokf3db1af2010-11-23 13:34:33 +09003582 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3583 ret.add(imi);
3584 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3585 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003586 }
3587 }
satokf3db1af2010-11-23 13:34:33 +09003588 return ret;
satok4e4569d2010-11-19 18:45:53 +09003589 }
3590 }
3591
satok42c5a162011-05-26 16:46:14 +09003592 @Override
satokb66d2872010-11-10 01:04:04 +09003593 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003594 // TODO: Make this work even for non-current users?
3595 if (!calledFromValidUser()) {
3596 return false;
3597 }
satokb66d2872010-11-10 01:04:04 +09003598 synchronized (mMethodMap) {
3599 if (subtype != null && mCurMethodId != null) {
3600 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003601 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003602 if (subtypeId != NOT_A_SUBTYPE_ID) {
3603 setInputMethodLocked(mCurMethodId, subtypeId);
3604 return true;
3605 }
3606 }
3607 return false;
3608 }
3609 }
3610
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003611 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003612 private static class InputMethodFileManager {
3613 private static final String SYSTEM_PATH = "system";
3614 private static final String INPUT_METHOD_PATH = "inputmethod";
3615 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3616 private static final String NODE_SUBTYPES = "subtypes";
3617 private static final String NODE_SUBTYPE = "subtype";
3618 private static final String NODE_IMI = "imi";
3619 private static final String ATTR_ID = "id";
3620 private static final String ATTR_LABEL = "label";
3621 private static final String ATTR_ICON = "icon";
3622 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003623 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003624 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3625 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3626 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003627 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003628 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3629 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003630 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003631 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003632 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003633 if (methodMap == null) {
3634 throw new NullPointerException("methodMap is null");
3635 }
3636 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003637 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003638 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3639 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003640 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003641 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003642 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3643 }
3644 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3645 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3646 if (!subtypeFile.exists()) {
3647 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003648 writeAdditionalInputMethodSubtypes(
3649 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003650 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003651 readAdditionalInputMethodSubtypes(
3652 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003653 }
3654 }
3655
3656 private void deleteAllInputMethodSubtypes(String imiId) {
3657 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003658 mAdditionalSubtypesMap.remove(imiId);
3659 writeAdditionalInputMethodSubtypes(
3660 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003661 }
3662 }
3663
3664 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003665 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003666 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003667 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003668 final int N = additionalSubtypes.length;
3669 for (int i = 0; i < N; ++i) {
3670 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003671 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003672 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003673 } else {
3674 Slog.w(TAG, "Duplicated subtype definition found: "
3675 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003676 }
3677 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003678 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3679 writeAdditionalInputMethodSubtypes(
3680 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003681 }
3682 }
3683
3684 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3685 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003686 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003687 }
3688 }
3689
3690 private static void writeAdditionalInputMethodSubtypes(
3691 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3692 HashMap<String, InputMethodInfo> methodMap) {
3693 // Safety net for the case that this function is called before methodMap is set.
3694 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3695 FileOutputStream fos = null;
3696 try {
3697 fos = subtypesFile.startWrite();
3698 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003699 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003700 out.startDocument(null, true);
3701 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3702 out.startTag(null, NODE_SUBTYPES);
3703 for (String imiId : allSubtypes.keySet()) {
3704 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3705 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3706 continue;
3707 }
3708 out.startTag(null, NODE_IMI);
3709 out.attribute(null, ATTR_ID, imiId);
3710 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3711 final int N = subtypesList.size();
3712 for (int i = 0; i < N; ++i) {
3713 final InputMethodSubtype subtype = subtypesList.get(i);
3714 out.startTag(null, NODE_SUBTYPE);
3715 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3716 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3717 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003718 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3719 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003720 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3721 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3722 out.attribute(null, ATTR_IS_AUXILIARY,
3723 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003724 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3725 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003726 out.endTag(null, NODE_SUBTYPE);
3727 }
3728 out.endTag(null, NODE_IMI);
3729 }
3730 out.endTag(null, NODE_SUBTYPES);
3731 out.endDocument();
3732 subtypesFile.finishWrite(fos);
3733 } catch (java.io.IOException e) {
3734 Slog.w(TAG, "Error writing subtypes", e);
3735 if (fos != null) {
3736 subtypesFile.failWrite(fos);
3737 }
3738 }
3739 }
3740
3741 private static void readAdditionalInputMethodSubtypes(
3742 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3743 if (allSubtypes == null || subtypesFile == null) return;
3744 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003745 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003746 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003747 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003748 int type = parser.getEventType();
3749 // Skip parsing until START_TAG
3750 while ((type = parser.next()) != XmlPullParser.START_TAG
3751 && type != XmlPullParser.END_DOCUMENT) {}
3752 String firstNodeName = parser.getName();
3753 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3754 throw new XmlPullParserException("Xml doesn't start with subtypes");
3755 }
3756 final int depth =parser.getDepth();
3757 String currentImiId = null;
3758 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3759 while (((type = parser.next()) != XmlPullParser.END_TAG
3760 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3761 if (type != XmlPullParser.START_TAG)
3762 continue;
3763 final String nodeName = parser.getName();
3764 if (NODE_IMI.equals(nodeName)) {
3765 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3766 if (TextUtils.isEmpty(currentImiId)) {
3767 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3768 continue;
3769 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003770 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003771 allSubtypes.put(currentImiId, tempSubtypesArray);
3772 } else if (NODE_SUBTYPE.equals(nodeName)) {
3773 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3774 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3775 continue;
3776 }
3777 final int icon = Integer.valueOf(
3778 parser.getAttributeValue(null, ATTR_ICON));
3779 final int label = Integer.valueOf(
3780 parser.getAttributeValue(null, ATTR_LABEL));
3781 final String imeSubtypeLocale =
3782 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003783 final String languageTag =
3784 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003785 final String imeSubtypeMode =
3786 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3787 final String imeSubtypeExtraValue =
3788 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003789 final boolean isAuxiliary = "1".equals(String.valueOf(
3790 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003791 final boolean isAsciiCapable = "1".equals(String.valueOf(
3792 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003793 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3794 .setSubtypeNameResId(label)
3795 .setSubtypeIconResId(icon)
3796 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003797 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003798 .setSubtypeMode(imeSubtypeMode)
3799 .setSubtypeExtraValue(imeSubtypeExtraValue)
3800 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003801 .setIsAsciiCapable(isAsciiCapable)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003802 .build();
satoke7c6998e2011-06-03 17:57:59 +09003803 tempSubtypesArray.add(subtype);
3804 }
3805 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003806 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3807 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003808 return;
satoke7c6998e2011-06-03 17:57:59 +09003809 }
3810 }
3811 }
3812
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003813 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3814 @NonNull
3815 private final Handler mHandler;
3816
3817 LocalServiceImpl(@NonNull final Handler handler) {
3818 mHandler = handler;
3819 }
3820
3821 @Override
3822 public void setInteractive(boolean interactive) {
3823 // Do everything in handler so as not to block the caller.
3824 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3825 interactive ? 1 : 0, 0));
3826 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003827
3828 @Override
3829 public void switchInputMethod(boolean forwardDirection) {
3830 // Do everything in handler so as not to block the caller.
3831 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3832 forwardDirection ? 1 : 0, 0));
3833 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003834 }
3835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 @Override
3837 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3838 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3839 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3842 + Binder.getCallingPid()
3843 + ", uid=" + Binder.getCallingUid());
3844 return;
3845 }
3846
3847 IInputMethod method;
3848 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003849 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 synchronized (mMethodMap) {
3854 p.println("Current Input Method Manager state:");
3855 int N = mMethodList.size();
3856 p.println(" Input Methods:");
3857 for (int i=0; i<N; i++) {
3858 InputMethodInfo info = mMethodList.get(i);
3859 p.println(" InputMethod #" + i + ":");
3860 info.dump(p, " ");
3861 }
3862 p.println(" Clients:");
3863 for (ClientState ci : mClients.values()) {
3864 p.println(" Client " + ci + ":");
3865 p.println(" client=" + ci.client);
3866 p.println(" inputContext=" + ci.inputContext);
3867 p.println(" sessionRequested=" + ci.sessionRequested);
3868 p.println(" curSession=" + ci.curSession);
3869 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003870 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003872 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3873 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003874 focusedWindowClient = mCurFocusedWindowClient;
3875 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3877 + " mBoundToMethod=" + mBoundToMethod);
3878 p.println(" mCurToken=" + mCurToken);
3879 p.println(" mCurIntent=" + mCurIntent);
3880 method = mCurMethod;
3881 p.println(" mCurMethod=" + mCurMethod);
3882 p.println(" mEnabledSession=" + mEnabledSession);
3883 p.println(" mShowRequested=" + mShowRequested
3884 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3885 + " mShowForced=" + mShowForced
3886 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003887 p.println(" mCurUserActionNotificationSequenceNumber="
3888 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003889 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003890 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003891 p.println(" mSwitchingController:");
3892 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003893 p.println(" mSettings:");
3894 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003896
Jeff Brownb88102f2010-09-08 11:49:43 -07003897 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 pw.flush();
3900 try {
3901 client.client.asBinder().dump(fd, args);
3902 } catch (RemoteException e) {
3903 p.println("Input method client dead: " + e);
3904 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003905 } else {
3906 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003908
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003909 if (focusedWindowClient != null && client != focusedWindowClient) {
3910 p.println(" ");
3911 p.println("Warning: Current input method client doesn't match the last focused. "
3912 + "window.");
3913 p.println("Dumping input method client in the last focused window just in case.");
3914 p.println(" ");
3915 pw.flush();
3916 try {
3917 focusedWindowClient.client.asBinder().dump(fd, args);
3918 } catch (RemoteException e) {
3919 p.println("Input method client in focused window dead: " + e);
3920 }
3921 }
3922
Jeff Brownb88102f2010-09-08 11:49:43 -07003923 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 pw.flush();
3926 try {
3927 method.asBinder().dump(fd, args);
3928 } catch (RemoteException e) {
3929 p.println("Input method service dead: " + e);
3930 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003931 } else {
3932 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 }
3934 }
3935}