blob: 507ac22bca0daee90d7b8e2f730fbeb1f75f27d0 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.ActivityManagerNative;
43import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070044import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090045import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090046import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090047import android.app.Notification;
48import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070049import android.app.PendingIntent;
Fyodor Kupolov6005b3f2015-11-23 17:41:50 -080050import android.app.SynchronousUserSwitchObserver;
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;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070064import android.content.pm.PackageManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.content.pm.ResolveInfo;
66import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080067import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070068import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.res.Resources;
70import android.content.res.TypedArray;
71import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080072import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080073import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080074import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070075import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040077import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070078import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090079import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.Handler;
81import android.os.IBinder;
82import android.os.IInterface;
83import android.os.Message;
84import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070085import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080087import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.ServiceManager;
89import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090090import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080091import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import android.provider.Settings;
93import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090094import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070095import android.util.ArrayMap;
96import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070097import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.EventLog;
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;
115import android.view.inputmethod.InputMethod;
116import android.view.inputmethod.InputMethodInfo;
117import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700118import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900119import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900120import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900121import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900122import android.widget.CompoundButton;
123import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900125import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900126import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
satoke7c6998e2011-06-03 17:57:59 +0900128import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900130import java.io.FileInputStream;
131import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import java.io.IOException;
133import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100134import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900136import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.util.HashMap;
138import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900139import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
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;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900194 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 // All known input methods. mMethodMap also serves as the global
197 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700198 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
199 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900200 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700201 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900202 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700204 // Used to bring IME service up to visible adjustment while it is being shown.
205 final ServiceConnection mVisibleConnection = new ServiceConnection() {
206 @Override public void onServiceConnected(ComponentName name, IBinder service) {
207 }
208
209 @Override public void onServiceDisconnected(ComponentName name) {
210 }
211 };
212 boolean mVisibleBound = false;
213
satok7cfc0ed2011-06-20 21:29:36 +0900214 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700215 private NotificationManager mNotificationManager;
216 private KeyguardManager mKeyguardManager;
217 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400218 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700219 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900220 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900221 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900222 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900223
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900224 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 final ClientState client;
226 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700227
228 IInputMethodSession session;
229 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public String toString() {
233 return "SessionState{uid " + client.uid + " pid " + client.pid
234 + " method " + Integer.toHexString(
235 System.identityHashCode(method))
236 + " session " + Integer.toHexString(
237 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700238 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 + "}";
240 }
241
242 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700243 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 client = _client;
245 method = _method;
246 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700247 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
Jeff Brownc28867a2013-03-26 15:42:39 -0700251 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final IInputMethodClient client;
253 final IInputContext inputContext;
254 final int uid;
255 final int pid;
256 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 boolean sessionRequested;
259 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 @Override
262 public String toString() {
263 return "ClientState{" + Integer.toHexString(
264 System.identityHashCode(this)) + " uid " + uid
265 + " pid " + pid + "}";
266 }
267
268 ClientState(IInputMethodClient _client, IInputContext _inputContext,
269 int _uid, int _pid) {
270 client = _client;
271 inputContext = _inputContext;
272 uid = _uid;
273 pid = _pid;
274 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
275 }
276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700278 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700281 * Set once the system is ready to run third party code.
282 */
283 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700285 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700286 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
287 * method. This is to be synchronized with the secure settings keyed with
288 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
289 *
290 * <p>This can be transiently {@code null} when the system is re-initializing input method
291 * settings, e.g., the system locale is just changed.</p>
292 *
293 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
294 * {@link InputMethodManagerService}.</p>
295 *
296 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700298 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * The current binding sequence number, incremented every time there is
303 * a new bind performed.
304 */
305 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
308 * The client that is currently bound to an input method.
309 */
310 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800313 * The last window token that we confirmed to be focused. This is always updated upon reports
314 * from the input method client. If the window state is already changed before the report is
315 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700316 */
317 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700319 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800320 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
321 */
322 ClientState mCurFocusedWindowClient;
323
324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * The input context last provided by the current client.
326 */
327 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * The attributes last provided by the current client.
331 */
332 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700335 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700337 *
338 * <p>This can be {@code null} when no input method is connected.</p>
339 *
340 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700342 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
satokab751aa2010-09-14 19:17:36 +0900346 * The current subtype of the current input method.
347 */
348 private InputMethodSubtype mCurrentSubtype;
349
satok4e4569d2010-11-19 18:45:53 +0900350 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900351 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700352 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900353
John Spurlocke0980502013-10-25 11:59:29 -0400354 // Was the keyguard locked when this client became current?
355 private boolean mCurClientInKeyguard;
356
satokab751aa2010-09-14 19:17:36 +0900357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * Set to true if our ServiceConnection is currently actively bound to
359 * a service (whether or not we have gotten its IBinder back yet).
360 */
361 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * Set if the client has asked for the input method to be shown.
365 */
366 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * Set if we were explicitly told to show the input method.
370 */
371 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * Set if we were forced to be shown.
375 */
376 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 /**
379 * Set if we last told the input method to show itself.
380 */
381 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /**
384 * The Intent used to connect to the current input method.
385 */
386 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
389 * The token we have made for the currently active input method, to
390 * identify it in the future.
391 */
392 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * If non-null, this is the input method service we are currently connected
396 * to.
397 */
398 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 /**
401 * Time that we last initiated a bind to the input method, to determine
402 * if we should try to disconnect and reconnect to it.
403 */
404 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
407 * Have we called mCurMethod.bindInput()?
408 */
409 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /**
412 * Currently enabled session. Only touched by service thread, not
413 * protected by a lock.
414 */
415 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700418 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700420 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900422 int mCurUserActionNotificationSequenceNumber = 0;
423
Joe Onorato857fd9b2011-01-27 15:08:35 -0800424 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900425
426 /**
427 * A set of status bits regarding the active IME.
428 *
429 * <p>This value is a combination of following two bits:</p>
430 * <dl>
431 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
432 * <dd>
433 * If this bit is ON, connected IME is ready to accept touch/key events.
434 * </dd>
435 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
436 * <dd>
437 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
438 * </dd>
439 * </dl>
440 * <em>Do not update this value outside of setImeWindowStatus.</em>
441 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800442 int mImeWindowVis;
443
Ken Wakasa05dbb652011-08-22 15:22:43 +0900444 private AlertDialog.Builder mDialogBuilder;
445 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900446 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900447 private InputMethodInfo[] mIms;
448 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900449 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700450 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800451 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900452 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
453 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500454 private final String mSlotIme;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700457 int mUserId;
458 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800459 String mLastEnabled = "";
460
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 /**
462 * <em>This constructor must be called within the lock.</em>
463 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 SettingsObserver(Handler handler) {
465 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700466 }
467
468 public void registerContentObserverLocked(int userId) {
469 if (mRegistered && mUserId == userId) {
470 return;
471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700473 if (mRegistered) {
474 mContext.getContentResolver().unregisterContentObserver(this);
475 mRegistered = false;
476 }
477 if (mUserId != userId) {
478 mLastEnabled = "";
479 mUserId = userId;
480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700482 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900483 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700484 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900485 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700486 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700487 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700488 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800489 resolver.registerContentObserver(Settings.Secure.getUriFor(
490 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
Michael Wright7b5a96b2014-08-09 19:28:42 -0700494 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800495 final Uri showImeUri = Settings.Secure.getUriFor(
496 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
497 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
498 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700500 if (showImeUri.equals(uri)) {
501 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800502 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
503 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
504 mContext.getContentResolver(),
505 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
506 0, mUserId) == 1;
507 if (mAccessibilityRequestingNoSoftKeyboard) {
508 final boolean showRequested = mShowRequested;
509 hideCurrentInputLocked(0, null);
510 mShowRequested = showRequested;
511 } else if (mShowRequested) {
512 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
513 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700514 } else {
515 boolean enabledChanged = false;
516 String newEnabled = mSettings.getEnabledInputMethodsStr();
517 if (!mLastEnabled.equals(newEnabled)) {
518 mLastEnabled = newEnabled;
519 enabledChanged = true;
520 }
521 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700525
526 @Override
527 public String toString() {
528 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
529 + " mLastEnabled=" + mLastEnabled + "}";
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800532
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900533 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900534 @Override
535 public void onReceive(Context context, Intent intent) {
536 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700537 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900538 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700539 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900540 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800541 } else if (Intent.ACTION_USER_ADDED.equals(action)
542 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100543 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800544 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700545 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
546 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
547 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
548 final String prevValue = intent.getStringExtra(
549 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
550 final String newValue = intent.getStringExtra(
551 Intent.EXTRA_SETTING_NEW_VALUE);
552 restoreEnabledInputMethods(mContext, prevValue, newValue);
553 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900554 } else {
555 Slog.w(TAG, "Unexpected intent " + intent);
556 }
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800559
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700560 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
561 // does not attempt to validate on the fly with any installed device policy, so must only
562 // be run in the context of initial device setup.
563 //
564 // TODO: Move this method to InputMethodUtils with adding unit tests.
565 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
566 if (DEBUG_RESTORE) {
567 Slog.i(TAG, "Restoring enabled input methods:");
568 Slog.i(TAG, "prev=" + prevValue);
569 Slog.i(TAG, " new=" + newValue);
570 }
571 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900572 ArrayMap<String, ArraySet<String>> prevMap =
573 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
574 ArrayMap<String, ArraySet<String>> newMap =
575 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700576
577 // Merge the restored ime+subtype enabled states into the live state
578 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
579 final String imeId = entry.getKey();
580 ArraySet<String> prevSubtypes = prevMap.get(imeId);
581 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700582 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700583 prevMap.put(imeId, prevSubtypes);
584 }
585 prevSubtypes.addAll(entry.getValue());
586 }
587
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700588 final String mergedImesAndSubtypesString =
589 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700590 if (DEBUG_RESTORE) {
591 Slog.i(TAG, "Merged IME string:");
592 Slog.i(TAG, " " + mergedImesAndSubtypesString);
593 }
594 Settings.Secure.putString(context.getContentResolver(),
595 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
596 }
597
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800598 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900599 private boolean isChangingPackagesOfCurrentUser() {
600 final int userId = getChangingUserId();
601 final boolean retval = userId == mSettings.getCurrentUserId();
602 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900603 if (!retval) {
604 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
605 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900606 }
607 return retval;
608 }
609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800611 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900612 if (!isChangingPackagesOfCurrentUser()) {
613 return false;
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900616 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 final int N = mMethodList.size();
618 if (curInputMethodId != null) {
619 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 InputMethodInfo imi = mMethodList.get(i);
621 if (imi.getId().equals(curInputMethodId)) {
622 for (String pkg : packages) {
623 if (imi.getPackageName().equals(pkg)) {
624 if (!doit) {
625 return true;
626 }
satok723a27e2010-11-11 14:58:11 +0900627 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800628 chooseNewDefaultIMELocked();
629 return true;
630 }
631 }
632 }
633 }
634 }
635 }
636 return false;
637 }
638
639 @Override
640 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900641 if (!isChangingPackagesOfCurrentUser()) {
642 return;
643 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800644 synchronized (mMethodMap) {
645 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900646 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800647 final int N = mMethodList.size();
648 if (curInputMethodId != null) {
649 for (int i=0; i<N; i++) {
650 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900651 final String imiId = imi.getId();
652 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800653 curIm = imi;
654 }
satoke7c6998e2011-06-03 17:57:59 +0900655
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900657 if (isPackageModified(imi.getPackageName())) {
658 mFileManager.deleteAllInputMethodSubtypes(imiId);
659 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800660 if (change == PACKAGE_TEMPORARY_CHANGE
661 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800663 + imi.getComponent());
664 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666 }
667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800668
Yohei Yukawa94e33302016-02-12 19:37:03 -0800669 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800672
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800673 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800674 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800675 if (change == PACKAGE_TEMPORARY_CHANGE
676 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800677 ServiceInfo si = null;
678 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900679 si = mIPackageManager.getServiceInfo(
680 curIm.getComponent(), 0, mSettings.getCurrentUserId());
681 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800682 }
683 if (si == null) {
684 // Uh oh, current input method is no longer around!
685 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800686 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900687 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800688 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800689 changed = true;
690 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900692 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800693 }
694 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800695 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800696 }
satokab751aa2010-09-14 19:17:36 +0900697
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698 if (curIm == null) {
699 // We currently don't have a default input method... is
700 // one now available?
701 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700702 } else if (!changed && isPackageModified(curIm.getPackageName())) {
703 // Even if the current input method is still available, mCurrentSubtype could
704 // be obsolete when the package is modified in practice.
705 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800708 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800709 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
712 }
713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800714
Jeff Brownc28867a2013-03-26 15:42:39 -0700715 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900716 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700717 private final IInputMethod mMethod;
718 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
Jeff Brownc28867a2013-03-26 15:42:39 -0700720 MethodCallback(InputMethodManagerService imms, IInputMethod method,
721 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900722 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700723 mMethod = method;
724 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800726
satoke7c6998e2011-06-03 17:57:59 +0900727 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700728 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700729 long ident = Binder.clearCallingIdentity();
730 try {
731 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
732 } finally {
733 Binder.restoreCallingIdentity(ident);
734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
satok01038492012-04-09 21:08:27 +0900738 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700739 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900740 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700741 public void onHardKeyboardStatusChange(boolean available) {
742 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
743 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900744 }
745
Michael Wright7b5a96b2014-08-09 19:28:42 -0700746 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900747 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700748 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900749 }
750 synchronized(mMethodMap) {
751 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
752 && mSwitchingDialog.isShowing()) {
753 mSwitchingDialogTitleView.findViewById(
754 com.android.internal.R.id.hard_keyboard_section).setVisibility(
755 available ? View.VISIBLE : View.GONE);
756 }
757 }
758 }
759 }
760
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800761 public static final class Lifecycle extends SystemService {
762 private InputMethodManagerService mService;
763
764 public Lifecycle(Context context) {
765 super(context);
766 mService = new InputMethodManagerService(context);
767 }
768
769 @Override
770 public void onStart() {
771 LocalServices.addService(InputMethodManagerInternal.class,
772 new LocalServiceImpl(mService.mHandler));
773 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
774 }
775
776 @Override
777 public void onBootPhase(int phase) {
778 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
779 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
780 .getService(Context.STATUS_BAR_SERVICE);
781 mService.systemRunning(statusBarService);
782 }
783 }
784 }
785
Seigo Nonaka7309b122015-08-17 18:34:13 -0700786 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900787 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800789 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700791 // Note: SettingsObserver doesn't register observers in its constructor.
792 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 mIWindowManager = IWindowManager.Stub.asInterface(
794 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700795 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800796 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900797 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 public void executeMessage(Message msg) {
799 handleMessage(msg);
800 }
Mita Yuned218c72012-12-06 17:18:25 -0800801 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800802 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
satok01038492012-04-09 21:08:27 +0900803 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700804 mHasFeature = context.getPackageManager().hasSystemFeature(
805 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500806 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
satok7cfc0ed2011-06-20 21:29:36 +0900807
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400808 Bundle extras = new Bundle();
809 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
810 mImeSwitcherNotification = new Notification.Builder(mContext)
811 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
812 .setWhen(0)
813 .setOngoing(true)
814 .addExtras(extras)
815 .setCategory(Notification.CATEGORY_SYSTEM)
816 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400817
satok7cfc0ed2011-06-20 21:29:36 +0900818 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900819 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900820
821 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900822
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900823 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900824 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800825 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
826 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700827 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900828 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800829
satok7cfc0ed2011-06-20 21:29:36 +0900830 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900831 int userId = 0;
832 try {
833 ActivityManagerNative.getDefault().registerUserSwitchObserver(
Fyodor Kupolov6005b3f2015-11-23 17:41:50 -0800834 new SynchronousUserSwitchObserver() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900835 @Override
Fyodor Kupolov6005b3f2015-11-23 17:41:50 -0800836 public void onUserSwitching(int newUserId)
837 throws RemoteException {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900838 synchronized(mMethodMap) {
839 switchUserLocked(newUserId);
840 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900841 }
842
843 @Override
844 public void onUserSwitchComplete(int newUserId) throws RemoteException {
845 }
Kenny Guy42979622015-04-13 18:03:05 +0000846
847 @Override
848 public void onForegroundProfileSwitch(int newProfileId) {
849 // Ignore.
850 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900851 });
852 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
853 } catch (RemoteException e) {
854 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
855 }
satok81f8b7c2012-12-04 20:42:56 +0900856 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900857
satokd87c2592010-09-29 11:52:06 +0900858 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900859 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800860 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700861
862 // Let the package manager query which are the default imes
863 // as they get certain permissions granted by default.
864 PackageManagerInternal packageManagerInternal = LocalServices.getService(
865 PackageManagerInternal.class);
866 packageManagerInternal.setImePackagesProvider(
867 new PackageManagerInternal.PackagesProvider() {
868 @Override
869 public String[] getPackages(int userId) {
870 synchronized (mMethodMap) {
871 final int currentUserId = mSettings.getCurrentUserId();
872 // TODO: We are switching the current user id in the settings
873 // object to query it and then revert the user id. Ideally, we
874 // should call a API in settings with the user id as an argument.
Yohei Yukawa68645a62016-02-17 07:54:20 -0800875 mSettings.switchCurrentUser(userId, true /* copyOnWrite */);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700876 List<InputMethodInfo> imes = mSettings
877 .getEnabledInputMethodListLocked();
878 String[] packageNames = null;
879 if (imes != null) {
880 final int imeCount = imes.size();
881 packageNames = new String[imeCount];
882 for (int i = 0; i < imeCount; i++) {
883 InputMethodInfo ime = imes.get(i);
884 packageNames[i] = ime.getPackageName();
885 }
886 }
Yohei Yukawa68645a62016-02-17 07:54:20 -0800887 // If the system is not ready, then we use copy-on-write mode.
888 final boolean useCopyOnWriteSettings = !mSystemReady;
889 mSettings.switchCurrentUser(currentUserId, useCopyOnWriteSettings);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700890 return packageNames;
891 }
892 }
893 });
894
Kenny Guy2a764942014-04-02 13:29:20 +0100895 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900896 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900897 synchronized (mMethodMap) {
898 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
899 mSettings, context);
900 }
satok0a1bcf42012-05-16 19:26:31 +0900901
902 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900903 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900904 if (DEBUG) {
905 Slog.d(TAG, "Initial default ime = " + defaultImiId);
906 }
satok0a1bcf42012-05-16 19:26:31 +0900907 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
908
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900909 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800910 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900911 }
satokd87c2592010-09-29 11:52:06 +0900912 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913
satok0a1bcf42012-05-16 19:26:31 +0900914 if (!mImeSelectedOnBoot) {
915 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900916 synchronized (mMethodMap) {
917 resetDefaultImeLocked(context);
918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900921 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700922 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900923 updateFromSettingsLocked(true);
924 }
satok5b927c432012-05-01 20:09:34 +0900925
926 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
927 // according to the new system locale.
928 final IntentFilter filter = new IntentFilter();
929 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
930 mContext.registerReceiver(
931 new BroadcastReceiver() {
932 @Override
933 public void onReceive(Context context, Intent intent) {
934 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900935 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900936 }
937 }
938 }, filter);
939 }
940
satok5b927c432012-05-01 20:09:34 +0900941 private void resetDefaultImeLocked(Context context) {
942 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800943 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900944 return;
945 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800946 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
947 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
948 if (suitableImes.isEmpty()) {
949 Slog.i(TAG, "No default found");
950 return;
satok5b927c432012-05-01 20:09:34 +0900951 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800952 final InputMethodInfo defIm = suitableImes.get(0);
953 Slog.i(TAG, "Default found, using " + defIm.getId());
954 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900955 }
956
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900957 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
958 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900959 if (!mSystemReady) {
960 // not system ready
961 return;
962 }
963 final Locale newLocale = mRes.getConfiguration().locale;
964 if (!updateOnlyWhenLocaleChanged
965 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
966 if (!updateOnlyWhenLocaleChanged) {
967 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800968 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900969 }
970 if (DEBUG) {
971 Slog.i(TAG, "Locale has been changed to " + newLocale);
972 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800973 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900974 if (!updateOnlyWhenLocaleChanged) {
975 final String selectedImiId = mSettings.getSelectedInputMethod();
976 if (TextUtils.isEmpty(selectedImiId)) {
977 // This is the first time of the user switch and
978 // set the current ime to the proper one.
979 resetDefaultImeLocked(mContext);
980 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900981 } else {
982 // If the locale is changed, needs to reset the default ime
983 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800985 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 mLastSystemLocale = newLocale;
987 if (!updateOnlyWhenLocaleChanged) {
988 try {
989 startInputInnerLocked();
990 } catch (RuntimeException e) {
991 Slog.w(TAG, "Unexpected exception", e);
992 }
993 }
994 }
995 }
996
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900997 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900998 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
999 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001000 }
1001
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001002 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001003 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1004 + " currentUserId=" + mSettings.getCurrentUserId());
1005
Yohei Yukawa81482972015-06-04 00:58:59 -07001006 // ContentObserver should be registered again when the user is changed
1007 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001008
1009 // If the system is not ready, then we use copy-on-write settings.
1010 final boolean useCopyOnWriteSettings = !mSystemReady;
1011 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001012 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001013 // InputMethodFileManager should be reset when the user is changed
1014 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001015 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001016
1017 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1018 + " defaultImiId=" + defaultImiId);
1019
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001020 // For secondary users, the list of enabled IMEs may not have been updated since the
1021 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1022 // not be empty even if the IME has been uninstalled by the primary user.
1023 // Even in such cases, IMMS works fine because it will find the most applicable
1024 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001025 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001026 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001027 initialUserSwitch /* needsToResetDefaultIme */);
1028 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001029 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1030 mSettings.getEnabledInputMethodListLocked(), newUserId,
1031 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001032 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001033
1034 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1035 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001036 }
1037
Kenny Guy2a764942014-04-02 13:29:20 +01001038 void updateCurrentProfileIds() {
Yohei Yukawad34e1482016-02-11 08:03:52 -08001039 List<UserInfo> profiles = mContext.getSystemService(UserManager.class)
1040 .getProfiles(mSettings.getCurrentUserId());
Kenny Guy2a764942014-04-02 13:29:20 +01001041 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1042 for (int i = 0; i < currentProfileIds.length; i++) {
1043 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001044 }
Kenny Guy2a764942014-04-02 13:29:20 +01001045 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001046 }
1047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 @Override
1049 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1050 throws RemoteException {
1051 try {
1052 return super.onTransact(code, data, reply, flags);
1053 } catch (RuntimeException e) {
1054 // The input method manager only throws security exceptions, so let's
1055 // log all others.
1056 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001057 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059 throw e;
1060 }
1061 }
1062
Svetoslav Ganova0027152013-06-25 14:59:53 -07001063 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001064 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001065 if (DEBUG) {
1066 Slog.d(TAG, "--- systemReady");
1067 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001068 if (!mSystemReady) {
1069 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001070 final int currentUserId = mSettings.getCurrentUserId();
1071 mSettings.switchCurrentUser(currentUserId, false /* copyOnWrite */);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001072 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1073 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001074 mStatusBar = statusBar;
Jason Monk3e189872016-01-12 09:10:34 -05001075 statusBar.setIconVisibility(mSlotIme, false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001076 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001077 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1078 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001079 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001080 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001081 mHardKeyboardListener);
1082 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001083 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001084 if (!mImeSelectedOnBoot) {
1085 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001086 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001087 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1088 mSettings.getEnabledInputMethodListLocked(),
1089 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001090 }
1091 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001092 try {
1093 startInputInnerLocked();
1094 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001096 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001097 }
1098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001101 // ---------------------------------------------------------------------------------------
1102 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1103 // 1) it comes from the system process
1104 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1105 private boolean calledFromValidUser() {
1106 final int uid = Binder.getCallingUid();
1107 final int userId = UserHandle.getUserId(uid);
1108 if (DEBUG) {
1109 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1110 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1111 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001112 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1113 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001114 }
Kenny Guy2a764942014-04-02 13:29:20 +01001115 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001116 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001118
1119 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1120 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1121 // must not manage background users' states in any functions.
1122 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1123 // by a token.
1124 if (mContext.checkCallingOrSelfPermission(
1125 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1126 == PackageManager.PERMISSION_GRANTED) {
1127 if (DEBUG) {
1128 Slog.d(TAG, "--- Access granted because the calling process has "
1129 + "the INTERACT_ACROSS_USERS_FULL permission");
1130 }
1131 return true;
1132 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001133 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1134 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001135 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001136 }
1137
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001138
1139 /**
1140 * Returns true iff the caller is identified to be the current input method with the token.
1141 * @param token The window token given to the input method when it was started.
1142 * @return true if and only if non-null valid token is specified.
1143 */
1144 private boolean calledWithValidToken(IBinder token) {
1145 if (token == null || mCurToken != token) {
1146 return false;
1147 }
1148 return true;
1149 }
1150
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001151 private boolean bindCurrentInputMethodService(
1152 Intent service, ServiceConnection conn, int flags) {
1153 if (service == null || conn == null) {
1154 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1155 return false;
1156 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001157 return mContext.bindServiceAsUser(service, conn, flags,
1158 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001159 }
1160
satoke7c6998e2011-06-03 17:57:59 +09001161 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001163 // TODO: Make this work even for non-current users?
1164 if (!calledFromValidUser()) {
1165 return Collections.emptyList();
1166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001168 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 }
1171
satoke7c6998e2011-06-03 17:57:59 +09001172 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001174 // TODO: Make this work even for non-current users?
1175 if (!calledFromValidUser()) {
1176 return Collections.emptyList();
1177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001179 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 }
1182
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001183 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001184 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001185 * @return enabled subtypes of the specified imi
1186 */
satoke7c6998e2011-06-03 17:57:59 +09001187 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001188 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001189 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001190 // TODO: Make this work even for non-current users?
1191 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001192 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001193 }
satok67ddf9c2010-11-17 09:45:54 +09001194 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001195 final InputMethodInfo imi;
1196 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001197 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001198 } else {
1199 imi = mMethodMap.get(imiId);
1200 }
1201 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001202 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001203 }
1204 return mSettings.getEnabledInputMethodSubtypeListLocked(
1205 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001206 }
1207 }
1208
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public void addClient(IInputMethodClient client,
1211 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001212 if (!calledFromValidUser()) {
1213 return;
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 synchronized (mMethodMap) {
1216 mClients.put(client.asBinder(), new ClientState(client,
1217 inputContext, uid, pid));
1218 }
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
satoke7c6998e2011-06-03 17:57:59 +09001221 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001223 if (!calledFromValidUser()) {
1224 return;
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001227 ClientState cs = mClients.remove(client.asBinder());
1228 if (cs != null) {
1229 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001230 if (mCurClient == cs) {
1231 mCurClient = null;
1232 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001233 if (mCurFocusedWindowClient == cs) {
1234 mCurFocusedWindowClient = null;
1235 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 void executeOrSendMessage(IInterface target, Message msg) {
1241 if (target.asBinder() instanceof Binder) {
1242 mCaller.sendMessage(msg);
1243 } else {
1244 handleMessage(msg);
1245 msg.recycle();
1246 }
1247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001248
Yohei Yukawa33e81792015-11-17 21:14:42 -08001249 void unbindCurrentClientLocked(
1250 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001252 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 + mCurClient.client.asBinder());
1254 if (mBoundToMethod) {
1255 mBoundToMethod = false;
1256 if (mCurMethod != null) {
1257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1258 MSG_UNBIND_INPUT, mCurMethod));
1259 }
1260 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001261
1262 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1263 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001264 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1265 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001268
The Android Open Source Project10592532009-03-18 17:39:46 -07001269 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 private int getImeShowFlags() {
1274 int flags = 0;
1275 if (mShowForced) {
1276 flags |= InputMethod.SHOW_FORCED
1277 | InputMethod.SHOW_EXPLICIT;
1278 } else if (mShowExplicitlyRequested) {
1279 flags |= InputMethod.SHOW_EXPLICIT;
1280 }
1281 return flags;
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 private int getAppShowFlags() {
1285 int flags = 0;
1286 if (mShowForced) {
1287 flags |= InputMethodManager.SHOW_FORCED;
1288 } else if (!mShowExplicitlyRequested) {
1289 flags |= InputMethodManager.SHOW_IMPLICIT;
1290 }
1291 return flags;
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
Dianne Hackborn7663d802012-02-24 13:08:49 -08001294 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 if (!mBoundToMethod) {
1296 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1297 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1298 mBoundToMethod = true;
1299 }
1300 final SessionState session = mCurClient.curSession;
1301 if (initial) {
1302 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1303 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1304 } else {
1305 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1306 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1307 }
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,
1319 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1320 int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 // If no method is currently selected, do nothing.
1322 if (mCurMethodId == null) {
1323 return mNoBinding;
1324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 ClientState cs = mClients.get(client.asBinder());
1327 if (cs == null) {
1328 throw new IllegalArgumentException("unknown client "
1329 + client.asBinder());
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 try {
1333 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1334 // Check with the window manager to make sure this client actually
1335 // has a window with focus. If not, reject. This is thread safe
1336 // because if the focus changes some time before or after, the
1337 // next client receiving focus that has any interest in input will
1338 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001339 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1341 return null;
1342 }
1343 } catch (RemoteException e) {
1344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
Dianne Hackborn7663d802012-02-24 13:08:49 -08001346 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1347 }
1348
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001349 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
1350 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001351 // If no method is currently selected, do nothing.
1352 if (mCurMethodId == null) {
1353 return mNoBinding;
1354 }
1355
Yohei Yukawad57ba672015-06-08 16:39:46 -07001356 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1357 attribute.packageName)) {
1358 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1359 + " uid=" + cs.uid + " package=" + attribute.packageName);
1360 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001361 }
1362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001364 // Was the keyguard locked when switching over to the new client?
1365 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 // If the client is changing, we need to switch over to the new
1367 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001368 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001369 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001370 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371
1372 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001373 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001374 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001375 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 // Bump up the sequence for this client and attach it.
1380 mCurSeq++;
1381 if (mCurSeq <= 0) mCurSeq = 1;
1382 mCurClient = cs;
1383 mCurInputContext = inputContext;
1384 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 // Check if the input method is changing.
1387 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1388 if (cs.curSession != null) {
1389 // Fast case: if we are already connected to the input method,
1390 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001391 return attachNewInputLocked(
1392 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 if (mHaveConnection) {
1395 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 // Return to client, and we will get back with it when
1397 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001398 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001399 return new InputBindResult(null, null, mCurId, mCurSeq,
1400 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 } else if (SystemClock.uptimeMillis()
1402 < (mLastBindTime+TIME_TO_RECONNECT)) {
1403 // In this case we have connected to the service, but
1404 // don't yet have its interface. If it hasn't been too
1405 // long since we did the connection, we'll return to
1406 // the client and wait to get the service interface so
1407 // we can report back. If it has been too long, we want
1408 // to fall through so we can try a disconnect/reconnect
1409 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001410 return new InputBindResult(null, null, mCurId, mCurSeq,
1411 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1414 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
1416 }
1417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001419 return startInputInnerLocked();
1420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001421
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001422 InputBindResult startInputInnerLocked() {
1423 if (mCurMethodId == null) {
1424 return mNoBinding;
1425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001427 if (!mSystemReady) {
1428 // If the system is not yet ready, we shouldn't be running third
1429 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001430 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1431 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1435 if (info == null) {
1436 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001439 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1442 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001443 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1444 com.android.internal.R.string.input_method_binding_label);
1445 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1446 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001447 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001448 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1449 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 mLastBindTime = SystemClock.uptimeMillis();
1451 mHaveConnection = true;
1452 mCurId = info.getId();
1453 mCurToken = new Binder();
1454 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001455 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 mIWindowManager.addWindowToken(mCurToken,
1457 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1458 } catch (RemoteException e) {
1459 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001460 return new InputBindResult(null, null, mCurId, mCurSeq,
1461 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 } else {
1463 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001464 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 + mCurIntent);
1466 }
1467 return null;
1468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
satoke7c6998e2011-06-03 17:57:59 +09001470 @Override
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001471 public InputBindResult startInput(
1472 /* @InputMethodClient.StartInputReason */ final int startInputReason,
1473 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1474 int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001475 if (!calledFromValidUser()) {
1476 return null;
1477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001479 if (DEBUG) {
1480 Slog.v(TAG, "startInput: reason="
1481 + InputMethodClient.getStartInputReason(startInputReason)
1482 + " client = " + client.asBinder()
1483 + " inputContext=" + inputContext
1484 + " attribute=" + attribute
1485 + " controlFlags=#" + Integer.toHexString(controlFlags));
1486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 final long ident = Binder.clearCallingIdentity();
1488 try {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001489 return startInputLocked(startInputReason, client, inputContext, attribute,
1490 controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 } finally {
1492 Binder.restoreCallingIdentity(ident);
1493 }
1494 }
1495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001496
satoke7c6998e2011-06-03 17:57:59 +09001497 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 public void finishInput(IInputMethodClient client) {
1499 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001500
satoke7c6998e2011-06-03 17:57:59 +09001501 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 public void onServiceConnected(ComponentName name, IBinder service) {
1503 synchronized (mMethodMap) {
1504 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1505 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001506 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001508 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001509 return;
1510 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001511 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001512 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1513 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001515 clearClientSessionLocked(mCurClient);
1516 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518 }
1519 }
1520 }
1521
Jeff Brownc28867a2013-03-26 15:42:39 -07001522 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1523 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 synchronized (mMethodMap) {
1525 if (mCurMethod != null && method != null
1526 && mCurMethod.asBinder() == method.asBinder()) {
1527 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001528 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001530 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001531 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 if (res.method != null) {
1533 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001534 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001536 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 }
1538 }
1539 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001540
1541 // Session abandoned. Close its associated input channel.
1542 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001544
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001545 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001546 if (mVisibleBound) {
1547 mContext.unbindService(mVisibleConnection);
1548 mVisibleBound = false;
1549 }
1550
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001551 if (mHaveConnection) {
1552 mContext.unbindService(this);
1553 mHaveConnection = false;
1554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001555
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001556 if (mCurToken != null) {
1557 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001558 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001559 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001560 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001561 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001562 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001563 mIWindowManager.removeWindowToken(mCurToken);
1564 } catch (RemoteException e) {
1565 }
1566 mCurToken = null;
1567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001568
The Android Open Source Project10592532009-03-18 17:39:46 -07001569 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001570 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
Yohei Yukawa33e81792015-11-17 21:14:42 -08001573 void resetCurrentMethodAndClient(
1574 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001575 mCurMethodId = null;
1576 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001577 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001578 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001579
1580 void requestClientSessionLocked(ClientState cs) {
1581 if (!cs.sessionRequested) {
1582 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1583 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1584 cs.sessionRequested = true;
1585 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1586 MSG_CREATE_SESSION, mCurMethod, channels[1],
1587 new MethodCallback(this, mCurMethod, channels[0])));
1588 }
1589 }
1590
1591 void clearClientSessionLocked(ClientState cs) {
1592 finishSessionLocked(cs.curSession);
1593 cs.curSession = null;
1594 cs.sessionRequested = false;
1595 }
1596
1597 private void finishSessionLocked(SessionState sessionState) {
1598 if (sessionState != null) {
1599 if (sessionState.session != null) {
1600 try {
1601 sessionState.session.finishSession();
1602 } catch (RemoteException e) {
1603 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001604 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001605 }
1606 sessionState.session = null;
1607 }
1608 if (sessionState.channel != null) {
1609 sessionState.channel.dispose();
1610 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001611 }
1612 }
1613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001615 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 if (mCurMethod != null) {
1617 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001618 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001620
Jeff Brownc28867a2013-03-26 15:42:39 -07001621 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001622 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 mCurMethod = null;
1624 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001625 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001626 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001629
satoke7c6998e2011-06-03 17:57:59 +09001630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 public void onServiceDisconnected(ComponentName name) {
1632 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001633 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 + " mCurIntent=" + mCurIntent);
1635 if (mCurMethod != null && mCurIntent != null
1636 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001637 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 // We consider this to be a new bind attempt, since the system
1639 // should now try to restart the service for us.
1640 mLastBindTime = SystemClock.uptimeMillis();
1641 mShowRequested = mInputShown;
1642 mInputShown = false;
1643 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001644 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1645 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1646 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 }
1648 }
1649 }
1650 }
1651
satokf9f01002011-05-19 21:31:50 +09001652 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1654 long ident = Binder.clearCallingIdentity();
1655 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001657 if (!calledWithValidToken(token)) {
1658 final int uid = Binder.getCallingUid();
1659 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1660 + " token:" + token);
1661 return;
1662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001664 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001665 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001666 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001669 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001670 CharSequence contentDescription = null;
1671 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001672 // Use PackageManager to load label
1673 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001674 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001675 mIPackageManager.getApplicationInfo(packageName, 0,
1676 mSettings.getCurrentUserId()));
1677 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001678 /* ignore */
1679 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001680 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001681 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001682 contentDescription != null
1683 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001684 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 }
1687 }
1688 } finally {
1689 Binder.restoreCallingIdentity(ident);
1690 }
1691 }
1692
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001693 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001694 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001695 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001696 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001697 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001698 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001699 // When physical keyboard is attached, we show the ime switcher (or notification if
1700 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1701 // exists in the IME switcher dialog. Might be OK to remove this condition once
1702 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1703 return true;
1704 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001705 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001706
1707 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1708 final int N = imis.size();
1709 if (N > 2) return true;
1710 if (N < 1) return false;
1711 int nonAuxCount = 0;
1712 int auxCount = 0;
1713 InputMethodSubtype nonAuxSubtype = null;
1714 InputMethodSubtype auxSubtype = null;
1715 for(int i = 0; i < N; ++i) {
1716 final InputMethodInfo imi = imis.get(i);
1717 final List<InputMethodSubtype> subtypes =
1718 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1719 final int subtypeCount = subtypes.size();
1720 if (subtypeCount == 0) {
1721 ++nonAuxCount;
1722 } else {
1723 for (int j = 0; j < subtypeCount; ++j) {
1724 final InputMethodSubtype subtype = subtypes.get(j);
1725 if (!subtype.isAuxiliary()) {
1726 ++nonAuxCount;
1727 nonAuxSubtype = subtype;
1728 } else {
1729 ++auxCount;
1730 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001731 }
1732 }
satok7cfc0ed2011-06-20 21:29:36 +09001733 }
1734 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001735 if (nonAuxCount > 1 || auxCount > 1) {
1736 return true;
1737 } else if (nonAuxCount == 1 && auxCount == 1) {
1738 if (nonAuxSubtype != null && auxSubtype != null
1739 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1740 || auxSubtype.overridesImplicitlyEnabledSubtype()
1741 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1742 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1743 return false;
1744 }
1745 return true;
1746 }
1747 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001748 }
1749
John Spurlocke0980502013-10-25 11:59:29 -04001750 private boolean isKeyguardLocked() {
1751 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1752 }
1753
satokdbf29502011-08-25 15:28:23 +09001754 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001755 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001756 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001757 if (!calledWithValidToken(token)) {
1758 final int uid = Binder.getCallingUid();
1759 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1760 + " token:" + token);
1761 return;
1762 }
1763
1764 synchronized (mMethodMap) {
1765 mImeWindowVis = vis;
1766 mBackDisposition = backDisposition;
1767 updateSystemUiLocked(token, vis, backDisposition);
1768 }
1769 }
1770
1771 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1772 synchronized (mMethodMap) {
1773 updateSystemUiLocked(token, vis, backDisposition);
1774 }
1775 }
1776
1777 // Caution! This method is called in this class. Handle multi-user carefully
1778 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1779 if (!calledWithValidToken(token)) {
1780 final int uid = Binder.getCallingUid();
1781 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1782 + " token:" + token);
1783 return;
1784 }
1785
1786 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1787 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001788 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001789 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001790 // apply policy for binder calls
1791 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1792 vis = 0;
satok06487a52010-10-29 11:37:18 +09001793 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001794 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1795 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1796 if (mStatusBar != null) {
1797 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1798 needsToShowImeSwitcher);
1799 }
1800 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1801 if (imi != null && needsToShowImeSwitcher) {
1802 // Used to load label
1803 final CharSequence title = mRes.getText(
1804 com.android.internal.R.string.select_input_method);
1805 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1806 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001807 mImeSwitcherNotification.setContentTitle(title)
1808 .setContentText(summary)
1809 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001810 try {
1811 if ((mNotificationManager != null)
1812 && !mIWindowManager.hasNavigationBar()) {
1813 if (DEBUG) {
1814 Slog.d(TAG, "--- show notification: label = " + summary);
1815 }
1816 mNotificationManager.notifyAsUser(null,
1817 com.android.internal.R.string.select_input_method,
1818 mImeSwitcherNotification.build(), UserHandle.ALL);
1819 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001820 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001821 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001822 }
1823 } else {
1824 if (mNotificationShown && mNotificationManager != null) {
1825 if (DEBUG) {
1826 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001827 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001828 mNotificationManager.cancelAsUser(null,
1829 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1830 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001831 }
satok06487a52010-10-29 11:37:18 +09001832 }
1833 } finally {
1834 Binder.restoreCallingIdentity(ident);
1835 }
1836 }
1837
satoke7c6998e2011-06-03 17:57:59 +09001838 @Override
satokf9f01002011-05-19 21:31:50 +09001839 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001840 if (!calledFromValidUser()) {
1841 return;
1842 }
satokf9f01002011-05-19 21:31:50 +09001843 synchronized (mMethodMap) {
1844 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1845 for (int i = 0; i < spans.length; ++i) {
1846 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001847 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001848 mSecureSuggestionSpans.put(ss, currentImi);
1849 }
1850 }
1851 }
1852 }
1853
satoke7c6998e2011-06-03 17:57:59 +09001854 @Override
satokf9f01002011-05-19 21:31:50 +09001855 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001856 if (!calledFromValidUser()) {
1857 return false;
1858 }
satokf9f01002011-05-19 21:31:50 +09001859 synchronized (mMethodMap) {
1860 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1861 // TODO: Do not send the intent if the process of the targetImi is already dead.
1862 if (targetImi != null) {
1863 final String[] suggestions = span.getSuggestions();
1864 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001865 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001866 final Intent intent = new Intent();
1867 // Ensures that only a class in the original IME package will receive the
1868 // notification.
satok42c5a162011-05-26 16:46:14 +09001869 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001870 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1871 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1872 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1873 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001874 final long ident = Binder.clearCallingIdentity();
1875 try {
1876 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1877 } finally {
1878 Binder.restoreCallingIdentity(ident);
1879 }
satokf9f01002011-05-19 21:31:50 +09001880 return true;
1881 }
1882 }
1883 return false;
1884 }
1885
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001886 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001887 updateInputMethodsFromSettingsLocked(enabledMayChange);
1888 updateKeyboardFromSettingsLocked();
1889 }
1890
1891 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001892 if (enabledMayChange) {
1893 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1894 for (int i=0; i<enabled.size(); i++) {
1895 // We allow the user to select "disabled until used" apps, so if they
1896 // are enabling one of those here we now need to make it enabled.
1897 InputMethodInfo imm = enabled.get(i);
1898 try {
1899 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1900 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1901 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001902 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001903 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001904 if (DEBUG) {
1905 Slog.d(TAG, "Update state(" + imm.getId()
1906 + "): DISABLED_UNTIL_USED -> DEFAULT");
1907 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001908 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1909 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001910 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1911 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001912 }
1913 } catch (RemoteException e) {
1914 }
1915 }
1916 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001917 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1918 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1919 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1920 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001921 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001922 // There is no input method selected, try to choose new applicable input method.
1923 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001924 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001925 }
1926 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001928 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001930 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001931 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
satokf3db1af2010-11-23 13:34:33 +09001933 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001934 } else {
1935 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001936 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001938 // Here is not the perfect place to reset the switching controller. Ideally
1939 // mSwitchingController and mSettings should be able to share the same state.
1940 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1941 // the same enabled IMEs list.
1942 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001943
1944 }
1945
1946 public void updateKeyboardFromSettingsLocked() {
1947 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1948 if (mSwitchingDialog != null
1949 && mSwitchingDialogTitleView != null
1950 && mSwitchingDialog.isShowing()) {
1951 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1952 com.android.internal.R.id.hard_keyboard_switch);
1953 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001956
Yohei Yukawab097b822015-12-01 10:43:08 -08001957 private void notifyInputMethodSubtypeChanged(final int userId,
1958 @Nullable final InputMethodInfo inputMethodInfo,
1959 @Nullable final InputMethodSubtype subtype) {
1960 final InputManagerInternal inputManagerInternal =
1961 LocalServices.getService(InputManagerInternal.class);
1962 if (inputManagerInternal != null) {
1963 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1964 }
1965 }
1966
satokab751aa2010-09-14 19:17:36 +09001967 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 InputMethodInfo info = mMethodMap.get(id);
1969 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001970 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001972
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001973 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001974 // We have already made sure that the package name belongs to the application's UID.
1975 // No further UID check is required.
1976 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1977 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001978 }
1979 }
1980
satokd81e9502012-05-21 12:58:45 +09001981 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001983 final int subtypeCount = info.getSubtypeCount();
1984 if (subtypeCount <= 0) {
1985 return;
satokcd7cd292010-11-20 15:46:23 +09001986 }
satokd81e9502012-05-21 12:58:45 +09001987 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1988 final InputMethodSubtype newSubtype;
1989 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1990 newSubtype = info.getSubtypeAt(subtypeId);
1991 } else {
1992 // If subtype is null, try to find the most applicable one from
1993 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001994 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001995 }
1996 if (newSubtype == null || oldSubtype == null) {
1997 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1998 + ", new subtype = " + newSubtype);
1999 return;
2000 }
2001 if (newSubtype != oldSubtype) {
2002 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2003 if (mCurMethod != null) {
2004 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002005 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002006 mCurMethod.changeInputMethodSubtype(newSubtype);
2007 } catch (RemoteException e) {
2008 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002009 return;
satokab751aa2010-09-14 19:17:36 +09002010 }
2011 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002012 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 return;
2015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002016
satokd81e9502012-05-21 12:58:45 +09002017 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 final long ident = Binder.clearCallingIdentity();
2019 try {
satokab751aa2010-09-14 19:17:36 +09002020 // Set a subtype to this input method.
2021 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002022 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2023 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2024 // because mCurMethodId is stored as a history in
2025 // setSelectedInputMethodAndSubtypeLocked().
2026 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027
2028 if (ActivityManagerNative.isSystemReady()) {
2029 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002030 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002032 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002034 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 } finally {
2036 Binder.restoreCallingIdentity(ident);
2037 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002038
2039 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2040 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002042
satok42c5a162011-05-26 16:46:14 +09002043 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002044 public boolean showSoftInput(IInputMethodClient client, int flags,
2045 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002046 if (!calledFromValidUser()) {
2047 return false;
2048 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002049 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 long ident = Binder.clearCallingIdentity();
2051 try {
2052 synchronized (mMethodMap) {
2053 if (mCurClient == null || client == null
2054 || mCurClient.client.asBinder() != client.asBinder()) {
2055 try {
2056 // We need to check if this is the current client with
2057 // focus in the window manager, to allow this call to
2058 // be made before input is started in it.
2059 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002060 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002061 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
2063 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002064 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 }
2066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002067
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002068 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002069 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
2071 } finally {
2072 Binder.restoreCallingIdentity(ident);
2073 }
2074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002075
The Android Open Source Project4df24232009-03-05 14:34:35 -08002076 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002078 if (mAccessibilityRequestingNoSoftKeyboard) {
2079 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 }
Anna Galusza9b278112016-01-04 11:37:37 -08002081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2083 mShowExplicitlyRequested = true;
2084 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002085 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2086 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002088
Dianne Hackborncc278702009-09-02 23:07:23 -07002089 if (!mSystemReady) {
2090 return false;
2091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002092
The Android Open Source Project4df24232009-03-05 14:34:35 -08002093 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002095 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002096 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2097 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2098 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002100 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002101 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002102 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002103 | Context.BIND_TREAT_LIKE_ACTIVITY
2104 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002105 mVisibleBound = true;
2106 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002107 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002109 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 // The client has asked to have the input method shown, but
2111 // we have been sitting here too long with a connection to the
2112 // service and no interface received, so let's disconnect/connect
2113 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002114 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002116 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002118 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002119 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002120 } else {
2121 if (DEBUG) {
2122 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2123 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002126
The Android Open Source Project4df24232009-03-05 14:34:35 -08002127 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002129
satok42c5a162011-05-26 16:46:14 +09002130 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002131 public boolean hideSoftInput(IInputMethodClient client, int flags,
2132 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002133 if (!calledFromValidUser()) {
2134 return false;
2135 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002136 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 long ident = Binder.clearCallingIdentity();
2138 try {
2139 synchronized (mMethodMap) {
2140 if (mCurClient == null || client == null
2141 || mCurClient.client.asBinder() != client.asBinder()) {
2142 try {
2143 // We need to check if this is the current client with
2144 // focus in the window manager, to allow this call to
2145 // be made before input is started in it.
2146 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002147 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2148 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002149 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002152 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
2154 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002155
Joe Onorato8a9b2202010-02-26 18:56:32 -08002156 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002157 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 }
2159 } finally {
2160 Binder.restoreCallingIdentity(ident);
2161 }
2162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002163
The Android Open Source Project4df24232009-03-05 14:34:35 -08002164 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2166 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002167 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 -08002168 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002171 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 -08002172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002174
2175 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2176 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2177 // to be updated with the new value sent from IME process. Even in such a transient state
2178 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2179 // application process as a valid request, and have even promised such a behavior with CTS
2180 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2181 // IMMS#InputShown indicates that the software keyboard is shown.
2182 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2183 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2184 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002185 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002186 if (shouldHideSoftInput) {
2187 // The IME will report its visible state again after the following message finally
2188 // delivered to the IME process as an IPC. Hence the inconsistency between
2189 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2190 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002191 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2192 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2193 res = true;
2194 } else {
2195 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002197 if (mHaveConnection && mVisibleBound) {
2198 mContext.unbindService(mVisibleConnection);
2199 mVisibleBound = false;
2200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 mInputShown = false;
2202 mShowRequested = false;
2203 mShowExplicitlyRequested = false;
2204 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002205 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002207
satok42c5a162011-05-26 16:46:14 +09002208 @Override
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002209 public InputBindResult windowGainedFocus(
2210 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2211 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
2212 int windowFlags, EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002213 // Needs to check the validity before clearing calling identity
2214 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002215 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 long ident = Binder.clearCallingIdentity();
2217 try {
2218 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002219 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2220 + InputMethodClient.getStartInputReason(startInputReason)
2221 + " client=" + client.asBinder()
2222 + " inputContext=" + inputContext
2223 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002224 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002226 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002227
Dianne Hackborn7663d802012-02-24 13:08:49 -08002228 ClientState cs = mClients.get(client.asBinder());
2229 if (cs == null) {
2230 throw new IllegalArgumentException("unknown client "
2231 + client.asBinder());
2232 }
2233
2234 try {
2235 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2236 // Check with the window manager to make sure this client actually
2237 // has a window with focus. If not, reject. This is thread safe
2238 // because if the focus changes some time before or after, the
2239 // next client receiving focus that has any interest in input will
2240 // be calling through here after that change happens.
2241 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2242 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2243 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002245 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002247
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002248 if (!calledFromValidUser) {
2249 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2250 Slog.w(TAG, "If you want to interect with IME, you need "
2251 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2252 hideCurrentInputLocked(0, null);
2253 return null;
2254 }
2255
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002256 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002257 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002258 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002259 if (attribute != null) {
2260 return startInputUncheckedLocked(cs, inputContext, attribute,
2261 controlFlags);
2262 }
2263 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002264 }
2265 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002266 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002267
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002268 // Should we auto-show the IME even if the caller has not
2269 // specified what should be done with it?
2270 // We only do this automatically if the window can resize
2271 // to accommodate the IME (so what the user sees will give
2272 // them good context without input information being obscured
2273 // by the IME) or if running on a large screen where there
2274 // is more room for the target window + IME.
2275 final boolean doAutoShow =
2276 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2277 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2278 || mRes.getConfiguration().isLayoutSizeAtLeast(
2279 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002280 final boolean isTextEditor =
2281 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2282
2283 // We want to start input before showing the IME, but after closing
2284 // it. We want to do this after closing it to help the IME disappear
2285 // more quickly (not get stuck behind it initializing itself for the
2286 // new focused input, even if its window wants to hide the IME).
2287 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2290 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002291 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2293 // There is no focus view, and this window will
2294 // be behind any soft input window, so hide the
2295 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002296 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002297 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002299 } else if (isTextEditor && doAutoShow && (softInputMode &
2300 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 // There is a focus view, and we are navigating forward
2302 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002303 // We only do this automatically if the window can resize
2304 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002305 // them good context without input information being obscured
2306 // by the IME) or if running on a large screen where there
2307 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002308 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002309 if (attribute != null) {
2310 res = startInputUncheckedLocked(cs, inputContext, attribute,
2311 controlFlags);
2312 didStart = true;
2313 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002314 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
2316 break;
2317 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2318 // Do nothing.
2319 break;
2320 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2321 if ((softInputMode &
2322 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002323 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002324 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 }
2326 break;
2327 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002328 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002329 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 break;
2331 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2332 if ((softInputMode &
2333 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002334 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002335 if (attribute != null) {
2336 res = startInputUncheckedLocked(cs, inputContext, attribute,
2337 controlFlags);
2338 didStart = true;
2339 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002340 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 }
2342 break;
2343 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002344 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002345 if (attribute != null) {
2346 res = startInputUncheckedLocked(cs, inputContext, attribute,
2347 controlFlags);
2348 didStart = true;
2349 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002350 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 break;
2352 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002353
2354 if (!didStart && attribute != null) {
2355 res = startInputUncheckedLocked(cs, inputContext, attribute,
2356 controlFlags);
2357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359 } finally {
2360 Binder.restoreCallingIdentity(ident);
2361 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002362
2363 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002365
satok42c5a162011-05-26 16:46:14 +09002366 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002367 public void showInputMethodPickerFromClient(
2368 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002369 if (!calledFromValidUser()) {
2370 return;
2371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 synchronized (mMethodMap) {
2373 if (mCurClient == null || client == null
2374 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002375 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002376 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 }
2378
satok440aab52010-11-25 09:43:11 +09002379 // Always call subtype picker, because subtype picker is a superset of input method
2380 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002381 mHandler.sendMessage(mCaller.obtainMessageI(
2382 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002383 }
2384 }
2385
satok42c5a162011-05-26 16:46:14 +09002386 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002388 if (!calledFromValidUser()) {
2389 return;
2390 }
satok28203512010-11-24 11:06:49 +09002391 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2392 }
2393
satok42c5a162011-05-26 16:46:14 +09002394 @Override
satok28203512010-11-24 11:06:49 +09002395 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002396 if (!calledFromValidUser()) {
2397 return;
2398 }
satok28203512010-11-24 11:06:49 +09002399 synchronized (mMethodMap) {
2400 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002401 setInputMethodWithSubtypeIdLocked(token, id,
2402 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2403 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002404 } else {
2405 setInputMethod(token, id);
2406 }
2407 }
satokab751aa2010-09-14 19:17:36 +09002408 }
2409
satok42c5a162011-05-26 16:46:14 +09002410 @Override
satokb416a712010-11-25 20:42:14 +09002411 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002412 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002413 if (!calledFromValidUser()) {
2414 return;
2415 }
satokb416a712010-11-25 20:42:14 +09002416 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002417 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2418 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002419 }
2420 }
2421
satok4fc87d62011-05-20 16:13:43 +09002422 @Override
satok735cf382010-11-11 20:40:09 +09002423 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002424 if (!calledFromValidUser()) {
2425 return false;
2426 }
satok735cf382010-11-11 20:40:09 +09002427 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002428 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002429 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002430 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002431 lastImi = mMethodMap.get(lastIme.first);
2432 } else {
2433 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002434 }
satok4fc87d62011-05-20 16:13:43 +09002435 String targetLastImiId = null;
2436 int subtypeId = NOT_A_SUBTYPE_ID;
2437 if (lastIme != null && lastImi != null) {
2438 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2439 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2440 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2441 : mCurrentSubtype.hashCode();
2442 // If the last IME is the same as the current IME and the last subtype is not
2443 // defined, there is no need to switch to the last IME.
2444 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2445 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002446 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002447 }
2448 }
2449
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002450 if (TextUtils.isEmpty(targetLastImiId)
2451 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002452 // This is a safety net. If the currentSubtype can't be added to the history
2453 // and the framework couldn't find the last ime, we will make the last ime be
2454 // the most applicable enabled keyboard subtype of the system imes.
2455 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2456 if (enabled != null) {
2457 final int N = enabled.size();
2458 final String locale = mCurrentSubtype == null
2459 ? mRes.getConfiguration().locale.toString()
2460 : mCurrentSubtype.getLocale();
2461 for (int i = 0; i < N; ++i) {
2462 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002463 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002464 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002465 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2466 InputMethodUtils.getSubtypes(imi),
2467 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002468 if (keyboardSubtype != null) {
2469 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002470 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002471 imi, keyboardSubtype.hashCode());
2472 if(keyboardSubtype.getLocale().equals(locale)) {
2473 break;
2474 }
2475 }
2476 }
2477 }
2478 }
2479 }
2480
2481 if (!TextUtils.isEmpty(targetLastImiId)) {
2482 if (DEBUG) {
2483 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2484 + ", from: " + mCurMethodId + ", " + subtypeId);
2485 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002486 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002487 return true;
2488 } else {
2489 return false;
2490 }
satok735cf382010-11-11 20:40:09 +09002491 }
2492 }
2493
satoke7c6998e2011-06-03 17:57:59 +09002494 @Override
satok688bd472012-02-09 20:09:17 +09002495 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002496 if (!calledFromValidUser()) {
2497 return false;
2498 }
satok688bd472012-02-09 20:09:17 +09002499 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002500 if (!calledWithValidToken(token)) {
2501 final int uid = Binder.getCallingUid();
2502 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2503 + " token:" + token);
2504 return false;
2505 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002506 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002507 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2508 if (nextSubtype == null) {
2509 return false;
2510 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002511 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2512 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002513 return true;
2514 }
2515 }
2516
2517 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002518 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2519 if (!calledFromValidUser()) {
2520 return false;
2521 }
2522 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002523 if (!calledWithValidToken(token)) {
2524 final int uid = Binder.getCallingUid();
2525 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2526 + "token. uid:" + uid + " token:" + token);
2527 return false;
2528 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002529 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002530 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2531 if (nextSubtype == null) {
2532 return false;
2533 }
2534 return true;
2535 }
2536 }
2537
2538 @Override
satok68f1b782011-04-11 14:26:04 +09002539 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002540 if (!calledFromValidUser()) {
2541 return null;
2542 }
satok68f1b782011-04-11 14:26:04 +09002543 synchronized (mMethodMap) {
2544 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2545 // TODO: Handle the case of the last IME with no subtypes
2546 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2547 || TextUtils.isEmpty(lastIme.second)) return null;
2548 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2549 if (lastImi == null) return null;
2550 try {
2551 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002552 final int lastSubtypeId =
2553 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002554 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2555 return null;
2556 }
2557 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002558 } catch (NumberFormatException e) {
2559 return null;
2560 }
2561 }
2562 }
2563
satoke7c6998e2011-06-03 17:57:59 +09002564 @Override
satokee5e77c2011-09-02 18:50:15 +09002565 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002566 if (!calledFromValidUser()) {
2567 return;
2568 }
satok91e88122011-07-18 11:11:42 +09002569 // By this IPC call, only a process which shares the same uid with the IME can add
2570 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002571 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002572 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002573 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002574 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002575 final String[] packageInfos;
2576 try {
2577 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2578 } catch (RemoteException e) {
2579 Slog.e(TAG, "Failed to get package infos");
2580 return;
2581 }
satok91e88122011-07-18 11:11:42 +09002582 if (packageInfos != null) {
2583 final int packageNum = packageInfos.length;
2584 for (int i = 0; i < packageNum; ++i) {
2585 if (packageInfos[i].equals(imi.getPackageName())) {
2586 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002587 final long ident = Binder.clearCallingIdentity();
2588 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002589 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002590 } finally {
2591 Binder.restoreCallingIdentity(ident);
2592 }
satokee5e77c2011-09-02 18:50:15 +09002593 return;
satok91e88122011-07-18 11:11:42 +09002594 }
2595 }
2596 }
satoke7c6998e2011-06-03 17:57:59 +09002597 }
satokee5e77c2011-09-02 18:50:15 +09002598 return;
satoke7c6998e2011-06-03 17:57:59 +09002599 }
2600
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002601 @Override
2602 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002603 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002604 }
2605
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002606 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002607 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002608 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002609 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002610 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002611 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002612 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2613 if (DEBUG) {
2614 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2615 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2616 + " actual: " + sequenceNumber);
2617 }
2618 return;
2619 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002620 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2621 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002622 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002623 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002624 }
2625 }
2626
satok28203512010-11-24 11:06:49 +09002627 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002629 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2630 }
2631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002633 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2634 if (token == null) {
2635 if (mContext.checkCallingOrSelfPermission(
2636 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2637 != PackageManager.PERMISSION_GRANTED) {
2638 throw new SecurityException(
2639 "Using null token requires permission "
2640 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002642 } else if (mCurToken != token) {
2643 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2644 + " token: " + token);
2645 return;
2646 }
2647
2648 final long ident = Binder.clearCallingIdentity();
2649 try {
2650 setInputMethodLocked(id, subtypeId);
2651 } finally {
2652 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 }
2654 }
2655
satok42c5a162011-05-26 16:46:14 +09002656 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002658 if (!calledFromValidUser()) {
2659 return;
2660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002662 if (!calledWithValidToken(token)) {
2663 final int uid = Binder.getCallingUid();
2664 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2665 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 return;
2667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 long ident = Binder.clearCallingIdentity();
2669 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002670 hideCurrentInputLocked(flags, null);
2671 } finally {
2672 Binder.restoreCallingIdentity(ident);
2673 }
2674 }
2675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002676
satok42c5a162011-05-26 16:46:14 +09002677 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002678 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002679 if (!calledFromValidUser()) {
2680 return;
2681 }
The Android Open Source Project4df24232009-03-05 14:34:35 -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 showMySoftInput due to an invalid token. uid:"
2686 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002687 return;
2688 }
2689 long ident = Binder.clearCallingIdentity();
2690 try {
2691 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 } finally {
2693 Binder.restoreCallingIdentity(ident);
2694 }
2695 }
2696 }
2697
2698 void setEnabledSessionInMainThread(SessionState session) {
2699 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002700 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002702 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002703 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 } catch (RemoteException e) {
2705 }
2706 }
2707 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002708 if (mEnabledSession != null && mEnabledSession.session != null) {
2709 try {
2710 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2711 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2712 } catch (RemoteException e) {
2713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 }
2715 }
2716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002717
satok42c5a162011-05-26 16:46:14 +09002718 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002720 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002722 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002723 final boolean showAuxSubtypes;
2724 switch (msg.arg1) {
2725 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2726 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2727 // implemented so that auxiliary subtypes will be excluded when the soft
2728 // keyboard is invisible.
2729 showAuxSubtypes = mInputShown;
2730 break;
2731 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2732 showAuxSubtypes = true;
2733 break;
2734 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2735 showAuxSubtypes = false;
2736 break;
2737 default:
2738 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2739 return false;
2740 }
2741 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002742 return true;
2743
satok47a44912010-10-06 16:03:58 +09002744 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002745 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002746 return true;
2747
2748 case MSG_SHOW_IM_CONFIG:
2749 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002750 return true;
2751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 case MSG_UNBIND_INPUT:
2755 try {
2756 ((IInputMethod)msg.obj).unbindInput();
2757 } catch (RemoteException e) {
2758 // There is nothing interesting about the method dying.
2759 }
2760 return true;
2761 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002762 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 try {
2764 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2765 } catch (RemoteException e) {
2766 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002767 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 return true;
2769 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002770 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002772 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002773 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002774 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 } catch (RemoteException e) {
2776 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002777 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 return true;
2779 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002780 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002782 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002783 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002784 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 } catch (RemoteException e) {
2786 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002787 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 return true;
2789 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002790 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002792 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2794 } catch (RemoteException e) {
2795 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002796 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002798 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002799 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002800 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002801 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002803 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002805 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002806 // Dispose the channel if the input method is not local to this process
2807 // because the remote proxy will get its own copy when unparceled.
2808 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002809 channel.dispose();
2810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002812 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002818 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 try {
2820 SessionState session = (SessionState)args.arg1;
2821 setEnabledSessionInMainThread(session);
2822 session.method.startInput((IInputContext)args.arg2,
2823 (EditorInfo)args.arg3);
2824 } catch (RemoteException e) {
2825 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002826 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 return true;
2828 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002829 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 try {
2831 SessionState session = (SessionState)args.arg1;
2832 setEnabledSessionInMainThread(session);
2833 session.method.restartInput((IInputContext)args.arg2,
2834 (EditorInfo)args.arg3);
2835 } catch (RemoteException e) {
2836 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002837 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002841
Yohei Yukawa33e81792015-11-17 21:14:42 -08002842 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002844 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 } catch (RemoteException e) {
2846 // There is nothing interesting about the last client dying.
2847 }
2848 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002849 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002850 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002851 IInputMethodClient client = (IInputMethodClient)args.arg1;
2852 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002854 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002856 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002857 } finally {
2858 // Dispose the channel if the input method is not local to this process
2859 // because the remote proxy will get its own copy when unparceled.
2860 if (res.channel != null && Binder.isProxy(client)) {
2861 res.channel.dispose();
2862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002864 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002866 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002867 case MSG_SET_ACTIVE:
2868 try {
2869 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2870 } catch (RemoteException e) {
2871 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2872 + ((ClientState)msg.obj).pid + " uid "
2873 + ((ClientState)msg.obj).uid);
2874 }
2875 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002876 case MSG_SET_INTERACTIVE:
2877 handleSetInteractive(msg.arg1 != 0);
2878 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002879 case MSG_SWITCH_IME:
2880 handleSwitchInputMethod(msg.arg1 != 0);
2881 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002882 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2883 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002884 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002885 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002886 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002887 } catch (RemoteException e) {
2888 Slog.w(TAG, "Got RemoteException sending "
2889 + "setUserActionNotificationSequenceNumber("
2890 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002891 + clientState.pid + " uid "
2892 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002893 }
2894 return true;
2895 }
satok01038492012-04-09 21:08:27 +09002896
2897 // --------------------------------------------------------------
2898 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002899 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002900 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 }
2902 return false;
2903 }
2904
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002905 private void handleSetInteractive(final boolean interactive) {
2906 synchronized (mMethodMap) {
2907 mIsInteractive = interactive;
2908 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2909
2910 // Inform the current client of the change in active status
2911 if (mCurClient != null && mCurClient.client != null) {
2912 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2913 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2914 }
2915 }
2916 }
2917
Yohei Yukawaae61f712015-12-09 13:00:10 -08002918 private void handleSwitchInputMethod(final boolean forwardDirection) {
2919 synchronized (mMethodMap) {
2920 // TODO: Support forwardDirection.
2921 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2922 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2923 if (nextSubtype == null) {
2924 return;
2925 }
2926 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2927 }
2928 }
2929
satokdc9ddae2011-10-06 12:22:36 +09002930 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002931 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2932 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002933 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002934 if (DEBUG) {
2935 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2936 }
satok723a27e2010-11-11 14:58:11 +09002937 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002938 return true;
2939 }
2940
2941 return false;
2942 }
2943
Yohei Yukawa94e33302016-02-12 19:37:03 -08002944 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002945 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002946 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002947 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002948 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08002949 mMethodList.clear();
2950 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002951
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002952 // Use for queryIntentServicesAsUser
2953 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002955 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002957 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2958 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002959
satoke7c6998e2011-06-03 17:57:59 +09002960 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2961 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 for (int i = 0; i < services.size(); ++i) {
2963 ResolveInfo ri = services.get(i);
2964 ServiceInfo si = ri.serviceInfo;
2965 ComponentName compName = new ComponentName(si.packageName, si.name);
2966 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2967 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002968 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 + ": it does not require the permission "
2970 + android.Manifest.permission.BIND_INPUT_METHOD);
2971 continue;
2972 }
2973
Joe Onorato8a9b2202010-02-26 18:56:32 -08002974 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975
2976 try {
satoke7c6998e2011-06-03 17:57:59 +09002977 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08002978 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002979 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08002980 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981
2982 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002983 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002985 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002986 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002987 }
2988 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002989
Yohei Yukawa859df052016-02-17 07:56:46 -08002990 // TODO: The following code should find better place to live.
2991 if (!resetDefaultEnabledIme) {
2992 boolean enabledImeFound = false;
2993 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
2994 final int N = enabledImes.size();
2995 for (int i = 0; i < N; ++i) {
2996 final InputMethodInfo imi = enabledImes.get(i);
2997 if (mMethodList.contains(imi)) {
2998 enabledImeFound = true;
2999 break;
3000 }
3001 }
3002 if (!enabledImeFound) {
3003 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3004 resetDefaultEnabledIme = true;
3005 resetSelectedInputMethodAndSubtypeLocked("");
3006 }
3007 }
3008
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003009 if (resetDefaultEnabledIme) {
3010 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003011 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003012 final int N = defaultEnabledIme.size();
3013 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003014 final InputMethodInfo imi = defaultEnabledIme.get(i);
3015 if (DEBUG) {
3016 Slog.d(TAG, "--- enable ime = " + imi);
3017 }
3018 setInputMethodEnabledLocked(imi.getId(), true);
3019 }
3020 }
3021
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003022 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003023 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003024 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003025 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3026 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003027 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003028 }
3029 } else {
3030 // Double check that the default IME is certainly enabled.
3031 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003032 }
3033 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003034 // Here is not the perfect place to reset the switching controller. Ideally
3035 // mSwitchingController and mSettings should be able to share the same state.
3036 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3037 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003038 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003042
satok217f5482010-12-15 05:19:19 +09003043 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003044 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003045 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003046 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3047 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003048 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003049 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003050 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003051 final int userId;
3052 synchronized (mMethodMap) {
3053 userId = mSettings.getCurrentUserId();
3054 }
3055 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003056 }
3057
3058 private void showConfigureInputMethods() {
3059 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3060 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3061 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3062 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003063 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003064 }
3065
satok2c93efc2012-04-02 19:33:47 +09003066 private boolean isScreenLocked() {
3067 return mKeyguardManager != null
3068 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3069 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003070
Seigo Nonaka14e13912015-05-06 21:04:13 -07003071 private void showInputMethodMenu(boolean showAuxSubtypes) {
3072 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003075 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003076
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003077 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003078 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003079 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003080
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003081 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003082 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003083 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3084 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003085 if (immis == null || immis.size() == 0) {
3086 return;
3087 }
3088
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003089 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090
satok688bd472012-02-09 20:09:17 +09003091 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003092 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003093 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003094
satokc3690562012-01-10 20:14:43 +09003095 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003096 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003097 if (currentSubtype != null) {
3098 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003099 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3100 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003101 }
3102 }
3103
Ken Wakasa761eb372011-03-04 19:06:18 +09003104 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003105 mIms = new InputMethodInfo[N];
3106 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003107 int checkedItem = 0;
3108 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003109 final ImeSubtypeListItem item = imList.get(i);
3110 mIms[i] = item.mImi;
3111 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003112 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003113 int subtypeId = mSubtypeIds[i];
3114 if ((subtypeId == NOT_A_SUBTYPE_ID)
3115 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3116 || (subtypeId == lastInputMethodSubtypeId)) {
3117 checkedItem = i;
3118 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003121
3122 final Context settingsContext = new ContextThemeWrapper(context,
3123 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3124
3125 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003126 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3127 @Override
3128 public void onCancel(DialogInterface dialog) {
3129 hideInputMethodMenu();
3130 }
3131 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003132
3133 final Context dialogContext = mDialogBuilder.getContext();
3134 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3135 com.android.internal.R.styleable.DialogPreference,
3136 com.android.internal.R.attr.alertDialogStyle, 0);
3137 final Drawable dialogIcon = a.getDrawable(
3138 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3139 a.recycle();
3140
3141 mDialogBuilder.setIcon(dialogIcon);
3142
Yohei Yukawad34e1482016-02-11 08:03:52 -08003143 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003144 final View tv = inflater.inflate(
3145 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3146 mDialogBuilder.setCustomTitle(tv);
3147
3148 // Setup layout for a toggle switch of the hardware keyboard
3149 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003150 mSwitchingDialogTitleView
3151 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003152 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003153 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003154 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003155 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003156 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003157 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3158 @Override
3159 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003160 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003161 // Ensure that the input method dialog is dismissed when changing
3162 // the hardware keyboard state.
3163 hideInputMethodMenu();
3164 }
3165 });
3166
Alan Viverette505e3ab2014-11-24 15:22:11 -08003167 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003168 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3169 final OnClickListener choiceListener = new OnClickListener() {
3170 @Override
3171 public void onClick(final DialogInterface dialog, final int which) {
3172 synchronized (mMethodMap) {
3173 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3174 || mSubtypeIds.length <= which) {
3175 return;
satok01038492012-04-09 21:08:27 +09003176 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003177 final InputMethodInfo im = mIms[which];
3178 int subtypeId = mSubtypeIds[which];
3179 adapter.mCheckedItem = which;
3180 adapter.notifyDataSetChanged();
3181 hideInputMethodMenu();
3182 if (im != null) {
3183 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3184 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003185 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003186 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003188 }
3189 }
3190 };
3191 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003193 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003194 final OnClickListener positiveListener = new OnClickListener() {
3195 @Override
3196 public void onClick(DialogInterface dialog, int whichButton) {
3197 showConfigureInputMethods();
3198 }
3199 };
satok82beadf2010-12-27 19:03:06 +09003200 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003201 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003204 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 mSwitchingDialog.getWindow().setType(
3206 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003207 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3208 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003209 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003210 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 mSwitchingDialog.show();
3212 }
3213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003214
Ken Wakasa05dbb652011-08-22 15:22:43 +09003215 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3216 private final LayoutInflater mInflater;
3217 private final int mTextViewResourceId;
3218 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003219 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003220 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3221 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3222 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003223
Ken Wakasa05dbb652011-08-22 15:22:43 +09003224 mTextViewResourceId = textViewResourceId;
3225 mItemsList = itemsList;
3226 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003227 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003228 }
3229
3230 @Override
3231 public View getView(int position, View convertView, ViewGroup parent) {
3232 final View view = convertView != null ? convertView
3233 : mInflater.inflate(mTextViewResourceId, null);
3234 if (position < 0 || position >= mItemsList.size()) return view;
3235 final ImeSubtypeListItem item = mItemsList.get(position);
3236 final CharSequence imeName = item.mImeName;
3237 final CharSequence subtypeName = item.mSubtypeName;
3238 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3239 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3240 if (TextUtils.isEmpty(subtypeName)) {
3241 firstTextView.setText(imeName);
3242 secondTextView.setVisibility(View.GONE);
3243 } else {
3244 firstTextView.setText(subtypeName);
3245 secondTextView.setText(imeName);
3246 secondTextView.setVisibility(View.VISIBLE);
3247 }
3248 final RadioButton radioButton =
3249 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3250 radioButton.setChecked(position == mCheckedItem);
3251 return view;
3252 }
3253 }
3254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003256 synchronized (mMethodMap) {
3257 hideInputMethodMenuLocked();
3258 }
3259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003260
The Android Open Source Project10592532009-03-18 17:39:46 -07003261 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003262 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263
The Android Open Source Project10592532009-03-18 17:39:46 -07003264 if (mSwitchingDialog != null) {
3265 mSwitchingDialog.dismiss();
3266 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003268
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003269 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003270 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003271 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003275
satok42c5a162011-05-26 16:46:14 +09003276 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003278 // TODO: Make this work even for non-current users?
3279 if (!calledFromValidUser()) {
3280 return false;
3281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 synchronized (mMethodMap) {
3283 if (mContext.checkCallingOrSelfPermission(
3284 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3285 != PackageManager.PERMISSION_GRANTED) {
3286 throw new SecurityException(
3287 "Requires permission "
3288 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3289 }
Anna Galusza9b278112016-01-04 11:37:37 -08003290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 long ident = Binder.clearCallingIdentity();
3292 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003293 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 } finally {
3295 Binder.restoreCallingIdentity(ident);
3296 }
3297 }
3298 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003299
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003300 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3301 // Make sure this is a valid input method.
3302 InputMethodInfo imm = mMethodMap.get(id);
3303 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003304 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003305 }
3306
satokd87c2592010-09-29 11:52:06 +09003307 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3308 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003309
satokd87c2592010-09-29 11:52:06 +09003310 if (enabled) {
3311 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3312 if (pair.first.equals(id)) {
3313 // We are enabling this input method, but it is already enabled.
3314 // Nothing to do. The previous state was enabled.
3315 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003316 }
3317 }
satokd87c2592010-09-29 11:52:06 +09003318 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3319 // Previous state was disabled.
3320 return false;
3321 } else {
3322 StringBuilder builder = new StringBuilder();
3323 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3324 builder, enabledInputMethodsList, id)) {
3325 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003326 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003327 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3328 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3329 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003330 }
3331 // Previous state was enabled.
3332 return true;
3333 } else {
3334 // We are disabling the input method but it is already disabled.
3335 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003336 return false;
3337 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003338 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003339 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003340
satok723a27e2010-11-11 14:58:11 +09003341 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3342 boolean setSubtypeOnly) {
3343 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003344 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003345
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003346 mCurUserActionNotificationSequenceNumber =
3347 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3348 if (DEBUG) {
3349 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3350 + mCurUserActionNotificationSequenceNumber);
3351 }
3352
3353 if (mCurClient != null && mCurClient.client != null) {
3354 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3355 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003356 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003357 }
3358
satok723a27e2010-11-11 14:58:11 +09003359 // Set Subtype here
3360 if (imi == null || subtypeId < 0) {
3361 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003362 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003363 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003364 if (subtypeId < imi.getSubtypeCount()) {
3365 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3366 mSettings.putSelectedSubtype(subtype.hashCode());
3367 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003368 } else {
3369 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003370 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003371 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003372 }
satokab751aa2010-09-14 19:17:36 +09003373 }
satok723a27e2010-11-11 14:58:11 +09003374
Yohei Yukawa68645a62016-02-17 07:54:20 -08003375 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003376 // Set InputMethod here
3377 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3378 }
3379 }
3380
3381 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3382 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3383 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3384 // newDefaultIme is empty when there is no candidate for the selected IME.
3385 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3386 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3387 if (subtypeHashCode != null) {
3388 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003389 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003390 imi, Integer.valueOf(subtypeHashCode));
3391 } catch (NumberFormatException e) {
3392 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3393 }
3394 }
3395 }
3396 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003397 }
3398
satok4e4569d2010-11-19 18:45:53 +09003399 // If there are no selected shortcuts, tries finding the most applicable ones.
3400 private Pair<InputMethodInfo, InputMethodSubtype>
3401 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3402 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3403 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003404 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003405 boolean foundInSystemIME = false;
3406
3407 // Search applicable subtype for each InputMethodInfo
3408 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003409 final String imiId = imi.getId();
3410 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3411 continue;
3412 }
satokcd7cd292010-11-20 15:46:23 +09003413 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003414 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003415 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003416 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003417 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003418 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003419 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003420 }
satokdf31ae62011-01-15 06:19:44 +09003421 // 2. Search by the system locale from enabledSubtypes.
3422 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003423 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003424 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003425 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003426 }
satoka86f5e42011-09-02 17:12:42 +09003427 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003428 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003429 final ArrayList<InputMethodSubtype> subtypesForSearch =
3430 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003431 ? InputMethodUtils.getSubtypes(imi)
3432 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003433 // 4. Search by the current subtype's locale from all subtypes.
3434 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003435 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003436 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003437 }
3438 // 5. Search by the system locale from all subtypes.
3439 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003440 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003441 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003442 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003443 }
satokcd7cd292010-11-20 15:46:23 +09003444 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003445 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003446 // The current input method is the most applicable IME.
3447 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003448 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003449 break;
satok7599a7f2010-12-22 13:45:23 +09003450 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003451 // The system input method is 2nd applicable IME.
3452 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003453 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003454 if ((imi.getServiceInfo().applicationInfo.flags
3455 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3456 foundInSystemIME = true;
3457 }
satok4e4569d2010-11-19 18:45:53 +09003458 }
3459 }
3460 }
3461 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003462 if (mostApplicableIMI != null) {
3463 Slog.w(TAG, "Most applicable shortcut input method was:"
3464 + mostApplicableIMI.getId());
3465 if (mostApplicableSubtype != null) {
3466 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3467 + "," + mostApplicableSubtype.getMode() + ","
3468 + mostApplicableSubtype.getLocale());
3469 }
3470 }
satok4e4569d2010-11-19 18:45:53 +09003471 }
satokcd7cd292010-11-20 15:46:23 +09003472 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003473 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003474 } else {
3475 return null;
3476 }
3477 }
3478
satokab751aa2010-09-14 19:17:36 +09003479 /**
3480 * @return Return the current subtype of this input method.
3481 */
satok42c5a162011-05-26 16:46:14 +09003482 @Override
satokab751aa2010-09-14 19:17:36 +09003483 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003484 // TODO: Make this work even for non-current users?
3485 if (!calledFromValidUser()) {
3486 return null;
3487 }
3488 synchronized (mMethodMap) {
3489 return getCurrentInputMethodSubtypeLocked();
3490 }
3491 }
3492
3493 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003494 if (mCurMethodId == null) {
3495 return null;
3496 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003497 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003498 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3499 if (imi == null || imi.getSubtypeCount() == 0) {
3500 return null;
satok4e4569d2010-11-19 18:45:53 +09003501 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003502 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003503 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3504 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003505 if (subtypeId == NOT_A_SUBTYPE_ID) {
3506 // If there are no selected subtypes, the framework will try to find
3507 // the most applicable subtype from explicitly or implicitly enabled
3508 // subtypes.
3509 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003510 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003511 // If there is only one explicitly or implicitly enabled subtype,
3512 // just returns it.
3513 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3514 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3515 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003516 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003517 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003518 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003519 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003520 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003521 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3522 true);
satok4e4569d2010-11-19 18:45:53 +09003523 }
satok3ef8b292010-11-23 06:06:29 +09003524 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003525 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003526 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003527 }
3528 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003529 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003530 }
3531
satok4e4569d2010-11-19 18:45:53 +09003532 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003533 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003534 @Override
satok4e4569d2010-11-19 18:45:53 +09003535 public List getShortcutInputMethodsAndSubtypes() {
3536 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003537 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003538 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003539 // If there are no selected shortcut subtypes, the framework will try to find
3540 // the most applicable subtype from all subtypes whose mode is
3541 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003542 Pair<InputMethodInfo, InputMethodSubtype> info =
3543 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003544 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003545 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003546 ret.add(info.first);
3547 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003548 }
satok3da92232011-01-11 22:46:30 +09003549 return ret;
satokf3db1af2010-11-23 13:34:33 +09003550 }
satokf3db1af2010-11-23 13:34:33 +09003551 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3552 ret.add(imi);
3553 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3554 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003555 }
3556 }
satokf3db1af2010-11-23 13:34:33 +09003557 return ret;
satok4e4569d2010-11-19 18:45:53 +09003558 }
3559 }
3560
satok42c5a162011-05-26 16:46:14 +09003561 @Override
satokb66d2872010-11-10 01:04:04 +09003562 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003563 // TODO: Make this work even for non-current users?
3564 if (!calledFromValidUser()) {
3565 return false;
3566 }
satokb66d2872010-11-10 01:04:04 +09003567 synchronized (mMethodMap) {
3568 if (subtype != null && mCurMethodId != null) {
3569 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003570 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003571 if (subtypeId != NOT_A_SUBTYPE_ID) {
3572 setInputMethodLocked(mCurMethodId, subtypeId);
3573 return true;
3574 }
3575 }
3576 return false;
3577 }
3578 }
3579
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003580 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003581 private static class InputMethodFileManager {
3582 private static final String SYSTEM_PATH = "system";
3583 private static final String INPUT_METHOD_PATH = "inputmethod";
3584 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3585 private static final String NODE_SUBTYPES = "subtypes";
3586 private static final String NODE_SUBTYPE = "subtype";
3587 private static final String NODE_IMI = "imi";
3588 private static final String ATTR_ID = "id";
3589 private static final String ATTR_LABEL = "label";
3590 private static final String ATTR_ICON = "icon";
3591 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003592 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003593 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3594 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3595 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3596 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3597 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003598 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003599 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003600 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003601 if (methodMap == null) {
3602 throw new NullPointerException("methodMap is null");
3603 }
3604 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003605 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003606 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3607 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003608 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003609 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003610 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3611 }
3612 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3613 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3614 if (!subtypeFile.exists()) {
3615 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003616 writeAdditionalInputMethodSubtypes(
3617 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003618 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003619 readAdditionalInputMethodSubtypes(
3620 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003621 }
3622 }
3623
3624 private void deleteAllInputMethodSubtypes(String imiId) {
3625 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003626 mAdditionalSubtypesMap.remove(imiId);
3627 writeAdditionalInputMethodSubtypes(
3628 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003629 }
3630 }
3631
3632 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003633 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003634 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003635 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003636 final int N = additionalSubtypes.length;
3637 for (int i = 0; i < N; ++i) {
3638 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003639 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003640 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003641 } else {
3642 Slog.w(TAG, "Duplicated subtype definition found: "
3643 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003644 }
3645 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003646 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3647 writeAdditionalInputMethodSubtypes(
3648 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003649 }
3650 }
3651
3652 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3653 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003654 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003655 }
3656 }
3657
3658 private static void writeAdditionalInputMethodSubtypes(
3659 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3660 HashMap<String, InputMethodInfo> methodMap) {
3661 // Safety net for the case that this function is called before methodMap is set.
3662 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3663 FileOutputStream fos = null;
3664 try {
3665 fos = subtypesFile.startWrite();
3666 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003667 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003668 out.startDocument(null, true);
3669 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3670 out.startTag(null, NODE_SUBTYPES);
3671 for (String imiId : allSubtypes.keySet()) {
3672 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3673 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3674 continue;
3675 }
3676 out.startTag(null, NODE_IMI);
3677 out.attribute(null, ATTR_ID, imiId);
3678 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3679 final int N = subtypesList.size();
3680 for (int i = 0; i < N; ++i) {
3681 final InputMethodSubtype subtype = subtypesList.get(i);
3682 out.startTag(null, NODE_SUBTYPE);
3683 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3684 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3685 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003686 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3687 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003688 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3689 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3690 out.attribute(null, ATTR_IS_AUXILIARY,
3691 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3692 out.endTag(null, NODE_SUBTYPE);
3693 }
3694 out.endTag(null, NODE_IMI);
3695 }
3696 out.endTag(null, NODE_SUBTYPES);
3697 out.endDocument();
3698 subtypesFile.finishWrite(fos);
3699 } catch (java.io.IOException e) {
3700 Slog.w(TAG, "Error writing subtypes", e);
3701 if (fos != null) {
3702 subtypesFile.failWrite(fos);
3703 }
3704 }
3705 }
3706
3707 private static void readAdditionalInputMethodSubtypes(
3708 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3709 if (allSubtypes == null || subtypesFile == null) return;
3710 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003711 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003712 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003713 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003714 int type = parser.getEventType();
3715 // Skip parsing until START_TAG
3716 while ((type = parser.next()) != XmlPullParser.START_TAG
3717 && type != XmlPullParser.END_DOCUMENT) {}
3718 String firstNodeName = parser.getName();
3719 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3720 throw new XmlPullParserException("Xml doesn't start with subtypes");
3721 }
3722 final int depth =parser.getDepth();
3723 String currentImiId = null;
3724 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3725 while (((type = parser.next()) != XmlPullParser.END_TAG
3726 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3727 if (type != XmlPullParser.START_TAG)
3728 continue;
3729 final String nodeName = parser.getName();
3730 if (NODE_IMI.equals(nodeName)) {
3731 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3732 if (TextUtils.isEmpty(currentImiId)) {
3733 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3734 continue;
3735 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003736 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003737 allSubtypes.put(currentImiId, tempSubtypesArray);
3738 } else if (NODE_SUBTYPE.equals(nodeName)) {
3739 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3740 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3741 continue;
3742 }
3743 final int icon = Integer.valueOf(
3744 parser.getAttributeValue(null, ATTR_ICON));
3745 final int label = Integer.valueOf(
3746 parser.getAttributeValue(null, ATTR_LABEL));
3747 final String imeSubtypeLocale =
3748 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003749 final String languageTag =
3750 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003751 final String imeSubtypeMode =
3752 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3753 final String imeSubtypeExtraValue =
3754 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003755 final boolean isAuxiliary = "1".equals(String.valueOf(
3756 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003757 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3758 .setSubtypeNameResId(label)
3759 .setSubtypeIconResId(icon)
3760 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003761 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003762 .setSubtypeMode(imeSubtypeMode)
3763 .setSubtypeExtraValue(imeSubtypeExtraValue)
3764 .setIsAuxiliary(isAuxiliary)
3765 .build();
satoke7c6998e2011-06-03 17:57:59 +09003766 tempSubtypesArray.add(subtype);
3767 }
3768 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003769 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3770 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003771 return;
satoke7c6998e2011-06-03 17:57:59 +09003772 }
3773 }
3774 }
3775
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003776 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3777 @NonNull
3778 private final Handler mHandler;
3779
3780 LocalServiceImpl(@NonNull final Handler handler) {
3781 mHandler = handler;
3782 }
3783
3784 @Override
3785 public void setInteractive(boolean interactive) {
3786 // Do everything in handler so as not to block the caller.
3787 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3788 interactive ? 1 : 0, 0));
3789 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003790
3791 @Override
3792 public void switchInputMethod(boolean forwardDirection) {
3793 // Do everything in handler so as not to block the caller.
3794 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3795 forwardDirection ? 1 : 0, 0));
3796 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003797 }
3798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 @Override
3800 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3801 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3802 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3805 + Binder.getCallingPid()
3806 + ", uid=" + Binder.getCallingUid());
3807 return;
3808 }
3809
3810 IInputMethod method;
3811 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003812 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 synchronized (mMethodMap) {
3817 p.println("Current Input Method Manager state:");
3818 int N = mMethodList.size();
3819 p.println(" Input Methods:");
3820 for (int i=0; i<N; i++) {
3821 InputMethodInfo info = mMethodList.get(i);
3822 p.println(" InputMethod #" + i + ":");
3823 info.dump(p, " ");
3824 }
3825 p.println(" Clients:");
3826 for (ClientState ci : mClients.values()) {
3827 p.println(" Client " + ci + ":");
3828 p.println(" client=" + ci.client);
3829 p.println(" inputContext=" + ci.inputContext);
3830 p.println(" sessionRequested=" + ci.sessionRequested);
3831 p.println(" curSession=" + ci.curSession);
3832 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003833 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003835 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3836 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003837 focusedWindowClient = mCurFocusedWindowClient;
3838 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3840 + " mBoundToMethod=" + mBoundToMethod);
3841 p.println(" mCurToken=" + mCurToken);
3842 p.println(" mCurIntent=" + mCurIntent);
3843 method = mCurMethod;
3844 p.println(" mCurMethod=" + mCurMethod);
3845 p.println(" mEnabledSession=" + mEnabledSession);
3846 p.println(" mShowRequested=" + mShowRequested
3847 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3848 + " mShowForced=" + mShowForced
3849 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003850 p.println(" mCurUserActionNotificationSequenceNumber="
3851 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003852 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003853 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003854 p.println(" mSwitchingController:");
3855 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003856 p.println(" mSettings:");
3857 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003859
Jeff Brownb88102f2010-09-08 11:49:43 -07003860 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 pw.flush();
3863 try {
3864 client.client.asBinder().dump(fd, args);
3865 } catch (RemoteException e) {
3866 p.println("Input method client dead: " + e);
3867 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003868 } else {
3869 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003871
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003872 if (focusedWindowClient != null && client != focusedWindowClient) {
3873 p.println(" ");
3874 p.println("Warning: Current input method client doesn't match the last focused. "
3875 + "window.");
3876 p.println("Dumping input method client in the last focused window just in case.");
3877 p.println(" ");
3878 pw.flush();
3879 try {
3880 focusedWindowClient.client.asBinder().dump(fd, args);
3881 } catch (RemoteException e) {
3882 p.println("Input method client in focused window dead: " + e);
3883 }
3884 }
3885
Jeff Brownb88102f2010-09-08 11:49:43 -07003886 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 pw.flush();
3889 try {
3890 method.asBinder().dump(fd, args);
3891 } catch (RemoteException e) {
3892 p.println("Input method service dead: " + e);
3893 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003894 } else {
3895 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 }
3897 }
3898}