blob: e04248303c2017ece858436ce5adc86dc78bd4bd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputMethodClient;
29import com.android.internal.view.IInputMethodManager;
30import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070031import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080033import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080034import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070040import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070041import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080042import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.ActivityManagerNative;
44import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070045import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090046import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090047import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090048import android.app.Notification;
49import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070050import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ComponentName;
53import android.content.ContentResolver;
54import android.content.Context;
55import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090057import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090059import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070061import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090062import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080066import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070067import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.res.Resources;
69import android.content.res.TypedArray;
70import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080071import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080072import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080073import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070074import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040076import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070077import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090078import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Handler;
80import android.os.IBinder;
81import android.os.IInterface;
82import android.os.Message;
83import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070084import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080086import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.ServiceManager;
88import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090089import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080090import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.provider.Settings;
92import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090093import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070094import android.util.ArrayMap;
95import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070096import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.EventLog;
Yohei Yukawae985c242016-02-24 18:27:04 -080098import android.util.LocaleList;
satokf9f01002011-05-19 21:31:50 +090099import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900100import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.PrintWriterPrinter;
102import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900103import android.util.Slog;
104import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900105import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700107import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.view.LayoutInflater;
109import android.view.View;
110import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700112import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900113import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700115import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.inputmethod.InputMethod;
117import android.view.inputmethod.InputMethodInfo;
118import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700119import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900120import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900121import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900122import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900123import android.widget.CompoundButton;
124import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900126import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900127import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900131import java.io.FileInputStream;
132import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.io.IOException;
134import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100135import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900137import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import java.util.HashMap;
139import java.util.List;
140
141/**
142 * This class provides a system service that manages input methods.
143 */
144public class InputMethodManagerService extends IInputMethodManager.Stub
145 implements ServiceConnection, Handler.Callback {
146 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700147 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700148 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700150 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
151 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
152 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final int MSG_UNBIND_INPUT = 1000;
155 static final int MSG_BIND_INPUT = 1010;
156 static final int MSG_SHOW_SOFT_INPUT = 1020;
157 static final int MSG_HIDE_SOFT_INPUT = 1030;
158 static final int MSG_ATTACH_TOKEN = 1040;
159 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 static final int MSG_START_INPUT = 2000;
162 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
Yohei Yukawa33e81792015-11-17 21:14:42 -0800164 static final int MSG_UNBIND_CLIENT = 3000;
165 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700166 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700167 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900168 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800169 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170
satok01038492012-04-09 21:08:27 +0900171 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
172
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700173 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
satokf9f01002011-05-19 21:31:50 +0900175 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
176
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900177 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900178 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900179
satok4e4569d2010-11-19 18:45:53 +0900180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800182 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900184 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700187 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700189 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900190 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900191 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900192 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800193 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900195 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 // All known input methods. mMethodMap also serves as the global
198 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700199 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
200 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900201 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700202 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900203 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700205 // Used to bring IME service up to visible adjustment while it is being shown.
206 final ServiceConnection mVisibleConnection = new ServiceConnection() {
207 @Override public void onServiceConnected(ComponentName name, IBinder service) {
208 }
209
210 @Override public void onServiceDisconnected(ComponentName name) {
211 }
212 };
213 boolean mVisibleBound = false;
214
satok7cfc0ed2011-06-20 21:29:36 +0900215 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700216 private NotificationManager mNotificationManager;
217 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700218 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400219 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700220 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900221 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900222 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900223 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900224
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900225 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 final ClientState client;
227 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700228
229 IInputMethodSession session;
230 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 @Override
233 public String toString() {
234 return "SessionState{uid " + client.uid + " pid " + client.pid
235 + " method " + Integer.toHexString(
236 System.identityHashCode(method))
237 + " session " + Integer.toHexString(
238 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700239 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 + "}";
241 }
242
243 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700244 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 client = _client;
246 method = _method;
247 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700248 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
Jeff Brownc28867a2013-03-26 15:42:39 -0700252 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 final IInputMethodClient client;
254 final IInputContext inputContext;
255 final int uid;
256 final int pid;
257 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 boolean sessionRequested;
260 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 @Override
263 public String toString() {
264 return "ClientState{" + Integer.toHexString(
265 System.identityHashCode(this)) + " uid " + uid
266 + " pid " + pid + "}";
267 }
268
269 ClientState(IInputMethodClient _client, IInputContext _inputContext,
270 int _uid, int _pid) {
271 client = _client;
272 inputContext = _inputContext;
273 uid = _uid;
274 pid = _pid;
275 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
276 }
277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700279 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700282 * Set once the system is ready to run third party code.
283 */
284 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700286 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700287 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
288 * method. This is to be synchronized with the secure settings keyed with
289 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
290 *
291 * <p>This can be transiently {@code null} when the system is re-initializing input method
292 * settings, e.g., the system locale is just changed.</p>
293 *
294 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
295 * {@link InputMethodManagerService}.</p>
296 *
297 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700299 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
303 * The current binding sequence number, incremented every time there is
304 * a new bind performed.
305 */
306 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
309 * The client that is currently bound to an input method.
310 */
311 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800314 * The last window token that we confirmed to be focused. This is always updated upon reports
315 * from the input method client. If the window state is already changed before the report is
316 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700317 */
318 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700320 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800321 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
322 */
323 ClientState mCurFocusedWindowClient;
324
325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 * The input context last provided by the current client.
327 */
328 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700331 * The missing method flags for the input context last provided by the current client.
332 *
333 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
334 */
335 int mCurInputContextMissingMethods;
336
337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 * The attributes last provided by the current client.
339 */
340 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700343 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700345 *
346 * <p>This can be {@code null} when no input method is connected.</p>
347 *
348 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700350 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
satokab751aa2010-09-14 19:17:36 +0900354 * The current subtype of the current input method.
355 */
356 private InputMethodSubtype mCurrentSubtype;
357
satok4e4569d2010-11-19 18:45:53 +0900358 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900359 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700360 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900361
John Spurlocke0980502013-10-25 11:59:29 -0400362 // Was the keyguard locked when this client became current?
363 private boolean mCurClientInKeyguard;
364
satokab751aa2010-09-14 19:17:36 +0900365 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 * Set to true if our ServiceConnection is currently actively bound to
367 * a service (whether or not we have gotten its IBinder back yet).
368 */
369 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 /**
372 * Set if the client has asked for the input method to be shown.
373 */
374 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
377 * Set if we were explicitly told to show the input method.
378 */
379 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
382 * Set if we were forced to be shown.
383 */
384 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
387 * Set if we last told the input method to show itself.
388 */
389 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
392 * The Intent used to connect to the current input method.
393 */
394 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 /**
397 * The token we have made for the currently active input method, to
398 * identify it in the future.
399 */
400 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * If non-null, this is the input method service we are currently connected
404 * to.
405 */
406 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * Time that we last initiated a bind to the input method, to determine
410 * if we should try to disconnect and reconnect to it.
411 */
412 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 /**
415 * Have we called mCurMethod.bindInput()?
416 */
417 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * Currently enabled session. Only touched by service thread, not
421 * protected by a lock.
422 */
423 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700426 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700428 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900430 int mCurUserActionNotificationSequenceNumber = 0;
431
Joe Onorato857fd9b2011-01-27 15:08:35 -0800432 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900433
434 /**
435 * A set of status bits regarding the active IME.
436 *
437 * <p>This value is a combination of following two bits:</p>
438 * <dl>
439 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
440 * <dd>
441 * If this bit is ON, connected IME is ready to accept touch/key events.
442 * </dd>
443 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
444 * <dd>
445 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
446 * </dd>
447 * </dl>
448 * <em>Do not update this value outside of setImeWindowStatus.</em>
449 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800450 int mImeWindowVis;
451
Ken Wakasa05dbb652011-08-22 15:22:43 +0900452 private AlertDialog.Builder mDialogBuilder;
453 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900454 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900455 private InputMethodInfo[] mIms;
456 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800457 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700458 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800459 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900460 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
461 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500462 private final String mSlotIme;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700465 int mUserId;
466 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700467 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800468 String mLastEnabled = "";
469
Yohei Yukawa81482972015-06-04 00:58:59 -0700470 /**
471 * <em>This constructor must be called within the lock.</em>
472 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 SettingsObserver(Handler handler) {
474 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700475 }
476
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800477 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700478 if (mRegistered && mUserId == userId) {
479 return;
480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700482 if (mRegistered) {
483 mContext.getContentResolver().unregisterContentObserver(this);
484 mRegistered = false;
485 }
486 if (mUserId != userId) {
487 mLastEnabled = "";
488 mUserId = userId;
489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900492 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700493 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900494 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700495 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700496 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700497 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800498 resolver.registerContentObserver(Settings.Secure.getUriFor(
499 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700500 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
Michael Wright7b5a96b2014-08-09 19:28:42 -0700503 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800504 final Uri showImeUri = Settings.Secure.getUriFor(
505 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
506 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
507 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700509 if (showImeUri.equals(uri)) {
510 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800511 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
512 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
513 mContext.getContentResolver(),
514 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
515 0, mUserId) == 1;
516 if (mAccessibilityRequestingNoSoftKeyboard) {
517 final boolean showRequested = mShowRequested;
518 hideCurrentInputLocked(0, null);
519 mShowRequested = showRequested;
520 } else if (mShowRequested) {
521 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
522 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700523 } else {
524 boolean enabledChanged = false;
525 String newEnabled = mSettings.getEnabledInputMethodsStr();
526 if (!mLastEnabled.equals(newEnabled)) {
527 mLastEnabled = newEnabled;
528 enabledChanged = true;
529 }
530 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700534
535 @Override
536 public String toString() {
537 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
538 + " mLastEnabled=" + mLastEnabled + "}";
539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900542 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900543 @Override
544 public void onReceive(Context context, Intent intent) {
545 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700546 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900547 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700548 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900549 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800550 } else if (Intent.ACTION_USER_ADDED.equals(action)
551 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100552 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800553 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700554 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
555 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
556 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
557 final String prevValue = intent.getStringExtra(
558 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
559 final String newValue = intent.getStringExtra(
560 Intent.EXTRA_SETTING_NEW_VALUE);
561 restoreEnabledInputMethods(mContext, prevValue, newValue);
562 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900563 } else {
564 Slog.w(TAG, "Unexpected intent " + intent);
565 }
566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700569 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
570 // does not attempt to validate on the fly with any installed device policy, so must only
571 // be run in the context of initial device setup.
572 //
573 // TODO: Move this method to InputMethodUtils with adding unit tests.
574 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
575 if (DEBUG_RESTORE) {
576 Slog.i(TAG, "Restoring enabled input methods:");
577 Slog.i(TAG, "prev=" + prevValue);
578 Slog.i(TAG, " new=" + newValue);
579 }
580 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900581 ArrayMap<String, ArraySet<String>> prevMap =
582 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
583 ArrayMap<String, ArraySet<String>> newMap =
584 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700585
586 // Merge the restored ime+subtype enabled states into the live state
587 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
588 final String imeId = entry.getKey();
589 ArraySet<String> prevSubtypes = prevMap.get(imeId);
590 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700591 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700592 prevMap.put(imeId, prevSubtypes);
593 }
594 prevSubtypes.addAll(entry.getValue());
595 }
596
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700597 final String mergedImesAndSubtypesString =
598 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700599 if (DEBUG_RESTORE) {
600 Slog.i(TAG, "Merged IME string:");
601 Slog.i(TAG, " " + mergedImesAndSubtypesString);
602 }
603 Settings.Secure.putString(context.getContentResolver(),
604 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
605 }
606
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800607 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900608 private boolean isChangingPackagesOfCurrentUser() {
609 final int userId = getChangingUserId();
610 final boolean retval = userId == mSettings.getCurrentUserId();
611 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900612 if (!retval) {
613 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
614 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900615 }
616 return retval;
617 }
618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900621 if (!isChangingPackagesOfCurrentUser()) {
622 return false;
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900625 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 final int N = mMethodList.size();
627 if (curInputMethodId != null) {
628 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 InputMethodInfo imi = mMethodList.get(i);
630 if (imi.getId().equals(curInputMethodId)) {
631 for (String pkg : packages) {
632 if (imi.getPackageName().equals(pkg)) {
633 if (!doit) {
634 return true;
635 }
satok723a27e2010-11-11 14:58:11 +0900636 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800637 chooseNewDefaultIMELocked();
638 return true;
639 }
640 }
641 }
642 }
643 }
644 }
645 return false;
646 }
647
648 @Override
649 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900650 if (!isChangingPackagesOfCurrentUser()) {
651 return;
652 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800653 synchronized (mMethodMap) {
654 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900655 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 final int N = mMethodList.size();
657 if (curInputMethodId != null) {
658 for (int i=0; i<N; i++) {
659 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900660 final String imiId = imi.getId();
661 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800662 curIm = imi;
663 }
satoke7c6998e2011-06-03 17:57:59 +0900664
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800665 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900666 if (isPackageModified(imi.getPackageName())) {
667 mFileManager.deleteAllInputMethodSubtypes(imiId);
668 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800669 if (change == PACKAGE_TEMPORARY_CHANGE
670 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800671 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 + imi.getComponent());
673 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 }
675 }
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
Yohei Yukawa94e33302016-02-12 19:37:03 -0800678 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800682 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800683 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800684 if (change == PACKAGE_TEMPORARY_CHANGE
685 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800686 ServiceInfo si = null;
687 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900688 si = mIPackageManager.getServiceInfo(
689 curIm.getComponent(), 0, mSettings.getCurrentUserId());
690 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800691 }
692 if (si == null) {
693 // Uh oh, current input method is no longer around!
694 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800695 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900696 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800697 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800698 changed = true;
699 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900701 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800702 }
703 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800704 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800705 }
satokab751aa2010-09-14 19:17:36 +0900706
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800707 if (curIm == null) {
708 // We currently don't have a default input method... is
709 // one now available?
710 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700711 } else if (!changed && isPackageModified(curIm.getPackageName())) {
712 // Even if the current input method is still available, mCurrentSubtype could
713 // be obsolete when the package is modified in practice.
714 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800717 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800718 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720 }
721 }
722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
Jeff Brownc28867a2013-03-26 15:42:39 -0700724 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900725 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700726 private final IInputMethod mMethod;
727 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800728
Jeff Brownc28867a2013-03-26 15:42:39 -0700729 MethodCallback(InputMethodManagerService imms, IInputMethod method,
730 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900731 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700732 mMethod = method;
733 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
satoke7c6998e2011-06-03 17:57:59 +0900736 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700737 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700738 long ident = Binder.clearCallingIdentity();
739 try {
740 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
741 } finally {
742 Binder.restoreCallingIdentity(ident);
743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
satok01038492012-04-09 21:08:27 +0900747 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700748 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900749 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700750 public void onHardKeyboardStatusChange(boolean available) {
751 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
752 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900753 }
754
Michael Wright7b5a96b2014-08-09 19:28:42 -0700755 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900756 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700757 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900758 }
759 synchronized(mMethodMap) {
760 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
761 && mSwitchingDialog.isShowing()) {
762 mSwitchingDialogTitleView.findViewById(
763 com.android.internal.R.id.hard_keyboard_section).setVisibility(
764 available ? View.VISIBLE : View.GONE);
765 }
766 }
767 }
768 }
769
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800770 public static final class Lifecycle extends SystemService {
771 private InputMethodManagerService mService;
772
773 public Lifecycle(Context context) {
774 super(context);
775 mService = new InputMethodManagerService(context);
776 }
777
778 @Override
779 public void onStart() {
780 LocalServices.addService(InputMethodManagerInternal.class,
781 new LocalServiceImpl(mService.mHandler));
782 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
783 }
784
785 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800786 public void onSwitchUser(@UserIdInt int userHandle) {
787 // Called on the system server's main looper thread.
788 // TODO: Dispatch this to a worker thread as needed.
789 mService.onSwitchUser(userHandle);
790 }
791
792 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800793 public void onBootPhase(int phase) {
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800794 // Called on the system server's main looper thread.
795 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800796 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
797 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
798 .getService(Context.STATUS_BAR_SERVICE);
799 mService.systemRunning(statusBarService);
800 }
801 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800802
803 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800804 public void onUnlockUser(@UserIdInt int userHandle) {
805 // Called on the system server's main looper thread.
806 // TODO: Dispatch this to a worker thread as needed.
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800807 mService.onUnlockUser(userHandle);
808 }
809 }
810
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800811 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800812 synchronized(mMethodMap) {
813 final int currentUserId = mSettings.getCurrentUserId();
814 if (DEBUG) {
815 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
816 }
817 if (userId != currentUserId) {
818 return;
819 }
820 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
821 // We need to rebuild IMEs.
822 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
823 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
824 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800825 }
826
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800827 void onSwitchUser(@UserIdInt int userId) {
828 synchronized (mMethodMap) {
829 switchUserLocked(userId);
830 }
831 }
832
Seigo Nonaka7309b122015-08-17 18:34:13 -0700833 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900834 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800836 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700838 // Note: SettingsObserver doesn't register observers in its constructor.
839 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 mIWindowManager = IWindowManager.Stub.asInterface(
841 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700842 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800843 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900844 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 public void executeMessage(Message msg) {
846 handleMessage(msg);
847 }
Mita Yuned218c72012-12-06 17:18:25 -0800848 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800849 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800850 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900851 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700852 mHasFeature = context.getPackageManager().hasSystemFeature(
853 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500854 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
satok7cfc0ed2011-06-20 21:29:36 +0900855
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400856 Bundle extras = new Bundle();
857 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
858 mImeSwitcherNotification = new Notification.Builder(mContext)
859 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
860 .setWhen(0)
861 .setOngoing(true)
862 .addExtras(extras)
863 .setCategory(Notification.CATEGORY_SYSTEM)
864 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400865
satok7cfc0ed2011-06-20 21:29:36 +0900866 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900867 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900868
869 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900870
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900871 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900872 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800873 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
874 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700875 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900876 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800877
satok7cfc0ed2011-06-20 21:29:36 +0900878 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900879 int userId = 0;
880 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900881 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
882 } catch (RemoteException e) {
883 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
884 }
satok81f8b7c2012-12-04 20:42:56 +0900885 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900886
satokd87c2592010-09-29 11:52:06 +0900887 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900888 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800889 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700890
Kenny Guy2a764942014-04-02 13:29:20 +0100891 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900892 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900893 synchronized (mMethodMap) {
894 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
895 mSettings, context);
896 }
satok0a1bcf42012-05-16 19:26:31 +0900897
898 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900899 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900900 if (DEBUG) {
901 Slog.d(TAG, "Initial default ime = " + defaultImiId);
902 }
satok0a1bcf42012-05-16 19:26:31 +0900903 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
904
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900905 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800906 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900907 }
satokd87c2592010-09-29 11:52:06 +0900908 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
satok0a1bcf42012-05-16 19:26:31 +0900910 if (!mImeSelectedOnBoot) {
911 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900912 synchronized (mMethodMap) {
913 resetDefaultImeLocked(context);
914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900917 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700918 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900919 updateFromSettingsLocked(true);
920 }
satok5b927c432012-05-01 20:09:34 +0900921
922 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
923 // according to the new system locale.
924 final IntentFilter filter = new IntentFilter();
925 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
926 mContext.registerReceiver(
927 new BroadcastReceiver() {
928 @Override
929 public void onReceive(Context context, Intent intent) {
930 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900931 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900932 }
933 }
934 }, filter);
935 }
936
satok5b927c432012-05-01 20:09:34 +0900937 private void resetDefaultImeLocked(Context context) {
938 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800939 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900940 return;
941 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800942 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
943 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
944 if (suitableImes.isEmpty()) {
945 Slog.i(TAG, "No default found");
946 return;
satok5b927c432012-05-01 20:09:34 +0900947 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800948 final InputMethodInfo defIm = suitableImes.get(0);
949 Slog.i(TAG, "Default found, using " + defIm.getId());
950 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900951 }
952
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900953 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
954 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 if (!mSystemReady) {
956 // not system ready
957 return;
958 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800959 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900960 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800961 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 if (!updateOnlyWhenLocaleChanged) {
963 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800964 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 }
966 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800967 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800969 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 if (!updateOnlyWhenLocaleChanged) {
971 final String selectedImiId = mSettings.getSelectedInputMethod();
972 if (TextUtils.isEmpty(selectedImiId)) {
973 // This is the first time of the user switch and
974 // set the current ime to the proper one.
975 resetDefaultImeLocked(mContext);
976 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900977 } else {
978 // If the locale is changed, needs to reset the default ime
979 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800981 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800982 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900983 if (!updateOnlyWhenLocaleChanged) {
984 try {
985 startInputInnerLocked();
986 } catch (RuntimeException e) {
987 Slog.w(TAG, "Unexpected exception", e);
988 }
989 }
990 }
991 }
992
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900993 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900994 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
995 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 }
997
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900998 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700999 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1000 + " currentUserId=" + mSettings.getCurrentUserId());
1001
Yohei Yukawa81482972015-06-04 00:58:59 -07001002 // ContentObserver should be registered again when the user is changed
1003 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001004
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001005 // If the system is not ready or the device is not yed unlocked by the user, then we use
1006 // copy-on-write settings.
1007 final boolean useCopyOnWriteSettings =
1008 !mSystemReady || !mUserManager.isUserUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001009 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001010 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001011 // InputMethodFileManager should be reset when the user is changed
1012 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001013 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001014
1015 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1016 + " defaultImiId=" + defaultImiId);
1017
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001018 // For secondary users, the list of enabled IMEs may not have been updated since the
1019 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1020 // not be empty even if the IME has been uninstalled by the primary user.
1021 // Even in such cases, IMMS works fine because it will find the most applicable
1022 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001023 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001024 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001025 initialUserSwitch /* needsToResetDefaultIme */);
1026 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001027 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1028 mSettings.getEnabledInputMethodListLocked(), newUserId,
1029 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001030 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001031
1032 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1033 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001034 }
1035
Kenny Guy2a764942014-04-02 13:29:20 +01001036 void updateCurrentProfileIds() {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001037 List<UserInfo> profiles = mUserManager.getProfiles(mSettings.getCurrentUserId());
Kenny Guy2a764942014-04-02 13:29:20 +01001038 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1039 for (int i = 0; i < currentProfileIds.length; i++) {
1040 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001041 }
Kenny Guy2a764942014-04-02 13:29:20 +01001042 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001043 }
1044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 @Override
1046 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1047 throws RemoteException {
1048 try {
1049 return super.onTransact(code, data, reply, flags);
1050 } catch (RuntimeException e) {
1051 // The input method manager only throws security exceptions, so let's
1052 // log all others.
1053 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001054 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 }
1056 throw e;
1057 }
1058 }
1059
Svetoslav Ganova0027152013-06-25 14:59:53 -07001060 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001061 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001062 if (DEBUG) {
1063 Slog.d(TAG, "--- systemReady");
1064 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001065 if (!mSystemReady) {
1066 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001067 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001068 mSettings.switchCurrentUser(currentUserId,
1069 !mUserManager.isUserUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001070 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1071 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001072 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001073 if (mStatusBar != null) {
1074 mStatusBar.setIconVisibility(mSlotIme, false);
1075 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001076 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001077 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1078 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001079 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001080 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001081 mHardKeyboardListener);
1082 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001083 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001084 if (!mImeSelectedOnBoot) {
1085 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001086 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001087 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1088 mSettings.getEnabledInputMethodListLocked(),
1089 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001090 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001091 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001092 try {
1093 startInputInnerLocked();
1094 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001096 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001097 }
1098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001101 // ---------------------------------------------------------------------------------------
1102 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1103 // 1) it comes from the system process
1104 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1105 private boolean calledFromValidUser() {
1106 final int uid = Binder.getCallingUid();
1107 final int userId = UserHandle.getUserId(uid);
1108 if (DEBUG) {
1109 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1110 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1111 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001112 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1113 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001114 }
Kenny Guy2a764942014-04-02 13:29:20 +01001115 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001116 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001117 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001118
1119 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1120 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1121 // must not manage background users' states in any functions.
1122 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1123 // by a token.
1124 if (mContext.checkCallingOrSelfPermission(
1125 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1126 == PackageManager.PERMISSION_GRANTED) {
1127 if (DEBUG) {
1128 Slog.d(TAG, "--- Access granted because the calling process has "
1129 + "the INTERACT_ACROSS_USERS_FULL permission");
1130 }
1131 return true;
1132 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001133 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1134 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001135 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001136 }
1137
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001138
1139 /**
1140 * Returns true iff the caller is identified to be the current input method with the token.
1141 * @param token The window token given to the input method when it was started.
1142 * @return true if and only if non-null valid token is specified.
1143 */
1144 private boolean calledWithValidToken(IBinder token) {
1145 if (token == null || mCurToken != token) {
1146 return false;
1147 }
1148 return true;
1149 }
1150
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001151 private boolean bindCurrentInputMethodService(
1152 Intent service, ServiceConnection conn, int flags) {
1153 if (service == null || conn == null) {
1154 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1155 return false;
1156 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001157 return mContext.bindServiceAsUser(service, conn, flags,
1158 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001159 }
1160
satoke7c6998e2011-06-03 17:57:59 +09001161 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001163 // TODO: Make this work even for non-current users?
1164 if (!calledFromValidUser()) {
1165 return Collections.emptyList();
1166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001168 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 }
1171
satoke7c6998e2011-06-03 17:57:59 +09001172 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001174 // TODO: Make this work even for non-current users?
1175 if (!calledFromValidUser()) {
1176 return Collections.emptyList();
1177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001179 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 }
1182
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001183 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001184 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001185 * @return enabled subtypes of the specified imi
1186 */
satoke7c6998e2011-06-03 17:57:59 +09001187 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001188 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001189 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001190 // TODO: Make this work even for non-current users?
1191 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001192 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001193 }
satok67ddf9c2010-11-17 09:45:54 +09001194 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001195 final InputMethodInfo imi;
1196 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001197 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001198 } else {
1199 imi = mMethodMap.get(imiId);
1200 }
1201 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001202 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001203 }
1204 return mSettings.getEnabledInputMethodSubtypeListLocked(
1205 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001206 }
1207 }
1208
satoke7c6998e2011-06-03 17:57:59 +09001209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 public void addClient(IInputMethodClient client,
1211 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001212 if (!calledFromValidUser()) {
1213 return;
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 synchronized (mMethodMap) {
1216 mClients.put(client.asBinder(), new ClientState(client,
1217 inputContext, uid, pid));
1218 }
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
satoke7c6998e2011-06-03 17:57:59 +09001221 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001223 if (!calledFromValidUser()) {
1224 return;
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001227 ClientState cs = mClients.remove(client.asBinder());
1228 if (cs != null) {
1229 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001230 if (mCurClient == cs) {
1231 mCurClient = null;
1232 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001233 if (mCurFocusedWindowClient == cs) {
1234 mCurFocusedWindowClient = null;
1235 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 void executeOrSendMessage(IInterface target, Message msg) {
1241 if (target.asBinder() instanceof Binder) {
1242 mCaller.sendMessage(msg);
1243 } else {
1244 handleMessage(msg);
1245 msg.recycle();
1246 }
1247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001248
Yohei Yukawa33e81792015-11-17 21:14:42 -08001249 void unbindCurrentClientLocked(
1250 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001252 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 + mCurClient.client.asBinder());
1254 if (mBoundToMethod) {
1255 mBoundToMethod = false;
1256 if (mCurMethod != null) {
1257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1258 MSG_UNBIND_INPUT, mCurMethod));
1259 }
1260 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001261
1262 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1263 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001264 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1265 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001268
The Android Open Source Project10592532009-03-18 17:39:46 -07001269 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 private int getImeShowFlags() {
1274 int flags = 0;
1275 if (mShowForced) {
1276 flags |= InputMethod.SHOW_FORCED
1277 | InputMethod.SHOW_EXPLICIT;
1278 } else if (mShowExplicitlyRequested) {
1279 flags |= InputMethod.SHOW_EXPLICIT;
1280 }
1281 return flags;
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 private int getAppShowFlags() {
1285 int flags = 0;
1286 if (mShowForced) {
1287 flags |= InputMethodManager.SHOW_FORCED;
1288 } else if (!mShowExplicitlyRequested) {
1289 flags |= InputMethodManager.SHOW_IMPLICIT;
1290 }
1291 return flags;
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
Dianne Hackborn7663d802012-02-24 13:08:49 -08001294 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 if (!mBoundToMethod) {
1296 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1297 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1298 mBoundToMethod = true;
1299 }
1300 final SessionState session = mCurClient.curSession;
1301 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001302 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1303 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1304 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001306 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1307 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1308 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001311 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001312 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001314 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001315 (session.channel != null ? session.channel.dup() : null),
1316 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001319 InputBindResult startInputLocked(
1320 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001321 IInputMethodClient client, IInputContext inputContext,
1322 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001323 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 // If no method is currently selected, do nothing.
1325 if (mCurMethodId == null) {
1326 return mNoBinding;
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 ClientState cs = mClients.get(client.asBinder());
1330 if (cs == null) {
1331 throw new IllegalArgumentException("unknown client "
1332 + client.asBinder());
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Yohei Yukawa74750f22016-03-22 12:54:22 -07001335 if (attribute == null) {
1336 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1337 + " uid=" + cs.uid + " pid=" + cs.pid);
1338 return null;
1339 }
1340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 try {
1342 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1343 // Check with the window manager to make sure this client actually
1344 // has a window with focus. If not, reject. This is thread safe
1345 // because if the focus changes some time before or after, the
1346 // next client receiving focus that has any interest in input will
1347 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001348 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1350 return null;
1351 }
1352 } catch (RemoteException e) {
1353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001354
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001355 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1356 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001357 }
1358
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001359 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001360 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001361 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001362 // If no method is currently selected, do nothing.
1363 if (mCurMethodId == null) {
1364 return mNoBinding;
1365 }
1366
Yohei Yukawad57ba672015-06-08 16:39:46 -07001367 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1368 attribute.packageName)) {
1369 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1370 + " uid=" + cs.uid + " package=" + attribute.packageName);
1371 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001372 }
1373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001375 // Was the keyguard locked when switching over to the new client?
1376 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 // If the client is changing, we need to switch over to the new
1378 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001379 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001380 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001381 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382
1383 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001384 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001385 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001386 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 // Bump up the sequence for this client and attach it.
1391 mCurSeq++;
1392 if (mCurSeq <= 0) mCurSeq = 1;
1393 mCurClient = cs;
1394 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001395 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 // Check if the input method is changing.
1399 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1400 if (cs.curSession != null) {
1401 // Fast case: if we are already connected to the input method,
1402 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001403 return attachNewInputLocked(
1404 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 if (mHaveConnection) {
1407 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 // Return to client, and we will get back with it when
1409 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001410 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001411 return new InputBindResult(null, null, mCurId, mCurSeq,
1412 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 } else if (SystemClock.uptimeMillis()
1414 < (mLastBindTime+TIME_TO_RECONNECT)) {
1415 // In this case we have connected to the service, but
1416 // don't yet have its interface. If it hasn't been too
1417 // long since we did the connection, we'll return to
1418 // the client and wait to get the service interface so
1419 // we can report back. If it has been too long, we want
1420 // to fall through so we can try a disconnect/reconnect
1421 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001422 return new InputBindResult(null, null, mCurId, mCurSeq,
1423 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1426 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428 }
1429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001431 return startInputInnerLocked();
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001434 InputBindResult startInputInnerLocked() {
1435 if (mCurMethodId == null) {
1436 return mNoBinding;
1437 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001438
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001439 if (!mSystemReady) {
1440 // If the system is not yet ready, we shouldn't be running third
1441 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001442 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1443 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1447 if (info == null) {
1448 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001451 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1454 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001455 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1456 com.android.internal.R.string.input_method_binding_label);
1457 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1458 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001459 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001460 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1461 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 mLastBindTime = SystemClock.uptimeMillis();
1463 mHaveConnection = true;
1464 mCurId = info.getId();
1465 mCurToken = new Binder();
1466 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001467 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 mIWindowManager.addWindowToken(mCurToken,
1469 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1470 } catch (RemoteException e) {
1471 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001472 return new InputBindResult(null, null, mCurId, mCurSeq,
1473 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 } else {
1475 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001476 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 + mCurIntent);
1478 }
1479 return null;
1480 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001481
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001482 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001483 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001484 IInputMethodClient client, IInputContext inputContext,
1485 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001486 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001487 if (!calledFromValidUser()) {
1488 return null;
1489 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001491 if (DEBUG) {
1492 Slog.v(TAG, "startInput: reason="
1493 + InputMethodClient.getStartInputReason(startInputReason)
1494 + " client = " + client.asBinder()
1495 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001496 + " missingMethods="
1497 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001498 + " attribute=" + attribute
1499 + " controlFlags=#" + Integer.toHexString(controlFlags));
1500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 final long ident = Binder.clearCallingIdentity();
1502 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001503 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1504 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 } finally {
1506 Binder.restoreCallingIdentity(ident);
1507 }
1508 }
1509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
satoke7c6998e2011-06-03 17:57:59 +09001511 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 public void finishInput(IInputMethodClient client) {
1513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
satoke7c6998e2011-06-03 17:57:59 +09001515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 public void onServiceConnected(ComponentName name, IBinder service) {
1517 synchronized (mMethodMap) {
1518 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1519 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001520 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001522 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001523 return;
1524 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001525 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001526 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1527 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001529 clearClientSessionLocked(mCurClient);
1530 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
1532 }
1533 }
1534 }
1535
Jeff Brownc28867a2013-03-26 15:42:39 -07001536 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1537 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 synchronized (mMethodMap) {
1539 if (mCurMethod != null && method != null
1540 && mCurMethod.asBinder() == method.asBinder()) {
1541 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001542 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001544 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001545 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 if (res.method != null) {
1547 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001548 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001550 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552 }
1553 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001554
1555 // Session abandoned. Close its associated input channel.
1556 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001558
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001559 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001560 if (mVisibleBound) {
1561 mContext.unbindService(mVisibleConnection);
1562 mVisibleBound = false;
1563 }
1564
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001565 if (mHaveConnection) {
1566 mContext.unbindService(this);
1567 mHaveConnection = false;
1568 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001569
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001570 if (mCurToken != null) {
1571 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001572 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001573 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001574 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001575 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001576 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001577 mIWindowManager.removeWindowToken(mCurToken);
1578 } catch (RemoteException e) {
1579 }
1580 mCurToken = null;
1581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
The Android Open Source Project10592532009-03-18 17:39:46 -07001583 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001584 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001586
Yohei Yukawa33e81792015-11-17 21:14:42 -08001587 void resetCurrentMethodAndClient(
1588 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001589 mCurMethodId = null;
1590 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001591 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001592 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001593
1594 void requestClientSessionLocked(ClientState cs) {
1595 if (!cs.sessionRequested) {
1596 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1597 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1598 cs.sessionRequested = true;
1599 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1600 MSG_CREATE_SESSION, mCurMethod, channels[1],
1601 new MethodCallback(this, mCurMethod, channels[0])));
1602 }
1603 }
1604
1605 void clearClientSessionLocked(ClientState cs) {
1606 finishSessionLocked(cs.curSession);
1607 cs.curSession = null;
1608 cs.sessionRequested = false;
1609 }
1610
1611 private void finishSessionLocked(SessionState sessionState) {
1612 if (sessionState != null) {
1613 if (sessionState.session != null) {
1614 try {
1615 sessionState.session.finishSession();
1616 } catch (RemoteException e) {
1617 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001618 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001619 }
1620 sessionState.session = null;
1621 }
1622 if (sessionState.channel != null) {
1623 sessionState.channel.dispose();
1624 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001625 }
1626 }
1627 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001628
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001629 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 if (mCurMethod != null) {
1631 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001632 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001634
Jeff Brownc28867a2013-03-26 15:42:39 -07001635 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001636 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 mCurMethod = null;
1638 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001639 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001640 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001643
satoke7c6998e2011-06-03 17:57:59 +09001644 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 public void onServiceDisconnected(ComponentName name) {
1646 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001647 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 + " mCurIntent=" + mCurIntent);
1649 if (mCurMethod != null && mCurIntent != null
1650 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001651 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 // We consider this to be a new bind attempt, since the system
1653 // should now try to restart the service for us.
1654 mLastBindTime = SystemClock.uptimeMillis();
1655 mShowRequested = mInputShown;
1656 mInputShown = false;
1657 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001658 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1659 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1660 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662 }
1663 }
1664 }
1665
satokf9f01002011-05-19 21:31:50 +09001666 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1668 long ident = Binder.clearCallingIdentity();
1669 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001671 if (!calledWithValidToken(token)) {
1672 final int uid = Binder.getCallingUid();
1673 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1674 + " token:" + token);
1675 return;
1676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001678 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001679 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001680 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001683 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001684 CharSequence contentDescription = null;
1685 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001686 // Use PackageManager to load label
1687 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001688 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001689 mIPackageManager.getApplicationInfo(packageName, 0,
1690 mSettings.getCurrentUserId()));
1691 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001692 /* ignore */
1693 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001694 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001695 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001696 contentDescription != null
1697 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001698 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701 }
1702 } finally {
1703 Binder.restoreCallingIdentity(ident);
1704 }
1705 }
1706
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001707 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001708 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001709 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001710 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001711 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001712 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001713 // When physical keyboard is attached, we show the ime switcher (or notification if
1714 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1715 // exists in the IME switcher dialog. Might be OK to remove this condition once
1716 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1717 return true;
1718 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001719 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001720
1721 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1722 final int N = imis.size();
1723 if (N > 2) return true;
1724 if (N < 1) return false;
1725 int nonAuxCount = 0;
1726 int auxCount = 0;
1727 InputMethodSubtype nonAuxSubtype = null;
1728 InputMethodSubtype auxSubtype = null;
1729 for(int i = 0; i < N; ++i) {
1730 final InputMethodInfo imi = imis.get(i);
1731 final List<InputMethodSubtype> subtypes =
1732 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1733 final int subtypeCount = subtypes.size();
1734 if (subtypeCount == 0) {
1735 ++nonAuxCount;
1736 } else {
1737 for (int j = 0; j < subtypeCount; ++j) {
1738 final InputMethodSubtype subtype = subtypes.get(j);
1739 if (!subtype.isAuxiliary()) {
1740 ++nonAuxCount;
1741 nonAuxSubtype = subtype;
1742 } else {
1743 ++auxCount;
1744 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001745 }
1746 }
satok7cfc0ed2011-06-20 21:29:36 +09001747 }
1748 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001749 if (nonAuxCount > 1 || auxCount > 1) {
1750 return true;
1751 } else if (nonAuxCount == 1 && auxCount == 1) {
1752 if (nonAuxSubtype != null && auxSubtype != null
1753 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1754 || auxSubtype.overridesImplicitlyEnabledSubtype()
1755 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1756 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1757 return false;
1758 }
1759 return true;
1760 }
1761 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001762 }
1763
John Spurlocke0980502013-10-25 11:59:29 -04001764 private boolean isKeyguardLocked() {
1765 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1766 }
1767
satokdbf29502011-08-25 15:28:23 +09001768 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001769 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001770 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001771 if (!calledWithValidToken(token)) {
1772 final int uid = Binder.getCallingUid();
1773 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1774 + " token:" + token);
1775 return;
1776 }
1777
1778 synchronized (mMethodMap) {
1779 mImeWindowVis = vis;
1780 mBackDisposition = backDisposition;
1781 updateSystemUiLocked(token, vis, backDisposition);
1782 }
1783 }
1784
1785 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1786 synchronized (mMethodMap) {
1787 updateSystemUiLocked(token, vis, backDisposition);
1788 }
1789 }
1790
1791 // Caution! This method is called in this class. Handle multi-user carefully
1792 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1793 if (!calledWithValidToken(token)) {
1794 final int uid = Binder.getCallingUid();
1795 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1796 + " token:" + token);
1797 return;
1798 }
1799
1800 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1801 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001802 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001803 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001804 // apply policy for binder calls
1805 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1806 vis = 0;
satok06487a52010-10-29 11:37:18 +09001807 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001808 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1809 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1810 if (mStatusBar != null) {
1811 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1812 needsToShowImeSwitcher);
1813 }
1814 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1815 if (imi != null && needsToShowImeSwitcher) {
1816 // Used to load label
1817 final CharSequence title = mRes.getText(
1818 com.android.internal.R.string.select_input_method);
1819 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1820 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001821 mImeSwitcherNotification.setContentTitle(title)
1822 .setContentText(summary)
1823 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001824 try {
1825 if ((mNotificationManager != null)
1826 && !mIWindowManager.hasNavigationBar()) {
1827 if (DEBUG) {
1828 Slog.d(TAG, "--- show notification: label = " + summary);
1829 }
1830 mNotificationManager.notifyAsUser(null,
1831 com.android.internal.R.string.select_input_method,
1832 mImeSwitcherNotification.build(), UserHandle.ALL);
1833 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001834 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001835 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001836 }
1837 } else {
1838 if (mNotificationShown && mNotificationManager != null) {
1839 if (DEBUG) {
1840 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001841 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001842 mNotificationManager.cancelAsUser(null,
1843 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1844 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001845 }
satok06487a52010-10-29 11:37:18 +09001846 }
1847 } finally {
1848 Binder.restoreCallingIdentity(ident);
1849 }
1850 }
1851
satoke7c6998e2011-06-03 17:57:59 +09001852 @Override
satokf9f01002011-05-19 21:31:50 +09001853 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001854 if (!calledFromValidUser()) {
1855 return;
1856 }
satokf9f01002011-05-19 21:31:50 +09001857 synchronized (mMethodMap) {
1858 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1859 for (int i = 0; i < spans.length; ++i) {
1860 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001861 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001862 mSecureSuggestionSpans.put(ss, currentImi);
1863 }
1864 }
1865 }
1866 }
1867
satoke7c6998e2011-06-03 17:57:59 +09001868 @Override
satokf9f01002011-05-19 21:31:50 +09001869 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001870 if (!calledFromValidUser()) {
1871 return false;
1872 }
satokf9f01002011-05-19 21:31:50 +09001873 synchronized (mMethodMap) {
1874 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1875 // TODO: Do not send the intent if the process of the targetImi is already dead.
1876 if (targetImi != null) {
1877 final String[] suggestions = span.getSuggestions();
1878 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001879 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001880 final Intent intent = new Intent();
1881 // Ensures that only a class in the original IME package will receive the
1882 // notification.
satok42c5a162011-05-26 16:46:14 +09001883 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001884 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1885 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1886 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1887 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001888 final long ident = Binder.clearCallingIdentity();
1889 try {
1890 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1891 } finally {
1892 Binder.restoreCallingIdentity(ident);
1893 }
satokf9f01002011-05-19 21:31:50 +09001894 return true;
1895 }
1896 }
1897 return false;
1898 }
1899
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001900 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001901 updateInputMethodsFromSettingsLocked(enabledMayChange);
1902 updateKeyboardFromSettingsLocked();
1903 }
1904
1905 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001906 if (enabledMayChange) {
1907 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1908 for (int i=0; i<enabled.size(); i++) {
1909 // We allow the user to select "disabled until used" apps, so if they
1910 // are enabling one of those here we now need to make it enabled.
1911 InputMethodInfo imm = enabled.get(i);
1912 try {
1913 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1914 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1915 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001916 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001917 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001918 if (DEBUG) {
1919 Slog.d(TAG, "Update state(" + imm.getId()
1920 + "): DISABLED_UNTIL_USED -> DEFAULT");
1921 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001922 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1923 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001924 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1925 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001926 }
1927 } catch (RemoteException e) {
1928 }
1929 }
1930 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001931 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1932 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1933 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1934 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001935 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001936 // There is no input method selected, try to choose new applicable input method.
1937 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001938 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001939 }
1940 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001942 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001944 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001945 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 }
satokf3db1af2010-11-23 13:34:33 +09001947 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001948 } else {
1949 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001950 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001952 // Here is not the perfect place to reset the switching controller. Ideally
1953 // mSwitchingController and mSettings should be able to share the same state.
1954 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1955 // the same enabled IMEs list.
1956 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001957
1958 }
1959
1960 public void updateKeyboardFromSettingsLocked() {
1961 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1962 if (mSwitchingDialog != null
1963 && mSwitchingDialogTitleView != null
1964 && mSwitchingDialog.isShowing()) {
1965 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1966 com.android.internal.R.id.hard_keyboard_switch);
1967 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001970
Yohei Yukawab097b822015-12-01 10:43:08 -08001971 private void notifyInputMethodSubtypeChanged(final int userId,
1972 @Nullable final InputMethodInfo inputMethodInfo,
1973 @Nullable final InputMethodSubtype subtype) {
1974 final InputManagerInternal inputManagerInternal =
1975 LocalServices.getService(InputManagerInternal.class);
1976 if (inputManagerInternal != null) {
1977 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1978 }
1979 }
1980
satokab751aa2010-09-14 19:17:36 +09001981 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 InputMethodInfo info = mMethodMap.get(id);
1983 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001984 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001986
satokd81e9502012-05-21 12:58:45 +09001987 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001989 final int subtypeCount = info.getSubtypeCount();
1990 if (subtypeCount <= 0) {
1991 return;
satokcd7cd292010-11-20 15:46:23 +09001992 }
satokd81e9502012-05-21 12:58:45 +09001993 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1994 final InputMethodSubtype newSubtype;
1995 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1996 newSubtype = info.getSubtypeAt(subtypeId);
1997 } else {
1998 // If subtype is null, try to find the most applicable one from
1999 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002000 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002001 }
2002 if (newSubtype == null || oldSubtype == null) {
2003 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2004 + ", new subtype = " + newSubtype);
2005 return;
2006 }
2007 if (newSubtype != oldSubtype) {
2008 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2009 if (mCurMethod != null) {
2010 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002011 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002012 mCurMethod.changeInputMethodSubtype(newSubtype);
2013 } catch (RemoteException e) {
2014 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002015 return;
satokab751aa2010-09-14 19:17:36 +09002016 }
2017 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002018 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 return;
2021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002022
satokd81e9502012-05-21 12:58:45 +09002023 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 final long ident = Binder.clearCallingIdentity();
2025 try {
satokab751aa2010-09-14 19:17:36 +09002026 // Set a subtype to this input method.
2027 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002028 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2029 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2030 // because mCurMethodId is stored as a history in
2031 // setSelectedInputMethodAndSubtypeLocked().
2032 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033
2034 if (ActivityManagerNative.isSystemReady()) {
2035 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002036 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002038 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002040 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 } finally {
2042 Binder.restoreCallingIdentity(ident);
2043 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002044
2045 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2046 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002048
satok42c5a162011-05-26 16:46:14 +09002049 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002050 public boolean showSoftInput(IInputMethodClient client, int flags,
2051 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002052 if (!calledFromValidUser()) {
2053 return false;
2054 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002055 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 long ident = Binder.clearCallingIdentity();
2057 try {
2058 synchronized (mMethodMap) {
2059 if (mCurClient == null || client == null
2060 || mCurClient.client.asBinder() != client.asBinder()) {
2061 try {
2062 // We need to check if this is the current client with
2063 // focus in the window manager, to allow this call to
2064 // be made before input is started in it.
2065 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002066 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002067 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
2069 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002070 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 }
2072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002073
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002074 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002075 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 }
2077 } finally {
2078 Binder.restoreCallingIdentity(ident);
2079 }
2080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002081
The Android Open Source Project4df24232009-03-05 14:34:35 -08002082 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002084 if (mAccessibilityRequestingNoSoftKeyboard) {
2085 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
Anna Galusza9b278112016-01-04 11:37:37 -08002087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2089 mShowExplicitlyRequested = true;
2090 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002091 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2092 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002094
Dianne Hackborncc278702009-09-02 23:07:23 -07002095 if (!mSystemReady) {
2096 return false;
2097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
The Android Open Source Project4df24232009-03-05 14:34:35 -08002099 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002101 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2103 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2104 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002106 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002107 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002108 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002109 | Context.BIND_TREAT_LIKE_ACTIVITY
2110 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002111 mVisibleBound = true;
2112 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002113 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002115 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 // The client has asked to have the input method shown, but
2117 // we have been sitting here too long with a connection to the
2118 // service and no interface received, so let's disconnect/connect
2119 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002120 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002122 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002124 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002125 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002126 } else {
2127 if (DEBUG) {
2128 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2129 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002132
The Android Open Source Project4df24232009-03-05 14:34:35 -08002133 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002135
satok42c5a162011-05-26 16:46:14 +09002136 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002137 public boolean hideSoftInput(IInputMethodClient client, int flags,
2138 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002139 if (!calledFromValidUser()) {
2140 return false;
2141 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002142 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 long ident = Binder.clearCallingIdentity();
2144 try {
2145 synchronized (mMethodMap) {
2146 if (mCurClient == null || client == null
2147 || mCurClient.client.asBinder() != client.asBinder()) {
2148 try {
2149 // We need to check if this is the current client with
2150 // focus in the window manager, to allow this call to
2151 // be made before input is started in it.
2152 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002153 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2154 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002155 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 }
2157 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002158 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
2160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002161
Joe Onorato8a9b2202010-02-26 18:56:32 -08002162 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002163 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 }
2165 } finally {
2166 Binder.restoreCallingIdentity(ident);
2167 }
2168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002169
The Android Open Source Project4df24232009-03-05 14:34:35 -08002170 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2172 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002173 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 -08002174 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
2176 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002177 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 -08002178 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002180
2181 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2182 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2183 // to be updated with the new value sent from IME process. Even in such a transient state
2184 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2185 // application process as a valid request, and have even promised such a behavior with CTS
2186 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2187 // IMMS#InputShown indicates that the software keyboard is shown.
2188 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2189 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2190 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002191 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002192 if (shouldHideSoftInput) {
2193 // The IME will report its visible state again after the following message finally
2194 // delivered to the IME process as an IPC. Hence the inconsistency between
2195 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2196 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002197 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2198 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2199 res = true;
2200 } else {
2201 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002203 if (mHaveConnection && mVisibleBound) {
2204 mContext.unbindService(mVisibleConnection);
2205 mVisibleBound = false;
2206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 mInputShown = false;
2208 mShowRequested = false;
2209 mShowExplicitlyRequested = false;
2210 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002211 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002213
satok42c5a162011-05-26 16:46:14 +09002214 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002215 public InputBindResult startInputOrWindowGainedFocus(
2216 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2217 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002218 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002219 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002220 if (windowToken != null) {
2221 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002222 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002223 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002224 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2225 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002226 }
2227 }
2228
2229 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002230 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2231 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002232 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2233 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002234 // Needs to check the validity before clearing calling identity
2235 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002236 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 long ident = Binder.clearCallingIdentity();
2238 try {
2239 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002240 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2241 + InputMethodClient.getStartInputReason(startInputReason)
2242 + " client=" + client.asBinder()
2243 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002244 + " missingMethods="
2245 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002246 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002247 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002249 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002250
Dianne Hackborn7663d802012-02-24 13:08:49 -08002251 ClientState cs = mClients.get(client.asBinder());
2252 if (cs == null) {
2253 throw new IllegalArgumentException("unknown client "
2254 + client.asBinder());
2255 }
2256
2257 try {
2258 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2259 // Check with the window manager to make sure this client actually
2260 // has a window with focus. If not, reject. This is thread safe
2261 // because if the focus changes some time before or after, the
2262 // next client receiving focus that has any interest in input will
2263 // be calling through here after that change happens.
2264 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2265 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2266 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002268 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002270
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002271 if (!calledFromValidUser) {
2272 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2273 Slog.w(TAG, "If you want to interect with IME, you need "
2274 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2275 hideCurrentInputLocked(0, null);
2276 return null;
2277 }
2278
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002279 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002280 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002281 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002282 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002283 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2284 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002285 }
2286 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002287 }
2288 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002289 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002290
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002291 // Should we auto-show the IME even if the caller has not
2292 // specified what should be done with it?
2293 // We only do this automatically if the window can resize
2294 // to accommodate the IME (so what the user sees will give
2295 // them good context without input information being obscured
2296 // by the IME) or if running on a large screen where there
2297 // is more room for the target window + IME.
2298 final boolean doAutoShow =
2299 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2300 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2301 || mRes.getConfiguration().isLayoutSizeAtLeast(
2302 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002303 final boolean isTextEditor =
2304 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2305
2306 // We want to start input before showing the IME, but after closing
2307 // it. We want to do this after closing it to help the IME disappear
2308 // more quickly (not get stuck behind it initializing itself for the
2309 // new focused input, even if its window wants to hide the IME).
2310 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2313 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002314 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2316 // There is no focus view, and this window will
2317 // be behind any soft input window, so hide the
2318 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002319 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002320 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002322 } else if (isTextEditor && doAutoShow && (softInputMode &
2323 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 // There is a focus view, and we are navigating forward
2325 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002326 // We only do this automatically if the window can resize
2327 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002328 // them good context without input information being obscured
2329 // by the IME) or if running on a large screen where there
2330 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002331 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002332 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002333 res = startInputUncheckedLocked(cs, inputContext,
2334 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002335 didStart = true;
2336 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002337 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
2339 break;
2340 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2341 // Do nothing.
2342 break;
2343 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2344 if ((softInputMode &
2345 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002346 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002347 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 }
2349 break;
2350 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002351 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002352 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 break;
2354 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2355 if ((softInputMode &
2356 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002357 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002358 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002359 res = startInputUncheckedLocked(cs, inputContext,
2360 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002361 didStart = true;
2362 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002363 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
2365 break;
2366 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002367 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002368 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002369 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2370 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002371 didStart = true;
2372 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002373 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 break;
2375 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002376
2377 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002378 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002379 controlFlags);
2380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 }
2382 } finally {
2383 Binder.restoreCallingIdentity(ident);
2384 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002385
2386 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002388
satok42c5a162011-05-26 16:46:14 +09002389 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002390 public void showInputMethodPickerFromClient(
2391 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002392 if (!calledFromValidUser()) {
2393 return;
2394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 synchronized (mMethodMap) {
2396 if (mCurClient == null || client == null
2397 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002398 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002399 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
2401
satok440aab52010-11-25 09:43:11 +09002402 // Always call subtype picker, because subtype picker is a superset of input method
2403 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002404 mHandler.sendMessage(mCaller.obtainMessageI(
2405 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002406 }
2407 }
2408
satok42c5a162011-05-26 16:46:14 +09002409 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002411 if (!calledFromValidUser()) {
2412 return;
2413 }
satok28203512010-11-24 11:06:49 +09002414 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2415 }
2416
satok42c5a162011-05-26 16:46:14 +09002417 @Override
satok28203512010-11-24 11:06:49 +09002418 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002419 if (!calledFromValidUser()) {
2420 return;
2421 }
satok28203512010-11-24 11:06:49 +09002422 synchronized (mMethodMap) {
2423 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002424 setInputMethodWithSubtypeIdLocked(token, id,
2425 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2426 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002427 } else {
2428 setInputMethod(token, id);
2429 }
2430 }
satokab751aa2010-09-14 19:17:36 +09002431 }
2432
satok42c5a162011-05-26 16:46:14 +09002433 @Override
satokb416a712010-11-25 20:42:14 +09002434 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002435 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002436 if (!calledFromValidUser()) {
2437 return;
2438 }
satokb416a712010-11-25 20:42:14 +09002439 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002440 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2441 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002442 }
2443 }
2444
satok4fc87d62011-05-20 16:13:43 +09002445 @Override
satok735cf382010-11-11 20:40:09 +09002446 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002447 if (!calledFromValidUser()) {
2448 return false;
2449 }
satok735cf382010-11-11 20:40:09 +09002450 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002451 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002452 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002453 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002454 lastImi = mMethodMap.get(lastIme.first);
2455 } else {
2456 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002457 }
satok4fc87d62011-05-20 16:13:43 +09002458 String targetLastImiId = null;
2459 int subtypeId = NOT_A_SUBTYPE_ID;
2460 if (lastIme != null && lastImi != null) {
2461 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2462 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2463 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2464 : mCurrentSubtype.hashCode();
2465 // If the last IME is the same as the current IME and the last subtype is not
2466 // defined, there is no need to switch to the last IME.
2467 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2468 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002469 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002470 }
2471 }
2472
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002473 if (TextUtils.isEmpty(targetLastImiId)
2474 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002475 // This is a safety net. If the currentSubtype can't be added to the history
2476 // and the framework couldn't find the last ime, we will make the last ime be
2477 // the most applicable enabled keyboard subtype of the system imes.
2478 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2479 if (enabled != null) {
2480 final int N = enabled.size();
2481 final String locale = mCurrentSubtype == null
2482 ? mRes.getConfiguration().locale.toString()
2483 : mCurrentSubtype.getLocale();
2484 for (int i = 0; i < N; ++i) {
2485 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002486 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002487 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002488 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2489 InputMethodUtils.getSubtypes(imi),
2490 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002491 if (keyboardSubtype != null) {
2492 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002493 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002494 imi, keyboardSubtype.hashCode());
2495 if(keyboardSubtype.getLocale().equals(locale)) {
2496 break;
2497 }
2498 }
2499 }
2500 }
2501 }
2502 }
2503
2504 if (!TextUtils.isEmpty(targetLastImiId)) {
2505 if (DEBUG) {
2506 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2507 + ", from: " + mCurMethodId + ", " + subtypeId);
2508 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002509 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002510 return true;
2511 } else {
2512 return false;
2513 }
satok735cf382010-11-11 20:40:09 +09002514 }
2515 }
2516
satoke7c6998e2011-06-03 17:57:59 +09002517 @Override
satok688bd472012-02-09 20:09:17 +09002518 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002519 if (!calledFromValidUser()) {
2520 return false;
2521 }
satok688bd472012-02-09 20:09:17 +09002522 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002523 if (!calledWithValidToken(token)) {
2524 final int uid = Binder.getCallingUid();
2525 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2526 + " token:" + token);
2527 return false;
2528 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002529 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002530 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2531 if (nextSubtype == null) {
2532 return false;
2533 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002534 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2535 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002536 return true;
2537 }
2538 }
2539
2540 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002541 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2542 if (!calledFromValidUser()) {
2543 return false;
2544 }
2545 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002546 if (!calledWithValidToken(token)) {
2547 final int uid = Binder.getCallingUid();
2548 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2549 + "token. uid:" + uid + " token:" + token);
2550 return false;
2551 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002552 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002553 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2554 if (nextSubtype == null) {
2555 return false;
2556 }
2557 return true;
2558 }
2559 }
2560
2561 @Override
satok68f1b782011-04-11 14:26:04 +09002562 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002563 if (!calledFromValidUser()) {
2564 return null;
2565 }
satok68f1b782011-04-11 14:26:04 +09002566 synchronized (mMethodMap) {
2567 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2568 // TODO: Handle the case of the last IME with no subtypes
2569 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2570 || TextUtils.isEmpty(lastIme.second)) return null;
2571 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2572 if (lastImi == null) return null;
2573 try {
2574 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002575 final int lastSubtypeId =
2576 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002577 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2578 return null;
2579 }
2580 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002581 } catch (NumberFormatException e) {
2582 return null;
2583 }
2584 }
2585 }
2586
satoke7c6998e2011-06-03 17:57:59 +09002587 @Override
satokee5e77c2011-09-02 18:50:15 +09002588 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002589 if (!calledFromValidUser()) {
2590 return;
2591 }
satok91e88122011-07-18 11:11:42 +09002592 // By this IPC call, only a process which shares the same uid with the IME can add
2593 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002594 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002595 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002596 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002597 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002598 final String[] packageInfos;
2599 try {
2600 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2601 } catch (RemoteException e) {
2602 Slog.e(TAG, "Failed to get package infos");
2603 return;
2604 }
satok91e88122011-07-18 11:11:42 +09002605 if (packageInfos != null) {
2606 final int packageNum = packageInfos.length;
2607 for (int i = 0; i < packageNum; ++i) {
2608 if (packageInfos[i].equals(imi.getPackageName())) {
2609 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002610 final long ident = Binder.clearCallingIdentity();
2611 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002612 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002613 } finally {
2614 Binder.restoreCallingIdentity(ident);
2615 }
satokee5e77c2011-09-02 18:50:15 +09002616 return;
satok91e88122011-07-18 11:11:42 +09002617 }
2618 }
2619 }
satoke7c6998e2011-06-03 17:57:59 +09002620 }
satokee5e77c2011-09-02 18:50:15 +09002621 return;
satoke7c6998e2011-06-03 17:57:59 +09002622 }
2623
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002624 @Override
2625 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002626 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002627 }
2628
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002629 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002630 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002631 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002632 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002633 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002634 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002635 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2636 if (DEBUG) {
2637 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2638 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2639 + " actual: " + sequenceNumber);
2640 }
2641 return;
2642 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002643 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2644 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002645 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002646 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002647 }
2648 }
2649
satok28203512010-11-24 11:06:49 +09002650 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002652 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2653 }
2654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002656 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2657 if (token == null) {
2658 if (mContext.checkCallingOrSelfPermission(
2659 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2660 != PackageManager.PERMISSION_GRANTED) {
2661 throw new SecurityException(
2662 "Using null token requires permission "
2663 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002665 } else if (mCurToken != token) {
2666 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2667 + " token: " + token);
2668 return;
2669 }
2670
2671 final long ident = Binder.clearCallingIdentity();
2672 try {
2673 setInputMethodLocked(id, subtypeId);
2674 } finally {
2675 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 }
2677 }
2678
satok42c5a162011-05-26 16:46:14 +09002679 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002681 if (!calledFromValidUser()) {
2682 return;
2683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002685 if (!calledWithValidToken(token)) {
2686 final int uid = Binder.getCallingUid();
2687 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2688 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 return;
2690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 long ident = Binder.clearCallingIdentity();
2692 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002693 hideCurrentInputLocked(flags, null);
2694 } finally {
2695 Binder.restoreCallingIdentity(ident);
2696 }
2697 }
2698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002699
satok42c5a162011-05-26 16:46:14 +09002700 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002701 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002702 if (!calledFromValidUser()) {
2703 return;
2704 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002705 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002706 if (!calledWithValidToken(token)) {
2707 final int uid = Binder.getCallingUid();
2708 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2709 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002710 return;
2711 }
2712 long ident = Binder.clearCallingIdentity();
2713 try {
2714 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 } finally {
2716 Binder.restoreCallingIdentity(ident);
2717 }
2718 }
2719 }
2720
2721 void setEnabledSessionInMainThread(SessionState session) {
2722 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002723 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002725 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002726 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 } catch (RemoteException e) {
2728 }
2729 }
2730 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002731 if (mEnabledSession != null && mEnabledSession.session != null) {
2732 try {
2733 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2734 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2735 } catch (RemoteException e) {
2736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 }
2738 }
2739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002740
satok42c5a162011-05-26 16:46:14 +09002741 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002743 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002745 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002746 final boolean showAuxSubtypes;
2747 switch (msg.arg1) {
2748 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2749 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2750 // implemented so that auxiliary subtypes will be excluded when the soft
2751 // keyboard is invisible.
2752 showAuxSubtypes = mInputShown;
2753 break;
2754 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2755 showAuxSubtypes = true;
2756 break;
2757 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2758 showAuxSubtypes = false;
2759 break;
2760 default:
2761 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2762 return false;
2763 }
2764 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002765 return true;
2766
satok47a44912010-10-06 16:03:58 +09002767 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002768 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002769 return true;
2770
2771 case MSG_SHOW_IM_CONFIG:
2772 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002773 return true;
2774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 case MSG_UNBIND_INPUT:
2778 try {
2779 ((IInputMethod)msg.obj).unbindInput();
2780 } catch (RemoteException e) {
2781 // There is nothing interesting about the method dying.
2782 }
2783 return true;
2784 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002785 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 try {
2787 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2788 } catch (RemoteException e) {
2789 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002790 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 return true;
2792 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002793 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002795 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002796 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002797 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 } catch (RemoteException e) {
2799 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002800 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 return true;
2802 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002803 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002805 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002806 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002807 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 } catch (RemoteException e) {
2809 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002810 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 return true;
2812 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002813 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002815 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2817 } catch (RemoteException e) {
2818 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002819 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002821 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002822 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002823 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002824 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002826 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002828 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002829 // Dispose the channel if the input method is not local to this process
2830 // because the remote proxy will get its own copy when unparceled.
2831 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002832 channel.dispose();
2833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002835 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002839
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002840 case MSG_START_INPUT: {
2841 int missingMethods = msg.arg1;
2842 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002844 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002846 session.method.startInput((IInputContext) args.arg2, missingMethods,
2847 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 } catch (RemoteException e) {
2849 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002850 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002852 }
2853 case MSG_RESTART_INPUT: {
2854 int missingMethods = msg.arg1;
2855 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002857 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002859 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2860 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 } catch (RemoteException e) {
2862 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002863 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002868
Yohei Yukawa33e81792015-11-17 21:14:42 -08002869 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002871 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 } catch (RemoteException e) {
2873 // There is nothing interesting about the last client dying.
2874 }
2875 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002876 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002877 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002878 IInputMethodClient client = (IInputMethodClient)args.arg1;
2879 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002881 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002883 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002884 } finally {
2885 // Dispose the channel if the input method is not local to this process
2886 // because the remote proxy will get its own copy when unparceled.
2887 if (res.channel != null && Binder.isProxy(client)) {
2888 res.channel.dispose();
2889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002891 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002893 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002894 case MSG_SET_ACTIVE:
2895 try {
2896 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2897 } catch (RemoteException e) {
2898 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2899 + ((ClientState)msg.obj).pid + " uid "
2900 + ((ClientState)msg.obj).uid);
2901 }
2902 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002903 case MSG_SET_INTERACTIVE:
2904 handleSetInteractive(msg.arg1 != 0);
2905 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002906 case MSG_SWITCH_IME:
2907 handleSwitchInputMethod(msg.arg1 != 0);
2908 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002909 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2910 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002911 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002912 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002913 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002914 } catch (RemoteException e) {
2915 Slog.w(TAG, "Got RemoteException sending "
2916 + "setUserActionNotificationSequenceNumber("
2917 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002918 + clientState.pid + " uid "
2919 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002920 }
2921 return true;
2922 }
satok01038492012-04-09 21:08:27 +09002923
2924 // --------------------------------------------------------------
2925 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002926 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002927 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 }
2929 return false;
2930 }
2931
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002932 private void handleSetInteractive(final boolean interactive) {
2933 synchronized (mMethodMap) {
2934 mIsInteractive = interactive;
2935 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2936
2937 // Inform the current client of the change in active status
2938 if (mCurClient != null && mCurClient.client != null) {
2939 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2940 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2941 }
2942 }
2943 }
2944
Yohei Yukawaae61f712015-12-09 13:00:10 -08002945 private void handleSwitchInputMethod(final boolean forwardDirection) {
2946 synchronized (mMethodMap) {
2947 // TODO: Support forwardDirection.
2948 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2949 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2950 if (nextSubtype == null) {
2951 return;
2952 }
2953 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2954 }
2955 }
2956
satokdc9ddae2011-10-06 12:22:36 +09002957 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002958 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2959 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002960 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002961 if (DEBUG) {
2962 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2963 }
satok723a27e2010-11-11 14:58:11 +09002964 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002965 return true;
2966 }
2967
2968 return false;
2969 }
2970
Yohei Yukawa94e33302016-02-12 19:37:03 -08002971 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002972 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002973 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002974 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002975 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08002976 mMethodList.clear();
2977 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002978
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002979 // Use for queryIntentServicesAsUser
2980 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981
Yohei Yukawaed4952a2016-02-17 07:57:25 -08002982 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
2983 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
2984 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002985 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002987 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2988 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002989
satoke7c6998e2011-06-03 17:57:59 +09002990 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2991 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 for (int i = 0; i < services.size(); ++i) {
2993 ResolveInfo ri = services.get(i);
2994 ServiceInfo si = ri.serviceInfo;
2995 ComponentName compName = new ComponentName(si.packageName, si.name);
2996 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2997 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002998 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 + ": it does not require the permission "
3000 + android.Manifest.permission.BIND_INPUT_METHOD);
3001 continue;
3002 }
3003
Joe Onorato8a9b2202010-02-26 18:56:32 -08003004 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005
3006 try {
satoke7c6998e2011-06-03 17:57:59 +09003007 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003008 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003009 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003010 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011
3012 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003013 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003015 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003016 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 }
3018 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003019
Yohei Yukawa859df052016-02-17 07:56:46 -08003020 // TODO: The following code should find better place to live.
3021 if (!resetDefaultEnabledIme) {
3022 boolean enabledImeFound = false;
3023 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3024 final int N = enabledImes.size();
3025 for (int i = 0; i < N; ++i) {
3026 final InputMethodInfo imi = enabledImes.get(i);
3027 if (mMethodList.contains(imi)) {
3028 enabledImeFound = true;
3029 break;
3030 }
3031 }
3032 if (!enabledImeFound) {
3033 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3034 resetDefaultEnabledIme = true;
3035 resetSelectedInputMethodAndSubtypeLocked("");
3036 }
3037 }
3038
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003039 if (resetDefaultEnabledIme) {
3040 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003041 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003042 final int N = defaultEnabledIme.size();
3043 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003044 final InputMethodInfo imi = defaultEnabledIme.get(i);
3045 if (DEBUG) {
3046 Slog.d(TAG, "--- enable ime = " + imi);
3047 }
3048 setInputMethodEnabledLocked(imi.getId(), true);
3049 }
3050 }
3051
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003052 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003053 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003054 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003055 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3056 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003057 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003058 }
3059 } else {
3060 // Double check that the default IME is certainly enabled.
3061 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003062 }
3063 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003064 // Here is not the perfect place to reset the switching controller. Ideally
3065 // mSwitchingController and mSettings should be able to share the same state.
3066 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3067 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003068 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003072
satok217f5482010-12-15 05:19:19 +09003073 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003074 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003075 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003076 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3077 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003078 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003079 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003080 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003081 final int userId;
3082 synchronized (mMethodMap) {
3083 userId = mSettings.getCurrentUserId();
3084 }
3085 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003086 }
3087
3088 private void showConfigureInputMethods() {
3089 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3090 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3091 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3092 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003093 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003094 }
3095
satok2c93efc2012-04-02 19:33:47 +09003096 private boolean isScreenLocked() {
3097 return mKeyguardManager != null
3098 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3099 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003100
Seigo Nonaka14e13912015-05-06 21:04:13 -07003101 private void showInputMethodMenu(boolean showAuxSubtypes) {
3102 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003105 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003106
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003107 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003108 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003109 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003110
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003111 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003112 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003113 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3114 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003115 if (immis == null || immis.size() == 0) {
3116 return;
3117 }
3118
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003119 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120
satok688bd472012-02-09 20:09:17 +09003121 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003122 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003123 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003124
satokc3690562012-01-10 20:14:43 +09003125 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003126 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003127 if (currentSubtype != null) {
3128 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003129 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3130 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003131 }
3132 }
3133
Ken Wakasa761eb372011-03-04 19:06:18 +09003134 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003135 mIms = new InputMethodInfo[N];
3136 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003137 int checkedItem = 0;
3138 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003139 final ImeSubtypeListItem item = imList.get(i);
3140 mIms[i] = item.mImi;
3141 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003142 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003143 int subtypeId = mSubtypeIds[i];
3144 if ((subtypeId == NOT_A_SUBTYPE_ID)
3145 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3146 || (subtypeId == lastInputMethodSubtypeId)) {
3147 checkedItem = i;
3148 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003151
3152 final Context settingsContext = new ContextThemeWrapper(context,
3153 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3154
3155 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003156 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3157 @Override
3158 public void onCancel(DialogInterface dialog) {
3159 hideInputMethodMenu();
3160 }
3161 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003162
3163 final Context dialogContext = mDialogBuilder.getContext();
3164 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3165 com.android.internal.R.styleable.DialogPreference,
3166 com.android.internal.R.attr.alertDialogStyle, 0);
3167 final Drawable dialogIcon = a.getDrawable(
3168 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3169 a.recycle();
3170
3171 mDialogBuilder.setIcon(dialogIcon);
3172
Yohei Yukawad34e1482016-02-11 08:03:52 -08003173 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003174 final View tv = inflater.inflate(
3175 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3176 mDialogBuilder.setCustomTitle(tv);
3177
3178 // Setup layout for a toggle switch of the hardware keyboard
3179 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003180 mSwitchingDialogTitleView
3181 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003182 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003183 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003184 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003185 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003186 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003187 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3188 @Override
3189 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003190 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003191 // Ensure that the input method dialog is dismissed when changing
3192 // the hardware keyboard state.
3193 hideInputMethodMenu();
3194 }
3195 });
3196
Alan Viverette505e3ab2014-11-24 15:22:11 -08003197 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003198 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3199 final OnClickListener choiceListener = new OnClickListener() {
3200 @Override
3201 public void onClick(final DialogInterface dialog, final int which) {
3202 synchronized (mMethodMap) {
3203 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3204 || mSubtypeIds.length <= which) {
3205 return;
satok01038492012-04-09 21:08:27 +09003206 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003207 final InputMethodInfo im = mIms[which];
3208 int subtypeId = mSubtypeIds[which];
3209 adapter.mCheckedItem = which;
3210 adapter.notifyDataSetChanged();
3211 hideInputMethodMenu();
3212 if (im != null) {
3213 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3214 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003215 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003216 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003218 }
3219 }
3220 };
3221 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003223 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003224 final OnClickListener positiveListener = new OnClickListener() {
3225 @Override
3226 public void onClick(DialogInterface dialog, int whichButton) {
3227 showConfigureInputMethods();
3228 }
3229 };
satok82beadf2010-12-27 19:03:06 +09003230 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003231 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003234 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 mSwitchingDialog.getWindow().setType(
3236 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003237 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3238 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003239 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003240 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 mSwitchingDialog.show();
3242 }
3243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003244
Ken Wakasa05dbb652011-08-22 15:22:43 +09003245 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3246 private final LayoutInflater mInflater;
3247 private final int mTextViewResourceId;
3248 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003249 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003250 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3251 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3252 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003253
Ken Wakasa05dbb652011-08-22 15:22:43 +09003254 mTextViewResourceId = textViewResourceId;
3255 mItemsList = itemsList;
3256 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003257 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003258 }
3259
3260 @Override
3261 public View getView(int position, View convertView, ViewGroup parent) {
3262 final View view = convertView != null ? convertView
3263 : mInflater.inflate(mTextViewResourceId, null);
3264 if (position < 0 || position >= mItemsList.size()) return view;
3265 final ImeSubtypeListItem item = mItemsList.get(position);
3266 final CharSequence imeName = item.mImeName;
3267 final CharSequence subtypeName = item.mSubtypeName;
3268 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3269 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3270 if (TextUtils.isEmpty(subtypeName)) {
3271 firstTextView.setText(imeName);
3272 secondTextView.setVisibility(View.GONE);
3273 } else {
3274 firstTextView.setText(subtypeName);
3275 secondTextView.setText(imeName);
3276 secondTextView.setVisibility(View.VISIBLE);
3277 }
3278 final RadioButton radioButton =
3279 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3280 radioButton.setChecked(position == mCheckedItem);
3281 return view;
3282 }
3283 }
3284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003286 synchronized (mMethodMap) {
3287 hideInputMethodMenuLocked();
3288 }
3289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003290
The Android Open Source Project10592532009-03-18 17:39:46 -07003291 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003292 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293
The Android Open Source Project10592532009-03-18 17:39:46 -07003294 if (mSwitchingDialog != null) {
3295 mSwitchingDialog.dismiss();
3296 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003298
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003299 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003300 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003301 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003305
satok42c5a162011-05-26 16:46:14 +09003306 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003308 // TODO: Make this work even for non-current users?
3309 if (!calledFromValidUser()) {
3310 return false;
3311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 synchronized (mMethodMap) {
3313 if (mContext.checkCallingOrSelfPermission(
3314 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3315 != PackageManager.PERMISSION_GRANTED) {
3316 throw new SecurityException(
3317 "Requires permission "
3318 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3319 }
Anna Galusza9b278112016-01-04 11:37:37 -08003320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 long ident = Binder.clearCallingIdentity();
3322 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003323 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 } finally {
3325 Binder.restoreCallingIdentity(ident);
3326 }
3327 }
3328 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003329
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003330 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3331 // Make sure this is a valid input method.
3332 InputMethodInfo imm = mMethodMap.get(id);
3333 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003334 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003335 }
3336
satokd87c2592010-09-29 11:52:06 +09003337 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3338 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003339
satokd87c2592010-09-29 11:52:06 +09003340 if (enabled) {
3341 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3342 if (pair.first.equals(id)) {
3343 // We are enabling this input method, but it is already enabled.
3344 // Nothing to do. The previous state was enabled.
3345 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003346 }
3347 }
satokd87c2592010-09-29 11:52:06 +09003348 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3349 // Previous state was disabled.
3350 return false;
3351 } else {
3352 StringBuilder builder = new StringBuilder();
3353 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3354 builder, enabledInputMethodsList, id)) {
3355 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003356 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003357 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3358 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3359 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003360 }
3361 // Previous state was enabled.
3362 return true;
3363 } else {
3364 // We are disabling the input method but it is already disabled.
3365 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003366 return false;
3367 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003368 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003369 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003370
satok723a27e2010-11-11 14:58:11 +09003371 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3372 boolean setSubtypeOnly) {
3373 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003374 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003375
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003376 mCurUserActionNotificationSequenceNumber =
3377 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3378 if (DEBUG) {
3379 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3380 + mCurUserActionNotificationSequenceNumber);
3381 }
3382
3383 if (mCurClient != null && mCurClient.client != null) {
3384 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3385 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003386 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003387 }
3388
satok723a27e2010-11-11 14:58:11 +09003389 // Set Subtype here
3390 if (imi == null || subtypeId < 0) {
3391 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003392 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003393 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003394 if (subtypeId < imi.getSubtypeCount()) {
3395 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3396 mSettings.putSelectedSubtype(subtype.hashCode());
3397 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003398 } else {
3399 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003400 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003401 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003402 }
satokab751aa2010-09-14 19:17:36 +09003403 }
satok723a27e2010-11-11 14:58:11 +09003404
Yohei Yukawa68645a62016-02-17 07:54:20 -08003405 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003406 // Set InputMethod here
3407 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3408 }
3409 }
3410
3411 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3412 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3413 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3414 // newDefaultIme is empty when there is no candidate for the selected IME.
3415 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3416 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3417 if (subtypeHashCode != null) {
3418 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003419 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003420 imi, Integer.valueOf(subtypeHashCode));
3421 } catch (NumberFormatException e) {
3422 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3423 }
3424 }
3425 }
3426 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003427 }
3428
satok4e4569d2010-11-19 18:45:53 +09003429 // If there are no selected shortcuts, tries finding the most applicable ones.
3430 private Pair<InputMethodInfo, InputMethodSubtype>
3431 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3432 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3433 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003434 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003435 boolean foundInSystemIME = false;
3436
3437 // Search applicable subtype for each InputMethodInfo
3438 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003439 final String imiId = imi.getId();
3440 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3441 continue;
3442 }
satokcd7cd292010-11-20 15:46:23 +09003443 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003444 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003445 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003446 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003447 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003448 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003449 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003450 }
satokdf31ae62011-01-15 06:19:44 +09003451 // 2. Search by the system locale from enabledSubtypes.
3452 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003453 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003454 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003455 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003456 }
satoka86f5e42011-09-02 17:12:42 +09003457 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003458 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003459 final ArrayList<InputMethodSubtype> subtypesForSearch =
3460 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003461 ? InputMethodUtils.getSubtypes(imi)
3462 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003463 // 4. Search by the current subtype's locale from all subtypes.
3464 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003465 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003466 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003467 }
3468 // 5. Search by the system locale from all subtypes.
3469 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003470 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003471 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003472 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003473 }
satokcd7cd292010-11-20 15:46:23 +09003474 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003475 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003476 // The current input method is the most applicable IME.
3477 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003478 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003479 break;
satok7599a7f2010-12-22 13:45:23 +09003480 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003481 // The system input method is 2nd applicable IME.
3482 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003483 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003484 if ((imi.getServiceInfo().applicationInfo.flags
3485 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3486 foundInSystemIME = true;
3487 }
satok4e4569d2010-11-19 18:45:53 +09003488 }
3489 }
3490 }
3491 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003492 if (mostApplicableIMI != null) {
3493 Slog.w(TAG, "Most applicable shortcut input method was:"
3494 + mostApplicableIMI.getId());
3495 if (mostApplicableSubtype != null) {
3496 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3497 + "," + mostApplicableSubtype.getMode() + ","
3498 + mostApplicableSubtype.getLocale());
3499 }
3500 }
satok4e4569d2010-11-19 18:45:53 +09003501 }
satokcd7cd292010-11-20 15:46:23 +09003502 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003503 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003504 } else {
3505 return null;
3506 }
3507 }
3508
satokab751aa2010-09-14 19:17:36 +09003509 /**
3510 * @return Return the current subtype of this input method.
3511 */
satok42c5a162011-05-26 16:46:14 +09003512 @Override
satokab751aa2010-09-14 19:17:36 +09003513 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003514 // TODO: Make this work even for non-current users?
3515 if (!calledFromValidUser()) {
3516 return null;
3517 }
3518 synchronized (mMethodMap) {
3519 return getCurrentInputMethodSubtypeLocked();
3520 }
3521 }
3522
3523 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003524 if (mCurMethodId == null) {
3525 return null;
3526 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003527 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003528 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3529 if (imi == null || imi.getSubtypeCount() == 0) {
3530 return null;
satok4e4569d2010-11-19 18:45:53 +09003531 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003532 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003533 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3534 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003535 if (subtypeId == NOT_A_SUBTYPE_ID) {
3536 // If there are no selected subtypes, the framework will try to find
3537 // the most applicable subtype from explicitly or implicitly enabled
3538 // subtypes.
3539 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003540 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003541 // If there is only one explicitly or implicitly enabled subtype,
3542 // just returns it.
3543 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3544 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3545 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003546 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003547 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003548 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003549 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003550 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003551 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3552 true);
satok4e4569d2010-11-19 18:45:53 +09003553 }
satok3ef8b292010-11-23 06:06:29 +09003554 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003555 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003556 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003557 }
3558 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003559 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003560 }
3561
satok4e4569d2010-11-19 18:45:53 +09003562 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003563 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003564 @Override
satok4e4569d2010-11-19 18:45:53 +09003565 public List getShortcutInputMethodsAndSubtypes() {
3566 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003567 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003568 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003569 // If there are no selected shortcut subtypes, the framework will try to find
3570 // the most applicable subtype from all subtypes whose mode is
3571 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003572 Pair<InputMethodInfo, InputMethodSubtype> info =
3573 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003574 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003575 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003576 ret.add(info.first);
3577 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003578 }
satok3da92232011-01-11 22:46:30 +09003579 return ret;
satokf3db1af2010-11-23 13:34:33 +09003580 }
satokf3db1af2010-11-23 13:34:33 +09003581 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3582 ret.add(imi);
3583 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3584 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003585 }
3586 }
satokf3db1af2010-11-23 13:34:33 +09003587 return ret;
satok4e4569d2010-11-19 18:45:53 +09003588 }
3589 }
3590
satok42c5a162011-05-26 16:46:14 +09003591 @Override
satokb66d2872010-11-10 01:04:04 +09003592 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003593 // TODO: Make this work even for non-current users?
3594 if (!calledFromValidUser()) {
3595 return false;
3596 }
satokb66d2872010-11-10 01:04:04 +09003597 synchronized (mMethodMap) {
3598 if (subtype != null && mCurMethodId != null) {
3599 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003600 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003601 if (subtypeId != NOT_A_SUBTYPE_ID) {
3602 setInputMethodLocked(mCurMethodId, subtypeId);
3603 return true;
3604 }
3605 }
3606 return false;
3607 }
3608 }
3609
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003610 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003611 private static class InputMethodFileManager {
3612 private static final String SYSTEM_PATH = "system";
3613 private static final String INPUT_METHOD_PATH = "inputmethod";
3614 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3615 private static final String NODE_SUBTYPES = "subtypes";
3616 private static final String NODE_SUBTYPE = "subtype";
3617 private static final String NODE_IMI = "imi";
3618 private static final String ATTR_ID = "id";
3619 private static final String ATTR_LABEL = "label";
3620 private static final String ATTR_ICON = "icon";
3621 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003622 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003623 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3624 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3625 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003626 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003627 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3628 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003629 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003630 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003631 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003632 if (methodMap == null) {
3633 throw new NullPointerException("methodMap is null");
3634 }
3635 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003636 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003637 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3638 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003639 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003640 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003641 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3642 }
3643 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3644 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3645 if (!subtypeFile.exists()) {
3646 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003647 writeAdditionalInputMethodSubtypes(
3648 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003649 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003650 readAdditionalInputMethodSubtypes(
3651 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003652 }
3653 }
3654
3655 private void deleteAllInputMethodSubtypes(String imiId) {
3656 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003657 mAdditionalSubtypesMap.remove(imiId);
3658 writeAdditionalInputMethodSubtypes(
3659 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003660 }
3661 }
3662
3663 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003664 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003665 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003666 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003667 final int N = additionalSubtypes.length;
3668 for (int i = 0; i < N; ++i) {
3669 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003670 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003671 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003672 } else {
3673 Slog.w(TAG, "Duplicated subtype definition found: "
3674 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003675 }
3676 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003677 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3678 writeAdditionalInputMethodSubtypes(
3679 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003680 }
3681 }
3682
3683 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3684 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003685 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003686 }
3687 }
3688
3689 private static void writeAdditionalInputMethodSubtypes(
3690 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3691 HashMap<String, InputMethodInfo> methodMap) {
3692 // Safety net for the case that this function is called before methodMap is set.
3693 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3694 FileOutputStream fos = null;
3695 try {
3696 fos = subtypesFile.startWrite();
3697 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003698 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003699 out.startDocument(null, true);
3700 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3701 out.startTag(null, NODE_SUBTYPES);
3702 for (String imiId : allSubtypes.keySet()) {
3703 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3704 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3705 continue;
3706 }
3707 out.startTag(null, NODE_IMI);
3708 out.attribute(null, ATTR_ID, imiId);
3709 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3710 final int N = subtypesList.size();
3711 for (int i = 0; i < N; ++i) {
3712 final InputMethodSubtype subtype = subtypesList.get(i);
3713 out.startTag(null, NODE_SUBTYPE);
3714 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3715 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3716 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003717 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3718 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003719 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3720 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3721 out.attribute(null, ATTR_IS_AUXILIARY,
3722 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003723 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3724 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003725 out.endTag(null, NODE_SUBTYPE);
3726 }
3727 out.endTag(null, NODE_IMI);
3728 }
3729 out.endTag(null, NODE_SUBTYPES);
3730 out.endDocument();
3731 subtypesFile.finishWrite(fos);
3732 } catch (java.io.IOException e) {
3733 Slog.w(TAG, "Error writing subtypes", e);
3734 if (fos != null) {
3735 subtypesFile.failWrite(fos);
3736 }
3737 }
3738 }
3739
3740 private static void readAdditionalInputMethodSubtypes(
3741 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3742 if (allSubtypes == null || subtypesFile == null) return;
3743 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003744 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003745 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003746 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003747 int type = parser.getEventType();
3748 // Skip parsing until START_TAG
3749 while ((type = parser.next()) != XmlPullParser.START_TAG
3750 && type != XmlPullParser.END_DOCUMENT) {}
3751 String firstNodeName = parser.getName();
3752 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3753 throw new XmlPullParserException("Xml doesn't start with subtypes");
3754 }
3755 final int depth =parser.getDepth();
3756 String currentImiId = null;
3757 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3758 while (((type = parser.next()) != XmlPullParser.END_TAG
3759 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3760 if (type != XmlPullParser.START_TAG)
3761 continue;
3762 final String nodeName = parser.getName();
3763 if (NODE_IMI.equals(nodeName)) {
3764 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3765 if (TextUtils.isEmpty(currentImiId)) {
3766 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3767 continue;
3768 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003769 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003770 allSubtypes.put(currentImiId, tempSubtypesArray);
3771 } else if (NODE_SUBTYPE.equals(nodeName)) {
3772 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3773 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3774 continue;
3775 }
3776 final int icon = Integer.valueOf(
3777 parser.getAttributeValue(null, ATTR_ICON));
3778 final int label = Integer.valueOf(
3779 parser.getAttributeValue(null, ATTR_LABEL));
3780 final String imeSubtypeLocale =
3781 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003782 final String languageTag =
3783 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003784 final String imeSubtypeMode =
3785 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3786 final String imeSubtypeExtraValue =
3787 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003788 final boolean isAuxiliary = "1".equals(String.valueOf(
3789 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003790 final boolean isAsciiCapable = "1".equals(String.valueOf(
3791 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003792 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3793 .setSubtypeNameResId(label)
3794 .setSubtypeIconResId(icon)
3795 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003796 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003797 .setSubtypeMode(imeSubtypeMode)
3798 .setSubtypeExtraValue(imeSubtypeExtraValue)
3799 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003800 .setIsAsciiCapable(isAsciiCapable)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003801 .build();
satoke7c6998e2011-06-03 17:57:59 +09003802 tempSubtypesArray.add(subtype);
3803 }
3804 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003805 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3806 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003807 return;
satoke7c6998e2011-06-03 17:57:59 +09003808 }
3809 }
3810 }
3811
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003812 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3813 @NonNull
3814 private final Handler mHandler;
3815
3816 LocalServiceImpl(@NonNull final Handler handler) {
3817 mHandler = handler;
3818 }
3819
3820 @Override
3821 public void setInteractive(boolean interactive) {
3822 // Do everything in handler so as not to block the caller.
3823 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3824 interactive ? 1 : 0, 0));
3825 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003826
3827 @Override
3828 public void switchInputMethod(boolean forwardDirection) {
3829 // Do everything in handler so as not to block the caller.
3830 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3831 forwardDirection ? 1 : 0, 0));
3832 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003833 }
3834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 @Override
3836 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3837 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3838 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3841 + Binder.getCallingPid()
3842 + ", uid=" + Binder.getCallingUid());
3843 return;
3844 }
3845
3846 IInputMethod method;
3847 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003848 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 synchronized (mMethodMap) {
3853 p.println("Current Input Method Manager state:");
3854 int N = mMethodList.size();
3855 p.println(" Input Methods:");
3856 for (int i=0; i<N; i++) {
3857 InputMethodInfo info = mMethodList.get(i);
3858 p.println(" InputMethod #" + i + ":");
3859 info.dump(p, " ");
3860 }
3861 p.println(" Clients:");
3862 for (ClientState ci : mClients.values()) {
3863 p.println(" Client " + ci + ":");
3864 p.println(" client=" + ci.client);
3865 p.println(" inputContext=" + ci.inputContext);
3866 p.println(" sessionRequested=" + ci.sessionRequested);
3867 p.println(" curSession=" + ci.curSession);
3868 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003869 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003871 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3872 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003873 focusedWindowClient = mCurFocusedWindowClient;
3874 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3876 + " mBoundToMethod=" + mBoundToMethod);
3877 p.println(" mCurToken=" + mCurToken);
3878 p.println(" mCurIntent=" + mCurIntent);
3879 method = mCurMethod;
3880 p.println(" mCurMethod=" + mCurMethod);
3881 p.println(" mEnabledSession=" + mEnabledSession);
3882 p.println(" mShowRequested=" + mShowRequested
3883 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3884 + " mShowForced=" + mShowForced
3885 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003886 p.println(" mCurUserActionNotificationSequenceNumber="
3887 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003888 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003889 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003890 p.println(" mSwitchingController:");
3891 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003892 p.println(" mSettings:");
3893 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003895
Jeff Brownb88102f2010-09-08 11:49:43 -07003896 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 pw.flush();
3899 try {
3900 client.client.asBinder().dump(fd, args);
3901 } catch (RemoteException e) {
3902 p.println("Input method client dead: " + e);
3903 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003904 } else {
3905 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003907
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003908 if (focusedWindowClient != null && client != focusedWindowClient) {
3909 p.println(" ");
3910 p.println("Warning: Current input method client doesn't match the last focused. "
3911 + "window.");
3912 p.println("Dumping input method client in the last focused window just in case.");
3913 p.println(" ");
3914 pw.flush();
3915 try {
3916 focusedWindowClient.client.asBinder().dump(fd, args);
3917 } catch (RemoteException e) {
3918 p.println("Input method client in focused window dead: " + e);
3919 }
3920 }
3921
Jeff Brownb88102f2010-09-08 11:49:43 -07003922 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 pw.flush();
3925 try {
3926 method.asBinder().dump(fd, args);
3927 } catch (RemoteException e) {
3928 p.println("Input method service dead: " + e);
3929 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003930 } else {
3931 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 }
3933 }
3934}