blob: 811e34e00bd26a1579294f9d981b5a92709c8139 [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;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700128import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
satoke7c6998e2011-06-03 17:57:59 +0900130import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900132import java.io.FileInputStream;
133import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.io.IOException;
135import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100136import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900138import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139import java.util.HashMap;
140import java.util.List;
141
142/**
143 * This class provides a system service that manages input methods.
144 */
145public class InputMethodManagerService extends IInputMethodManager.Stub
146 implements ServiceConnection, Handler.Callback {
147 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700148 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700149 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700151 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
152 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
153 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 static final int MSG_UNBIND_INPUT = 1000;
156 static final int MSG_BIND_INPUT = 1010;
157 static final int MSG_SHOW_SOFT_INPUT = 1020;
158 static final int MSG_HIDE_SOFT_INPUT = 1030;
159 static final int MSG_ATTACH_TOKEN = 1040;
160 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 static final int MSG_START_INPUT = 2000;
163 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164
Yohei Yukawa33e81792015-11-17 21:14:42 -0800165 static final int MSG_UNBIND_CLIENT = 3000;
166 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700167 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700168 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900169 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800170 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
satok01038492012-04-09 21:08:27 +0900172 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
173
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700174 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
satokf9f01002011-05-19 21:31:50 +0900176 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
177
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900178 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900179 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900180
satok4e4569d2010-11-19 18:45:53 +0900181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800183 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900185 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700188 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700190 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900191 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900192 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900193 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800194 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900196 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 // All known input methods. mMethodMap also serves as the global
199 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700200 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
201 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900202 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700203 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900204 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700206 // Used to bring IME service up to visible adjustment while it is being shown.
207 final ServiceConnection mVisibleConnection = new ServiceConnection() {
208 @Override public void onServiceConnected(ComponentName name, IBinder service) {
209 }
210
211 @Override public void onServiceDisconnected(ComponentName name) {
212 }
213 };
214 boolean mVisibleBound = false;
215
satok7cfc0ed2011-06-20 21:29:36 +0900216 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700217 private NotificationManager mNotificationManager;
218 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700219 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400220 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700221 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900222 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900223 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900224 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900225
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900226 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 final ClientState client;
228 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700229
230 IInputMethodSession session;
231 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 @Override
234 public String toString() {
235 return "SessionState{uid " + client.uid + " pid " + client.pid
236 + " method " + Integer.toHexString(
237 System.identityHashCode(method))
238 + " session " + Integer.toHexString(
239 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700240 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 + "}";
242 }
243
244 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700245 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 client = _client;
247 method = _method;
248 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700249 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800252
Jeff Brownc28867a2013-03-26 15:42:39 -0700253 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 final IInputMethodClient client;
255 final IInputContext inputContext;
256 final int uid;
257 final int pid;
258 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 boolean sessionRequested;
261 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 @Override
264 public String toString() {
265 return "ClientState{" + Integer.toHexString(
266 System.identityHashCode(this)) + " uid " + uid
267 + " pid " + pid + "}";
268 }
269
270 ClientState(IInputMethodClient _client, IInputContext _inputContext,
271 int _uid, int _pid) {
272 client = _client;
273 inputContext = _inputContext;
274 uid = _uid;
275 pid = _pid;
276 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
277 }
278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700280 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700283 * Set once the system is ready to run third party code.
284 */
285 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700287 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700288 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
289 * method. This is to be synchronized with the secure settings keyed with
290 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
291 *
292 * <p>This can be transiently {@code null} when the system is re-initializing input method
293 * settings, e.g., the system locale is just changed.</p>
294 *
295 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
296 * {@link InputMethodManagerService}.</p>
297 *
298 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700300 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * The current binding sequence number, incremented every time there is
305 * a new bind performed.
306 */
307 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The client that is currently bound to an input method.
311 */
312 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800315 * The last window token that we confirmed to be focused. This is always updated upon reports
316 * from the input method client. If the window state is already changed before the report is
317 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700318 */
319 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700321 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800322 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
323 */
324 ClientState mCurFocusedWindowClient;
325
326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 * The input context last provided by the current client.
328 */
329 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700332 * The missing method flags for the input context last provided by the current client.
333 *
334 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
335 */
336 int mCurInputContextMissingMethods;
337
338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 * The attributes last provided by the current client.
340 */
341 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700344 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700346 *
347 * <p>This can be {@code null} when no input method is connected.</p>
348 *
349 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700351 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 /**
satokab751aa2010-09-14 19:17:36 +0900355 * The current subtype of the current input method.
356 */
357 private InputMethodSubtype mCurrentSubtype;
358
satok4e4569d2010-11-19 18:45:53 +0900359 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900360 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700361 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900362
John Spurlocke0980502013-10-25 11:59:29 -0400363 // Was the keyguard locked when this client became current?
364 private boolean mCurClientInKeyguard;
365
satokab751aa2010-09-14 19:17:36 +0900366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 * Set to true if our ServiceConnection is currently actively bound to
368 * a service (whether or not we have gotten its IBinder back yet).
369 */
370 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
373 * Set if the client has asked for the input method to be shown.
374 */
375 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 /**
378 * Set if we were explicitly told to show the input method.
379 */
380 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /**
383 * Set if we were forced to be shown.
384 */
385 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Set if we last told the input method to show itself.
389 */
390 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * The Intent used to connect to the current input method.
394 */
395 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 /**
398 * The token we have made for the currently active input method, to
399 * identify it in the future.
400 */
401 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
404 * If non-null, this is the input method service we are currently connected
405 * to.
406 */
407 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 /**
410 * Time that we last initiated a bind to the input method, to determine
411 * if we should try to disconnect and reconnect to it.
412 */
413 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 /**
416 * Have we called mCurMethod.bindInput()?
417 */
418 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 /**
421 * Currently enabled session. Only touched by service thread, not
422 * protected by a lock.
423 */
424 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700427 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700429 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900431 int mCurUserActionNotificationSequenceNumber = 0;
432
Joe Onorato857fd9b2011-01-27 15:08:35 -0800433 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900434
435 /**
436 * A set of status bits regarding the active IME.
437 *
438 * <p>This value is a combination of following two bits:</p>
439 * <dl>
440 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
441 * <dd>
442 * If this bit is ON, connected IME is ready to accept touch/key events.
443 * </dd>
444 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
445 * <dd>
446 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
447 * </dd>
448 * </dl>
449 * <em>Do not update this value outside of setImeWindowStatus.</em>
450 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800451 int mImeWindowVis;
452
Ken Wakasa05dbb652011-08-22 15:22:43 +0900453 private AlertDialog.Builder mDialogBuilder;
454 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900455 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700456 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900457 private InputMethodInfo[] mIms;
458 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800459 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700460 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800461 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900462 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
463 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500464 private final String mSlotIme;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700467 int mUserId;
468 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700469 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800470 String mLastEnabled = "";
471
Yohei Yukawa81482972015-06-04 00:58:59 -0700472 /**
473 * <em>This constructor must be called within the lock.</em>
474 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 SettingsObserver(Handler handler) {
476 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700477 }
478
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800479 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700480 if (mRegistered && mUserId == userId) {
481 return;
482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700484 if (mRegistered) {
485 mContext.getContentResolver().unregisterContentObserver(this);
486 mRegistered = false;
487 }
488 if (mUserId != userId) {
489 mLastEnabled = "";
490 mUserId = userId;
491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700493 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900494 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700495 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900496 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700497 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700498 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700499 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800500 resolver.registerContentObserver(Settings.Secure.getUriFor(
501 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700502 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800504
Michael Wright7b5a96b2014-08-09 19:28:42 -0700505 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800506 final Uri showImeUri = Settings.Secure.getUriFor(
507 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
508 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
509 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700511 if (showImeUri.equals(uri)) {
512 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800513 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
514 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
515 mContext.getContentResolver(),
516 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
517 0, mUserId) == 1;
518 if (mAccessibilityRequestingNoSoftKeyboard) {
519 final boolean showRequested = mShowRequested;
520 hideCurrentInputLocked(0, null);
521 mShowRequested = showRequested;
522 } else if (mShowRequested) {
523 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
524 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700525 } else {
526 boolean enabledChanged = false;
527 String newEnabled = mSettings.getEnabledInputMethodsStr();
528 if (!mLastEnabled.equals(newEnabled)) {
529 mLastEnabled = newEnabled;
530 enabledChanged = true;
531 }
532 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700536
537 @Override
538 public String toString() {
539 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
540 + " mLastEnabled=" + mLastEnabled + "}";
541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800543
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900544 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900545 @Override
546 public void onReceive(Context context, Intent intent) {
547 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700548 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900549 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700550 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900551 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800552 } else if (Intent.ACTION_USER_ADDED.equals(action)
553 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100554 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800555 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700556 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
557 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
558 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
559 final String prevValue = intent.getStringExtra(
560 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
561 final String newValue = intent.getStringExtra(
562 Intent.EXTRA_SETTING_NEW_VALUE);
563 restoreEnabledInputMethods(mContext, prevValue, newValue);
564 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900565 } else {
566 Slog.w(TAG, "Unexpected intent " + intent);
567 }
568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700571 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
572 // does not attempt to validate on the fly with any installed device policy, so must only
573 // be run in the context of initial device setup.
574 //
575 // TODO: Move this method to InputMethodUtils with adding unit tests.
576 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
577 if (DEBUG_RESTORE) {
578 Slog.i(TAG, "Restoring enabled input methods:");
579 Slog.i(TAG, "prev=" + prevValue);
580 Slog.i(TAG, " new=" + newValue);
581 }
582 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900583 ArrayMap<String, ArraySet<String>> prevMap =
584 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
585 ArrayMap<String, ArraySet<String>> newMap =
586 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700587
588 // Merge the restored ime+subtype enabled states into the live state
589 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
590 final String imeId = entry.getKey();
591 ArraySet<String> prevSubtypes = prevMap.get(imeId);
592 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700593 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700594 prevMap.put(imeId, prevSubtypes);
595 }
596 prevSubtypes.addAll(entry.getValue());
597 }
598
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700599 final String mergedImesAndSubtypesString =
600 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700601 if (DEBUG_RESTORE) {
602 Slog.i(TAG, "Merged IME string:");
603 Slog.i(TAG, " " + mergedImesAndSubtypesString);
604 }
605 Settings.Secure.putString(context.getContentResolver(),
606 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
607 }
608
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800609 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900610 private boolean isChangingPackagesOfCurrentUser() {
611 final int userId = getChangingUserId();
612 final boolean retval = userId == mSettings.getCurrentUserId();
613 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900614 if (!retval) {
615 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
616 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900617 }
618 return retval;
619 }
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800622 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900623 if (!isChangingPackagesOfCurrentUser()) {
624 return false;
625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900627 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 final int N = mMethodList.size();
629 if (curInputMethodId != null) {
630 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800631 InputMethodInfo imi = mMethodList.get(i);
632 if (imi.getId().equals(curInputMethodId)) {
633 for (String pkg : packages) {
634 if (imi.getPackageName().equals(pkg)) {
635 if (!doit) {
636 return true;
637 }
satok723a27e2010-11-11 14:58:11 +0900638 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800639 chooseNewDefaultIMELocked();
640 return true;
641 }
642 }
643 }
644 }
645 }
646 }
647 return false;
648 }
649
650 @Override
651 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900652 if (!isChangingPackagesOfCurrentUser()) {
653 return;
654 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800655 synchronized (mMethodMap) {
656 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900657 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800658 final int N = mMethodList.size();
659 if (curInputMethodId != null) {
660 for (int i=0; i<N; i++) {
661 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900662 final String imiId = imi.getId();
663 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800664 curIm = imi;
665 }
satoke7c6998e2011-06-03 17:57:59 +0900666
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800667 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900668 if (isPackageModified(imi.getPackageName())) {
669 mFileManager.deleteAllInputMethodSubtypes(imiId);
670 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800671 if (change == PACKAGE_TEMPORARY_CHANGE
672 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800673 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 + imi.getComponent());
675 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
677 }
678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
Yohei Yukawa94e33302016-02-12 19:37:03 -0800680 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800684 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800685 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800686 if (change == PACKAGE_TEMPORARY_CHANGE
687 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800688 ServiceInfo si = null;
689 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900690 si = mIPackageManager.getServiceInfo(
691 curIm.getComponent(), 0, mSettings.getCurrentUserId());
692 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800693 }
694 if (si == null) {
695 // Uh oh, current input method is no longer around!
696 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800697 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900698 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800699 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800700 changed = true;
701 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800702 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900703 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800704 }
705 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800706 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800707 }
satokab751aa2010-09-14 19:17:36 +0900708
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800709 if (curIm == null) {
710 // We currently don't have a default input method... is
711 // one now available?
712 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700713 } else if (!changed && isPackageModified(curIm.getPackageName())) {
714 // Even if the current input method is still available, mCurrentSubtype could
715 // be obsolete when the package is modified in practice.
716 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800718
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800719 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800720 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722 }
723 }
724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800725
Jeff Brownc28867a2013-03-26 15:42:39 -0700726 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900727 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700728 private final IInputMethod mMethod;
729 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
Jeff Brownc28867a2013-03-26 15:42:39 -0700731 MethodCallback(InputMethodManagerService imms, IInputMethod method,
732 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900733 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700734 mMethod = method;
735 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
satoke7c6998e2011-06-03 17:57:59 +0900738 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700739 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700740 long ident = Binder.clearCallingIdentity();
741 try {
742 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
743 } finally {
744 Binder.restoreCallingIdentity(ident);
745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800748
satok01038492012-04-09 21:08:27 +0900749 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700750 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900751 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700752 public void onHardKeyboardStatusChange(boolean available) {
753 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
754 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900755 }
756
Michael Wright7b5a96b2014-08-09 19:28:42 -0700757 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900758 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700759 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900760 }
761 synchronized(mMethodMap) {
762 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
763 && mSwitchingDialog.isShowing()) {
764 mSwitchingDialogTitleView.findViewById(
765 com.android.internal.R.id.hard_keyboard_section).setVisibility(
766 available ? View.VISIBLE : View.GONE);
767 }
768 }
769 }
770 }
771
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800772 public static final class Lifecycle extends SystemService {
773 private InputMethodManagerService mService;
774
775 public Lifecycle(Context context) {
776 super(context);
777 mService = new InputMethodManagerService(context);
778 }
779
780 @Override
781 public void onStart() {
782 LocalServices.addService(InputMethodManagerInternal.class,
783 new LocalServiceImpl(mService.mHandler));
784 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
785 }
786
787 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800788 public void onSwitchUser(@UserIdInt int userHandle) {
789 // Called on the system server's main looper thread.
790 // TODO: Dispatch this to a worker thread as needed.
791 mService.onSwitchUser(userHandle);
792 }
793
794 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800795 public void onBootPhase(int phase) {
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800796 // Called on the system server's main looper thread.
797 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800798 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
799 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
800 .getService(Context.STATUS_BAR_SERVICE);
801 mService.systemRunning(statusBarService);
802 }
803 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800804
805 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800806 public void onUnlockUser(@UserIdInt int userHandle) {
807 // Called on the system server's main looper thread.
808 // TODO: Dispatch this to a worker thread as needed.
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800809 mService.onUnlockUser(userHandle);
810 }
811 }
812
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800813 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800814 synchronized(mMethodMap) {
815 final int currentUserId = mSettings.getCurrentUserId();
816 if (DEBUG) {
817 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
818 }
819 if (userId != currentUserId) {
820 return;
821 }
822 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
823 // We need to rebuild IMEs.
824 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
825 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
826 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800827 }
828
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800829 void onSwitchUser(@UserIdInt int userId) {
830 synchronized (mMethodMap) {
831 switchUserLocked(userId);
832 }
833 }
834
Seigo Nonaka7309b122015-08-17 18:34:13 -0700835 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800838 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700840 // Note: SettingsObserver doesn't register observers in its constructor.
841 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 mIWindowManager = IWindowManager.Stub.asInterface(
843 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700844 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800845 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900846 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 public void executeMessage(Message msg) {
848 handleMessage(msg);
849 }
Mita Yuned218c72012-12-06 17:18:25 -0800850 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800851 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800852 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900853 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700854 mHasFeature = context.getPackageManager().hasSystemFeature(
855 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500856 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
satok7cfc0ed2011-06-20 21:29:36 +0900857
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400858 Bundle extras = new Bundle();
859 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
860 mImeSwitcherNotification = new Notification.Builder(mContext)
861 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
862 .setWhen(0)
863 .setOngoing(true)
864 .addExtras(extras)
865 .setCategory(Notification.CATEGORY_SYSTEM)
866 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400867
satok7cfc0ed2011-06-20 21:29:36 +0900868 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900869 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900870
871 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900872
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900873 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900874 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800875 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
876 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700877 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900878 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800879
satok7cfc0ed2011-06-20 21:29:36 +0900880 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900881 int userId = 0;
882 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900883 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
884 } catch (RemoteException e) {
885 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
886 }
satok81f8b7c2012-12-04 20:42:56 +0900887 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900888
satokd87c2592010-09-29 11:52:06 +0900889 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900890 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800891 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700892
Kenny Guy2a764942014-04-02 13:29:20 +0100893 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900894 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900895 synchronized (mMethodMap) {
896 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
897 mSettings, context);
898 }
satok0a1bcf42012-05-16 19:26:31 +0900899
900 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900902 if (DEBUG) {
903 Slog.d(TAG, "Initial default ime = " + defaultImiId);
904 }
satok0a1bcf42012-05-16 19:26:31 +0900905 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
906
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900907 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800908 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900909 }
satokd87c2592010-09-29 11:52:06 +0900910 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911
satok0a1bcf42012-05-16 19:26:31 +0900912 if (!mImeSelectedOnBoot) {
913 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900914 synchronized (mMethodMap) {
915 resetDefaultImeLocked(context);
916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900919 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700920 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900921 updateFromSettingsLocked(true);
922 }
satok5b927c432012-05-01 20:09:34 +0900923
924 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
925 // according to the new system locale.
926 final IntentFilter filter = new IntentFilter();
927 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
928 mContext.registerReceiver(
929 new BroadcastReceiver() {
930 @Override
931 public void onReceive(Context context, Intent intent) {
932 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900933 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900934 }
935 }
936 }, filter);
937 }
938
satok5b927c432012-05-01 20:09:34 +0900939 private void resetDefaultImeLocked(Context context) {
940 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800941 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900942 return;
943 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800944 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
945 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
946 if (suitableImes.isEmpty()) {
947 Slog.i(TAG, "No default found");
948 return;
satok5b927c432012-05-01 20:09:34 +0900949 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800950 final InputMethodInfo defIm = suitableImes.get(0);
951 Slog.i(TAG, "Default found, using " + defIm.getId());
952 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900953 }
954
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900955 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
956 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 if (!mSystemReady) {
958 // not system ready
959 return;
960 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800961 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800963 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 if (!updateOnlyWhenLocaleChanged) {
965 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800966 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900967 }
968 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800969 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900970 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800971 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900972 if (!updateOnlyWhenLocaleChanged) {
973 final String selectedImiId = mSettings.getSelectedInputMethod();
974 if (TextUtils.isEmpty(selectedImiId)) {
975 // This is the first time of the user switch and
976 // set the current ime to the proper one.
977 resetDefaultImeLocked(mContext);
978 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900979 } else {
980 // If the locale is changed, needs to reset the default ime
981 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900982 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800983 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800984 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 if (!updateOnlyWhenLocaleChanged) {
986 try {
987 startInputInnerLocked();
988 } catch (RuntimeException e) {
989 Slog.w(TAG, "Unexpected exception", e);
990 }
991 }
992 }
993 }
994
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900995 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900996 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
997 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 }
999
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001000 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001001 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1002 + " currentUserId=" + mSettings.getCurrentUserId());
1003
Yohei Yukawa81482972015-06-04 00:58:59 -07001004 // ContentObserver should be registered again when the user is changed
1005 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001006
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001007 // If the system is not ready or the device is not yed unlocked by the user, then we use
1008 // copy-on-write settings.
1009 final boolean useCopyOnWriteSettings =
1010 !mSystemReady || !mUserManager.isUserUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001011 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001012 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001013 // InputMethodFileManager should be reset when the user is changed
1014 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001015 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001016
1017 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1018 + " defaultImiId=" + defaultImiId);
1019
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001020 // For secondary users, the list of enabled IMEs may not have been updated since the
1021 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1022 // not be empty even if the IME has been uninstalled by the primary user.
1023 // Even in such cases, IMMS works fine because it will find the most applicable
1024 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001025 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001026 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001027 initialUserSwitch /* needsToResetDefaultIme */);
1028 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001029 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1030 mSettings.getEnabledInputMethodListLocked(), newUserId,
1031 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001032 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001033
1034 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1035 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001036 }
1037
Kenny Guy2a764942014-04-02 13:29:20 +01001038 void updateCurrentProfileIds() {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001039 List<UserInfo> profiles = mUserManager.getProfiles(mSettings.getCurrentUserId());
Kenny Guy2a764942014-04-02 13:29:20 +01001040 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1041 for (int i = 0; i < currentProfileIds.length; i++) {
1042 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001043 }
Kenny Guy2a764942014-04-02 13:29:20 +01001044 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001045 }
1046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 @Override
1048 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1049 throws RemoteException {
1050 try {
1051 return super.onTransact(code, data, reply, flags);
1052 } catch (RuntimeException e) {
1053 // The input method manager only throws security exceptions, so let's
1054 // log all others.
1055 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001056 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 throw e;
1059 }
1060 }
1061
Svetoslav Ganova0027152013-06-25 14:59:53 -07001062 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001063 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001064 if (DEBUG) {
1065 Slog.d(TAG, "--- systemReady");
1066 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001067 if (!mSystemReady) {
1068 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001069 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001070 mSettings.switchCurrentUser(currentUserId,
1071 !mUserManager.isUserUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001072 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1073 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001074 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001075 if (mStatusBar != null) {
1076 mStatusBar.setIconVisibility(mSlotIme, false);
1077 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001078 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001079 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1080 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001081 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001082 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001083 mHardKeyboardListener);
1084 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001085 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001086 if (!mImeSelectedOnBoot) {
1087 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001088 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001089 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1090 mSettings.getEnabledInputMethodListLocked(),
1091 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001092 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001093 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001094 try {
1095 startInputInnerLocked();
1096 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001097 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001098 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001099 }
1100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001103 // ---------------------------------------------------------------------------------------
1104 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1105 // 1) it comes from the system process
1106 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1107 private boolean calledFromValidUser() {
1108 final int uid = Binder.getCallingUid();
1109 final int userId = UserHandle.getUserId(uid);
1110 if (DEBUG) {
1111 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1112 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1113 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001114 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1115 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001116 }
Kenny Guy2a764942014-04-02 13:29:20 +01001117 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001118 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001119 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001120
1121 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1122 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1123 // must not manage background users' states in any functions.
1124 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1125 // by a token.
1126 if (mContext.checkCallingOrSelfPermission(
1127 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1128 == PackageManager.PERMISSION_GRANTED) {
1129 if (DEBUG) {
1130 Slog.d(TAG, "--- Access granted because the calling process has "
1131 + "the INTERACT_ACROSS_USERS_FULL permission");
1132 }
1133 return true;
1134 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001135 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1136 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001137 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001138 }
1139
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001140
1141 /**
1142 * Returns true iff the caller is identified to be the current input method with the token.
1143 * @param token The window token given to the input method when it was started.
1144 * @return true if and only if non-null valid token is specified.
1145 */
1146 private boolean calledWithValidToken(IBinder token) {
1147 if (token == null || mCurToken != token) {
1148 return false;
1149 }
1150 return true;
1151 }
1152
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001153 private boolean bindCurrentInputMethodService(
1154 Intent service, ServiceConnection conn, int flags) {
1155 if (service == null || conn == null) {
1156 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1157 return false;
1158 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001159 return mContext.bindServiceAsUser(service, conn, flags,
1160 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001161 }
1162
satoke7c6998e2011-06-03 17:57:59 +09001163 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001165 // TODO: Make this work even for non-current users?
1166 if (!calledFromValidUser()) {
1167 return Collections.emptyList();
1168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001170 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 }
1173
satoke7c6998e2011-06-03 17:57:59 +09001174 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001176 // TODO: Make this work even for non-current users?
1177 if (!calledFromValidUser()) {
1178 return Collections.emptyList();
1179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001181 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
1184
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001185 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001186 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001187 * @return enabled subtypes of the specified imi
1188 */
satoke7c6998e2011-06-03 17:57:59 +09001189 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001190 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001191 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001192 // TODO: Make this work even for non-current users?
1193 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001194 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001195 }
satok67ddf9c2010-11-17 09:45:54 +09001196 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001197 final InputMethodInfo imi;
1198 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001199 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001200 } else {
1201 imi = mMethodMap.get(imiId);
1202 }
1203 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001204 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001205 }
1206 return mSettings.getEnabledInputMethodSubtypeListLocked(
1207 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001208 }
1209 }
1210
satoke7c6998e2011-06-03 17:57:59 +09001211 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 public void addClient(IInputMethodClient client,
1213 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001214 if (!calledFromValidUser()) {
1215 return;
1216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 synchronized (mMethodMap) {
1218 mClients.put(client.asBinder(), new ClientState(client,
1219 inputContext, uid, pid));
1220 }
1221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001222
satoke7c6998e2011-06-03 17:57:59 +09001223 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001225 if (!calledFromValidUser()) {
1226 return;
1227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001229 ClientState cs = mClients.remove(client.asBinder());
1230 if (cs != null) {
1231 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001232 if (mCurClient == cs) {
1233 mCurClient = null;
1234 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001235 if (mCurFocusedWindowClient == cs) {
1236 mCurFocusedWindowClient = null;
1237 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 void executeOrSendMessage(IInterface target, Message msg) {
1243 if (target.asBinder() instanceof Binder) {
1244 mCaller.sendMessage(msg);
1245 } else {
1246 handleMessage(msg);
1247 msg.recycle();
1248 }
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
Yohei Yukawa33e81792015-11-17 21:14:42 -08001251 void unbindCurrentClientLocked(
1252 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001254 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 + mCurClient.client.asBinder());
1256 if (mBoundToMethod) {
1257 mBoundToMethod = false;
1258 if (mCurMethod != null) {
1259 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1260 MSG_UNBIND_INPUT, mCurMethod));
1261 }
1262 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001263
1264 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1265 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001266 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1267 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
The Android Open Source Project10592532009-03-18 17:39:46 -07001271 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 private int getImeShowFlags() {
1276 int flags = 0;
1277 if (mShowForced) {
1278 flags |= InputMethod.SHOW_FORCED
1279 | InputMethod.SHOW_EXPLICIT;
1280 } else if (mShowExplicitlyRequested) {
1281 flags |= InputMethod.SHOW_EXPLICIT;
1282 }
1283 return flags;
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 private int getAppShowFlags() {
1287 int flags = 0;
1288 if (mShowForced) {
1289 flags |= InputMethodManager.SHOW_FORCED;
1290 } else if (!mShowExplicitlyRequested) {
1291 flags |= InputMethodManager.SHOW_IMPLICIT;
1292 }
1293 return flags;
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
Dianne Hackborn7663d802012-02-24 13:08:49 -08001296 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 if (!mBoundToMethod) {
1298 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1299 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1300 mBoundToMethod = true;
1301 }
1302 final SessionState session = mCurClient.curSession;
1303 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001304 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1305 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1306 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001308 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1309 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1310 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001313 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001314 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001316 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001317 (session.channel != null ? session.channel.dup() : null),
1318 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001320
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001321 InputBindResult startInputLocked(
1322 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001323 IInputMethodClient client, IInputContext inputContext,
1324 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001325 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 // If no method is currently selected, do nothing.
1327 if (mCurMethodId == null) {
1328 return mNoBinding;
1329 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 ClientState cs = mClients.get(client.asBinder());
1332 if (cs == null) {
1333 throw new IllegalArgumentException("unknown client "
1334 + client.asBinder());
1335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336
Yohei Yukawa74750f22016-03-22 12:54:22 -07001337 if (attribute == null) {
1338 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1339 + " uid=" + cs.uid + " pid=" + cs.pid);
1340 return null;
1341 }
1342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 try {
1344 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1345 // Check with the window manager to make sure this client actually
1346 // has a window with focus. If not, reject. This is thread safe
1347 // because if the focus changes some time before or after, the
1348 // next client receiving focus that has any interest in input will
1349 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001350 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1352 return null;
1353 }
1354 } catch (RemoteException e) {
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001357 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1358 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001359 }
1360
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001361 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001362 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001363 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001364 // If no method is currently selected, do nothing.
1365 if (mCurMethodId == null) {
1366 return mNoBinding;
1367 }
1368
Yohei Yukawad57ba672015-06-08 16:39:46 -07001369 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1370 attribute.packageName)) {
1371 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1372 + " uid=" + cs.uid + " package=" + attribute.packageName);
1373 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001374 }
1375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001377 // Was the keyguard locked when switching over to the new client?
1378 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 // If the client is changing, we need to switch over to the new
1380 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001381 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001382 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001383 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384
1385 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001386 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001387 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001388 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
1390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 // Bump up the sequence for this client and attach it.
1393 mCurSeq++;
1394 if (mCurSeq <= 0) mCurSeq = 1;
1395 mCurClient = cs;
1396 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001397 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 // Check if the input method is changing.
1401 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1402 if (cs.curSession != null) {
1403 // Fast case: if we are already connected to the input method,
1404 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001405 return attachNewInputLocked(
1406 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
1408 if (mHaveConnection) {
1409 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 // Return to client, and we will get back with it when
1411 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001412 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001413 return new InputBindResult(null, null, mCurId, mCurSeq,
1414 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 } else if (SystemClock.uptimeMillis()
1416 < (mLastBindTime+TIME_TO_RECONNECT)) {
1417 // In this case we have connected to the service, but
1418 // don't yet have its interface. If it hasn't been too
1419 // long since we did the connection, we'll return to
1420 // the client and wait to get the service interface so
1421 // we can report back. If it has been too long, we want
1422 // to fall through so we can try a disconnect/reconnect
1423 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001424 return new InputBindResult(null, null, mCurId, mCurSeq,
1425 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1428 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 }
1431 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001432
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001433 return startInputInnerLocked();
1434 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001436 InputBindResult startInputInnerLocked() {
1437 if (mCurMethodId == null) {
1438 return mNoBinding;
1439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001440
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001441 if (!mSystemReady) {
1442 // If the system is not yet ready, we shouldn't be running third
1443 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001444 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1445 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1449 if (info == null) {
1450 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001452
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001453 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1456 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001457 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1458 com.android.internal.R.string.input_method_binding_label);
1459 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1460 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001461 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001462 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1463 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 mLastBindTime = SystemClock.uptimeMillis();
1465 mHaveConnection = true;
1466 mCurId = info.getId();
1467 mCurToken = new Binder();
1468 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001469 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 mIWindowManager.addWindowToken(mCurToken,
1471 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1472 } catch (RemoteException e) {
1473 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001474 return new InputBindResult(null, null, mCurId, mCurSeq,
1475 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 } else {
1477 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001478 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 + mCurIntent);
1480 }
1481 return null;
1482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001483
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001484 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001485 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001486 IInputMethodClient client, IInputContext inputContext,
1487 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001488 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001489 if (!calledFromValidUser()) {
1490 return null;
1491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001493 if (DEBUG) {
1494 Slog.v(TAG, "startInput: reason="
1495 + InputMethodClient.getStartInputReason(startInputReason)
1496 + " client = " + client.asBinder()
1497 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001498 + " missingMethods="
1499 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001500 + " attribute=" + attribute
1501 + " controlFlags=#" + Integer.toHexString(controlFlags));
1502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 final long ident = Binder.clearCallingIdentity();
1504 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001505 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1506 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 } finally {
1508 Binder.restoreCallingIdentity(ident);
1509 }
1510 }
1511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001512
satoke7c6998e2011-06-03 17:57:59 +09001513 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 public void finishInput(IInputMethodClient client) {
1515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
satoke7c6998e2011-06-03 17:57:59 +09001517 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 public void onServiceConnected(ComponentName name, IBinder service) {
1519 synchronized (mMethodMap) {
1520 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1521 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001522 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001523 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001524 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001525 return;
1526 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001527 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001528 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1529 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001531 clearClientSessionLocked(mCurClient);
1532 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
1534 }
1535 }
1536 }
1537
Jeff Brownc28867a2013-03-26 15:42:39 -07001538 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1539 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 synchronized (mMethodMap) {
1541 if (mCurMethod != null && method != null
1542 && mCurMethod.asBinder() == method.asBinder()) {
1543 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001544 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001546 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001547 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 if (res.method != null) {
1549 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001550 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001552 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554 }
1555 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001556
1557 // Session abandoned. Close its associated input channel.
1558 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001561 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001562 if (mVisibleBound) {
1563 mContext.unbindService(mVisibleConnection);
1564 mVisibleBound = false;
1565 }
1566
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001567 if (mHaveConnection) {
1568 mContext.unbindService(this);
1569 mHaveConnection = false;
1570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001571
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001572 if (mCurToken != null) {
1573 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001574 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001575 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001576 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001577 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001578 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001579 mIWindowManager.removeWindowToken(mCurToken);
1580 } catch (RemoteException e) {
1581 }
1582 mCurToken = null;
1583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001584
The Android Open Source Project10592532009-03-18 17:39:46 -07001585 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001586 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
Yohei Yukawa33e81792015-11-17 21:14:42 -08001589 void resetCurrentMethodAndClient(
1590 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001591 mCurMethodId = null;
1592 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001593 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001594 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001595
1596 void requestClientSessionLocked(ClientState cs) {
1597 if (!cs.sessionRequested) {
1598 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1599 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1600 cs.sessionRequested = true;
1601 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1602 MSG_CREATE_SESSION, mCurMethod, channels[1],
1603 new MethodCallback(this, mCurMethod, channels[0])));
1604 }
1605 }
1606
1607 void clearClientSessionLocked(ClientState cs) {
1608 finishSessionLocked(cs.curSession);
1609 cs.curSession = null;
1610 cs.sessionRequested = false;
1611 }
1612
1613 private void finishSessionLocked(SessionState sessionState) {
1614 if (sessionState != null) {
1615 if (sessionState.session != null) {
1616 try {
1617 sessionState.session.finishSession();
1618 } catch (RemoteException e) {
1619 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001620 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001621 }
1622 sessionState.session = null;
1623 }
1624 if (sessionState.channel != null) {
1625 sessionState.channel.dispose();
1626 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001627 }
1628 }
1629 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001630
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001631 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 if (mCurMethod != null) {
1633 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001634 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001636
Jeff Brownc28867a2013-03-26 15:42:39 -07001637 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001638 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 mCurMethod = null;
1640 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001641 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001642 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
satoke7c6998e2011-06-03 17:57:59 +09001646 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 public void onServiceDisconnected(ComponentName name) {
1648 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001649 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 + " mCurIntent=" + mCurIntent);
1651 if (mCurMethod != null && mCurIntent != null
1652 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001653 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 // We consider this to be a new bind attempt, since the system
1655 // should now try to restart the service for us.
1656 mLastBindTime = SystemClock.uptimeMillis();
1657 mShowRequested = mInputShown;
1658 mInputShown = false;
1659 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001660 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1661 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1662 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664 }
1665 }
1666 }
1667
satokf9f01002011-05-19 21:31:50 +09001668 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1670 long ident = Binder.clearCallingIdentity();
1671 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001673 if (!calledWithValidToken(token)) {
1674 final int uid = Binder.getCallingUid();
1675 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1676 + " token:" + token);
1677 return;
1678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001680 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001681 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001682 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001685 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001686 CharSequence contentDescription = null;
1687 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001688 // Use PackageManager to load label
1689 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001690 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001691 mIPackageManager.getApplicationInfo(packageName, 0,
1692 mSettings.getCurrentUserId()));
1693 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001694 /* ignore */
1695 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001696 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001697 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001698 contentDescription != null
1699 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001700 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
1703 }
1704 } finally {
1705 Binder.restoreCallingIdentity(ident);
1706 }
1707 }
1708
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001709 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001710 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001711 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001712 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001713 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001714 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001715 // When physical keyboard is attached, we show the ime switcher (or notification if
1716 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1717 // exists in the IME switcher dialog. Might be OK to remove this condition once
1718 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1719 return true;
Yohei Yukawa89398382016-03-29 11:37:04 -07001720 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1721 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001722 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001723
1724 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1725 final int N = imis.size();
1726 if (N > 2) return true;
1727 if (N < 1) return false;
1728 int nonAuxCount = 0;
1729 int auxCount = 0;
1730 InputMethodSubtype nonAuxSubtype = null;
1731 InputMethodSubtype auxSubtype = null;
1732 for(int i = 0; i < N; ++i) {
1733 final InputMethodInfo imi = imis.get(i);
1734 final List<InputMethodSubtype> subtypes =
1735 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1736 final int subtypeCount = subtypes.size();
1737 if (subtypeCount == 0) {
1738 ++nonAuxCount;
1739 } else {
1740 for (int j = 0; j < subtypeCount; ++j) {
1741 final InputMethodSubtype subtype = subtypes.get(j);
1742 if (!subtype.isAuxiliary()) {
1743 ++nonAuxCount;
1744 nonAuxSubtype = subtype;
1745 } else {
1746 ++auxCount;
1747 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001748 }
1749 }
satok7cfc0ed2011-06-20 21:29:36 +09001750 }
1751 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001752 if (nonAuxCount > 1 || auxCount > 1) {
1753 return true;
1754 } else if (nonAuxCount == 1 && auxCount == 1) {
1755 if (nonAuxSubtype != null && auxSubtype != null
1756 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1757 || auxSubtype.overridesImplicitlyEnabledSubtype()
1758 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1759 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1760 return false;
1761 }
1762 return true;
1763 }
1764 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001765 }
1766
John Spurlocke0980502013-10-25 11:59:29 -04001767 private boolean isKeyguardLocked() {
1768 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1769 }
1770
satokdbf29502011-08-25 15:28:23 +09001771 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001772 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001773 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001774 if (!calledWithValidToken(token)) {
1775 final int uid = Binder.getCallingUid();
1776 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1777 + " token:" + token);
1778 return;
1779 }
1780
1781 synchronized (mMethodMap) {
1782 mImeWindowVis = vis;
1783 mBackDisposition = backDisposition;
1784 updateSystemUiLocked(token, vis, backDisposition);
1785 }
1786 }
1787
1788 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1789 synchronized (mMethodMap) {
1790 updateSystemUiLocked(token, vis, backDisposition);
1791 }
1792 }
1793
1794 // Caution! This method is called in this class. Handle multi-user carefully
1795 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1796 if (!calledWithValidToken(token)) {
1797 final int uid = Binder.getCallingUid();
1798 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1799 + " token:" + token);
1800 return;
1801 }
1802
1803 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1804 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001805 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001806 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001807 // apply policy for binder calls
1808 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1809 vis = 0;
satok06487a52010-10-29 11:37:18 +09001810 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001811 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1812 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1813 if (mStatusBar != null) {
1814 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1815 needsToShowImeSwitcher);
1816 }
1817 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1818 if (imi != null && needsToShowImeSwitcher) {
1819 // Used to load label
1820 final CharSequence title = mRes.getText(
1821 com.android.internal.R.string.select_input_method);
1822 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1823 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001824 mImeSwitcherNotification.setContentTitle(title)
1825 .setContentText(summary)
1826 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001827 try {
1828 if ((mNotificationManager != null)
1829 && !mIWindowManager.hasNavigationBar()) {
1830 if (DEBUG) {
1831 Slog.d(TAG, "--- show notification: label = " + summary);
1832 }
1833 mNotificationManager.notifyAsUser(null,
1834 com.android.internal.R.string.select_input_method,
1835 mImeSwitcherNotification.build(), UserHandle.ALL);
1836 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001837 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001838 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001839 }
1840 } else {
1841 if (mNotificationShown && mNotificationManager != null) {
1842 if (DEBUG) {
1843 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001844 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001845 mNotificationManager.cancelAsUser(null,
1846 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1847 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001848 }
satok06487a52010-10-29 11:37:18 +09001849 }
1850 } finally {
1851 Binder.restoreCallingIdentity(ident);
1852 }
1853 }
1854
satoke7c6998e2011-06-03 17:57:59 +09001855 @Override
satokf9f01002011-05-19 21:31:50 +09001856 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001857 if (!calledFromValidUser()) {
1858 return;
1859 }
satokf9f01002011-05-19 21:31:50 +09001860 synchronized (mMethodMap) {
1861 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1862 for (int i = 0; i < spans.length; ++i) {
1863 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001864 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001865 mSecureSuggestionSpans.put(ss, currentImi);
1866 }
1867 }
1868 }
1869 }
1870
satoke7c6998e2011-06-03 17:57:59 +09001871 @Override
satokf9f01002011-05-19 21:31:50 +09001872 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001873 if (!calledFromValidUser()) {
1874 return false;
1875 }
satokf9f01002011-05-19 21:31:50 +09001876 synchronized (mMethodMap) {
1877 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1878 // TODO: Do not send the intent if the process of the targetImi is already dead.
1879 if (targetImi != null) {
1880 final String[] suggestions = span.getSuggestions();
1881 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001882 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001883 final Intent intent = new Intent();
1884 // Ensures that only a class in the original IME package will receive the
1885 // notification.
satok42c5a162011-05-26 16:46:14 +09001886 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001887 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1888 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1889 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1890 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001891 final long ident = Binder.clearCallingIdentity();
1892 try {
1893 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1894 } finally {
1895 Binder.restoreCallingIdentity(ident);
1896 }
satokf9f01002011-05-19 21:31:50 +09001897 return true;
1898 }
1899 }
1900 return false;
1901 }
1902
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001903 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001904 updateInputMethodsFromSettingsLocked(enabledMayChange);
1905 updateKeyboardFromSettingsLocked();
1906 }
1907
1908 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001909 if (enabledMayChange) {
1910 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1911 for (int i=0; i<enabled.size(); i++) {
1912 // We allow the user to select "disabled until used" apps, so if they
1913 // are enabling one of those here we now need to make it enabled.
1914 InputMethodInfo imm = enabled.get(i);
1915 try {
1916 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1917 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1918 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001919 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001920 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001921 if (DEBUG) {
1922 Slog.d(TAG, "Update state(" + imm.getId()
1923 + "): DISABLED_UNTIL_USED -> DEFAULT");
1924 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001925 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1926 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001927 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1928 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001929 }
1930 } catch (RemoteException e) {
1931 }
1932 }
1933 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001934 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1935 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1936 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1937 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001938 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001939 // There is no input method selected, try to choose new applicable input method.
1940 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001941 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001942 }
1943 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001945 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001947 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001948 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
satokf3db1af2010-11-23 13:34:33 +09001950 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001951 } else {
1952 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001953 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001955 // Here is not the perfect place to reset the switching controller. Ideally
1956 // mSwitchingController and mSettings should be able to share the same state.
1957 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1958 // the same enabled IMEs list.
1959 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001960
1961 }
1962
1963 public void updateKeyboardFromSettingsLocked() {
1964 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1965 if (mSwitchingDialog != null
1966 && mSwitchingDialogTitleView != null
1967 && mSwitchingDialog.isShowing()) {
1968 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1969 com.android.internal.R.id.hard_keyboard_switch);
1970 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001973
Yohei Yukawab097b822015-12-01 10:43:08 -08001974 private void notifyInputMethodSubtypeChanged(final int userId,
1975 @Nullable final InputMethodInfo inputMethodInfo,
1976 @Nullable final InputMethodSubtype subtype) {
1977 final InputManagerInternal inputManagerInternal =
1978 LocalServices.getService(InputManagerInternal.class);
1979 if (inputManagerInternal != null) {
1980 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1981 }
1982 }
1983
satokab751aa2010-09-14 19:17:36 +09001984 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 InputMethodInfo info = mMethodMap.get(id);
1986 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001987 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001989
satokd81e9502012-05-21 12:58:45 +09001990 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001992 final int subtypeCount = info.getSubtypeCount();
1993 if (subtypeCount <= 0) {
1994 return;
satokcd7cd292010-11-20 15:46:23 +09001995 }
satokd81e9502012-05-21 12:58:45 +09001996 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1997 final InputMethodSubtype newSubtype;
1998 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1999 newSubtype = info.getSubtypeAt(subtypeId);
2000 } else {
2001 // If subtype is null, try to find the most applicable one from
2002 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002003 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002004 }
2005 if (newSubtype == null || oldSubtype == null) {
2006 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2007 + ", new subtype = " + newSubtype);
2008 return;
2009 }
2010 if (newSubtype != oldSubtype) {
2011 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2012 if (mCurMethod != null) {
2013 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002014 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002015 mCurMethod.changeInputMethodSubtype(newSubtype);
2016 } catch (RemoteException e) {
2017 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002018 return;
satokab751aa2010-09-14 19:17:36 +09002019 }
2020 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002021 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 return;
2024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002025
satokd81e9502012-05-21 12:58:45 +09002026 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 final long ident = Binder.clearCallingIdentity();
2028 try {
satokab751aa2010-09-14 19:17:36 +09002029 // Set a subtype to this input method.
2030 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002031 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2032 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2033 // because mCurMethodId is stored as a history in
2034 // setSelectedInputMethodAndSubtypeLocked().
2035 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036
2037 if (ActivityManagerNative.isSystemReady()) {
2038 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002039 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002041 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002043 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 } finally {
2045 Binder.restoreCallingIdentity(ident);
2046 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002047
2048 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2049 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002051
satok42c5a162011-05-26 16:46:14 +09002052 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002053 public boolean showSoftInput(IInputMethodClient client, int flags,
2054 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002055 if (!calledFromValidUser()) {
2056 return false;
2057 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002058 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 long ident = Binder.clearCallingIdentity();
2060 try {
2061 synchronized (mMethodMap) {
2062 if (mCurClient == null || client == null
2063 || mCurClient.client.asBinder() != client.asBinder()) {
2064 try {
2065 // We need to check if this is the current client with
2066 // focus in the window manager, to allow this call to
2067 // be made before input is started in it.
2068 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002069 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002073 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
2075 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002076
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002077 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002078 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 }
2080 } finally {
2081 Binder.restoreCallingIdentity(ident);
2082 }
2083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002084
The Android Open Source Project4df24232009-03-05 14:34:35 -08002085 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002087 if (mAccessibilityRequestingNoSoftKeyboard) {
2088 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 }
Anna Galusza9b278112016-01-04 11:37:37 -08002090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2092 mShowExplicitlyRequested = true;
2093 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002094 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2095 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002097
Dianne Hackborncc278702009-09-02 23:07:23 -07002098 if (!mSystemReady) {
2099 return false;
2100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002101
The Android Open Source Project4df24232009-03-05 14:34:35 -08002102 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002104 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002105 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2106 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2107 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002109 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002110 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002111 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002112 | Context.BIND_TREAT_LIKE_ACTIVITY
2113 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002114 mVisibleBound = true;
2115 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002116 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002118 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 // The client has asked to have the input method shown, but
2120 // we have been sitting here too long with a connection to the
2121 // service and no interface received, so let's disconnect/connect
2122 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002123 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002125 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002127 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002128 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002129 } else {
2130 if (DEBUG) {
2131 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2132 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002135
The Android Open Source Project4df24232009-03-05 14:34:35 -08002136 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002138
satok42c5a162011-05-26 16:46:14 +09002139 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002140 public boolean hideSoftInput(IInputMethodClient client, int flags,
2141 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002142 if (!calledFromValidUser()) {
2143 return false;
2144 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002145 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 long ident = Binder.clearCallingIdentity();
2147 try {
2148 synchronized (mMethodMap) {
2149 if (mCurClient == null || client == null
2150 || mCurClient.client.asBinder() != client.asBinder()) {
2151 try {
2152 // We need to check if this is the current client with
2153 // focus in the window manager, to allow this call to
2154 // be made before input is started in it.
2155 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002156 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2157 + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002161 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
2163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002164
Joe Onorato8a9b2202010-02-26 18:56:32 -08002165 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002166 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168 } finally {
2169 Binder.restoreCallingIdentity(ident);
2170 }
2171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002172
The Android Open Source Project4df24232009-03-05 14:34:35 -08002173 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2175 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002176 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 -08002177 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
2179 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002180 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 -08002181 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002183
2184 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2185 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2186 // to be updated with the new value sent from IME process. Even in such a transient state
2187 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2188 // application process as a valid request, and have even promised such a behavior with CTS
2189 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2190 // IMMS#InputShown indicates that the software keyboard is shown.
2191 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2192 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2193 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002194 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002195 if (shouldHideSoftInput) {
2196 // The IME will report its visible state again after the following message finally
2197 // delivered to the IME process as an IPC. Hence the inconsistency between
2198 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2199 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002200 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2201 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2202 res = true;
2203 } else {
2204 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002206 if (mHaveConnection && mVisibleBound) {
2207 mContext.unbindService(mVisibleConnection);
2208 mVisibleBound = false;
2209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 mInputShown = false;
2211 mShowRequested = false;
2212 mShowExplicitlyRequested = false;
2213 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002214 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002216
satok42c5a162011-05-26 16:46:14 +09002217 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002218 public InputBindResult startInputOrWindowGainedFocus(
2219 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2220 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002221 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002222 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002223 if (windowToken != null) {
2224 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002225 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002226 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002227 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2228 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002229 }
2230 }
2231
2232 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002233 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2234 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002235 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2236 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002237 // Needs to check the validity before clearing calling identity
2238 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002239 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 long ident = Binder.clearCallingIdentity();
2241 try {
2242 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002243 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2244 + InputMethodClient.getStartInputReason(startInputReason)
2245 + " client=" + client.asBinder()
2246 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002247 + " missingMethods="
2248 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002249 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002250 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002252 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002253
Dianne Hackborn7663d802012-02-24 13:08:49 -08002254 ClientState cs = mClients.get(client.asBinder());
2255 if (cs == null) {
2256 throw new IllegalArgumentException("unknown client "
2257 + client.asBinder());
2258 }
2259
2260 try {
2261 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2262 // Check with the window manager to make sure this client actually
2263 // has a window with focus. If not, reject. This is thread safe
2264 // because if the focus changes some time before or after, the
2265 // next client receiving focus that has any interest in input will
2266 // be calling through here after that change happens.
2267 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2268 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2269 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002271 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002273
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002274 if (!calledFromValidUser) {
2275 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2276 Slog.w(TAG, "If you want to interect with IME, you need "
2277 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2278 hideCurrentInputLocked(0, null);
2279 return null;
2280 }
2281
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002282 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002283 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002284 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002285 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002286 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2287 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002288 }
2289 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002290 }
2291 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002292 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002293
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002294 // Should we auto-show the IME even if the caller has not
2295 // specified what should be done with it?
2296 // We only do this automatically if the window can resize
2297 // to accommodate the IME (so what the user sees will give
2298 // them good context without input information being obscured
2299 // by the IME) or if running on a large screen where there
2300 // is more room for the target window + IME.
2301 final boolean doAutoShow =
2302 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2303 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2304 || mRes.getConfiguration().isLayoutSizeAtLeast(
2305 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002306 final boolean isTextEditor =
2307 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2308
2309 // We want to start input before showing the IME, but after closing
2310 // it. We want to do this after closing it to help the IME disappear
2311 // more quickly (not get stuck behind it initializing itself for the
2312 // new focused input, even if its window wants to hide the IME).
2313 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2316 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002317 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2319 // There is no focus view, and this window will
2320 // be behind any soft input window, so hide the
2321 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002322 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002323 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002325 } else if (isTextEditor && doAutoShow && (softInputMode &
2326 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 // There is a focus view, and we are navigating forward
2328 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002329 // We only do this automatically if the window can resize
2330 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002331 // them good context without input information being obscured
2332 // by the IME) or if running on a large screen where there
2333 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002334 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002335 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002336 res = startInputUncheckedLocked(cs, inputContext,
2337 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002338 didStart = true;
2339 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002340 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 }
2342 break;
2343 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2344 // Do nothing.
2345 break;
2346 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2347 if ((softInputMode &
2348 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002349 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002350 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 }
2352 break;
2353 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002354 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002355 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 break;
2357 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2358 if ((softInputMode &
2359 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002360 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002361 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002362 res = startInputUncheckedLocked(cs, inputContext,
2363 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002364 didStart = true;
2365 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002366 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 break;
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002370 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002371 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002372 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2373 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002374 didStart = true;
2375 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002376 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 break;
2378 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002379
2380 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002381 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002382 controlFlags);
2383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385 } finally {
2386 Binder.restoreCallingIdentity(ident);
2387 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002388
2389 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002391
satok42c5a162011-05-26 16:46:14 +09002392 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002393 public void showInputMethodPickerFromClient(
2394 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002395 if (!calledFromValidUser()) {
2396 return;
2397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 synchronized (mMethodMap) {
2399 if (mCurClient == null || client == null
2400 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002401 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002402 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
2404
satok440aab52010-11-25 09:43:11 +09002405 // Always call subtype picker, because subtype picker is a superset of input method
2406 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002407 mHandler.sendMessage(mCaller.obtainMessageI(
2408 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002409 }
2410 }
2411
satok42c5a162011-05-26 16:46:14 +09002412 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002414 if (!calledFromValidUser()) {
2415 return;
2416 }
satok28203512010-11-24 11:06:49 +09002417 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2418 }
2419
satok42c5a162011-05-26 16:46:14 +09002420 @Override
satok28203512010-11-24 11:06:49 +09002421 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002422 if (!calledFromValidUser()) {
2423 return;
2424 }
satok28203512010-11-24 11:06:49 +09002425 synchronized (mMethodMap) {
2426 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002427 setInputMethodWithSubtypeIdLocked(token, id,
2428 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2429 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002430 } else {
2431 setInputMethod(token, id);
2432 }
2433 }
satokab751aa2010-09-14 19:17:36 +09002434 }
2435
satok42c5a162011-05-26 16:46:14 +09002436 @Override
satokb416a712010-11-25 20:42:14 +09002437 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002438 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002439 if (!calledFromValidUser()) {
2440 return;
2441 }
satokb416a712010-11-25 20:42:14 +09002442 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002443 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2444 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002445 }
2446 }
2447
satok4fc87d62011-05-20 16:13:43 +09002448 @Override
satok735cf382010-11-11 20:40:09 +09002449 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002450 if (!calledFromValidUser()) {
2451 return false;
2452 }
satok735cf382010-11-11 20:40:09 +09002453 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002454 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002455 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002456 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002457 lastImi = mMethodMap.get(lastIme.first);
2458 } else {
2459 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002460 }
satok4fc87d62011-05-20 16:13:43 +09002461 String targetLastImiId = null;
2462 int subtypeId = NOT_A_SUBTYPE_ID;
2463 if (lastIme != null && lastImi != null) {
2464 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2465 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2466 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2467 : mCurrentSubtype.hashCode();
2468 // If the last IME is the same as the current IME and the last subtype is not
2469 // defined, there is no need to switch to the last IME.
2470 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2471 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002472 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002473 }
2474 }
2475
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002476 if (TextUtils.isEmpty(targetLastImiId)
2477 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002478 // This is a safety net. If the currentSubtype can't be added to the history
2479 // and the framework couldn't find the last ime, we will make the last ime be
2480 // the most applicable enabled keyboard subtype of the system imes.
2481 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2482 if (enabled != null) {
2483 final int N = enabled.size();
2484 final String locale = mCurrentSubtype == null
2485 ? mRes.getConfiguration().locale.toString()
2486 : mCurrentSubtype.getLocale();
2487 for (int i = 0; i < N; ++i) {
2488 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002489 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002490 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002491 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2492 InputMethodUtils.getSubtypes(imi),
2493 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002494 if (keyboardSubtype != null) {
2495 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002496 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002497 imi, keyboardSubtype.hashCode());
2498 if(keyboardSubtype.getLocale().equals(locale)) {
2499 break;
2500 }
2501 }
2502 }
2503 }
2504 }
2505 }
2506
2507 if (!TextUtils.isEmpty(targetLastImiId)) {
2508 if (DEBUG) {
2509 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2510 + ", from: " + mCurMethodId + ", " + subtypeId);
2511 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002512 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002513 return true;
2514 } else {
2515 return false;
2516 }
satok735cf382010-11-11 20:40:09 +09002517 }
2518 }
2519
satoke7c6998e2011-06-03 17:57:59 +09002520 @Override
satok688bd472012-02-09 20:09:17 +09002521 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002522 if (!calledFromValidUser()) {
2523 return false;
2524 }
satok688bd472012-02-09 20:09:17 +09002525 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002526 if (!calledWithValidToken(token)) {
2527 final int uid = Binder.getCallingUid();
2528 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2529 + " token:" + token);
2530 return false;
2531 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002532 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002533 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2534 if (nextSubtype == null) {
2535 return false;
2536 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002537 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2538 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002539 return true;
2540 }
2541 }
2542
2543 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002544 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2545 if (!calledFromValidUser()) {
2546 return false;
2547 }
2548 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002549 if (!calledWithValidToken(token)) {
2550 final int uid = Binder.getCallingUid();
2551 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2552 + "token. uid:" + uid + " token:" + token);
2553 return false;
2554 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002555 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002556 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2557 if (nextSubtype == null) {
2558 return false;
2559 }
2560 return true;
2561 }
2562 }
2563
2564 @Override
satok68f1b782011-04-11 14:26:04 +09002565 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002566 if (!calledFromValidUser()) {
2567 return null;
2568 }
satok68f1b782011-04-11 14:26:04 +09002569 synchronized (mMethodMap) {
2570 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2571 // TODO: Handle the case of the last IME with no subtypes
2572 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2573 || TextUtils.isEmpty(lastIme.second)) return null;
2574 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2575 if (lastImi == null) return null;
2576 try {
2577 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002578 final int lastSubtypeId =
2579 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002580 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2581 return null;
2582 }
2583 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002584 } catch (NumberFormatException e) {
2585 return null;
2586 }
2587 }
2588 }
2589
satoke7c6998e2011-06-03 17:57:59 +09002590 @Override
satokee5e77c2011-09-02 18:50:15 +09002591 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002592 if (!calledFromValidUser()) {
2593 return;
2594 }
satok91e88122011-07-18 11:11:42 +09002595 // By this IPC call, only a process which shares the same uid with the IME can add
2596 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002597 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002598 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002599 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002600 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002601 final String[] packageInfos;
2602 try {
2603 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2604 } catch (RemoteException e) {
2605 Slog.e(TAG, "Failed to get package infos");
2606 return;
2607 }
satok91e88122011-07-18 11:11:42 +09002608 if (packageInfos != null) {
2609 final int packageNum = packageInfos.length;
2610 for (int i = 0; i < packageNum; ++i) {
2611 if (packageInfos[i].equals(imi.getPackageName())) {
2612 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002613 final long ident = Binder.clearCallingIdentity();
2614 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002615 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002616 } finally {
2617 Binder.restoreCallingIdentity(ident);
2618 }
satokee5e77c2011-09-02 18:50:15 +09002619 return;
satok91e88122011-07-18 11:11:42 +09002620 }
2621 }
2622 }
satoke7c6998e2011-06-03 17:57:59 +09002623 }
satokee5e77c2011-09-02 18:50:15 +09002624 return;
satoke7c6998e2011-06-03 17:57:59 +09002625 }
2626
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002627 @Override
2628 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002629 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002630 }
2631
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002632 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002633 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002634 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002635 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002636 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002637 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002638 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2639 if (DEBUG) {
2640 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2641 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2642 + " actual: " + sequenceNumber);
2643 }
2644 return;
2645 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002646 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2647 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002648 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002649 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002650 }
2651 }
2652
satok28203512010-11-24 11:06:49 +09002653 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002655 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2656 }
2657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002659 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2660 if (token == null) {
2661 if (mContext.checkCallingOrSelfPermission(
2662 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2663 != PackageManager.PERMISSION_GRANTED) {
2664 throw new SecurityException(
2665 "Using null token requires permission "
2666 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002668 } else if (mCurToken != token) {
2669 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2670 + " token: " + token);
2671 return;
2672 }
2673
2674 final long ident = Binder.clearCallingIdentity();
2675 try {
2676 setInputMethodLocked(id, subtypeId);
2677 } finally {
2678 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 }
2680 }
2681
satok42c5a162011-05-26 16:46:14 +09002682 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002684 if (!calledFromValidUser()) {
2685 return;
2686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002688 if (!calledWithValidToken(token)) {
2689 final int uid = Binder.getCallingUid();
2690 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2691 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 return;
2693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 long ident = Binder.clearCallingIdentity();
2695 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002696 hideCurrentInputLocked(flags, null);
2697 } finally {
2698 Binder.restoreCallingIdentity(ident);
2699 }
2700 }
2701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002702
satok42c5a162011-05-26 16:46:14 +09002703 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002704 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002705 if (!calledFromValidUser()) {
2706 return;
2707 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002708 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002709 if (!calledWithValidToken(token)) {
2710 final int uid = Binder.getCallingUid();
2711 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2712 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002713 return;
2714 }
2715 long ident = Binder.clearCallingIdentity();
2716 try {
2717 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 } finally {
2719 Binder.restoreCallingIdentity(ident);
2720 }
2721 }
2722 }
2723
2724 void setEnabledSessionInMainThread(SessionState session) {
2725 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002726 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002728 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002729 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 } catch (RemoteException e) {
2731 }
2732 }
2733 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002734 if (mEnabledSession != null && mEnabledSession.session != null) {
2735 try {
2736 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2737 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2738 } catch (RemoteException e) {
2739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 }
2741 }
2742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002743
satok42c5a162011-05-26 16:46:14 +09002744 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002746 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002748 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002749 final boolean showAuxSubtypes;
2750 switch (msg.arg1) {
2751 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2752 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2753 // implemented so that auxiliary subtypes will be excluded when the soft
2754 // keyboard is invisible.
2755 showAuxSubtypes = mInputShown;
2756 break;
2757 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2758 showAuxSubtypes = true;
2759 break;
2760 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2761 showAuxSubtypes = false;
2762 break;
2763 default:
2764 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2765 return false;
2766 }
2767 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002768 return true;
2769
satok47a44912010-10-06 16:03:58 +09002770 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002771 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002772 return true;
2773
2774 case MSG_SHOW_IM_CONFIG:
2775 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002776 return true;
2777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 case MSG_UNBIND_INPUT:
2781 try {
2782 ((IInputMethod)msg.obj).unbindInput();
2783 } catch (RemoteException e) {
2784 // There is nothing interesting about the method dying.
2785 }
2786 return true;
2787 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002788 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 try {
2790 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2791 } catch (RemoteException e) {
2792 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002793 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 return true;
2795 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002796 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002798 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002799 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002800 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 } catch (RemoteException e) {
2802 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002803 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 return true;
2805 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002806 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002808 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002809 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002810 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 } catch (RemoteException e) {
2812 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002813 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 return true;
2815 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002816 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002818 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2820 } catch (RemoteException e) {
2821 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002822 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002824 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002825 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002826 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002827 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002829 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002831 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002832 // Dispose the channel if the input method is not local to this process
2833 // because the remote proxy will get its own copy when unparceled.
2834 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002835 channel.dispose();
2836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002838 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002842
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002843 case MSG_START_INPUT: {
2844 int missingMethods = msg.arg1;
2845 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002847 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002849 session.method.startInput((IInputContext) args.arg2, missingMethods,
2850 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 } catch (RemoteException e) {
2852 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002853 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002855 }
2856 case MSG_RESTART_INPUT: {
2857 int missingMethods = msg.arg1;
2858 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002860 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002862 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2863 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 } catch (RemoteException e) {
2865 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002866 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002871
Yohei Yukawa33e81792015-11-17 21:14:42 -08002872 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002874 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 } catch (RemoteException e) {
2876 // There is nothing interesting about the last client dying.
2877 }
2878 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002879 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002880 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002881 IInputMethodClient client = (IInputMethodClient)args.arg1;
2882 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002884 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002886 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002887 } finally {
2888 // Dispose the channel if the input method is not local to this process
2889 // because the remote proxy will get its own copy when unparceled.
2890 if (res.channel != null && Binder.isProxy(client)) {
2891 res.channel.dispose();
2892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002894 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002896 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002897 case MSG_SET_ACTIVE:
2898 try {
2899 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2900 } catch (RemoteException e) {
2901 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2902 + ((ClientState)msg.obj).pid + " uid "
2903 + ((ClientState)msg.obj).uid);
2904 }
2905 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002906 case MSG_SET_INTERACTIVE:
2907 handleSetInteractive(msg.arg1 != 0);
2908 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002909 case MSG_SWITCH_IME:
2910 handleSwitchInputMethod(msg.arg1 != 0);
2911 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002912 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2913 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002914 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002915 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002916 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002917 } catch (RemoteException e) {
2918 Slog.w(TAG, "Got RemoteException sending "
2919 + "setUserActionNotificationSequenceNumber("
2920 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002921 + clientState.pid + " uid "
2922 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002923 }
2924 return true;
2925 }
satok01038492012-04-09 21:08:27 +09002926
2927 // --------------------------------------------------------------
2928 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002929 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002930 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 }
2932 return false;
2933 }
2934
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002935 private void handleSetInteractive(final boolean interactive) {
2936 synchronized (mMethodMap) {
2937 mIsInteractive = interactive;
2938 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2939
2940 // Inform the current client of the change in active status
2941 if (mCurClient != null && mCurClient.client != null) {
2942 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2943 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2944 }
2945 }
2946 }
2947
Yohei Yukawaae61f712015-12-09 13:00:10 -08002948 private void handleSwitchInputMethod(final boolean forwardDirection) {
2949 synchronized (mMethodMap) {
2950 // TODO: Support forwardDirection.
2951 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2952 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2953 if (nextSubtype == null) {
2954 return;
2955 }
2956 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002957 if (mSubtypeSwitchedByShortCutToast != null) {
2958 mSubtypeSwitchedByShortCutToast.cancel();
2959 mSubtypeSwitchedByShortCutToast = null;
2960 }
2961 if ((mImeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
2962 // IME window is shown. The user should be able to visually understand that the
2963 // subtype is changed in most of cases. To avoid UI overlap, we do not show a toast
2964 // in this case.
2965 return;
2966 }
2967 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2968 if (newInputMethodInfo == null) {
2969 return;
2970 }
2971 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
2972 newInputMethodInfo, mCurrentSubtype);
2973 if (!TextUtils.isEmpty(toastText)) {
2974 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText.toString(),
2975 Toast.LENGTH_SHORT);
2976 mSubtypeSwitchedByShortCutToast.show();
2977 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08002978 }
2979 }
2980
satokdc9ddae2011-10-06 12:22:36 +09002981 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002982 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2983 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002984 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002985 if (DEBUG) {
2986 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2987 }
satok723a27e2010-11-11 14:58:11 +09002988 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002989 return true;
2990 }
2991
2992 return false;
2993 }
2994
Yohei Yukawa94e33302016-02-12 19:37:03 -08002995 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002996 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002997 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002998 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002999 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003000 mMethodList.clear();
3001 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003002
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003003 // Use for queryIntentServicesAsUser
3004 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003006 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3007 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3008 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003009 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003011 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3012 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003013
satoke7c6998e2011-06-03 17:57:59 +09003014 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3015 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 for (int i = 0; i < services.size(); ++i) {
3017 ResolveInfo ri = services.get(i);
3018 ServiceInfo si = ri.serviceInfo;
3019 ComponentName compName = new ComponentName(si.packageName, si.name);
3020 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3021 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003022 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 + ": it does not require the permission "
3024 + android.Manifest.permission.BIND_INPUT_METHOD);
3025 continue;
3026 }
3027
Joe Onorato8a9b2202010-02-26 18:56:32 -08003028 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029
3030 try {
satoke7c6998e2011-06-03 17:57:59 +09003031 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003032 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003033 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003034 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035
3036 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003037 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003039 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003040 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 }
3042 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003043
Yohei Yukawa859df052016-02-17 07:56:46 -08003044 // TODO: The following code should find better place to live.
3045 if (!resetDefaultEnabledIme) {
3046 boolean enabledImeFound = false;
3047 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3048 final int N = enabledImes.size();
3049 for (int i = 0; i < N; ++i) {
3050 final InputMethodInfo imi = enabledImes.get(i);
3051 if (mMethodList.contains(imi)) {
3052 enabledImeFound = true;
3053 break;
3054 }
3055 }
3056 if (!enabledImeFound) {
3057 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3058 resetDefaultEnabledIme = true;
3059 resetSelectedInputMethodAndSubtypeLocked("");
3060 }
3061 }
3062
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003063 if (resetDefaultEnabledIme) {
3064 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003065 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003066 final int N = defaultEnabledIme.size();
3067 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003068 final InputMethodInfo imi = defaultEnabledIme.get(i);
3069 if (DEBUG) {
3070 Slog.d(TAG, "--- enable ime = " + imi);
3071 }
3072 setInputMethodEnabledLocked(imi.getId(), true);
3073 }
3074 }
3075
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003076 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003077 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003078 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003079 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3080 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003081 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003082 }
3083 } else {
3084 // Double check that the default IME is certainly enabled.
3085 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003086 }
3087 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003088 // Here is not the perfect place to reset the switching controller. Ideally
3089 // mSwitchingController and mSettings should be able to share the same state.
3090 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3091 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003092 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003096
satok217f5482010-12-15 05:19:19 +09003097 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003098 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003099 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003100 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3101 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003102 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003103 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003104 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003105 final int userId;
3106 synchronized (mMethodMap) {
3107 userId = mSettings.getCurrentUserId();
3108 }
3109 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003110 }
3111
3112 private void showConfigureInputMethods() {
3113 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3114 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3115 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3116 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003117 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003118 }
3119
satok2c93efc2012-04-02 19:33:47 +09003120 private boolean isScreenLocked() {
3121 return mKeyguardManager != null
3122 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3123 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003124
Seigo Nonaka14e13912015-05-06 21:04:13 -07003125 private void showInputMethodMenu(boolean showAuxSubtypes) {
3126 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003129 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003130
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003131 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003132 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003133 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003134
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003135 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003136 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003137 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3138 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003139 if (immis == null || immis.size() == 0) {
3140 return;
3141 }
3142
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003143 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144
satok688bd472012-02-09 20:09:17 +09003145 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003146 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003147 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003148
satokc3690562012-01-10 20:14:43 +09003149 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003150 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003151 if (currentSubtype != null) {
3152 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003153 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3154 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003155 }
3156 }
3157
Ken Wakasa761eb372011-03-04 19:06:18 +09003158 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003159 mIms = new InputMethodInfo[N];
3160 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003161 int checkedItem = 0;
3162 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003163 final ImeSubtypeListItem item = imList.get(i);
3164 mIms[i] = item.mImi;
3165 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003166 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003167 int subtypeId = mSubtypeIds[i];
3168 if ((subtypeId == NOT_A_SUBTYPE_ID)
3169 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3170 || (subtypeId == lastInputMethodSubtypeId)) {
3171 checkedItem = i;
3172 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003175
3176 final Context settingsContext = new ContextThemeWrapper(context,
3177 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3178
3179 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003180 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3181 @Override
3182 public void onCancel(DialogInterface dialog) {
3183 hideInputMethodMenu();
3184 }
3185 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003186
3187 final Context dialogContext = mDialogBuilder.getContext();
3188 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3189 com.android.internal.R.styleable.DialogPreference,
3190 com.android.internal.R.attr.alertDialogStyle, 0);
3191 final Drawable dialogIcon = a.getDrawable(
3192 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3193 a.recycle();
3194
3195 mDialogBuilder.setIcon(dialogIcon);
3196
Yohei Yukawad34e1482016-02-11 08:03:52 -08003197 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003198 final View tv = inflater.inflate(
3199 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3200 mDialogBuilder.setCustomTitle(tv);
3201
3202 // Setup layout for a toggle switch of the hardware keyboard
3203 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003204 mSwitchingDialogTitleView
3205 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003206 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003207 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003208 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003209 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003210 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003211 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3212 @Override
3213 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003214 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003215 // Ensure that the input method dialog is dismissed when changing
3216 // the hardware keyboard state.
3217 hideInputMethodMenu();
3218 }
3219 });
3220
Alan Viverette505e3ab2014-11-24 15:22:11 -08003221 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003222 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3223 final OnClickListener choiceListener = new OnClickListener() {
3224 @Override
3225 public void onClick(final DialogInterface dialog, final int which) {
3226 synchronized (mMethodMap) {
3227 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3228 || mSubtypeIds.length <= which) {
3229 return;
satok01038492012-04-09 21:08:27 +09003230 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003231 final InputMethodInfo im = mIms[which];
3232 int subtypeId = mSubtypeIds[which];
3233 adapter.mCheckedItem = which;
3234 adapter.notifyDataSetChanged();
3235 hideInputMethodMenu();
3236 if (im != null) {
3237 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3238 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003239 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003240 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003242 }
3243 }
3244 };
3245 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003248 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 mSwitchingDialog.getWindow().setType(
3250 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003251 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3252 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003253 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003254 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 mSwitchingDialog.show();
3256 }
3257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003258
Ken Wakasa05dbb652011-08-22 15:22:43 +09003259 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3260 private final LayoutInflater mInflater;
3261 private final int mTextViewResourceId;
3262 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003263 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003264 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3265 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3266 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003267
Ken Wakasa05dbb652011-08-22 15:22:43 +09003268 mTextViewResourceId = textViewResourceId;
3269 mItemsList = itemsList;
3270 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003271 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003272 }
3273
3274 @Override
3275 public View getView(int position, View convertView, ViewGroup parent) {
3276 final View view = convertView != null ? convertView
3277 : mInflater.inflate(mTextViewResourceId, null);
3278 if (position < 0 || position >= mItemsList.size()) return view;
3279 final ImeSubtypeListItem item = mItemsList.get(position);
3280 final CharSequence imeName = item.mImeName;
3281 final CharSequence subtypeName = item.mSubtypeName;
3282 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3283 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3284 if (TextUtils.isEmpty(subtypeName)) {
3285 firstTextView.setText(imeName);
3286 secondTextView.setVisibility(View.GONE);
3287 } else {
3288 firstTextView.setText(subtypeName);
3289 secondTextView.setText(imeName);
3290 secondTextView.setVisibility(View.VISIBLE);
3291 }
3292 final RadioButton radioButton =
3293 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3294 radioButton.setChecked(position == mCheckedItem);
3295 return view;
3296 }
3297 }
3298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003300 synchronized (mMethodMap) {
3301 hideInputMethodMenuLocked();
3302 }
3303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003304
The Android Open Source Project10592532009-03-18 17:39:46 -07003305 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003306 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307
The Android Open Source Project10592532009-03-18 17:39:46 -07003308 if (mSwitchingDialog != null) {
3309 mSwitchingDialog.dismiss();
3310 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003312
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003313 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003314 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003315 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003319
satok42c5a162011-05-26 16:46:14 +09003320 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003322 // TODO: Make this work even for non-current users?
3323 if (!calledFromValidUser()) {
3324 return false;
3325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 synchronized (mMethodMap) {
3327 if (mContext.checkCallingOrSelfPermission(
3328 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3329 != PackageManager.PERMISSION_GRANTED) {
3330 throw new SecurityException(
3331 "Requires permission "
3332 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3333 }
Anna Galusza9b278112016-01-04 11:37:37 -08003334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 long ident = Binder.clearCallingIdentity();
3336 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003337 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 } finally {
3339 Binder.restoreCallingIdentity(ident);
3340 }
3341 }
3342 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003343
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003344 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3345 // Make sure this is a valid input method.
3346 InputMethodInfo imm = mMethodMap.get(id);
3347 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003348 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003349 }
3350
satokd87c2592010-09-29 11:52:06 +09003351 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3352 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003353
satokd87c2592010-09-29 11:52:06 +09003354 if (enabled) {
3355 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3356 if (pair.first.equals(id)) {
3357 // We are enabling this input method, but it is already enabled.
3358 // Nothing to do. The previous state was enabled.
3359 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003360 }
3361 }
satokd87c2592010-09-29 11:52:06 +09003362 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3363 // Previous state was disabled.
3364 return false;
3365 } else {
3366 StringBuilder builder = new StringBuilder();
3367 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3368 builder, enabledInputMethodsList, id)) {
3369 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003370 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003371 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3372 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3373 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003374 }
3375 // Previous state was enabled.
3376 return true;
3377 } else {
3378 // We are disabling the input method but it is already disabled.
3379 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003380 return false;
3381 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003382 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003383 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003384
satok723a27e2010-11-11 14:58:11 +09003385 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3386 boolean setSubtypeOnly) {
3387 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003388 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003389
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003390 mCurUserActionNotificationSequenceNumber =
3391 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3392 if (DEBUG) {
3393 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3394 + mCurUserActionNotificationSequenceNumber);
3395 }
3396
3397 if (mCurClient != null && mCurClient.client != null) {
3398 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3399 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003400 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003401 }
3402
satok723a27e2010-11-11 14:58:11 +09003403 // Set Subtype here
3404 if (imi == null || subtypeId < 0) {
3405 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003406 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003407 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003408 if (subtypeId < imi.getSubtypeCount()) {
3409 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3410 mSettings.putSelectedSubtype(subtype.hashCode());
3411 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003412 } else {
3413 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003414 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003415 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003416 }
satokab751aa2010-09-14 19:17:36 +09003417 }
satok723a27e2010-11-11 14:58:11 +09003418
Yohei Yukawa68645a62016-02-17 07:54:20 -08003419 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003420 // Set InputMethod here
3421 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3422 }
3423 }
3424
3425 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3426 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3427 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3428 // newDefaultIme is empty when there is no candidate for the selected IME.
3429 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3430 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3431 if (subtypeHashCode != null) {
3432 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003433 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003434 imi, Integer.valueOf(subtypeHashCode));
3435 } catch (NumberFormatException e) {
3436 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3437 }
3438 }
3439 }
3440 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003441 }
3442
satok4e4569d2010-11-19 18:45:53 +09003443 // If there are no selected shortcuts, tries finding the most applicable ones.
3444 private Pair<InputMethodInfo, InputMethodSubtype>
3445 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3446 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3447 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003448 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003449 boolean foundInSystemIME = false;
3450
3451 // Search applicable subtype for each InputMethodInfo
3452 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003453 final String imiId = imi.getId();
3454 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3455 continue;
3456 }
satokcd7cd292010-11-20 15:46:23 +09003457 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003458 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003459 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003460 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003461 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003462 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003463 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003464 }
satokdf31ae62011-01-15 06:19:44 +09003465 // 2. Search by the system locale from enabledSubtypes.
3466 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003467 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003468 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003469 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003470 }
satoka86f5e42011-09-02 17:12:42 +09003471 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003472 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003473 final ArrayList<InputMethodSubtype> subtypesForSearch =
3474 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003475 ? InputMethodUtils.getSubtypes(imi)
3476 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003477 // 4. Search by the current subtype's locale from all subtypes.
3478 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003479 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003480 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003481 }
3482 // 5. Search by the system locale from all subtypes.
3483 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003484 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003485 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003486 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003487 }
satokcd7cd292010-11-20 15:46:23 +09003488 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003489 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003490 // The current input method is the most applicable IME.
3491 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003492 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003493 break;
satok7599a7f2010-12-22 13:45:23 +09003494 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003495 // The system input method is 2nd applicable IME.
3496 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003497 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003498 if ((imi.getServiceInfo().applicationInfo.flags
3499 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3500 foundInSystemIME = true;
3501 }
satok4e4569d2010-11-19 18:45:53 +09003502 }
3503 }
3504 }
3505 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003506 if (mostApplicableIMI != null) {
3507 Slog.w(TAG, "Most applicable shortcut input method was:"
3508 + mostApplicableIMI.getId());
3509 if (mostApplicableSubtype != null) {
3510 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3511 + "," + mostApplicableSubtype.getMode() + ","
3512 + mostApplicableSubtype.getLocale());
3513 }
3514 }
satok4e4569d2010-11-19 18:45:53 +09003515 }
satokcd7cd292010-11-20 15:46:23 +09003516 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003517 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003518 } else {
3519 return null;
3520 }
3521 }
3522
satokab751aa2010-09-14 19:17:36 +09003523 /**
3524 * @return Return the current subtype of this input method.
3525 */
satok42c5a162011-05-26 16:46:14 +09003526 @Override
satokab751aa2010-09-14 19:17:36 +09003527 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003528 // TODO: Make this work even for non-current users?
3529 if (!calledFromValidUser()) {
3530 return null;
3531 }
3532 synchronized (mMethodMap) {
3533 return getCurrentInputMethodSubtypeLocked();
3534 }
3535 }
3536
3537 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003538 if (mCurMethodId == null) {
3539 return null;
3540 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003541 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003542 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3543 if (imi == null || imi.getSubtypeCount() == 0) {
3544 return null;
satok4e4569d2010-11-19 18:45:53 +09003545 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003546 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003547 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3548 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003549 if (subtypeId == NOT_A_SUBTYPE_ID) {
3550 // If there are no selected subtypes, the framework will try to find
3551 // the most applicable subtype from explicitly or implicitly enabled
3552 // subtypes.
3553 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003554 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003555 // If there is only one explicitly or implicitly enabled subtype,
3556 // just returns it.
3557 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3558 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3559 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003560 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003561 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003562 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003563 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003564 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003565 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3566 true);
satok4e4569d2010-11-19 18:45:53 +09003567 }
satok3ef8b292010-11-23 06:06:29 +09003568 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003569 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003570 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003571 }
3572 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003573 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003574 }
3575
satok4e4569d2010-11-19 18:45:53 +09003576 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003577 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003578 @Override
satok4e4569d2010-11-19 18:45:53 +09003579 public List getShortcutInputMethodsAndSubtypes() {
3580 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003581 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003582 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003583 // If there are no selected shortcut subtypes, the framework will try to find
3584 // the most applicable subtype from all subtypes whose mode is
3585 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003586 Pair<InputMethodInfo, InputMethodSubtype> info =
3587 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003588 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003589 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003590 ret.add(info.first);
3591 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003592 }
satok3da92232011-01-11 22:46:30 +09003593 return ret;
satokf3db1af2010-11-23 13:34:33 +09003594 }
satokf3db1af2010-11-23 13:34:33 +09003595 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3596 ret.add(imi);
3597 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3598 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003599 }
3600 }
satokf3db1af2010-11-23 13:34:33 +09003601 return ret;
satok4e4569d2010-11-19 18:45:53 +09003602 }
3603 }
3604
satok42c5a162011-05-26 16:46:14 +09003605 @Override
satokb66d2872010-11-10 01:04:04 +09003606 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003607 // TODO: Make this work even for non-current users?
3608 if (!calledFromValidUser()) {
3609 return false;
3610 }
satokb66d2872010-11-10 01:04:04 +09003611 synchronized (mMethodMap) {
3612 if (subtype != null && mCurMethodId != null) {
3613 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003614 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003615 if (subtypeId != NOT_A_SUBTYPE_ID) {
3616 setInputMethodLocked(mCurMethodId, subtypeId);
3617 return true;
3618 }
3619 }
3620 return false;
3621 }
3622 }
3623
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003624 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003625 private static class InputMethodFileManager {
3626 private static final String SYSTEM_PATH = "system";
3627 private static final String INPUT_METHOD_PATH = "inputmethod";
3628 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3629 private static final String NODE_SUBTYPES = "subtypes";
3630 private static final String NODE_SUBTYPE = "subtype";
3631 private static final String NODE_IMI = "imi";
3632 private static final String ATTR_ID = "id";
3633 private static final String ATTR_LABEL = "label";
3634 private static final String ATTR_ICON = "icon";
3635 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003636 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003637 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3638 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3639 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003640 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003641 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3642 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003643 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003644 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003645 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003646 if (methodMap == null) {
3647 throw new NullPointerException("methodMap is null");
3648 }
3649 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003650 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003651 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3652 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003653 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003654 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003655 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3656 }
3657 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3658 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3659 if (!subtypeFile.exists()) {
3660 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003661 writeAdditionalInputMethodSubtypes(
3662 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003663 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003664 readAdditionalInputMethodSubtypes(
3665 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003666 }
3667 }
3668
3669 private void deleteAllInputMethodSubtypes(String imiId) {
3670 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003671 mAdditionalSubtypesMap.remove(imiId);
3672 writeAdditionalInputMethodSubtypes(
3673 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003674 }
3675 }
3676
3677 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003678 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003679 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003680 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003681 final int N = additionalSubtypes.length;
3682 for (int i = 0; i < N; ++i) {
3683 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003684 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003685 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003686 } else {
3687 Slog.w(TAG, "Duplicated subtype definition found: "
3688 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003689 }
3690 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003691 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3692 writeAdditionalInputMethodSubtypes(
3693 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003694 }
3695 }
3696
3697 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3698 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003699 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003700 }
3701 }
3702
3703 private static void writeAdditionalInputMethodSubtypes(
3704 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3705 HashMap<String, InputMethodInfo> methodMap) {
3706 // Safety net for the case that this function is called before methodMap is set.
3707 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3708 FileOutputStream fos = null;
3709 try {
3710 fos = subtypesFile.startWrite();
3711 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003712 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003713 out.startDocument(null, true);
3714 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3715 out.startTag(null, NODE_SUBTYPES);
3716 for (String imiId : allSubtypes.keySet()) {
3717 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3718 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3719 continue;
3720 }
3721 out.startTag(null, NODE_IMI);
3722 out.attribute(null, ATTR_ID, imiId);
3723 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3724 final int N = subtypesList.size();
3725 for (int i = 0; i < N; ++i) {
3726 final InputMethodSubtype subtype = subtypesList.get(i);
3727 out.startTag(null, NODE_SUBTYPE);
3728 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3729 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3730 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003731 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3732 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003733 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3734 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3735 out.attribute(null, ATTR_IS_AUXILIARY,
3736 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003737 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3738 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003739 out.endTag(null, NODE_SUBTYPE);
3740 }
3741 out.endTag(null, NODE_IMI);
3742 }
3743 out.endTag(null, NODE_SUBTYPES);
3744 out.endDocument();
3745 subtypesFile.finishWrite(fos);
3746 } catch (java.io.IOException e) {
3747 Slog.w(TAG, "Error writing subtypes", e);
3748 if (fos != null) {
3749 subtypesFile.failWrite(fos);
3750 }
3751 }
3752 }
3753
3754 private static void readAdditionalInputMethodSubtypes(
3755 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3756 if (allSubtypes == null || subtypesFile == null) return;
3757 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003758 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003759 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003760 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003761 int type = parser.getEventType();
3762 // Skip parsing until START_TAG
3763 while ((type = parser.next()) != XmlPullParser.START_TAG
3764 && type != XmlPullParser.END_DOCUMENT) {}
3765 String firstNodeName = parser.getName();
3766 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3767 throw new XmlPullParserException("Xml doesn't start with subtypes");
3768 }
3769 final int depth =parser.getDepth();
3770 String currentImiId = null;
3771 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3772 while (((type = parser.next()) != XmlPullParser.END_TAG
3773 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3774 if (type != XmlPullParser.START_TAG)
3775 continue;
3776 final String nodeName = parser.getName();
3777 if (NODE_IMI.equals(nodeName)) {
3778 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3779 if (TextUtils.isEmpty(currentImiId)) {
3780 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3781 continue;
3782 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003783 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003784 allSubtypes.put(currentImiId, tempSubtypesArray);
3785 } else if (NODE_SUBTYPE.equals(nodeName)) {
3786 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3787 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3788 continue;
3789 }
3790 final int icon = Integer.valueOf(
3791 parser.getAttributeValue(null, ATTR_ICON));
3792 final int label = Integer.valueOf(
3793 parser.getAttributeValue(null, ATTR_LABEL));
3794 final String imeSubtypeLocale =
3795 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003796 final String languageTag =
3797 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003798 final String imeSubtypeMode =
3799 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3800 final String imeSubtypeExtraValue =
3801 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003802 final boolean isAuxiliary = "1".equals(String.valueOf(
3803 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003804 final boolean isAsciiCapable = "1".equals(String.valueOf(
3805 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003806 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3807 .setSubtypeNameResId(label)
3808 .setSubtypeIconResId(icon)
3809 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003810 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003811 .setSubtypeMode(imeSubtypeMode)
3812 .setSubtypeExtraValue(imeSubtypeExtraValue)
3813 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003814 .setIsAsciiCapable(isAsciiCapable)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003815 .build();
satoke7c6998e2011-06-03 17:57:59 +09003816 tempSubtypesArray.add(subtype);
3817 }
3818 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003819 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3820 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003821 return;
satoke7c6998e2011-06-03 17:57:59 +09003822 }
3823 }
3824 }
3825
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003826 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3827 @NonNull
3828 private final Handler mHandler;
3829
3830 LocalServiceImpl(@NonNull final Handler handler) {
3831 mHandler = handler;
3832 }
3833
3834 @Override
3835 public void setInteractive(boolean interactive) {
3836 // Do everything in handler so as not to block the caller.
3837 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3838 interactive ? 1 : 0, 0));
3839 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003840
3841 @Override
3842 public void switchInputMethod(boolean forwardDirection) {
3843 // Do everything in handler so as not to block the caller.
3844 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3845 forwardDirection ? 1 : 0, 0));
3846 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003847 }
3848
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003849 private static String imeWindowStatusToString(final int imeWindowVis) {
3850 final StringBuilder sb = new StringBuilder();
3851 boolean first = true;
3852 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3853 sb.append("Active");
3854 first = false;
3855 }
3856 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3857 if (!first) {
3858 sb.append("|");
3859 }
3860 sb.append("Visible");
3861 }
3862 return sb.toString();
3863 }
3864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 @Override
3866 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3867 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3868 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3871 + Binder.getCallingPid()
3872 + ", uid=" + Binder.getCallingUid());
3873 return;
3874 }
3875
3876 IInputMethod method;
3877 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003878 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 synchronized (mMethodMap) {
3883 p.println("Current Input Method Manager state:");
3884 int N = mMethodList.size();
3885 p.println(" Input Methods:");
3886 for (int i=0; i<N; i++) {
3887 InputMethodInfo info = mMethodList.get(i);
3888 p.println(" InputMethod #" + i + ":");
3889 info.dump(p, " ");
3890 }
3891 p.println(" Clients:");
3892 for (ClientState ci : mClients.values()) {
3893 p.println(" Client " + ci + ":");
3894 p.println(" client=" + ci.client);
3895 p.println(" inputContext=" + ci.inputContext);
3896 p.println(" sessionRequested=" + ci.sessionRequested);
3897 p.println(" curSession=" + ci.curSession);
3898 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003899 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003901 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3902 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003903 focusedWindowClient = mCurFocusedWindowClient;
3904 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3906 + " mBoundToMethod=" + mBoundToMethod);
3907 p.println(" mCurToken=" + mCurToken);
3908 p.println(" mCurIntent=" + mCurIntent);
3909 method = mCurMethod;
3910 p.println(" mCurMethod=" + mCurMethod);
3911 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003912 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 p.println(" mShowRequested=" + mShowRequested
3914 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3915 + " mShowForced=" + mShowForced
3916 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003917 p.println(" mCurUserActionNotificationSequenceNumber="
3918 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003919 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003920 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003921 p.println(" mSwitchingController:");
3922 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003923 p.println(" mSettings:");
3924 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003926
Jeff Brownb88102f2010-09-08 11:49:43 -07003927 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003929 pw.flush();
3930 try {
3931 client.client.asBinder().dump(fd, args);
3932 } catch (RemoteException e) {
3933 p.println("Input method client dead: " + e);
3934 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003935 } else {
3936 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003938
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003939 if (focusedWindowClient != null && client != focusedWindowClient) {
3940 p.println(" ");
3941 p.println("Warning: Current input method client doesn't match the last focused. "
3942 + "window.");
3943 p.println("Dumping input method client in the last focused window just in case.");
3944 p.println(" ");
3945 pw.flush();
3946 try {
3947 focusedWindowClient.client.asBinder().dump(fd, args);
3948 } catch (RemoteException e) {
3949 p.println("Input method client in focused window dead: " + e);
3950 }
3951 }
3952
Jeff Brownb88102f2010-09-08 11:49:43 -07003953 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 pw.flush();
3956 try {
3957 method.asBinder().dump(fd, args);
3958 } catch (RemoteException e) {
3959 p.println("Input method service dead: " + e);
3960 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003961 } else {
3962 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 }
3964 }
3965}