blob: 898d5b73b8d68440688de80aaee8c1044db85526 [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;
218 private 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;
Jason Monk3e189872016-01-12 09:10:34 -05001073 statusBar.setIconVisibility(mSlotIme, false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001074 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001075 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1076 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001077 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001078 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001079 mHardKeyboardListener);
1080 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001081 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001082 if (!mImeSelectedOnBoot) {
1083 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001084 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001085 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1086 mSettings.getEnabledInputMethodListLocked(),
1087 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001088 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001089 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001090 try {
1091 startInputInnerLocked();
1092 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001093 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001094 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001095 }
1096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001098
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001099 // ---------------------------------------------------------------------------------------
1100 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1101 // 1) it comes from the system process
1102 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1103 private boolean calledFromValidUser() {
1104 final int uid = Binder.getCallingUid();
1105 final int userId = UserHandle.getUserId(uid);
1106 if (DEBUG) {
1107 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1108 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1109 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001110 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1111 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001112 }
Kenny Guy2a764942014-04-02 13:29:20 +01001113 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001114 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001115 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001116
1117 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1118 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1119 // must not manage background users' states in any functions.
1120 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1121 // by a token.
1122 if (mContext.checkCallingOrSelfPermission(
1123 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1124 == PackageManager.PERMISSION_GRANTED) {
1125 if (DEBUG) {
1126 Slog.d(TAG, "--- Access granted because the calling process has "
1127 + "the INTERACT_ACROSS_USERS_FULL permission");
1128 }
1129 return true;
1130 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001131 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1132 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001133 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001134 }
1135
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001136
1137 /**
1138 * Returns true iff the caller is identified to be the current input method with the token.
1139 * @param token The window token given to the input method when it was started.
1140 * @return true if and only if non-null valid token is specified.
1141 */
1142 private boolean calledWithValidToken(IBinder token) {
1143 if (token == null || mCurToken != token) {
1144 return false;
1145 }
1146 return true;
1147 }
1148
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001149 private boolean bindCurrentInputMethodService(
1150 Intent service, ServiceConnection conn, int flags) {
1151 if (service == null || conn == null) {
1152 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1153 return false;
1154 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001155 return mContext.bindServiceAsUser(service, conn, flags,
1156 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001157 }
1158
satoke7c6998e2011-06-03 17:57:59 +09001159 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001161 // TODO: Make this work even for non-current users?
1162 if (!calledFromValidUser()) {
1163 return Collections.emptyList();
1164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001166 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
1168 }
1169
satoke7c6998e2011-06-03 17:57:59 +09001170 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001172 // TODO: Make this work even for non-current users?
1173 if (!calledFromValidUser()) {
1174 return Collections.emptyList();
1175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001177 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179 }
1180
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001181 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001182 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001183 * @return enabled subtypes of the specified imi
1184 */
satoke7c6998e2011-06-03 17:57:59 +09001185 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001186 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001187 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001188 // TODO: Make this work even for non-current users?
1189 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001190 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001191 }
satok67ddf9c2010-11-17 09:45:54 +09001192 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001193 final InputMethodInfo imi;
1194 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001195 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001196 } else {
1197 imi = mMethodMap.get(imiId);
1198 }
1199 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001200 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001201 }
1202 return mSettings.getEnabledInputMethodSubtypeListLocked(
1203 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001204 }
1205 }
1206
satoke7c6998e2011-06-03 17:57:59 +09001207 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 public void addClient(IInputMethodClient client,
1209 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001210 if (!calledFromValidUser()) {
1211 return;
1212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 synchronized (mMethodMap) {
1214 mClients.put(client.asBinder(), new ClientState(client,
1215 inputContext, uid, pid));
1216 }
1217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
satoke7c6998e2011-06-03 17:57:59 +09001219 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001221 if (!calledFromValidUser()) {
1222 return;
1223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001225 ClientState cs = mClients.remove(client.asBinder());
1226 if (cs != null) {
1227 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001228 if (mCurClient == cs) {
1229 mCurClient = null;
1230 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001231 if (mCurFocusedWindowClient == cs) {
1232 mCurFocusedWindowClient = null;
1233 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 void executeOrSendMessage(IInterface target, Message msg) {
1239 if (target.asBinder() instanceof Binder) {
1240 mCaller.sendMessage(msg);
1241 } else {
1242 handleMessage(msg);
1243 msg.recycle();
1244 }
1245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001246
Yohei Yukawa33e81792015-11-17 21:14:42 -08001247 void unbindCurrentClientLocked(
1248 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001250 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 + mCurClient.client.asBinder());
1252 if (mBoundToMethod) {
1253 mBoundToMethod = false;
1254 if (mCurMethod != null) {
1255 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1256 MSG_UNBIND_INPUT, mCurMethod));
1257 }
1258 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001259
1260 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1261 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001262 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1263 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001266
The Android Open Source Project10592532009-03-18 17:39:46 -07001267 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 private int getImeShowFlags() {
1272 int flags = 0;
1273 if (mShowForced) {
1274 flags |= InputMethod.SHOW_FORCED
1275 | InputMethod.SHOW_EXPLICIT;
1276 } else if (mShowExplicitlyRequested) {
1277 flags |= InputMethod.SHOW_EXPLICIT;
1278 }
1279 return flags;
1280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 private int getAppShowFlags() {
1283 int flags = 0;
1284 if (mShowForced) {
1285 flags |= InputMethodManager.SHOW_FORCED;
1286 } else if (!mShowExplicitlyRequested) {
1287 flags |= InputMethodManager.SHOW_IMPLICIT;
1288 }
1289 return flags;
1290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
Dianne Hackborn7663d802012-02-24 13:08:49 -08001292 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 if (!mBoundToMethod) {
1294 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1295 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1296 mBoundToMethod = true;
1297 }
1298 final SessionState session = mCurClient.curSession;
1299 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001300 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1301 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1302 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001304 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1305 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1306 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
1308 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001309 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001310 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001312 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001313 (session.channel != null ? session.channel.dup() : null),
1314 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001317 InputBindResult startInputLocked(
1318 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001319 IInputMethodClient client, IInputContext inputContext,
1320 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001321 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 // If no method is currently selected, do nothing.
1323 if (mCurMethodId == null) {
1324 return mNoBinding;
1325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 ClientState cs = mClients.get(client.asBinder());
1328 if (cs == null) {
1329 throw new IllegalArgumentException("unknown client "
1330 + client.asBinder());
1331 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001332
Yohei Yukawa74750f22016-03-22 12:54:22 -07001333 if (attribute == null) {
1334 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1335 + " uid=" + cs.uid + " pid=" + cs.pid);
1336 return null;
1337 }
1338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 try {
1340 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1341 // Check with the window manager to make sure this client actually
1342 // has a window with focus. If not, reject. This is thread safe
1343 // because if the focus changes some time before or after, the
1344 // next client receiving focus that has any interest in input will
1345 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001346 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1348 return null;
1349 }
1350 } catch (RemoteException e) {
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001353 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1354 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001355 }
1356
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001357 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001358 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001359 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001360 // If no method is currently selected, do nothing.
1361 if (mCurMethodId == null) {
1362 return mNoBinding;
1363 }
1364
Yohei Yukawad57ba672015-06-08 16:39:46 -07001365 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1366 attribute.packageName)) {
1367 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1368 + " uid=" + cs.uid + " package=" + attribute.packageName);
1369 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001370 }
1371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001373 // Was the keyguard locked when switching over to the new client?
1374 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 // If the client is changing, we need to switch over to the new
1376 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001377 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001378 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001379 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001382 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001383 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001384 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 // Bump up the sequence for this client and attach it.
1389 mCurSeq++;
1390 if (mCurSeq <= 0) mCurSeq = 1;
1391 mCurClient = cs;
1392 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001393 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 // Check if the input method is changing.
1397 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1398 if (cs.curSession != null) {
1399 // Fast case: if we are already connected to the input method,
1400 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001401 return attachNewInputLocked(
1402 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404 if (mHaveConnection) {
1405 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 // Return to client, and we will get back with it when
1407 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001408 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001409 return new InputBindResult(null, null, mCurId, mCurSeq,
1410 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 } else if (SystemClock.uptimeMillis()
1412 < (mLastBindTime+TIME_TO_RECONNECT)) {
1413 // In this case we have connected to the service, but
1414 // don't yet have its interface. If it hasn't been too
1415 // long since we did the connection, we'll return to
1416 // the client and wait to get the service interface so
1417 // we can report back. If it has been too long, we want
1418 // to fall through so we can try a disconnect/reconnect
1419 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001420 return new InputBindResult(null, null, mCurId, mCurSeq,
1421 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001423 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1424 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426 }
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001429 return startInputInnerLocked();
1430 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001432 InputBindResult startInputInnerLocked() {
1433 if (mCurMethodId == null) {
1434 return mNoBinding;
1435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001437 if (!mSystemReady) {
1438 // If the system is not yet ready, we shouldn't be running third
1439 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001440 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1441 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1445 if (info == null) {
1446 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001449 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1452 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001453 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1454 com.android.internal.R.string.input_method_binding_label);
1455 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1456 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001457 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001458 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1459 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 mLastBindTime = SystemClock.uptimeMillis();
1461 mHaveConnection = true;
1462 mCurId = info.getId();
1463 mCurToken = new Binder();
1464 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001465 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 mIWindowManager.addWindowToken(mCurToken,
1467 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1468 } catch (RemoteException e) {
1469 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001470 return new InputBindResult(null, null, mCurId, mCurSeq,
1471 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 } else {
1473 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001474 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 + mCurIntent);
1476 }
1477 return null;
1478 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001479
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001480 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001481 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001482 IInputMethodClient client, IInputContext inputContext,
1483 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001484 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001485 if (!calledFromValidUser()) {
1486 return null;
1487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001489 if (DEBUG) {
1490 Slog.v(TAG, "startInput: reason="
1491 + InputMethodClient.getStartInputReason(startInputReason)
1492 + " client = " + client.asBinder()
1493 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001494 + " missingMethods="
1495 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001496 + " attribute=" + attribute
1497 + " controlFlags=#" + Integer.toHexString(controlFlags));
1498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 final long ident = Binder.clearCallingIdentity();
1500 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001501 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1502 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 } finally {
1504 Binder.restoreCallingIdentity(ident);
1505 }
1506 }
1507 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508
satoke7c6998e2011-06-03 17:57:59 +09001509 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 public void finishInput(IInputMethodClient client) {
1511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001512
satoke7c6998e2011-06-03 17:57:59 +09001513 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 public void onServiceConnected(ComponentName name, IBinder service) {
1515 synchronized (mMethodMap) {
1516 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1517 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001518 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001519 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001520 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001521 return;
1522 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001523 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001524 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1525 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001527 clearClientSessionLocked(mCurClient);
1528 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530 }
1531 }
1532 }
1533
Jeff Brownc28867a2013-03-26 15:42:39 -07001534 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1535 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 synchronized (mMethodMap) {
1537 if (mCurMethod != null && method != null
1538 && mCurMethod.asBinder() == method.asBinder()) {
1539 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001540 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001542 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001543 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (res.method != null) {
1545 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001546 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001548 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550 }
1551 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001552
1553 // Session abandoned. Close its associated input channel.
1554 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001556
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001557 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001558 if (mVisibleBound) {
1559 mContext.unbindService(mVisibleConnection);
1560 mVisibleBound = false;
1561 }
1562
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001563 if (mHaveConnection) {
1564 mContext.unbindService(this);
1565 mHaveConnection = false;
1566 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001567
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001568 if (mCurToken != null) {
1569 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001570 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001571 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001572 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001573 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001574 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001575 mIWindowManager.removeWindowToken(mCurToken);
1576 } catch (RemoteException e) {
1577 }
1578 mCurToken = null;
1579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001580
The Android Open Source Project10592532009-03-18 17:39:46 -07001581 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001582 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001584
Yohei Yukawa33e81792015-11-17 21:14:42 -08001585 void resetCurrentMethodAndClient(
1586 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001587 mCurMethodId = null;
1588 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001589 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001590 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001591
1592 void requestClientSessionLocked(ClientState cs) {
1593 if (!cs.sessionRequested) {
1594 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1595 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1596 cs.sessionRequested = true;
1597 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1598 MSG_CREATE_SESSION, mCurMethod, channels[1],
1599 new MethodCallback(this, mCurMethod, channels[0])));
1600 }
1601 }
1602
1603 void clearClientSessionLocked(ClientState cs) {
1604 finishSessionLocked(cs.curSession);
1605 cs.curSession = null;
1606 cs.sessionRequested = false;
1607 }
1608
1609 private void finishSessionLocked(SessionState sessionState) {
1610 if (sessionState != null) {
1611 if (sessionState.session != null) {
1612 try {
1613 sessionState.session.finishSession();
1614 } catch (RemoteException e) {
1615 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001616 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001617 }
1618 sessionState.session = null;
1619 }
1620 if (sessionState.channel != null) {
1621 sessionState.channel.dispose();
1622 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001623 }
1624 }
1625 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001626
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001627 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 if (mCurMethod != null) {
1629 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001630 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001632
Jeff Brownc28867a2013-03-26 15:42:39 -07001633 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001634 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 mCurMethod = null;
1636 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001637 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001638 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
satoke7c6998e2011-06-03 17:57:59 +09001642 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 public void onServiceDisconnected(ComponentName name) {
1644 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001645 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 + " mCurIntent=" + mCurIntent);
1647 if (mCurMethod != null && mCurIntent != null
1648 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001649 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 // We consider this to be a new bind attempt, since the system
1651 // should now try to restart the service for us.
1652 mLastBindTime = SystemClock.uptimeMillis();
1653 mShowRequested = mInputShown;
1654 mInputShown = false;
1655 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001656 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1657 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1658 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660 }
1661 }
1662 }
1663
satokf9f01002011-05-19 21:31:50 +09001664 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1666 long ident = Binder.clearCallingIdentity();
1667 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001669 if (!calledWithValidToken(token)) {
1670 final int uid = Binder.getCallingUid();
1671 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1672 + " token:" + token);
1673 return;
1674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001676 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001677 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001678 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001681 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001682 CharSequence contentDescription = null;
1683 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001684 // Use PackageManager to load label
1685 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001686 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001687 mIPackageManager.getApplicationInfo(packageName, 0,
1688 mSettings.getCurrentUserId()));
1689 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001690 /* ignore */
1691 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001692 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001693 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001694 contentDescription != null
1695 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001696 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
1699 }
1700 } finally {
1701 Binder.restoreCallingIdentity(ident);
1702 }
1703 }
1704
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001705 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001706 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001707 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001708 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001709 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001710 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001711 // When physical keyboard is attached, we show the ime switcher (or notification if
1712 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1713 // exists in the IME switcher dialog. Might be OK to remove this condition once
1714 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1715 return true;
1716 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001717 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001718
1719 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1720 final int N = imis.size();
1721 if (N > 2) return true;
1722 if (N < 1) return false;
1723 int nonAuxCount = 0;
1724 int auxCount = 0;
1725 InputMethodSubtype nonAuxSubtype = null;
1726 InputMethodSubtype auxSubtype = null;
1727 for(int i = 0; i < N; ++i) {
1728 final InputMethodInfo imi = imis.get(i);
1729 final List<InputMethodSubtype> subtypes =
1730 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1731 final int subtypeCount = subtypes.size();
1732 if (subtypeCount == 0) {
1733 ++nonAuxCount;
1734 } else {
1735 for (int j = 0; j < subtypeCount; ++j) {
1736 final InputMethodSubtype subtype = subtypes.get(j);
1737 if (!subtype.isAuxiliary()) {
1738 ++nonAuxCount;
1739 nonAuxSubtype = subtype;
1740 } else {
1741 ++auxCount;
1742 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001743 }
1744 }
satok7cfc0ed2011-06-20 21:29:36 +09001745 }
1746 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001747 if (nonAuxCount > 1 || auxCount > 1) {
1748 return true;
1749 } else if (nonAuxCount == 1 && auxCount == 1) {
1750 if (nonAuxSubtype != null && auxSubtype != null
1751 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1752 || auxSubtype.overridesImplicitlyEnabledSubtype()
1753 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1754 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1755 return false;
1756 }
1757 return true;
1758 }
1759 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001760 }
1761
John Spurlocke0980502013-10-25 11:59:29 -04001762 private boolean isKeyguardLocked() {
1763 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1764 }
1765
satokdbf29502011-08-25 15:28:23 +09001766 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001767 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001768 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001769 if (!calledWithValidToken(token)) {
1770 final int uid = Binder.getCallingUid();
1771 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1772 + " token:" + token);
1773 return;
1774 }
1775
1776 synchronized (mMethodMap) {
1777 mImeWindowVis = vis;
1778 mBackDisposition = backDisposition;
1779 updateSystemUiLocked(token, vis, backDisposition);
1780 }
1781 }
1782
1783 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1784 synchronized (mMethodMap) {
1785 updateSystemUiLocked(token, vis, backDisposition);
1786 }
1787 }
1788
1789 // Caution! This method is called in this class. Handle multi-user carefully
1790 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1791 if (!calledWithValidToken(token)) {
1792 final int uid = Binder.getCallingUid();
1793 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1794 + " token:" + token);
1795 return;
1796 }
1797
1798 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1799 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001800 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001801 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001802 // apply policy for binder calls
1803 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1804 vis = 0;
satok06487a52010-10-29 11:37:18 +09001805 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001806 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1807 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1808 if (mStatusBar != null) {
1809 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1810 needsToShowImeSwitcher);
1811 }
1812 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1813 if (imi != null && needsToShowImeSwitcher) {
1814 // Used to load label
1815 final CharSequence title = mRes.getText(
1816 com.android.internal.R.string.select_input_method);
1817 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1818 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001819 mImeSwitcherNotification.setContentTitle(title)
1820 .setContentText(summary)
1821 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001822 try {
1823 if ((mNotificationManager != null)
1824 && !mIWindowManager.hasNavigationBar()) {
1825 if (DEBUG) {
1826 Slog.d(TAG, "--- show notification: label = " + summary);
1827 }
1828 mNotificationManager.notifyAsUser(null,
1829 com.android.internal.R.string.select_input_method,
1830 mImeSwitcherNotification.build(), UserHandle.ALL);
1831 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001832 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001833 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001834 }
1835 } else {
1836 if (mNotificationShown && mNotificationManager != null) {
1837 if (DEBUG) {
1838 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001839 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001840 mNotificationManager.cancelAsUser(null,
1841 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1842 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001843 }
satok06487a52010-10-29 11:37:18 +09001844 }
1845 } finally {
1846 Binder.restoreCallingIdentity(ident);
1847 }
1848 }
1849
satoke7c6998e2011-06-03 17:57:59 +09001850 @Override
satokf9f01002011-05-19 21:31:50 +09001851 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001852 if (!calledFromValidUser()) {
1853 return;
1854 }
satokf9f01002011-05-19 21:31:50 +09001855 synchronized (mMethodMap) {
1856 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1857 for (int i = 0; i < spans.length; ++i) {
1858 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001859 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001860 mSecureSuggestionSpans.put(ss, currentImi);
1861 }
1862 }
1863 }
1864 }
1865
satoke7c6998e2011-06-03 17:57:59 +09001866 @Override
satokf9f01002011-05-19 21:31:50 +09001867 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001868 if (!calledFromValidUser()) {
1869 return false;
1870 }
satokf9f01002011-05-19 21:31:50 +09001871 synchronized (mMethodMap) {
1872 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1873 // TODO: Do not send the intent if the process of the targetImi is already dead.
1874 if (targetImi != null) {
1875 final String[] suggestions = span.getSuggestions();
1876 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001877 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001878 final Intent intent = new Intent();
1879 // Ensures that only a class in the original IME package will receive the
1880 // notification.
satok42c5a162011-05-26 16:46:14 +09001881 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001882 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1883 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1884 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1885 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001886 final long ident = Binder.clearCallingIdentity();
1887 try {
1888 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1889 } finally {
1890 Binder.restoreCallingIdentity(ident);
1891 }
satokf9f01002011-05-19 21:31:50 +09001892 return true;
1893 }
1894 }
1895 return false;
1896 }
1897
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001898 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001899 updateInputMethodsFromSettingsLocked(enabledMayChange);
1900 updateKeyboardFromSettingsLocked();
1901 }
1902
1903 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001904 if (enabledMayChange) {
1905 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1906 for (int i=0; i<enabled.size(); i++) {
1907 // We allow the user to select "disabled until used" apps, so if they
1908 // are enabling one of those here we now need to make it enabled.
1909 InputMethodInfo imm = enabled.get(i);
1910 try {
1911 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1912 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1913 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001914 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001915 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001916 if (DEBUG) {
1917 Slog.d(TAG, "Update state(" + imm.getId()
1918 + "): DISABLED_UNTIL_USED -> DEFAULT");
1919 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001920 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1921 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001922 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1923 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001924 }
1925 } catch (RemoteException e) {
1926 }
1927 }
1928 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001929 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1930 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1931 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1932 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001933 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001934 // There is no input method selected, try to choose new applicable input method.
1935 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001936 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001937 }
1938 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001940 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001942 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001943 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 }
satokf3db1af2010-11-23 13:34:33 +09001945 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001946 } else {
1947 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001948 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001950 // Here is not the perfect place to reset the switching controller. Ideally
1951 // mSwitchingController and mSettings should be able to share the same state.
1952 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1953 // the same enabled IMEs list.
1954 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001955
1956 }
1957
1958 public void updateKeyboardFromSettingsLocked() {
1959 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1960 if (mSwitchingDialog != null
1961 && mSwitchingDialogTitleView != null
1962 && mSwitchingDialog.isShowing()) {
1963 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1964 com.android.internal.R.id.hard_keyboard_switch);
1965 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001968
Yohei Yukawab097b822015-12-01 10:43:08 -08001969 private void notifyInputMethodSubtypeChanged(final int userId,
1970 @Nullable final InputMethodInfo inputMethodInfo,
1971 @Nullable final InputMethodSubtype subtype) {
1972 final InputManagerInternal inputManagerInternal =
1973 LocalServices.getService(InputManagerInternal.class);
1974 if (inputManagerInternal != null) {
1975 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1976 }
1977 }
1978
satokab751aa2010-09-14 19:17:36 +09001979 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 InputMethodInfo info = mMethodMap.get(id);
1981 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001982 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001984
satokd81e9502012-05-21 12:58:45 +09001985 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001987 final int subtypeCount = info.getSubtypeCount();
1988 if (subtypeCount <= 0) {
1989 return;
satokcd7cd292010-11-20 15:46:23 +09001990 }
satokd81e9502012-05-21 12:58:45 +09001991 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1992 final InputMethodSubtype newSubtype;
1993 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1994 newSubtype = info.getSubtypeAt(subtypeId);
1995 } else {
1996 // If subtype is null, try to find the most applicable one from
1997 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001998 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001999 }
2000 if (newSubtype == null || oldSubtype == null) {
2001 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2002 + ", new subtype = " + newSubtype);
2003 return;
2004 }
2005 if (newSubtype != oldSubtype) {
2006 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2007 if (mCurMethod != null) {
2008 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002009 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002010 mCurMethod.changeInputMethodSubtype(newSubtype);
2011 } catch (RemoteException e) {
2012 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002013 return;
satokab751aa2010-09-14 19:17:36 +09002014 }
2015 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002016 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 return;
2019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002020
satokd81e9502012-05-21 12:58:45 +09002021 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 final long ident = Binder.clearCallingIdentity();
2023 try {
satokab751aa2010-09-14 19:17:36 +09002024 // Set a subtype to this input method.
2025 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002026 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2027 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2028 // because mCurMethodId is stored as a history in
2029 // setSelectedInputMethodAndSubtypeLocked().
2030 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031
2032 if (ActivityManagerNative.isSystemReady()) {
2033 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002034 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002036 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002038 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 } finally {
2040 Binder.restoreCallingIdentity(ident);
2041 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002042
2043 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2044 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002046
satok42c5a162011-05-26 16:46:14 +09002047 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002048 public boolean showSoftInput(IInputMethodClient client, int flags,
2049 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002050 if (!calledFromValidUser()) {
2051 return false;
2052 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002053 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 long ident = Binder.clearCallingIdentity();
2055 try {
2056 synchronized (mMethodMap) {
2057 if (mCurClient == null || client == null
2058 || mCurClient.client.asBinder() != client.asBinder()) {
2059 try {
2060 // We need to check if this is the current client with
2061 // focus in the window manager, to allow this call to
2062 // be made before input is started in it.
2063 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002064 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002065 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
2067 } catch (RemoteException e) {
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002071
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002072 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002073 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
2075 } finally {
2076 Binder.restoreCallingIdentity(ident);
2077 }
2078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002079
The Android Open Source Project4df24232009-03-05 14:34:35 -08002080 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002082 if (mAccessibilityRequestingNoSoftKeyboard) {
2083 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
Anna Galusza9b278112016-01-04 11:37:37 -08002085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2087 mShowExplicitlyRequested = true;
2088 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002089 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2090 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
Dianne Hackborncc278702009-09-02 23:07:23 -07002093 if (!mSystemReady) {
2094 return false;
2095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002096
The Android Open Source Project4df24232009-03-05 14:34:35 -08002097 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002099 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002100 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2101 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2102 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002104 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002105 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002106 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002107 | Context.BIND_TREAT_LIKE_ACTIVITY
2108 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002109 mVisibleBound = true;
2110 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002113 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 // The client has asked to have the input method shown, but
2115 // we have been sitting here too long with a connection to the
2116 // service and no interface received, so let's disconnect/connect
2117 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002120 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002122 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002123 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002124 } else {
2125 if (DEBUG) {
2126 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2127 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002133
satok42c5a162011-05-26 16:46:14 +09002134 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002135 public boolean hideSoftInput(IInputMethodClient client, int flags,
2136 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002137 if (!calledFromValidUser()) {
2138 return false;
2139 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002140 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 long ident = Binder.clearCallingIdentity();
2142 try {
2143 synchronized (mMethodMap) {
2144 if (mCurClient == null || client == null
2145 || mCurClient.client.asBinder() != client.asBinder()) {
2146 try {
2147 // We need to check if this is the current client with
2148 // focus in the window manager, to allow this call to
2149 // be made before input is started in it.
2150 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002151 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2152 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002153 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 }
2155 } catch (RemoteException e) {
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002159
Joe Onorato8a9b2202010-02-26 18:56:32 -08002160 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002161 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
2163 } finally {
2164 Binder.restoreCallingIdentity(ident);
2165 }
2166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
The Android Open Source Project4df24232009-03-05 14:34:35 -08002168 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2170 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002171 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 -08002172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
2174 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002175 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 -08002176 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002178
2179 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2180 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2181 // to be updated with the new value sent from IME process. Even in such a transient state
2182 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2183 // application process as a valid request, and have even promised such a behavior with CTS
2184 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2185 // IMMS#InputShown indicates that the software keyboard is shown.
2186 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2187 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2188 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002189 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002190 if (shouldHideSoftInput) {
2191 // The IME will report its visible state again after the following message finally
2192 // delivered to the IME process as an IPC. Hence the inconsistency between
2193 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2194 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002195 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2196 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2197 res = true;
2198 } else {
2199 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002201 if (mHaveConnection && mVisibleBound) {
2202 mContext.unbindService(mVisibleConnection);
2203 mVisibleBound = false;
2204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 mInputShown = false;
2206 mShowRequested = false;
2207 mShowExplicitlyRequested = false;
2208 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002209 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002211
satok42c5a162011-05-26 16:46:14 +09002212 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002213 public InputBindResult startInputOrWindowGainedFocus(
2214 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2215 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002216 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002217 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002218 if (windowToken != null) {
2219 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002220 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002221 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002222 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2223 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002224 }
2225 }
2226
2227 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002228 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2229 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002230 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2231 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002232 // Needs to check the validity before clearing calling identity
2233 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002234 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 long ident = Binder.clearCallingIdentity();
2236 try {
2237 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002238 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2239 + InputMethodClient.getStartInputReason(startInputReason)
2240 + " client=" + client.asBinder()
2241 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002242 + " missingMethods="
2243 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002244 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002245 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002247 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002248
Dianne Hackborn7663d802012-02-24 13:08:49 -08002249 ClientState cs = mClients.get(client.asBinder());
2250 if (cs == null) {
2251 throw new IllegalArgumentException("unknown client "
2252 + client.asBinder());
2253 }
2254
2255 try {
2256 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2257 // Check with the window manager to make sure this client actually
2258 // has a window with focus. If not, reject. This is thread safe
2259 // because if the focus changes some time before or after, the
2260 // next client receiving focus that has any interest in input will
2261 // be calling through here after that change happens.
2262 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2263 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2264 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002268
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002269 if (!calledFromValidUser) {
2270 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2271 Slog.w(TAG, "If you want to interect with IME, you need "
2272 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2273 hideCurrentInputLocked(0, null);
2274 return null;
2275 }
2276
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002277 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002278 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002279 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002280 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002281 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2282 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283 }
2284 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002285 }
2286 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002287 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002288
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002289 // Should we auto-show the IME even if the caller has not
2290 // specified what should be done with it?
2291 // We only do this automatically if the window can resize
2292 // to accommodate the IME (so what the user sees will give
2293 // them good context without input information being obscured
2294 // by the IME) or if running on a large screen where there
2295 // is more room for the target window + IME.
2296 final boolean doAutoShow =
2297 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2298 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2299 || mRes.getConfiguration().isLayoutSizeAtLeast(
2300 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002301 final boolean isTextEditor =
2302 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2303
2304 // We want to start input before showing the IME, but after closing
2305 // it. We want to do this after closing it to help the IME disappear
2306 // more quickly (not get stuck behind it initializing itself for the
2307 // new focused input, even if its window wants to hide the IME).
2308 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2311 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002312 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2314 // There is no focus view, and this window will
2315 // be behind any soft input window, so hide the
2316 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002317 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002318 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002320 } else if (isTextEditor && doAutoShow && (softInputMode &
2321 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 // There is a focus view, and we are navigating forward
2323 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002324 // We only do this automatically if the window can resize
2325 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002326 // them good context without input information being obscured
2327 // by the IME) or if running on a large screen where there
2328 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002329 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002330 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002331 res = startInputUncheckedLocked(cs, inputContext,
2332 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002333 didStart = true;
2334 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002335 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
2337 break;
2338 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2339 // Do nothing.
2340 break;
2341 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2342 if ((softInputMode &
2343 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002344 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002345 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 }
2347 break;
2348 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002349 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002350 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 break;
2352 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2353 if ((softInputMode &
2354 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002355 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002356 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002357 res = startInputUncheckedLocked(cs, inputContext,
2358 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002359 didStart = true;
2360 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002361 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 }
2363 break;
2364 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002365 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002366 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002367 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2368 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002369 didStart = true;
2370 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002371 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 break;
2373 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002374
2375 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002376 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002377 controlFlags);
2378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380 } finally {
2381 Binder.restoreCallingIdentity(ident);
2382 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002383
2384 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002386
satok42c5a162011-05-26 16:46:14 +09002387 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002388 public void showInputMethodPickerFromClient(
2389 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002390 if (!calledFromValidUser()) {
2391 return;
2392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 synchronized (mMethodMap) {
2394 if (mCurClient == null || client == null
2395 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002396 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002397 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 }
2399
satok440aab52010-11-25 09:43:11 +09002400 // Always call subtype picker, because subtype picker is a superset of input method
2401 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002402 mHandler.sendMessage(mCaller.obtainMessageI(
2403 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002404 }
2405 }
2406
satok42c5a162011-05-26 16:46:14 +09002407 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002409 if (!calledFromValidUser()) {
2410 return;
2411 }
satok28203512010-11-24 11:06:49 +09002412 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2413 }
2414
satok42c5a162011-05-26 16:46:14 +09002415 @Override
satok28203512010-11-24 11:06:49 +09002416 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002417 if (!calledFromValidUser()) {
2418 return;
2419 }
satok28203512010-11-24 11:06:49 +09002420 synchronized (mMethodMap) {
2421 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002422 setInputMethodWithSubtypeIdLocked(token, id,
2423 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2424 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002425 } else {
2426 setInputMethod(token, id);
2427 }
2428 }
satokab751aa2010-09-14 19:17:36 +09002429 }
2430
satok42c5a162011-05-26 16:46:14 +09002431 @Override
satokb416a712010-11-25 20:42:14 +09002432 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002433 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002434 if (!calledFromValidUser()) {
2435 return;
2436 }
satokb416a712010-11-25 20:42:14 +09002437 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002438 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2439 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002440 }
2441 }
2442
satok4fc87d62011-05-20 16:13:43 +09002443 @Override
satok735cf382010-11-11 20:40:09 +09002444 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002445 if (!calledFromValidUser()) {
2446 return false;
2447 }
satok735cf382010-11-11 20:40:09 +09002448 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002449 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002450 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002451 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002452 lastImi = mMethodMap.get(lastIme.first);
2453 } else {
2454 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002455 }
satok4fc87d62011-05-20 16:13:43 +09002456 String targetLastImiId = null;
2457 int subtypeId = NOT_A_SUBTYPE_ID;
2458 if (lastIme != null && lastImi != null) {
2459 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2460 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2461 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2462 : mCurrentSubtype.hashCode();
2463 // If the last IME is the same as the current IME and the last subtype is not
2464 // defined, there is no need to switch to the last IME.
2465 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2466 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002467 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002468 }
2469 }
2470
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002471 if (TextUtils.isEmpty(targetLastImiId)
2472 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002473 // This is a safety net. If the currentSubtype can't be added to the history
2474 // and the framework couldn't find the last ime, we will make the last ime be
2475 // the most applicable enabled keyboard subtype of the system imes.
2476 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2477 if (enabled != null) {
2478 final int N = enabled.size();
2479 final String locale = mCurrentSubtype == null
2480 ? mRes.getConfiguration().locale.toString()
2481 : mCurrentSubtype.getLocale();
2482 for (int i = 0; i < N; ++i) {
2483 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002484 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002485 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002486 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2487 InputMethodUtils.getSubtypes(imi),
2488 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002489 if (keyboardSubtype != null) {
2490 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002491 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002492 imi, keyboardSubtype.hashCode());
2493 if(keyboardSubtype.getLocale().equals(locale)) {
2494 break;
2495 }
2496 }
2497 }
2498 }
2499 }
2500 }
2501
2502 if (!TextUtils.isEmpty(targetLastImiId)) {
2503 if (DEBUG) {
2504 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2505 + ", from: " + mCurMethodId + ", " + subtypeId);
2506 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002507 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002508 return true;
2509 } else {
2510 return false;
2511 }
satok735cf382010-11-11 20:40:09 +09002512 }
2513 }
2514
satoke7c6998e2011-06-03 17:57:59 +09002515 @Override
satok688bd472012-02-09 20:09:17 +09002516 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002517 if (!calledFromValidUser()) {
2518 return false;
2519 }
satok688bd472012-02-09 20:09:17 +09002520 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002521 if (!calledWithValidToken(token)) {
2522 final int uid = Binder.getCallingUid();
2523 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2524 + " token:" + token);
2525 return false;
2526 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002527 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002528 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2529 if (nextSubtype == null) {
2530 return false;
2531 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002532 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2533 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002534 return true;
2535 }
2536 }
2537
2538 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002539 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2540 if (!calledFromValidUser()) {
2541 return false;
2542 }
2543 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002544 if (!calledWithValidToken(token)) {
2545 final int uid = Binder.getCallingUid();
2546 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2547 + "token. uid:" + uid + " token:" + token);
2548 return false;
2549 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002550 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002551 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2552 if (nextSubtype == null) {
2553 return false;
2554 }
2555 return true;
2556 }
2557 }
2558
2559 @Override
satok68f1b782011-04-11 14:26:04 +09002560 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002561 if (!calledFromValidUser()) {
2562 return null;
2563 }
satok68f1b782011-04-11 14:26:04 +09002564 synchronized (mMethodMap) {
2565 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2566 // TODO: Handle the case of the last IME with no subtypes
2567 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2568 || TextUtils.isEmpty(lastIme.second)) return null;
2569 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2570 if (lastImi == null) return null;
2571 try {
2572 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002573 final int lastSubtypeId =
2574 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002575 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2576 return null;
2577 }
2578 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002579 } catch (NumberFormatException e) {
2580 return null;
2581 }
2582 }
2583 }
2584
satoke7c6998e2011-06-03 17:57:59 +09002585 @Override
satokee5e77c2011-09-02 18:50:15 +09002586 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002587 if (!calledFromValidUser()) {
2588 return;
2589 }
satok91e88122011-07-18 11:11:42 +09002590 // By this IPC call, only a process which shares the same uid with the IME can add
2591 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002592 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002593 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002594 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002595 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002596 final String[] packageInfos;
2597 try {
2598 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2599 } catch (RemoteException e) {
2600 Slog.e(TAG, "Failed to get package infos");
2601 return;
2602 }
satok91e88122011-07-18 11:11:42 +09002603 if (packageInfos != null) {
2604 final int packageNum = packageInfos.length;
2605 for (int i = 0; i < packageNum; ++i) {
2606 if (packageInfos[i].equals(imi.getPackageName())) {
2607 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002608 final long ident = Binder.clearCallingIdentity();
2609 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002610 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002611 } finally {
2612 Binder.restoreCallingIdentity(ident);
2613 }
satokee5e77c2011-09-02 18:50:15 +09002614 return;
satok91e88122011-07-18 11:11:42 +09002615 }
2616 }
2617 }
satoke7c6998e2011-06-03 17:57:59 +09002618 }
satokee5e77c2011-09-02 18:50:15 +09002619 return;
satoke7c6998e2011-06-03 17:57:59 +09002620 }
2621
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002622 @Override
2623 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002624 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002625 }
2626
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002627 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002628 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002629 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002630 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002631 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002632 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002633 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2634 if (DEBUG) {
2635 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2636 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2637 + " actual: " + sequenceNumber);
2638 }
2639 return;
2640 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002641 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2642 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002643 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002644 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002645 }
2646 }
2647
satok28203512010-11-24 11:06:49 +09002648 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002650 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2651 }
2652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002654 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2655 if (token == null) {
2656 if (mContext.checkCallingOrSelfPermission(
2657 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2658 != PackageManager.PERMISSION_GRANTED) {
2659 throw new SecurityException(
2660 "Using null token requires permission "
2661 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002663 } else if (mCurToken != token) {
2664 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2665 + " token: " + token);
2666 return;
2667 }
2668
2669 final long ident = Binder.clearCallingIdentity();
2670 try {
2671 setInputMethodLocked(id, subtypeId);
2672 } finally {
2673 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 }
2675 }
2676
satok42c5a162011-05-26 16:46:14 +09002677 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002679 if (!calledFromValidUser()) {
2680 return;
2681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002683 if (!calledWithValidToken(token)) {
2684 final int uid = Binder.getCallingUid();
2685 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2686 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 return;
2688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 long ident = Binder.clearCallingIdentity();
2690 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002691 hideCurrentInputLocked(flags, null);
2692 } finally {
2693 Binder.restoreCallingIdentity(ident);
2694 }
2695 }
2696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002697
satok42c5a162011-05-26 16:46:14 +09002698 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002699 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002700 if (!calledFromValidUser()) {
2701 return;
2702 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002703 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002704 if (!calledWithValidToken(token)) {
2705 final int uid = Binder.getCallingUid();
2706 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2707 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002708 return;
2709 }
2710 long ident = Binder.clearCallingIdentity();
2711 try {
2712 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 } finally {
2714 Binder.restoreCallingIdentity(ident);
2715 }
2716 }
2717 }
2718
2719 void setEnabledSessionInMainThread(SessionState session) {
2720 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002721 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002723 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002724 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 } catch (RemoteException e) {
2726 }
2727 }
2728 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002729 if (mEnabledSession != null && mEnabledSession.session != null) {
2730 try {
2731 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2732 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2733 } catch (RemoteException e) {
2734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
2736 }
2737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002738
satok42c5a162011-05-26 16:46:14 +09002739 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002741 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002743 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002744 final boolean showAuxSubtypes;
2745 switch (msg.arg1) {
2746 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2747 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2748 // implemented so that auxiliary subtypes will be excluded when the soft
2749 // keyboard is invisible.
2750 showAuxSubtypes = mInputShown;
2751 break;
2752 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2753 showAuxSubtypes = true;
2754 break;
2755 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2756 showAuxSubtypes = false;
2757 break;
2758 default:
2759 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2760 return false;
2761 }
2762 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002763 return true;
2764
satok47a44912010-10-06 16:03:58 +09002765 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002766 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002767 return true;
2768
2769 case MSG_SHOW_IM_CONFIG:
2770 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002771 return true;
2772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 case MSG_UNBIND_INPUT:
2776 try {
2777 ((IInputMethod)msg.obj).unbindInput();
2778 } catch (RemoteException e) {
2779 // There is nothing interesting about the method dying.
2780 }
2781 return true;
2782 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002783 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 try {
2785 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2786 } catch (RemoteException e) {
2787 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002788 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 return true;
2790 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002791 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002793 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002794 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002795 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 } catch (RemoteException e) {
2797 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002798 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 return true;
2800 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002801 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002803 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002804 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002805 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 } catch (RemoteException e) {
2807 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002808 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 return true;
2810 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002811 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002813 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2815 } catch (RemoteException e) {
2816 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002817 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002819 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002820 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002821 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002822 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002824 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002826 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002827 // Dispose the channel if the input method is not local to this process
2828 // because the remote proxy will get its own copy when unparceled.
2829 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002830 channel.dispose();
2831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002833 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002837
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002838 case MSG_START_INPUT: {
2839 int missingMethods = msg.arg1;
2840 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002842 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002844 session.method.startInput((IInputContext) args.arg2, missingMethods,
2845 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 } catch (RemoteException e) {
2847 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002848 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002850 }
2851 case MSG_RESTART_INPUT: {
2852 int missingMethods = msg.arg1;
2853 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002855 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002857 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2858 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 } catch (RemoteException e) {
2860 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002861 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002866
Yohei Yukawa33e81792015-11-17 21:14:42 -08002867 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002869 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 } catch (RemoteException e) {
2871 // There is nothing interesting about the last client dying.
2872 }
2873 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002874 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002875 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002876 IInputMethodClient client = (IInputMethodClient)args.arg1;
2877 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002879 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002881 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002882 } finally {
2883 // Dispose the channel if the input method is not local to this process
2884 // because the remote proxy will get its own copy when unparceled.
2885 if (res.channel != null && Binder.isProxy(client)) {
2886 res.channel.dispose();
2887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002889 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002891 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002892 case MSG_SET_ACTIVE:
2893 try {
2894 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2895 } catch (RemoteException e) {
2896 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2897 + ((ClientState)msg.obj).pid + " uid "
2898 + ((ClientState)msg.obj).uid);
2899 }
2900 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002901 case MSG_SET_INTERACTIVE:
2902 handleSetInteractive(msg.arg1 != 0);
2903 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002904 case MSG_SWITCH_IME:
2905 handleSwitchInputMethod(msg.arg1 != 0);
2906 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002907 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2908 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002909 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002910 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002911 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002912 } catch (RemoteException e) {
2913 Slog.w(TAG, "Got RemoteException sending "
2914 + "setUserActionNotificationSequenceNumber("
2915 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002916 + clientState.pid + " uid "
2917 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002918 }
2919 return true;
2920 }
satok01038492012-04-09 21:08:27 +09002921
2922 // --------------------------------------------------------------
2923 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002924 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002925 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 }
2927 return false;
2928 }
2929
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002930 private void handleSetInteractive(final boolean interactive) {
2931 synchronized (mMethodMap) {
2932 mIsInteractive = interactive;
2933 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2934
2935 // Inform the current client of the change in active status
2936 if (mCurClient != null && mCurClient.client != null) {
2937 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2938 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2939 }
2940 }
2941 }
2942
Yohei Yukawaae61f712015-12-09 13:00:10 -08002943 private void handleSwitchInputMethod(final boolean forwardDirection) {
2944 synchronized (mMethodMap) {
2945 // TODO: Support forwardDirection.
2946 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2947 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2948 if (nextSubtype == null) {
2949 return;
2950 }
2951 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2952 }
2953 }
2954
satokdc9ddae2011-10-06 12:22:36 +09002955 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002956 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2957 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002958 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002959 if (DEBUG) {
2960 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2961 }
satok723a27e2010-11-11 14:58:11 +09002962 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002963 return true;
2964 }
2965
2966 return false;
2967 }
2968
Yohei Yukawa94e33302016-02-12 19:37:03 -08002969 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002970 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002971 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002972 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002973 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08002974 mMethodList.clear();
2975 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002976
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002977 // Use for queryIntentServicesAsUser
2978 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979
Yohei Yukawaed4952a2016-02-17 07:57:25 -08002980 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
2981 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
2982 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002983 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002985 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2986 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002987
satoke7c6998e2011-06-03 17:57:59 +09002988 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2989 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 for (int i = 0; i < services.size(); ++i) {
2991 ResolveInfo ri = services.get(i);
2992 ServiceInfo si = ri.serviceInfo;
2993 ComponentName compName = new ComponentName(si.packageName, si.name);
2994 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2995 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002996 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 + ": it does not require the permission "
2998 + android.Manifest.permission.BIND_INPUT_METHOD);
2999 continue;
3000 }
3001
Joe Onorato8a9b2202010-02-26 18:56:32 -08003002 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003
3004 try {
satoke7c6998e2011-06-03 17:57:59 +09003005 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003006 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003007 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003008 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009
3010 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003011 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003013 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003014 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 }
3016 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003017
Yohei Yukawa859df052016-02-17 07:56:46 -08003018 // TODO: The following code should find better place to live.
3019 if (!resetDefaultEnabledIme) {
3020 boolean enabledImeFound = false;
3021 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3022 final int N = enabledImes.size();
3023 for (int i = 0; i < N; ++i) {
3024 final InputMethodInfo imi = enabledImes.get(i);
3025 if (mMethodList.contains(imi)) {
3026 enabledImeFound = true;
3027 break;
3028 }
3029 }
3030 if (!enabledImeFound) {
3031 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3032 resetDefaultEnabledIme = true;
3033 resetSelectedInputMethodAndSubtypeLocked("");
3034 }
3035 }
3036
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003037 if (resetDefaultEnabledIme) {
3038 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003039 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003040 final int N = defaultEnabledIme.size();
3041 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003042 final InputMethodInfo imi = defaultEnabledIme.get(i);
3043 if (DEBUG) {
3044 Slog.d(TAG, "--- enable ime = " + imi);
3045 }
3046 setInputMethodEnabledLocked(imi.getId(), true);
3047 }
3048 }
3049
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003050 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003051 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003052 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003053 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3054 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003055 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003056 }
3057 } else {
3058 // Double check that the default IME is certainly enabled.
3059 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003060 }
3061 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003062 // Here is not the perfect place to reset the switching controller. Ideally
3063 // mSwitchingController and mSettings should be able to share the same state.
3064 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3065 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003066 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003070
satok217f5482010-12-15 05:19:19 +09003071 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003072 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003073 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003074 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3075 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003076 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003077 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003078 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003079 final int userId;
3080 synchronized (mMethodMap) {
3081 userId = mSettings.getCurrentUserId();
3082 }
3083 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003084 }
3085
3086 private void showConfigureInputMethods() {
3087 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3088 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3089 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3090 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003091 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003092 }
3093
satok2c93efc2012-04-02 19:33:47 +09003094 private boolean isScreenLocked() {
3095 return mKeyguardManager != null
3096 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3097 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003098
Seigo Nonaka14e13912015-05-06 21:04:13 -07003099 private void showInputMethodMenu(boolean showAuxSubtypes) {
3100 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003103 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003104
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003105 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003106 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003107 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003108
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003109 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003110 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003111 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3112 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003113 if (immis == null || immis.size() == 0) {
3114 return;
3115 }
3116
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003117 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118
satok688bd472012-02-09 20:09:17 +09003119 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003120 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003121 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003122
satokc3690562012-01-10 20:14:43 +09003123 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003124 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003125 if (currentSubtype != null) {
3126 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003127 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3128 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003129 }
3130 }
3131
Ken Wakasa761eb372011-03-04 19:06:18 +09003132 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003133 mIms = new InputMethodInfo[N];
3134 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003135 int checkedItem = 0;
3136 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003137 final ImeSubtypeListItem item = imList.get(i);
3138 mIms[i] = item.mImi;
3139 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003140 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003141 int subtypeId = mSubtypeIds[i];
3142 if ((subtypeId == NOT_A_SUBTYPE_ID)
3143 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3144 || (subtypeId == lastInputMethodSubtypeId)) {
3145 checkedItem = i;
3146 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003149
3150 final Context settingsContext = new ContextThemeWrapper(context,
3151 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3152
3153 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003154 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3155 @Override
3156 public void onCancel(DialogInterface dialog) {
3157 hideInputMethodMenu();
3158 }
3159 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003160
3161 final Context dialogContext = mDialogBuilder.getContext();
3162 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3163 com.android.internal.R.styleable.DialogPreference,
3164 com.android.internal.R.attr.alertDialogStyle, 0);
3165 final Drawable dialogIcon = a.getDrawable(
3166 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3167 a.recycle();
3168
3169 mDialogBuilder.setIcon(dialogIcon);
3170
Yohei Yukawad34e1482016-02-11 08:03:52 -08003171 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003172 final View tv = inflater.inflate(
3173 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3174 mDialogBuilder.setCustomTitle(tv);
3175
3176 // Setup layout for a toggle switch of the hardware keyboard
3177 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003178 mSwitchingDialogTitleView
3179 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003180 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003181 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003182 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003183 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003184 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003185 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3186 @Override
3187 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003188 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003189 // Ensure that the input method dialog is dismissed when changing
3190 // the hardware keyboard state.
3191 hideInputMethodMenu();
3192 }
3193 });
3194
Alan Viverette505e3ab2014-11-24 15:22:11 -08003195 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003196 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3197 final OnClickListener choiceListener = new OnClickListener() {
3198 @Override
3199 public void onClick(final DialogInterface dialog, final int which) {
3200 synchronized (mMethodMap) {
3201 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3202 || mSubtypeIds.length <= which) {
3203 return;
satok01038492012-04-09 21:08:27 +09003204 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003205 final InputMethodInfo im = mIms[which];
3206 int subtypeId = mSubtypeIds[which];
3207 adapter.mCheckedItem = which;
3208 adapter.notifyDataSetChanged();
3209 hideInputMethodMenu();
3210 if (im != null) {
3211 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3212 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003213 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003214 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003216 }
3217 }
3218 };
3219 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003221 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003222 final OnClickListener positiveListener = new OnClickListener() {
3223 @Override
3224 public void onClick(DialogInterface dialog, int whichButton) {
3225 showConfigureInputMethods();
3226 }
3227 };
satok82beadf2010-12-27 19:03:06 +09003228 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003229 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003232 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 mSwitchingDialog.getWindow().setType(
3234 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003235 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3236 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003237 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003238 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 mSwitchingDialog.show();
3240 }
3241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003242
Ken Wakasa05dbb652011-08-22 15:22:43 +09003243 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3244 private final LayoutInflater mInflater;
3245 private final int mTextViewResourceId;
3246 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003247 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003248 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3249 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3250 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003251
Ken Wakasa05dbb652011-08-22 15:22:43 +09003252 mTextViewResourceId = textViewResourceId;
3253 mItemsList = itemsList;
3254 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003255 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003256 }
3257
3258 @Override
3259 public View getView(int position, View convertView, ViewGroup parent) {
3260 final View view = convertView != null ? convertView
3261 : mInflater.inflate(mTextViewResourceId, null);
3262 if (position < 0 || position >= mItemsList.size()) return view;
3263 final ImeSubtypeListItem item = mItemsList.get(position);
3264 final CharSequence imeName = item.mImeName;
3265 final CharSequence subtypeName = item.mSubtypeName;
3266 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3267 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3268 if (TextUtils.isEmpty(subtypeName)) {
3269 firstTextView.setText(imeName);
3270 secondTextView.setVisibility(View.GONE);
3271 } else {
3272 firstTextView.setText(subtypeName);
3273 secondTextView.setText(imeName);
3274 secondTextView.setVisibility(View.VISIBLE);
3275 }
3276 final RadioButton radioButton =
3277 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3278 radioButton.setChecked(position == mCheckedItem);
3279 return view;
3280 }
3281 }
3282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003284 synchronized (mMethodMap) {
3285 hideInputMethodMenuLocked();
3286 }
3287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003288
The Android Open Source Project10592532009-03-18 17:39:46 -07003289 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003290 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291
The Android Open Source Project10592532009-03-18 17:39:46 -07003292 if (mSwitchingDialog != null) {
3293 mSwitchingDialog.dismiss();
3294 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003296
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003297 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003298 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003299 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003303
satok42c5a162011-05-26 16:46:14 +09003304 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003306 // TODO: Make this work even for non-current users?
3307 if (!calledFromValidUser()) {
3308 return false;
3309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 synchronized (mMethodMap) {
3311 if (mContext.checkCallingOrSelfPermission(
3312 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3313 != PackageManager.PERMISSION_GRANTED) {
3314 throw new SecurityException(
3315 "Requires permission "
3316 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3317 }
Anna Galusza9b278112016-01-04 11:37:37 -08003318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 long ident = Binder.clearCallingIdentity();
3320 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003321 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 } finally {
3323 Binder.restoreCallingIdentity(ident);
3324 }
3325 }
3326 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003327
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003328 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3329 // Make sure this is a valid input method.
3330 InputMethodInfo imm = mMethodMap.get(id);
3331 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003332 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003333 }
3334
satokd87c2592010-09-29 11:52:06 +09003335 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3336 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003337
satokd87c2592010-09-29 11:52:06 +09003338 if (enabled) {
3339 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3340 if (pair.first.equals(id)) {
3341 // We are enabling this input method, but it is already enabled.
3342 // Nothing to do. The previous state was enabled.
3343 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003344 }
3345 }
satokd87c2592010-09-29 11:52:06 +09003346 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3347 // Previous state was disabled.
3348 return false;
3349 } else {
3350 StringBuilder builder = new StringBuilder();
3351 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3352 builder, enabledInputMethodsList, id)) {
3353 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003354 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003355 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3356 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3357 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003358 }
3359 // Previous state was enabled.
3360 return true;
3361 } else {
3362 // We are disabling the input method but it is already disabled.
3363 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003364 return false;
3365 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003366 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003367 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003368
satok723a27e2010-11-11 14:58:11 +09003369 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3370 boolean setSubtypeOnly) {
3371 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003372 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003373
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003374 mCurUserActionNotificationSequenceNumber =
3375 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3376 if (DEBUG) {
3377 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3378 + mCurUserActionNotificationSequenceNumber);
3379 }
3380
3381 if (mCurClient != null && mCurClient.client != null) {
3382 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3383 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003384 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003385 }
3386
satok723a27e2010-11-11 14:58:11 +09003387 // Set Subtype here
3388 if (imi == null || subtypeId < 0) {
3389 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003390 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003391 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003392 if (subtypeId < imi.getSubtypeCount()) {
3393 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3394 mSettings.putSelectedSubtype(subtype.hashCode());
3395 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003396 } else {
3397 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003398 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003399 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003400 }
satokab751aa2010-09-14 19:17:36 +09003401 }
satok723a27e2010-11-11 14:58:11 +09003402
Yohei Yukawa68645a62016-02-17 07:54:20 -08003403 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003404 // Set InputMethod here
3405 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3406 }
3407 }
3408
3409 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3410 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3411 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3412 // newDefaultIme is empty when there is no candidate for the selected IME.
3413 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3414 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3415 if (subtypeHashCode != null) {
3416 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003417 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003418 imi, Integer.valueOf(subtypeHashCode));
3419 } catch (NumberFormatException e) {
3420 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3421 }
3422 }
3423 }
3424 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003425 }
3426
satok4e4569d2010-11-19 18:45:53 +09003427 // If there are no selected shortcuts, tries finding the most applicable ones.
3428 private Pair<InputMethodInfo, InputMethodSubtype>
3429 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3430 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3431 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003432 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003433 boolean foundInSystemIME = false;
3434
3435 // Search applicable subtype for each InputMethodInfo
3436 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003437 final String imiId = imi.getId();
3438 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3439 continue;
3440 }
satokcd7cd292010-11-20 15:46:23 +09003441 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003442 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003443 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003444 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003445 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003446 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003447 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003448 }
satokdf31ae62011-01-15 06:19:44 +09003449 // 2. Search by the system locale from enabledSubtypes.
3450 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003451 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003452 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003453 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003454 }
satoka86f5e42011-09-02 17:12:42 +09003455 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003456 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003457 final ArrayList<InputMethodSubtype> subtypesForSearch =
3458 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003459 ? InputMethodUtils.getSubtypes(imi)
3460 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003461 // 4. Search by the current subtype's locale from all subtypes.
3462 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003463 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003464 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003465 }
3466 // 5. Search by the system locale from all subtypes.
3467 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003468 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003469 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003470 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003471 }
satokcd7cd292010-11-20 15:46:23 +09003472 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003473 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003474 // The current input method is the most applicable IME.
3475 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003476 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003477 break;
satok7599a7f2010-12-22 13:45:23 +09003478 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003479 // The system input method is 2nd applicable IME.
3480 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003481 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003482 if ((imi.getServiceInfo().applicationInfo.flags
3483 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3484 foundInSystemIME = true;
3485 }
satok4e4569d2010-11-19 18:45:53 +09003486 }
3487 }
3488 }
3489 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003490 if (mostApplicableIMI != null) {
3491 Slog.w(TAG, "Most applicable shortcut input method was:"
3492 + mostApplicableIMI.getId());
3493 if (mostApplicableSubtype != null) {
3494 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3495 + "," + mostApplicableSubtype.getMode() + ","
3496 + mostApplicableSubtype.getLocale());
3497 }
3498 }
satok4e4569d2010-11-19 18:45:53 +09003499 }
satokcd7cd292010-11-20 15:46:23 +09003500 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003501 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003502 } else {
3503 return null;
3504 }
3505 }
3506
satokab751aa2010-09-14 19:17:36 +09003507 /**
3508 * @return Return the current subtype of this input method.
3509 */
satok42c5a162011-05-26 16:46:14 +09003510 @Override
satokab751aa2010-09-14 19:17:36 +09003511 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003512 // TODO: Make this work even for non-current users?
3513 if (!calledFromValidUser()) {
3514 return null;
3515 }
3516 synchronized (mMethodMap) {
3517 return getCurrentInputMethodSubtypeLocked();
3518 }
3519 }
3520
3521 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003522 if (mCurMethodId == null) {
3523 return null;
3524 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003525 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003526 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3527 if (imi == null || imi.getSubtypeCount() == 0) {
3528 return null;
satok4e4569d2010-11-19 18:45:53 +09003529 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003530 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003531 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3532 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003533 if (subtypeId == NOT_A_SUBTYPE_ID) {
3534 // If there are no selected subtypes, the framework will try to find
3535 // the most applicable subtype from explicitly or implicitly enabled
3536 // subtypes.
3537 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003538 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003539 // If there is only one explicitly or implicitly enabled subtype,
3540 // just returns it.
3541 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3542 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3543 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003544 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003545 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003546 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003547 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003548 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003549 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3550 true);
satok4e4569d2010-11-19 18:45:53 +09003551 }
satok3ef8b292010-11-23 06:06:29 +09003552 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003553 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003554 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003555 }
3556 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003557 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003558 }
3559
satok4e4569d2010-11-19 18:45:53 +09003560 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003561 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003562 @Override
satok4e4569d2010-11-19 18:45:53 +09003563 public List getShortcutInputMethodsAndSubtypes() {
3564 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003565 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003566 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003567 // If there are no selected shortcut subtypes, the framework will try to find
3568 // the most applicable subtype from all subtypes whose mode is
3569 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003570 Pair<InputMethodInfo, InputMethodSubtype> info =
3571 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003572 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003573 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003574 ret.add(info.first);
3575 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003576 }
satok3da92232011-01-11 22:46:30 +09003577 return ret;
satokf3db1af2010-11-23 13:34:33 +09003578 }
satokf3db1af2010-11-23 13:34:33 +09003579 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3580 ret.add(imi);
3581 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3582 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003583 }
3584 }
satokf3db1af2010-11-23 13:34:33 +09003585 return ret;
satok4e4569d2010-11-19 18:45:53 +09003586 }
3587 }
3588
satok42c5a162011-05-26 16:46:14 +09003589 @Override
satokb66d2872010-11-10 01:04:04 +09003590 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003591 // TODO: Make this work even for non-current users?
3592 if (!calledFromValidUser()) {
3593 return false;
3594 }
satokb66d2872010-11-10 01:04:04 +09003595 synchronized (mMethodMap) {
3596 if (subtype != null && mCurMethodId != null) {
3597 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003598 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003599 if (subtypeId != NOT_A_SUBTYPE_ID) {
3600 setInputMethodLocked(mCurMethodId, subtypeId);
3601 return true;
3602 }
3603 }
3604 return false;
3605 }
3606 }
3607
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003608 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003609 private static class InputMethodFileManager {
3610 private static final String SYSTEM_PATH = "system";
3611 private static final String INPUT_METHOD_PATH = "inputmethod";
3612 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3613 private static final String NODE_SUBTYPES = "subtypes";
3614 private static final String NODE_SUBTYPE = "subtype";
3615 private static final String NODE_IMI = "imi";
3616 private static final String ATTR_ID = "id";
3617 private static final String ATTR_LABEL = "label";
3618 private static final String ATTR_ICON = "icon";
3619 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003620 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003621 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3622 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3623 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003624 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003625 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3626 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003627 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003628 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003629 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003630 if (methodMap == null) {
3631 throw new NullPointerException("methodMap is null");
3632 }
3633 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003634 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003635 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3636 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003637 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003638 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003639 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3640 }
3641 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3642 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3643 if (!subtypeFile.exists()) {
3644 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003645 writeAdditionalInputMethodSubtypes(
3646 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003647 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003648 readAdditionalInputMethodSubtypes(
3649 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003650 }
3651 }
3652
3653 private void deleteAllInputMethodSubtypes(String imiId) {
3654 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003655 mAdditionalSubtypesMap.remove(imiId);
3656 writeAdditionalInputMethodSubtypes(
3657 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003658 }
3659 }
3660
3661 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003662 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003663 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003664 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003665 final int N = additionalSubtypes.length;
3666 for (int i = 0; i < N; ++i) {
3667 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003668 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003669 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003670 } else {
3671 Slog.w(TAG, "Duplicated subtype definition found: "
3672 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003673 }
3674 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003675 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3676 writeAdditionalInputMethodSubtypes(
3677 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003678 }
3679 }
3680
3681 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3682 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003683 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003684 }
3685 }
3686
3687 private static void writeAdditionalInputMethodSubtypes(
3688 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3689 HashMap<String, InputMethodInfo> methodMap) {
3690 // Safety net for the case that this function is called before methodMap is set.
3691 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3692 FileOutputStream fos = null;
3693 try {
3694 fos = subtypesFile.startWrite();
3695 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003696 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003697 out.startDocument(null, true);
3698 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3699 out.startTag(null, NODE_SUBTYPES);
3700 for (String imiId : allSubtypes.keySet()) {
3701 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3702 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3703 continue;
3704 }
3705 out.startTag(null, NODE_IMI);
3706 out.attribute(null, ATTR_ID, imiId);
3707 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3708 final int N = subtypesList.size();
3709 for (int i = 0; i < N; ++i) {
3710 final InputMethodSubtype subtype = subtypesList.get(i);
3711 out.startTag(null, NODE_SUBTYPE);
3712 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3713 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3714 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003715 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3716 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003717 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3718 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3719 out.attribute(null, ATTR_IS_AUXILIARY,
3720 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003721 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3722 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003723 out.endTag(null, NODE_SUBTYPE);
3724 }
3725 out.endTag(null, NODE_IMI);
3726 }
3727 out.endTag(null, NODE_SUBTYPES);
3728 out.endDocument();
3729 subtypesFile.finishWrite(fos);
3730 } catch (java.io.IOException e) {
3731 Slog.w(TAG, "Error writing subtypes", e);
3732 if (fos != null) {
3733 subtypesFile.failWrite(fos);
3734 }
3735 }
3736 }
3737
3738 private static void readAdditionalInputMethodSubtypes(
3739 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3740 if (allSubtypes == null || subtypesFile == null) return;
3741 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003742 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003743 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003744 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003745 int type = parser.getEventType();
3746 // Skip parsing until START_TAG
3747 while ((type = parser.next()) != XmlPullParser.START_TAG
3748 && type != XmlPullParser.END_DOCUMENT) {}
3749 String firstNodeName = parser.getName();
3750 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3751 throw new XmlPullParserException("Xml doesn't start with subtypes");
3752 }
3753 final int depth =parser.getDepth();
3754 String currentImiId = null;
3755 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3756 while (((type = parser.next()) != XmlPullParser.END_TAG
3757 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3758 if (type != XmlPullParser.START_TAG)
3759 continue;
3760 final String nodeName = parser.getName();
3761 if (NODE_IMI.equals(nodeName)) {
3762 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3763 if (TextUtils.isEmpty(currentImiId)) {
3764 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3765 continue;
3766 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003767 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003768 allSubtypes.put(currentImiId, tempSubtypesArray);
3769 } else if (NODE_SUBTYPE.equals(nodeName)) {
3770 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3771 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3772 continue;
3773 }
3774 final int icon = Integer.valueOf(
3775 parser.getAttributeValue(null, ATTR_ICON));
3776 final int label = Integer.valueOf(
3777 parser.getAttributeValue(null, ATTR_LABEL));
3778 final String imeSubtypeLocale =
3779 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003780 final String languageTag =
3781 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003782 final String imeSubtypeMode =
3783 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3784 final String imeSubtypeExtraValue =
3785 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003786 final boolean isAuxiliary = "1".equals(String.valueOf(
3787 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003788 final boolean isAsciiCapable = "1".equals(String.valueOf(
3789 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003790 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3791 .setSubtypeNameResId(label)
3792 .setSubtypeIconResId(icon)
3793 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003794 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003795 .setSubtypeMode(imeSubtypeMode)
3796 .setSubtypeExtraValue(imeSubtypeExtraValue)
3797 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003798 .setIsAsciiCapable(isAsciiCapable)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003799 .build();
satoke7c6998e2011-06-03 17:57:59 +09003800 tempSubtypesArray.add(subtype);
3801 }
3802 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003803 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3804 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003805 return;
satoke7c6998e2011-06-03 17:57:59 +09003806 }
3807 }
3808 }
3809
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003810 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3811 @NonNull
3812 private final Handler mHandler;
3813
3814 LocalServiceImpl(@NonNull final Handler handler) {
3815 mHandler = handler;
3816 }
3817
3818 @Override
3819 public void setInteractive(boolean interactive) {
3820 // Do everything in handler so as not to block the caller.
3821 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3822 interactive ? 1 : 0, 0));
3823 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003824
3825 @Override
3826 public void switchInputMethod(boolean forwardDirection) {
3827 // Do everything in handler so as not to block the caller.
3828 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3829 forwardDirection ? 1 : 0, 0));
3830 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003831 }
3832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 @Override
3834 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3835 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3836 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3839 + Binder.getCallingPid()
3840 + ", uid=" + Binder.getCallingUid());
3841 return;
3842 }
3843
3844 IInputMethod method;
3845 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003846 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 synchronized (mMethodMap) {
3851 p.println("Current Input Method Manager state:");
3852 int N = mMethodList.size();
3853 p.println(" Input Methods:");
3854 for (int i=0; i<N; i++) {
3855 InputMethodInfo info = mMethodList.get(i);
3856 p.println(" InputMethod #" + i + ":");
3857 info.dump(p, " ");
3858 }
3859 p.println(" Clients:");
3860 for (ClientState ci : mClients.values()) {
3861 p.println(" Client " + ci + ":");
3862 p.println(" client=" + ci.client);
3863 p.println(" inputContext=" + ci.inputContext);
3864 p.println(" sessionRequested=" + ci.sessionRequested);
3865 p.println(" curSession=" + ci.curSession);
3866 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003867 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003869 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3870 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003871 focusedWindowClient = mCurFocusedWindowClient;
3872 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3874 + " mBoundToMethod=" + mBoundToMethod);
3875 p.println(" mCurToken=" + mCurToken);
3876 p.println(" mCurIntent=" + mCurIntent);
3877 method = mCurMethod;
3878 p.println(" mCurMethod=" + mCurMethod);
3879 p.println(" mEnabledSession=" + mEnabledSession);
3880 p.println(" mShowRequested=" + mShowRequested
3881 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3882 + " mShowForced=" + mShowForced
3883 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003884 p.println(" mCurUserActionNotificationSequenceNumber="
3885 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003886 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003887 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003888 p.println(" mSwitchingController:");
3889 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003890 p.println(" mSettings:");
3891 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003893
Jeff Brownb88102f2010-09-08 11:49:43 -07003894 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 pw.flush();
3897 try {
3898 client.client.asBinder().dump(fd, args);
3899 } catch (RemoteException e) {
3900 p.println("Input method client dead: " + e);
3901 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003902 } else {
3903 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003905
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003906 if (focusedWindowClient != null && client != focusedWindowClient) {
3907 p.println(" ");
3908 p.println("Warning: Current input method client doesn't match the last focused. "
3909 + "window.");
3910 p.println("Dumping input method client in the last focused window just in case.");
3911 p.println(" ");
3912 pw.flush();
3913 try {
3914 focusedWindowClient.client.asBinder().dump(fd, args);
3915 } catch (RemoteException e) {
3916 p.println("Input method client in focused window dead: " + e);
3917 }
3918 }
3919
Jeff Brownb88102f2010-09-08 11:49:43 -07003920 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 pw.flush();
3923 try {
3924 method.asBinder().dump(fd, args);
3925 } catch (RemoteException e) {
3926 p.println("Input method service dead: " + e);
3927 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003928 } else {
3929 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 }
3931 }
3932}