blob: 5d8fe7c68fe8f6026cb9661fea0ab86c115c2a3a [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
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070018import static java.lang.annotation.RetentionPolicy.SOURCE;
19
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080020import com.android.internal.content.PackageMonitor;
Satoshi Kataokad7443c82013-10-15 17:45:43 +090021import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090022import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090023import com.android.internal.inputmethod.InputMethodUtils;
24import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070026import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090027import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputContext;
29import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import com.android.internal.view.IInputMethodClient;
31import com.android.internal.view.IInputMethodManager;
32import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070033import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080035import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080036import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090040import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070042import android.annotation.IntDef;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070043import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070044import android.annotation.Nullable;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080045import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.app.ActivityManagerNative;
47import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070048import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090049import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090050import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090051import android.app.Notification;
52import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070053import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090054import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.ComponentName;
56import android.content.ContentResolver;
57import android.content.Context;
58import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090060import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090062import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070064import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090065import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.content.pm.PackageManager;
67import android.content.pm.ResolveInfo;
68import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070069import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.res.Resources;
71import android.content.res.TypedArray;
72import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080073import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080074import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080075import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070076import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040078import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070079import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090080import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Handler;
82import android.os.IBinder;
83import android.os.IInterface;
84import android.os.Message;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070085import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070087import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080089import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.ServiceManager;
91import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090092import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080093import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.provider.Settings;
95import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090096import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070097import android.util.ArrayMap;
98import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070099import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +0900101import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900102import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.util.PrintWriterPrinter;
104import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900105import android.util.Slog;
106import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900107import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700109import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900110import android.view.LayoutInflater;
111import android.view.View;
112import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700114import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900115import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.inputmethod.InputBinding;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700117import android.view.inputmethod.InputConnectionInspector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.view.inputmethod.InputMethod;
119import android.view.inputmethod.InputMethodInfo;
120import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700121import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900122import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900123import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900125import android.widget.CompoundButton;
126import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900127import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900128import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900129import android.widget.TextView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700130import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
satoke7c6998e2011-06-03 17:57:59 +0900132import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900134import java.io.FileInputStream;
135import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.io.IOException;
137import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700138import java.lang.annotation.Retention;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100139import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900141import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.util.HashMap;
143import java.util.List;
144
145/**
146 * This class provides a system service that manages input methods.
147 */
148public class InputMethodManagerService extends IInputMethodManager.Stub
149 implements ServiceConnection, Handler.Callback {
150 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700151 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700152 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700154 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
155 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
156 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 static final int MSG_UNBIND_INPUT = 1000;
159 static final int MSG_BIND_INPUT = 1010;
160 static final int MSG_SHOW_SOFT_INPUT = 1020;
161 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700162 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final int MSG_ATTACH_TOKEN = 1040;
164 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 static final int MSG_START_INPUT = 2000;
167 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
Yohei Yukawa33e81792015-11-17 21:14:42 -0800169 static final int MSG_UNBIND_CLIENT = 3000;
170 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700171 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700172 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900173 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800174 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
satok01038492012-04-09 21:08:27 +0900176 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
177
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700178 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
satokf9f01002011-05-19 21:31:50 +0900180 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
181
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900182 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900183 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900184
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700185 @Retention(SOURCE)
186 @IntDef({HardKeyboardBehavior.WIRELESS_AFFORDANCE, HardKeyboardBehavior.WIRED_AFFORDANCE})
187 private @interface HardKeyboardBehavior {
188 int WIRELESS_AFFORDANCE = 0;
189 int WIRED_AFFORDANCE = 1;
190 }
satok4e4569d2010-11-19 18:45:53 +0900191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800193 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900195 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700198 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700200 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900201 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900202 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900203 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800204 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900206 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 // All known input methods. mMethodMap also serves as the global
209 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700210 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
211 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900212 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700213 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900214 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700216 // Used to bring IME service up to visible adjustment while it is being shown.
217 final ServiceConnection mVisibleConnection = new ServiceConnection() {
218 @Override public void onServiceConnected(ComponentName name, IBinder service) {
219 }
220
221 @Override public void onServiceDisconnected(ComponentName name) {
222 }
223 };
224 boolean mVisibleBound = false;
225
satok7cfc0ed2011-06-20 21:29:36 +0900226 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700227 private NotificationManager mNotificationManager;
228 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700229 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400230 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700231 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900232 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900233 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900234 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900235
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900236 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 final ClientState client;
238 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700239
240 IInputMethodSession session;
241 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 @Override
244 public String toString() {
245 return "SessionState{uid " + client.uid + " pid " + client.pid
246 + " method " + Integer.toHexString(
247 System.identityHashCode(method))
248 + " session " + Integer.toHexString(
249 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700250 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 + "}";
252 }
253
254 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700255 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 client = _client;
257 method = _method;
258 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700259 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
Jeff Brownc28867a2013-03-26 15:42:39 -0700263 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 final IInputMethodClient client;
265 final IInputContext inputContext;
266 final int uid;
267 final int pid;
268 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 boolean sessionRequested;
271 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 @Override
274 public String toString() {
275 return "ClientState{" + Integer.toHexString(
276 System.identityHashCode(this)) + " uid " + uid
277 + " pid " + pid + "}";
278 }
279
280 ClientState(IInputMethodClient _client, IInputContext _inputContext,
281 int _uid, int _pid) {
282 client = _client;
283 inputContext = _inputContext;
284 uid = _uid;
285 pid = _pid;
286 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
287 }
288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700290 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700293 * Set once the system is ready to run third party code.
294 */
295 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700297 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700298 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
299 * method. This is to be synchronized with the secure settings keyed with
300 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
301 *
302 * <p>This can be transiently {@code null} when the system is re-initializing input method
303 * settings, e.g., the system locale is just changed.</p>
304 *
305 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
306 * {@link InputMethodManagerService}.</p>
307 *
308 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700310 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
314 * The current binding sequence number, incremented every time there is
315 * a new bind performed.
316 */
317 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * The client that is currently bound to an input method.
321 */
322 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800325 * The last window token that we confirmed to be focused. This is always updated upon reports
326 * from the input method client. If the window state is already changed before the report is
327 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700328 */
329 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700331 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800332 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
333 */
334 ClientState mCurFocusedWindowClient;
335
336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * The input context last provided by the current client.
338 */
339 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700342 * The missing method flags for the input context last provided by the current client.
343 *
344 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
345 */
346 int mCurInputContextMissingMethods;
347
348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * The attributes last provided by the current client.
350 */
351 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700354 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700356 *
357 * <p>This can be {@code null} when no input method is connected.</p>
358 *
359 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700361 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
satokab751aa2010-09-14 19:17:36 +0900365 * The current subtype of the current input method.
366 */
367 private InputMethodSubtype mCurrentSubtype;
368
satok4e4569d2010-11-19 18:45:53 +0900369 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900370 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700371 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900372
John Spurlocke0980502013-10-25 11:59:29 -0400373 // Was the keyguard locked when this client became current?
374 private boolean mCurClientInKeyguard;
375
satokab751aa2010-09-14 19:17:36 +0900376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * Set to true if our ServiceConnection is currently actively bound to
378 * a service (whether or not we have gotten its IBinder back yet).
379 */
380 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 /**
383 * Set if the client has asked for the input method to be shown.
384 */
385 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Set if we were explicitly told to show the input method.
389 */
390 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 /**
393 * Set if we were forced to be shown.
394 */
395 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 /**
398 * Set if we last told the input method to show itself.
399 */
400 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * The Intent used to connect to the current input method.
404 */
405 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 /**
408 * The token we have made for the currently active input method, to
409 * identify it in the future.
410 */
411 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 /**
414 * If non-null, this is the input method service we are currently connected
415 * to.
416 */
417 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 /**
420 * Time that we last initiated a bind to the input method, to determine
421 * if we should try to disconnect and reconnect to it.
422 */
423 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
426 * Have we called mCurMethod.bindInput()?
427 */
428 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 /**
431 * Currently enabled session. Only touched by service thread, not
432 * protected by a lock.
433 */
434 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700437 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700439 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900441 int mCurUserActionNotificationSequenceNumber = 0;
442
Joe Onorato857fd9b2011-01-27 15:08:35 -0800443 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900444
445 /**
446 * A set of status bits regarding the active IME.
447 *
448 * <p>This value is a combination of following two bits:</p>
449 * <dl>
450 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
451 * <dd>
452 * If this bit is ON, connected IME is ready to accept touch/key events.
453 * </dd>
454 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
455 * <dd>
456 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
457 * </dd>
458 * </dl>
459 * <em>Do not update this value outside of setImeWindowStatus.</em>
460 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800461 int mImeWindowVis;
462
Ken Wakasa05dbb652011-08-22 15:22:43 +0900463 private AlertDialog.Builder mDialogBuilder;
464 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900465 private View mSwitchingDialogTitleView;
Yohei Yukawaebda7d72016-04-02 17:39:23 -0700466 private Toast mSubtypeSwitchedByShortCutToast;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900467 private InputMethodInfo[] mIms;
468 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800469 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700470 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800471 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900472 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
473 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500474 private final String mSlotIme;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700475 @HardKeyboardBehavior
476 private final int mHardKeyboardBehavior;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700479 int mUserId;
480 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -0700481 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800482 String mLastEnabled = "";
483
Yohei Yukawa81482972015-06-04 00:58:59 -0700484 /**
485 * <em>This constructor must be called within the lock.</em>
486 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 SettingsObserver(Handler handler) {
488 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700489 }
490
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800491 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700492 if (mRegistered && mUserId == userId) {
493 return;
494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700496 if (mRegistered) {
497 mContext.getContentResolver().unregisterContentObserver(this);
498 mRegistered = false;
499 }
500 if (mUserId != userId) {
501 mLastEnabled = "";
502 mUserId = userId;
503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700505 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900506 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700507 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900508 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700509 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700510 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700511 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800512 resolver.registerContentObserver(Settings.Secure.getUriFor(
513 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700514 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
Michael Wright7b5a96b2014-08-09 19:28:42 -0700517 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800518 final Uri showImeUri = Settings.Secure.getUriFor(
519 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
520 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
521 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700523 if (showImeUri.equals(uri)) {
524 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800525 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
526 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
527 mContext.getContentResolver(),
528 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
529 0, mUserId) == 1;
530 if (mAccessibilityRequestingNoSoftKeyboard) {
531 final boolean showRequested = mShowRequested;
532 hideCurrentInputLocked(0, null);
533 mShowRequested = showRequested;
534 } else if (mShowRequested) {
535 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
536 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700537 } else {
538 boolean enabledChanged = false;
539 String newEnabled = mSettings.getEnabledInputMethodsStr();
540 if (!mLastEnabled.equals(newEnabled)) {
541 mLastEnabled = newEnabled;
542 enabledChanged = true;
543 }
544 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
547 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700548
549 @Override
550 public String toString() {
551 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
552 + " mLastEnabled=" + mLastEnabled + "}";
553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900556 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900557 @Override
558 public void onReceive(Context context, Intent intent) {
559 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700560 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900561 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700562 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900563 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800564 } else if (Intent.ACTION_USER_ADDED.equals(action)
565 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100566 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800567 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700568 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
569 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
570 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
571 final String prevValue = intent.getStringExtra(
572 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
573 final String newValue = intent.getStringExtra(
574 Intent.EXTRA_SETTING_NEW_VALUE);
575 restoreEnabledInputMethods(mContext, prevValue, newValue);
576 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900577 } else {
578 Slog.w(TAG, "Unexpected intent " + intent);
579 }
580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800582
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700583 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
584 // does not attempt to validate on the fly with any installed device policy, so must only
585 // be run in the context of initial device setup.
586 //
587 // TODO: Move this method to InputMethodUtils with adding unit tests.
588 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
589 if (DEBUG_RESTORE) {
590 Slog.i(TAG, "Restoring enabled input methods:");
591 Slog.i(TAG, "prev=" + prevValue);
592 Slog.i(TAG, " new=" + newValue);
593 }
594 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900595 ArrayMap<String, ArraySet<String>> prevMap =
596 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
597 ArrayMap<String, ArraySet<String>> newMap =
598 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700599
600 // Merge the restored ime+subtype enabled states into the live state
601 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
602 final String imeId = entry.getKey();
603 ArraySet<String> prevSubtypes = prevMap.get(imeId);
604 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700605 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700606 prevMap.put(imeId, prevSubtypes);
607 }
608 prevSubtypes.addAll(entry.getValue());
609 }
610
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700611 final String mergedImesAndSubtypesString =
612 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700613 if (DEBUG_RESTORE) {
614 Slog.i(TAG, "Merged IME string:");
615 Slog.i(TAG, " " + mergedImesAndSubtypesString);
616 }
617 Settings.Secure.putString(context.getContentResolver(),
618 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
619 }
620
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800621 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900622 private boolean isChangingPackagesOfCurrentUser() {
623 final int userId = getChangingUserId();
624 final boolean retval = userId == mSettings.getCurrentUserId();
625 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900626 if (!retval) {
627 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
628 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900629 }
630 return retval;
631 }
632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800634 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 if (!isChangingPackagesOfCurrentUser()) {
636 return false;
637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900639 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 final int N = mMethodList.size();
641 if (curInputMethodId != null) {
642 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800643 InputMethodInfo imi = mMethodList.get(i);
644 if (imi.getId().equals(curInputMethodId)) {
645 for (String pkg : packages) {
646 if (imi.getPackageName().equals(pkg)) {
647 if (!doit) {
648 return true;
649 }
satok723a27e2010-11-11 14:58:11 +0900650 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800651 chooseNewDefaultIMELocked();
652 return true;
653 }
654 }
655 }
656 }
657 }
658 }
659 return false;
660 }
661
662 @Override
663 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900664 if (!isChangingPackagesOfCurrentUser()) {
665 return;
666 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800667 synchronized (mMethodMap) {
668 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900669 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800670 final int N = mMethodList.size();
671 if (curInputMethodId != null) {
672 for (int i=0; i<N; i++) {
673 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900674 final String imiId = imi.getId();
675 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800676 curIm = imi;
677 }
satoke7c6998e2011-06-03 17:57:59 +0900678
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800679 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900680 if (isPackageModified(imi.getPackageName())) {
681 mFileManager.deleteAllInputMethodSubtypes(imiId);
682 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800683 if (change == PACKAGE_TEMPORARY_CHANGE
684 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800685 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800686 + imi.getComponent());
687 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
689 }
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
Yohei Yukawa94e33302016-02-12 19:37:03 -0800692 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800695
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800696 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800697 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698 if (change == PACKAGE_TEMPORARY_CHANGE
699 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800700 ServiceInfo si = null;
701 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900702 si = mIPackageManager.getServiceInfo(
703 curIm.getComponent(), 0, mSettings.getCurrentUserId());
704 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800705 }
706 if (si == null) {
707 // Uh oh, current input method is no longer around!
708 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800709 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900710 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800711 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800712 changed = true;
713 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800714 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900715 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800716 }
717 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800718 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800719 }
satokab751aa2010-09-14 19:17:36 +0900720
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800721 if (curIm == null) {
722 // We currently don't have a default input method... is
723 // one now available?
724 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700725 } else if (!changed && isPackageModified(curIm.getPackageName())) {
726 // Even if the current input method is still available, mCurrentSubtype could
727 // be obsolete when the package is modified in practice.
728 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800731 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800732 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734 }
735 }
736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
Jeff Brownc28867a2013-03-26 15:42:39 -0700738 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900739 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700740 private final IInputMethod mMethod;
741 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800742
Jeff Brownc28867a2013-03-26 15:42:39 -0700743 MethodCallback(InputMethodManagerService imms, IInputMethod method,
744 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900745 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700746 mMethod = method;
747 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
satoke7c6998e2011-06-03 17:57:59 +0900750 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700751 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700752 long ident = Binder.clearCallingIdentity();
753 try {
754 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
755 } finally {
756 Binder.restoreCallingIdentity(ident);
757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
satok01038492012-04-09 21:08:27 +0900761 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700762 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900763 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700764 public void onHardKeyboardStatusChange(boolean available) {
765 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
766 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900767 }
768
Michael Wright7b5a96b2014-08-09 19:28:42 -0700769 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900770 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700771 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900772 }
773 synchronized(mMethodMap) {
774 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
775 && mSwitchingDialog.isShowing()) {
776 mSwitchingDialogTitleView.findViewById(
777 com.android.internal.R.id.hard_keyboard_section).setVisibility(
778 available ? View.VISIBLE : View.GONE);
779 }
780 }
781 }
782 }
783
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800784 public static final class Lifecycle extends SystemService {
785 private InputMethodManagerService mService;
786
787 public Lifecycle(Context context) {
788 super(context);
789 mService = new InputMethodManagerService(context);
790 }
791
792 @Override
793 public void onStart() {
794 LocalServices.addService(InputMethodManagerInternal.class,
795 new LocalServiceImpl(mService.mHandler));
796 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
797 }
798
799 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800800 public void onSwitchUser(@UserIdInt int userHandle) {
801 // Called on the system server's main looper thread.
802 // TODO: Dispatch this to a worker thread as needed.
803 mService.onSwitchUser(userHandle);
804 }
805
806 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800807 public void onBootPhase(int phase) {
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800808 // Called on the system server's main looper thread.
809 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800810 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
811 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
812 .getService(Context.STATUS_BAR_SERVICE);
813 mService.systemRunning(statusBarService);
814 }
815 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800816
817 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800818 public void onUnlockUser(@UserIdInt int userHandle) {
819 // Called on the system server's main looper thread.
820 // TODO: Dispatch this to a worker thread as needed.
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800821 mService.onUnlockUser(userHandle);
822 }
823 }
824
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800825 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800826 synchronized(mMethodMap) {
827 final int currentUserId = mSettings.getCurrentUserId();
828 if (DEBUG) {
829 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
830 }
831 if (userId != currentUserId) {
832 return;
833 }
834 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
835 // We need to rebuild IMEs.
836 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
837 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
838 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800839 }
840
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800841 void onSwitchUser(@UserIdInt int userId) {
842 synchronized (mMethodMap) {
843 switchUserLocked(userId);
844 }
845 }
846
Seigo Nonaka7309b122015-08-17 18:34:13 -0700847 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900848 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800850 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700852 // Note: SettingsObserver doesn't register observers in its constructor.
853 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 mIWindowManager = IWindowManager.Stub.asInterface(
855 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700856 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800857 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900858 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 public void executeMessage(Message msg) {
860 handleMessage(msg);
861 }
Mita Yuned218c72012-12-06 17:18:25 -0800862 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800863 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800864 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900865 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700866 mHasFeature = context.getPackageManager().hasSystemFeature(
867 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500868 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700869 mHardKeyboardBehavior = mContext.getResources().getInteger(
870 com.android.internal.R.integer.config_externalHardKeyboardBehavior);
satok7cfc0ed2011-06-20 21:29:36 +0900871
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400872 Bundle extras = new Bundle();
873 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
874 mImeSwitcherNotification = new Notification.Builder(mContext)
875 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
876 .setWhen(0)
877 .setOngoing(true)
878 .addExtras(extras)
879 .setCategory(Notification.CATEGORY_SYSTEM)
880 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400881
satok7cfc0ed2011-06-20 21:29:36 +0900882 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900883 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900884
885 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900886
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900887 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900888 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800889 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
890 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700891 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800893
satok7cfc0ed2011-06-20 21:29:36 +0900894 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900895 int userId = 0;
896 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
898 } catch (RemoteException e) {
899 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
900 }
satok81f8b7c2012-12-04 20:42:56 +0900901 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900902
satokd87c2592010-09-29 11:52:06 +0900903 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900904 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800905 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700906
Kenny Guy2a764942014-04-02 13:29:20 +0100907 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900908 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900909 synchronized (mMethodMap) {
910 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
911 mSettings, context);
912 }
satok0a1bcf42012-05-16 19:26:31 +0900913
914 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900915 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900916 if (DEBUG) {
917 Slog.d(TAG, "Initial default ime = " + defaultImiId);
918 }
satok0a1bcf42012-05-16 19:26:31 +0900919 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
920
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900921 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800922 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900923 }
satokd87c2592010-09-29 11:52:06 +0900924 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925
satok0a1bcf42012-05-16 19:26:31 +0900926 if (!mImeSelectedOnBoot) {
927 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900928 synchronized (mMethodMap) {
929 resetDefaultImeLocked(context);
930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800932
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900933 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700934 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900935 updateFromSettingsLocked(true);
936 }
satok5b927c432012-05-01 20:09:34 +0900937
938 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
939 // according to the new system locale.
940 final IntentFilter filter = new IntentFilter();
941 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
942 mContext.registerReceiver(
943 new BroadcastReceiver() {
944 @Override
945 public void onReceive(Context context, Intent intent) {
946 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900947 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900948 }
949 }
950 }, filter);
951 }
952
satok5b927c432012-05-01 20:09:34 +0900953 private void resetDefaultImeLocked(Context context) {
954 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800955 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900956 return;
957 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800958 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
959 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
960 if (suitableImes.isEmpty()) {
961 Slog.i(TAG, "No default found");
962 return;
satok5b927c432012-05-01 20:09:34 +0900963 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800964 final InputMethodInfo defIm = suitableImes.get(0);
965 Slog.i(TAG, "Default found, using " + defIm.getId());
966 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900967 }
968
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900969 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
970 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 if (!mSystemReady) {
972 // not system ready
973 return;
974 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800975 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900976 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800977 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900978 if (!updateOnlyWhenLocaleChanged) {
979 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800980 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900981 }
982 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800983 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900984 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800985 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900986 if (!updateOnlyWhenLocaleChanged) {
987 final String selectedImiId = mSettings.getSelectedInputMethod();
988 if (TextUtils.isEmpty(selectedImiId)) {
989 // This is the first time of the user switch and
990 // set the current ime to the proper one.
991 resetDefaultImeLocked(mContext);
992 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900993 } else {
994 // If the locale is changed, needs to reset the default ime
995 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800997 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800998 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900999 if (!updateOnlyWhenLocaleChanged) {
1000 try {
1001 startInputInnerLocked();
1002 } catch (RuntimeException e) {
1003 Slog.w(TAG, "Unexpected exception", e);
1004 }
1005 }
1006 }
1007 }
1008
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001009 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001010 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
1011 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001012 }
1013
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001014 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001015 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1016 + " currentUserId=" + mSettings.getCurrentUserId());
1017
Yohei Yukawa81482972015-06-04 00:58:59 -07001018 // ContentObserver should be registered again when the user is changed
1019 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001020
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001021 // If the system is not ready or the device is not yed unlocked by the user, then we use
1022 // copy-on-write settings.
1023 final boolean useCopyOnWriteSettings =
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001024 !mSystemReady || !mUserManager.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001025 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001026 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001027 // InputMethodFileManager should be reset when the user is changed
1028 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001029 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001030
1031 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1032 + " defaultImiId=" + defaultImiId);
1033
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001034 // For secondary users, the list of enabled IMEs may not have been updated since the
1035 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1036 // not be empty even if the IME has been uninstalled by the primary user.
1037 // Even in such cases, IMMS works fine because it will find the most applicable
1038 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001039 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001040 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001041 initialUserSwitch /* needsToResetDefaultIme */);
1042 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001043 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1044 mSettings.getEnabledInputMethodListLocked(), newUserId,
1045 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001046 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001047
1048 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1049 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001050 }
1051
Kenny Guy2a764942014-04-02 13:29:20 +01001052 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001053 mSettings.setCurrentProfileIds(
1054 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001055 }
1056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 @Override
1058 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1059 throws RemoteException {
1060 try {
1061 return super.onTransact(code, data, reply, flags);
1062 } catch (RuntimeException e) {
1063 // The input method manager only throws security exceptions, so let's
1064 // log all others.
1065 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001066 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 }
1068 throw e;
1069 }
1070 }
1071
Svetoslav Ganova0027152013-06-25 14:59:53 -07001072 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001073 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001074 if (DEBUG) {
1075 Slog.d(TAG, "--- systemReady");
1076 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001077 if (!mSystemReady) {
1078 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001079 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001080 mSettings.switchCurrentUser(currentUserId,
Jeff Sharkeyce18c812016-04-27 16:00:41 -06001081 !mUserManager.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001082 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1083 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001084 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001085 if (mStatusBar != null) {
1086 mStatusBar.setIconVisibility(mSlotIme, false);
1087 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001088 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001089 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1090 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001091 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001092 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001093 mHardKeyboardListener);
1094 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001095 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001096 if (!mImeSelectedOnBoot) {
1097 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001098 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001099 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1100 mSettings.getEnabledInputMethodListLocked(),
1101 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001102 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001103 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001104 try {
1105 startInputInnerLocked();
1106 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001107 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001108 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001109 }
1110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001112
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001113 // ---------------------------------------------------------------------------------------
1114 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1115 // 1) it comes from the system process
1116 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1117 private boolean calledFromValidUser() {
1118 final int uid = Binder.getCallingUid();
1119 final int userId = UserHandle.getUserId(uid);
1120 if (DEBUG) {
1121 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1122 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1123 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001124 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1125 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001126 }
Kenny Guy2a764942014-04-02 13:29:20 +01001127 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001128 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001129 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001130
1131 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1132 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1133 // must not manage background users' states in any functions.
1134 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1135 // by a token.
1136 if (mContext.checkCallingOrSelfPermission(
1137 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1138 == PackageManager.PERMISSION_GRANTED) {
1139 if (DEBUG) {
1140 Slog.d(TAG, "--- Access granted because the calling process has "
1141 + "the INTERACT_ACROSS_USERS_FULL permission");
1142 }
1143 return true;
1144 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001145 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1146 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001147 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001148 }
1149
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001150
1151 /**
1152 * Returns true iff the caller is identified to be the current input method with the token.
1153 * @param token The window token given to the input method when it was started.
1154 * @return true if and only if non-null valid token is specified.
1155 */
1156 private boolean calledWithValidToken(IBinder token) {
1157 if (token == null || mCurToken != token) {
1158 return false;
1159 }
1160 return true;
1161 }
1162
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001163 private boolean bindCurrentInputMethodService(
1164 Intent service, ServiceConnection conn, int flags) {
1165 if (service == null || conn == null) {
1166 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1167 return false;
1168 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001169 return mContext.bindServiceAsUser(service, conn, flags,
1170 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001171 }
1172
satoke7c6998e2011-06-03 17:57:59 +09001173 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001175 // TODO: Make this work even for non-current users?
1176 if (!calledFromValidUser()) {
1177 return Collections.emptyList();
1178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001180 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182 }
1183
satoke7c6998e2011-06-03 17:57:59 +09001184 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001186 // TODO: Make this work even for non-current users?
1187 if (!calledFromValidUser()) {
1188 return Collections.emptyList();
1189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001191 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
1194
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001195 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001196 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001197 * @return enabled subtypes of the specified imi
1198 */
satoke7c6998e2011-06-03 17:57:59 +09001199 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001200 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001201 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001202 // TODO: Make this work even for non-current users?
1203 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001204 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001205 }
satok67ddf9c2010-11-17 09:45:54 +09001206 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001207 final InputMethodInfo imi;
1208 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001209 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001210 } else {
1211 imi = mMethodMap.get(imiId);
1212 }
1213 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001214 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001215 }
1216 return mSettings.getEnabledInputMethodSubtypeListLocked(
1217 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001218 }
1219 }
1220
satoke7c6998e2011-06-03 17:57:59 +09001221 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public void addClient(IInputMethodClient client,
1223 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001224 if (!calledFromValidUser()) {
1225 return;
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 synchronized (mMethodMap) {
1228 mClients.put(client.asBinder(), new ClientState(client,
1229 inputContext, uid, pid));
1230 }
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001235 if (!calledFromValidUser()) {
1236 return;
1237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001239 ClientState cs = mClients.remove(client.asBinder());
1240 if (cs != null) {
1241 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001242 if (mCurClient == cs) {
1243 mCurClient = null;
1244 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001245 if (mCurFocusedWindowClient == cs) {
1246 mCurFocusedWindowClient = null;
1247 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 void executeOrSendMessage(IInterface target, Message msg) {
1253 if (target.asBinder() instanceof Binder) {
1254 mCaller.sendMessage(msg);
1255 } else {
1256 handleMessage(msg);
1257 msg.recycle();
1258 }
1259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
Yohei Yukawa33e81792015-11-17 21:14:42 -08001261 void unbindCurrentClientLocked(
1262 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001264 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 + mCurClient.client.asBinder());
1266 if (mBoundToMethod) {
1267 mBoundToMethod = false;
1268 if (mCurMethod != null) {
1269 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1270 MSG_UNBIND_INPUT, mCurMethod));
1271 }
1272 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001273
1274 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1275 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001276 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1277 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001280
The Android Open Source Project10592532009-03-18 17:39:46 -07001281 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 private int getImeShowFlags() {
1286 int flags = 0;
1287 if (mShowForced) {
1288 flags |= InputMethod.SHOW_FORCED
1289 | InputMethod.SHOW_EXPLICIT;
1290 } else if (mShowExplicitlyRequested) {
1291 flags |= InputMethod.SHOW_EXPLICIT;
1292 }
1293 return flags;
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 private int getAppShowFlags() {
1297 int flags = 0;
1298 if (mShowForced) {
1299 flags |= InputMethodManager.SHOW_FORCED;
1300 } else if (!mShowExplicitlyRequested) {
1301 flags |= InputMethodManager.SHOW_IMPLICIT;
1302 }
1303 return flags;
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
Dianne Hackborn7663d802012-02-24 13:08:49 -08001306 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 if (!mBoundToMethod) {
1308 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1309 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1310 mBoundToMethod = true;
1311 }
1312 final SessionState session = mCurClient.curSession;
1313 if (initial) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001314 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1315 MSG_START_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1316 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001318 executeOrSendMessage(session.method, mCaller.obtainMessageIOOO(
1319 MSG_RESTART_INPUT, mCurInputContextMissingMethods, session, mCurInputContext,
1320 mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001323 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001326 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001327 (session.channel != null ? session.channel.dup() : null),
1328 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001330
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001331 InputBindResult startInputLocked(
1332 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001333 IInputMethodClient client, IInputContext inputContext,
1334 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001335 @Nullable EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 // If no method is currently selected, do nothing.
1337 if (mCurMethodId == null) {
1338 return mNoBinding;
1339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 ClientState cs = mClients.get(client.asBinder());
1342 if (cs == null) {
1343 throw new IllegalArgumentException("unknown client "
1344 + client.asBinder());
1345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
Yohei Yukawa74750f22016-03-22 12:54:22 -07001347 if (attribute == null) {
1348 Slog.w(TAG, "Ignoring startInput with null EditorInfo."
1349 + " uid=" + cs.uid + " pid=" + cs.pid);
1350 return null;
1351 }
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 try {
1354 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1355 // Check with the window manager to make sure this client actually
1356 // has a window with focus. If not, reject. This is thread safe
1357 // because if the focus changes some time before or after, the
1358 // next client receiving focus that has any interest in input will
1359 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001360 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1362 return null;
1363 }
1364 } catch (RemoteException e) {
1365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001367 return startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
1368 controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001369 }
1370
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001371 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001372 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001373 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001374 // If no method is currently selected, do nothing.
1375 if (mCurMethodId == null) {
1376 return mNoBinding;
1377 }
1378
Yohei Yukawad57ba672015-06-08 16:39:46 -07001379 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1380 attribute.packageName)) {
1381 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1382 + " uid=" + cs.uid + " package=" + attribute.packageName);
1383 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001384 }
1385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001387 // Was the keyguard locked when switching over to the new client?
1388 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 // If the client is changing, we need to switch over to the new
1390 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001391 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001392 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001393 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394
1395 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001396 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001397 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001398 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
1400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 // Bump up the sequence for this client and attach it.
1403 mCurSeq++;
1404 if (mCurSeq <= 0) mCurSeq = 1;
1405 mCurClient = cs;
1406 mCurInputContext = inputContext;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001407 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 // Check if the input method is changing.
1411 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1412 if (cs.curSession != null) {
1413 // Fast case: if we are already connected to the input method,
1414 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001415 return attachNewInputLocked(
1416 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
1418 if (mHaveConnection) {
1419 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 // Return to client, and we will get back with it when
1421 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001422 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001423 return new InputBindResult(null, null, mCurId, mCurSeq,
1424 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 } else if (SystemClock.uptimeMillis()
1426 < (mLastBindTime+TIME_TO_RECONNECT)) {
1427 // In this case we have connected to the service, but
1428 // don't yet have its interface. If it hasn't been too
1429 // long since we did the connection, we'll return to
1430 // the client and wait to get the service interface so
1431 // we can report back. If it has been too long, we want
1432 // to fall through so we can try a disconnect/reconnect
1433 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001434 return new InputBindResult(null, null, mCurId, mCurSeq,
1435 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001437 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1438 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
1440 }
1441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001443 return startInputInnerLocked();
1444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001446 InputBindResult startInputInnerLocked() {
1447 if (mCurMethodId == null) {
1448 return mNoBinding;
1449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001451 if (!mSystemReady) {
1452 // If the system is not yet ready, we shouldn't be running third
1453 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001454 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1455 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1459 if (info == null) {
1460 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001463 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1466 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001467 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1468 com.android.internal.R.string.input_method_binding_label);
1469 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1470 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001471 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001472 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1473 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 mLastBindTime = SystemClock.uptimeMillis();
1475 mHaveConnection = true;
1476 mCurId = info.getId();
1477 mCurToken = new Binder();
1478 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001479 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 mIWindowManager.addWindowToken(mCurToken,
1481 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1482 } catch (RemoteException e) {
1483 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001484 return new InputBindResult(null, null, mCurId, mCurSeq,
1485 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 } else {
1487 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001488 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 + mCurIntent);
1490 }
1491 return null;
1492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001494 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001495 /* @InputMethodClient.StartInputReason */ final int startInputReason,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001496 IInputMethodClient client, IInputContext inputContext,
1497 /* @InputConnectionInspector.missingMethods */ final int missingMethods,
Yohei Yukawa74750f22016-03-22 12:54:22 -07001498 @Nullable EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001499 if (!calledFromValidUser()) {
1500 return null;
1501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001503 if (DEBUG) {
1504 Slog.v(TAG, "startInput: reason="
1505 + InputMethodClient.getStartInputReason(startInputReason)
1506 + " client = " + client.asBinder()
1507 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001508 + " missingMethods="
1509 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001510 + " attribute=" + attribute
1511 + " controlFlags=#" + Integer.toHexString(controlFlags));
1512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 final long ident = Binder.clearCallingIdentity();
1514 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07001515 return startInputLocked(startInputReason, client, inputContext, missingMethods,
1516 attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 } finally {
1518 Binder.restoreCallingIdentity(ident);
1519 }
1520 }
1521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001522
satoke7c6998e2011-06-03 17:57:59 +09001523 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 public void finishInput(IInputMethodClient client) {
1525 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001526
satoke7c6998e2011-06-03 17:57:59 +09001527 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 public void onServiceConnected(ComponentName name, IBinder service) {
1529 synchronized (mMethodMap) {
1530 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1531 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001532 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001533 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001534 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001535 return;
1536 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001537 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001538 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1539 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001541 clearClientSessionLocked(mCurClient);
1542 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
1544 }
1545 }
1546 }
1547
Jeff Brownc28867a2013-03-26 15:42:39 -07001548 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1549 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 synchronized (mMethodMap) {
1551 if (mCurMethod != null && method != null
1552 && mCurMethod.asBinder() == method.asBinder()) {
1553 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001554 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001556 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001557 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 if (res.method != null) {
1559 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001560 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001562 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 }
1564 }
1565 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001566
1567 // Session abandoned. Close its associated input channel.
1568 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001570
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001571 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001572 if (mVisibleBound) {
1573 mContext.unbindService(mVisibleConnection);
1574 mVisibleBound = false;
1575 }
1576
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001577 if (mHaveConnection) {
1578 mContext.unbindService(this);
1579 mHaveConnection = false;
1580 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001581
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001582 if (mCurToken != null) {
1583 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001584 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001585 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001586 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001587 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001588 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001589 mIWindowManager.removeWindowToken(mCurToken);
1590 } catch (RemoteException e) {
1591 }
1592 mCurToken = null;
1593 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001594
The Android Open Source Project10592532009-03-18 17:39:46 -07001595 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001596 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001597 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001598
Yohei Yukawa33e81792015-11-17 21:14:42 -08001599 void resetCurrentMethodAndClient(
1600 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001601 mCurMethodId = null;
1602 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001603 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001604 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001605
1606 void requestClientSessionLocked(ClientState cs) {
1607 if (!cs.sessionRequested) {
1608 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1609 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1610 cs.sessionRequested = true;
1611 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1612 MSG_CREATE_SESSION, mCurMethod, channels[1],
1613 new MethodCallback(this, mCurMethod, channels[0])));
1614 }
1615 }
1616
1617 void clearClientSessionLocked(ClientState cs) {
1618 finishSessionLocked(cs.curSession);
1619 cs.curSession = null;
1620 cs.sessionRequested = false;
1621 }
1622
1623 private void finishSessionLocked(SessionState sessionState) {
1624 if (sessionState != null) {
1625 if (sessionState.session != null) {
1626 try {
1627 sessionState.session.finishSession();
1628 } catch (RemoteException e) {
1629 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001630 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001631 }
1632 sessionState.session = null;
1633 }
1634 if (sessionState.channel != null) {
1635 sessionState.channel.dispose();
1636 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001637 }
1638 }
1639 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001640
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001641 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 if (mCurMethod != null) {
1643 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001644 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001646
Jeff Brownc28867a2013-03-26 15:42:39 -07001647 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001648 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 mCurMethod = null;
1650 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001651 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001652 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001655
satoke7c6998e2011-06-03 17:57:59 +09001656 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 public void onServiceDisconnected(ComponentName name) {
1658 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001659 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 + " mCurIntent=" + mCurIntent);
1661 if (mCurMethod != null && mCurIntent != null
1662 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001663 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 // We consider this to be a new bind attempt, since the system
1665 // should now try to restart the service for us.
1666 mLastBindTime = SystemClock.uptimeMillis();
1667 mShowRequested = mInputShown;
1668 mInputShown = false;
1669 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001670 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1671 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1672 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674 }
1675 }
1676 }
1677
satokf9f01002011-05-19 21:31:50 +09001678 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1680 long ident = Binder.clearCallingIdentity();
1681 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001683 if (!calledWithValidToken(token)) {
1684 final int uid = Binder.getCallingUid();
1685 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1686 + " token:" + token);
1687 return;
1688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001691 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001692 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001695 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001696 CharSequence contentDescription = null;
1697 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001698 // Use PackageManager to load label
1699 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001700 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001701 mIPackageManager.getApplicationInfo(packageName, 0,
1702 mSettings.getCurrentUserId()));
1703 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001704 /* ignore */
1705 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001706 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001707 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001708 contentDescription != null
1709 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001710 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
1713 }
1714 } finally {
1715 Binder.restoreCallingIdentity(ident);
1716 }
1717 }
1718
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001719 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001720 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001721 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001722 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001723 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001724 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawafa0e47e2016-04-05 09:55:56 -07001725 if (mHardKeyboardBehavior == HardKeyboardBehavior.WIRELESS_AFFORDANCE) {
1726 // When physical keyboard is attached, we show the ime switcher (or notification if
1727 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1728 // exists in the IME switcher dialog. Might be OK to remove this condition once
1729 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1730 return true;
1731 }
Yohei Yukawa89398382016-03-29 11:37:04 -07001732 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
1733 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001734 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001735
1736 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1737 final int N = imis.size();
1738 if (N > 2) return true;
1739 if (N < 1) return false;
1740 int nonAuxCount = 0;
1741 int auxCount = 0;
1742 InputMethodSubtype nonAuxSubtype = null;
1743 InputMethodSubtype auxSubtype = null;
1744 for(int i = 0; i < N; ++i) {
1745 final InputMethodInfo imi = imis.get(i);
1746 final List<InputMethodSubtype> subtypes =
1747 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1748 final int subtypeCount = subtypes.size();
1749 if (subtypeCount == 0) {
1750 ++nonAuxCount;
1751 } else {
1752 for (int j = 0; j < subtypeCount; ++j) {
1753 final InputMethodSubtype subtype = subtypes.get(j);
1754 if (!subtype.isAuxiliary()) {
1755 ++nonAuxCount;
1756 nonAuxSubtype = subtype;
1757 } else {
1758 ++auxCount;
1759 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001760 }
1761 }
satok7cfc0ed2011-06-20 21:29:36 +09001762 }
1763 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001764 if (nonAuxCount > 1 || auxCount > 1) {
1765 return true;
1766 } else if (nonAuxCount == 1 && auxCount == 1) {
1767 if (nonAuxSubtype != null && auxSubtype != null
1768 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1769 || auxSubtype.overridesImplicitlyEnabledSubtype()
1770 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1771 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1772 return false;
1773 }
1774 return true;
1775 }
1776 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001777 }
1778
John Spurlocke0980502013-10-25 11:59:29 -04001779 private boolean isKeyguardLocked() {
1780 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1781 }
1782
satokdbf29502011-08-25 15:28:23 +09001783 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001784 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001785 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001786 if (!calledWithValidToken(token)) {
1787 final int uid = Binder.getCallingUid();
1788 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1789 + " token:" + token);
1790 return;
1791 }
1792
1793 synchronized (mMethodMap) {
1794 mImeWindowVis = vis;
1795 mBackDisposition = backDisposition;
1796 updateSystemUiLocked(token, vis, backDisposition);
1797 }
1798 }
1799
1800 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1801 synchronized (mMethodMap) {
1802 updateSystemUiLocked(token, vis, backDisposition);
1803 }
1804 }
1805
1806 // Caution! This method is called in this class. Handle multi-user carefully
1807 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1808 if (!calledWithValidToken(token)) {
1809 final int uid = Binder.getCallingUid();
1810 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1811 + " token:" + token);
1812 return;
1813 }
1814
1815 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1816 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001817 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001818 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001819 // apply policy for binder calls
1820 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1821 vis = 0;
satok06487a52010-10-29 11:37:18 +09001822 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001823 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1824 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1825 if (mStatusBar != null) {
1826 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1827 needsToShowImeSwitcher);
1828 }
1829 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1830 if (imi != null && needsToShowImeSwitcher) {
1831 // Used to load label
1832 final CharSequence title = mRes.getText(
1833 com.android.internal.R.string.select_input_method);
1834 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1835 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001836 mImeSwitcherNotification.setContentTitle(title)
1837 .setContentText(summary)
1838 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001839 try {
1840 if ((mNotificationManager != null)
1841 && !mIWindowManager.hasNavigationBar()) {
1842 if (DEBUG) {
1843 Slog.d(TAG, "--- show notification: label = " + summary);
1844 }
1845 mNotificationManager.notifyAsUser(null,
1846 com.android.internal.R.string.select_input_method,
1847 mImeSwitcherNotification.build(), UserHandle.ALL);
1848 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001849 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001850 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001851 }
1852 } else {
1853 if (mNotificationShown && mNotificationManager != null) {
1854 if (DEBUG) {
1855 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001856 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001857 mNotificationManager.cancelAsUser(null,
1858 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1859 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001860 }
satok06487a52010-10-29 11:37:18 +09001861 }
1862 } finally {
1863 Binder.restoreCallingIdentity(ident);
1864 }
1865 }
1866
satoke7c6998e2011-06-03 17:57:59 +09001867 @Override
satokf9f01002011-05-19 21:31:50 +09001868 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001869 if (!calledFromValidUser()) {
1870 return;
1871 }
satokf9f01002011-05-19 21:31:50 +09001872 synchronized (mMethodMap) {
1873 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1874 for (int i = 0; i < spans.length; ++i) {
1875 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001876 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001877 mSecureSuggestionSpans.put(ss, currentImi);
1878 }
1879 }
1880 }
1881 }
1882
satoke7c6998e2011-06-03 17:57:59 +09001883 @Override
satokf9f01002011-05-19 21:31:50 +09001884 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001885 if (!calledFromValidUser()) {
1886 return false;
1887 }
satokf9f01002011-05-19 21:31:50 +09001888 synchronized (mMethodMap) {
1889 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1890 // TODO: Do not send the intent if the process of the targetImi is already dead.
1891 if (targetImi != null) {
1892 final String[] suggestions = span.getSuggestions();
1893 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001894 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001895 final Intent intent = new Intent();
1896 // Ensures that only a class in the original IME package will receive the
1897 // notification.
satok42c5a162011-05-26 16:46:14 +09001898 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001899 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1900 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1901 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1902 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001903 final long ident = Binder.clearCallingIdentity();
1904 try {
1905 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1906 } finally {
1907 Binder.restoreCallingIdentity(ident);
1908 }
satokf9f01002011-05-19 21:31:50 +09001909 return true;
1910 }
1911 }
1912 return false;
1913 }
1914
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001915 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001916 updateInputMethodsFromSettingsLocked(enabledMayChange);
1917 updateKeyboardFromSettingsLocked();
1918 }
1919
1920 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001921 if (enabledMayChange) {
1922 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1923 for (int i=0; i<enabled.size(); i++) {
1924 // We allow the user to select "disabled until used" apps, so if they
1925 // are enabling one of those here we now need to make it enabled.
1926 InputMethodInfo imm = enabled.get(i);
1927 try {
1928 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1929 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1930 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001931 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001932 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001933 if (DEBUG) {
1934 Slog.d(TAG, "Update state(" + imm.getId()
1935 + "): DISABLED_UNTIL_USED -> DEFAULT");
1936 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001937 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1938 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001939 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1940 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001941 }
1942 } catch (RemoteException e) {
1943 }
1944 }
1945 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001946 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1947 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1948 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1949 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001950 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001951 // There is no input method selected, try to choose new applicable input method.
1952 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001953 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001954 }
1955 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001957 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001959 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001960 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
satokf3db1af2010-11-23 13:34:33 +09001962 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001963 } else {
1964 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001965 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001967 // Here is not the perfect place to reset the switching controller. Ideally
1968 // mSwitchingController and mSettings should be able to share the same state.
1969 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1970 // the same enabled IMEs list.
1971 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001972
1973 }
1974
1975 public void updateKeyboardFromSettingsLocked() {
1976 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1977 if (mSwitchingDialog != null
1978 && mSwitchingDialogTitleView != null
1979 && mSwitchingDialog.isShowing()) {
1980 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1981 com.android.internal.R.id.hard_keyboard_switch);
1982 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001985
Yohei Yukawab097b822015-12-01 10:43:08 -08001986 private void notifyInputMethodSubtypeChanged(final int userId,
1987 @Nullable final InputMethodInfo inputMethodInfo,
1988 @Nullable final InputMethodSubtype subtype) {
1989 final InputManagerInternal inputManagerInternal =
1990 LocalServices.getService(InputManagerInternal.class);
1991 if (inputManagerInternal != null) {
1992 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1993 }
1994 }
1995
satokab751aa2010-09-14 19:17:36 +09001996 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 InputMethodInfo info = mMethodMap.get(id);
1998 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001999 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002001
satokd81e9502012-05-21 12:58:45 +09002002 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002004 final int subtypeCount = info.getSubtypeCount();
2005 if (subtypeCount <= 0) {
2006 return;
satokcd7cd292010-11-20 15:46:23 +09002007 }
satokd81e9502012-05-21 12:58:45 +09002008 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2009 final InputMethodSubtype newSubtype;
2010 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2011 newSubtype = info.getSubtypeAt(subtypeId);
2012 } else {
2013 // If subtype is null, try to find the most applicable one from
2014 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002015 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09002016 }
2017 if (newSubtype == null || oldSubtype == null) {
2018 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
2019 + ", new subtype = " + newSubtype);
2020 return;
2021 }
2022 if (newSubtype != oldSubtype) {
2023 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
2024 if (mCurMethod != null) {
2025 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002026 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09002027 mCurMethod.changeInputMethodSubtype(newSubtype);
2028 } catch (RemoteException e) {
2029 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08002030 return;
satokab751aa2010-09-14 19:17:36 +09002031 }
2032 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002033 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 return;
2036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002037
satokd81e9502012-05-21 12:58:45 +09002038 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 final long ident = Binder.clearCallingIdentity();
2040 try {
satokab751aa2010-09-14 19:17:36 +09002041 // Set a subtype to this input method.
2042 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002043 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2044 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2045 // because mCurMethodId is stored as a history in
2046 // setSelectedInputMethodAndSubtypeLocked().
2047 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048
2049 if (ActivityManagerNative.isSystemReady()) {
2050 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002051 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002053 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002055 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 } finally {
2057 Binder.restoreCallingIdentity(ident);
2058 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002059
2060 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2061 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002063
satok42c5a162011-05-26 16:46:14 +09002064 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002065 public boolean showSoftInput(IInputMethodClient client, int flags,
2066 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002067 if (!calledFromValidUser()) {
2068 return false;
2069 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002070 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 long ident = Binder.clearCallingIdentity();
2072 try {
2073 synchronized (mMethodMap) {
2074 if (mCurClient == null || client == null
2075 || mCurClient.client.asBinder() != client.asBinder()) {
2076 try {
2077 // We need to check if this is the current client with
2078 // focus in the window manager, to allow this call to
2079 // be made before input is started in it.
2080 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002081 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002082 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002085 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002088
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002089 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002090 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092 } finally {
2093 Binder.restoreCallingIdentity(ident);
2094 }
2095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002096
The Android Open Source Project4df24232009-03-05 14:34:35 -08002097 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002099 if (mAccessibilityRequestingNoSoftKeyboard) {
2100 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
Anna Galusza9b278112016-01-04 11:37:37 -08002102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2104 mShowExplicitlyRequested = true;
2105 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002106 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2107 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002109
Dianne Hackborncc278702009-09-02 23:07:23 -07002110 if (!mSystemReady) {
2111 return false;
2112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002113
The Android Open Source Project4df24232009-03-05 14:34:35 -08002114 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002116 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2118 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2119 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002121 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002122 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002123 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002124 | Context.BIND_TREAT_LIKE_ACTIVITY
2125 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002126 mVisibleBound = true;
2127 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002128 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002130 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 // The client has asked to have the input method shown, but
2132 // we have been sitting here too long with a connection to the
2133 // service and no interface received, so let's disconnect/connect
2134 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002135 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002137 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002139 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002140 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002141 } else {
2142 if (DEBUG) {
2143 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2144 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002147
The Android Open Source Project4df24232009-03-05 14:34:35 -08002148 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002150
satok42c5a162011-05-26 16:46:14 +09002151 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002152 public boolean hideSoftInput(IInputMethodClient client, int flags,
2153 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002154 if (!calledFromValidUser()) {
2155 return false;
2156 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002157 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 long ident = Binder.clearCallingIdentity();
2159 try {
2160 synchronized (mMethodMap) {
2161 if (mCurClient == null || client == null
2162 || mCurClient.client.asBinder() != client.asBinder()) {
2163 try {
2164 // We need to check if this is the current client with
2165 // focus in the window manager, to allow this call to
2166 // be made before input is started in it.
2167 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002168 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2169 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002170 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
2172 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002173 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 }
2175 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002176
Joe Onorato8a9b2202010-02-26 18:56:32 -08002177 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002178 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
2180 } finally {
2181 Binder.restoreCallingIdentity(ident);
2182 }
2183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002184
The Android Open Source Project4df24232009-03-05 14:34:35 -08002185 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2187 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002188 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 -08002189 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
2191 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002192 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 -08002193 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002195
2196 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2197 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2198 // to be updated with the new value sent from IME process. Even in such a transient state
2199 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2200 // application process as a valid request, and have even promised such a behavior with CTS
2201 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2202 // IMMS#InputShown indicates that the software keyboard is shown.
2203 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2204 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2205 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002206 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002207 if (shouldHideSoftInput) {
2208 // The IME will report its visible state again after the following message finally
2209 // delivered to the IME process as an IPC. Hence the inconsistency between
2210 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2211 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002212 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2213 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2214 res = true;
2215 } else {
2216 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002218 if (mHaveConnection && mVisibleBound) {
2219 mContext.unbindService(mVisibleConnection);
2220 mVisibleBound = false;
2221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 mInputShown = false;
2223 mShowRequested = false;
2224 mShowExplicitlyRequested = false;
2225 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002226 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002228
satok42c5a162011-05-26 16:46:14 +09002229 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002230 public InputBindResult startInputOrWindowGainedFocus(
2231 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2232 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa74750f22016-03-22 12:54:22 -07002233 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002234 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002235 if (windowToken != null) {
2236 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002237 softInputMode, windowFlags, attribute, inputContext, missingMethods);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002238 } else {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002239 return startInput(startInputReason, client, inputContext, missingMethods, attribute,
2240 controlFlags);
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002241 }
2242 }
2243
2244 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002245 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2246 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002247 int windowFlags, EditorInfo attribute, IInputContext inputContext,
2248 /* @InputConnectionInspector.missingMethods */ final int missingMethods) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002249 // Needs to check the validity before clearing calling identity
2250 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002251 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 long ident = Binder.clearCallingIdentity();
2253 try {
2254 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002255 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2256 + InputMethodClient.getStartInputReason(startInputReason)
2257 + " client=" + client.asBinder()
2258 + " inputContext=" + inputContext
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002259 + " missingMethods="
2260 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002261 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002262 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002264 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002265
Dianne Hackborn7663d802012-02-24 13:08:49 -08002266 ClientState cs = mClients.get(client.asBinder());
2267 if (cs == null) {
2268 throw new IllegalArgumentException("unknown client "
2269 + client.asBinder());
2270 }
2271
2272 try {
2273 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2274 // Check with the window manager to make sure this client actually
2275 // has a window with focus. If not, reject. This is thread safe
2276 // because if the focus changes some time before or after, the
2277 // next client receiving focus that has any interest in input will
2278 // be calling through here after that change happens.
2279 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2280 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2281 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002283 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002285
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002286 if (!calledFromValidUser) {
2287 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2288 Slog.w(TAG, "If you want to interect with IME, you need "
2289 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2290 hideCurrentInputLocked(0, null);
2291 return null;
2292 }
2293
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002294 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002295 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002296 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002297 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002298 return startInputUncheckedLocked(cs, inputContext, missingMethods,
2299 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002300 }
2301 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002302 }
2303 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002304 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002305
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002306 // Should we auto-show the IME even if the caller has not
2307 // specified what should be done with it?
2308 // We only do this automatically if the window can resize
2309 // to accommodate the IME (so what the user sees will give
2310 // them good context without input information being obscured
2311 // by the IME) or if running on a large screen where there
2312 // is more room for the target window + IME.
2313 final boolean doAutoShow =
2314 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2315 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2316 || mRes.getConfiguration().isLayoutSizeAtLeast(
2317 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002318 final boolean isTextEditor =
2319 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2320
2321 // We want to start input before showing the IME, but after closing
2322 // it. We want to do this after closing it to help the IME disappear
2323 // more quickly (not get stuck behind it initializing itself for the
2324 // new focused input, even if its window wants to hide the IME).
2325 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2328 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002329 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2331 // There is no focus view, and this window will
2332 // be behind any soft input window, so hide the
2333 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002334 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002335 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002337 } else if (isTextEditor && doAutoShow && (softInputMode &
2338 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 // There is a focus view, and we are navigating forward
2340 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002341 // We only do this automatically if the window can resize
2342 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002343 // them good context without input information being obscured
2344 // by the IME) or if running on a large screen where there
2345 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002346 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002347 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002348 res = startInputUncheckedLocked(cs, inputContext,
2349 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002350 didStart = true;
2351 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002352 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354 break;
2355 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2356 // Do nothing.
2357 break;
2358 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2359 if ((softInputMode &
2360 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002361 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002362 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
2364 break;
2365 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002366 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002367 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 break;
2369 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2370 if ((softInputMode &
2371 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002372 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002373 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002374 res = startInputUncheckedLocked(cs, inputContext,
2375 missingMethods, attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002376 didStart = true;
2377 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002378 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380 break;
2381 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002382 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002383 if (attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002384 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
2385 attribute, controlFlags);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002386 didStart = true;
2387 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002388 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 break;
2390 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002391
2392 if (!didStart && attribute != null) {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002393 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Dianne Hackborn7663d802012-02-24 13:08:49 -08002394 controlFlags);
2395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 }
2397 } finally {
2398 Binder.restoreCallingIdentity(ident);
2399 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002400
2401 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002403
satok42c5a162011-05-26 16:46:14 +09002404 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002405 public void showInputMethodPickerFromClient(
2406 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002407 if (!calledFromValidUser()) {
2408 return;
2409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 synchronized (mMethodMap) {
2411 if (mCurClient == null || client == null
2412 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002413 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002414 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 }
2416
satok440aab52010-11-25 09:43:11 +09002417 // Always call subtype picker, because subtype picker is a superset of input method
2418 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002419 mHandler.sendMessage(mCaller.obtainMessageI(
2420 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002421 }
2422 }
2423
satok42c5a162011-05-26 16:46:14 +09002424 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 if (!calledFromValidUser()) {
2427 return;
2428 }
satok28203512010-11-24 11:06:49 +09002429 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2430 }
2431
satok42c5a162011-05-26 16:46:14 +09002432 @Override
satok28203512010-11-24 11:06:49 +09002433 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002434 if (!calledFromValidUser()) {
2435 return;
2436 }
satok28203512010-11-24 11:06:49 +09002437 synchronized (mMethodMap) {
2438 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002439 setInputMethodWithSubtypeIdLocked(token, id,
2440 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2441 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002442 } else {
2443 setInputMethod(token, id);
2444 }
2445 }
satokab751aa2010-09-14 19:17:36 +09002446 }
2447
satok42c5a162011-05-26 16:46:14 +09002448 @Override
satokb416a712010-11-25 20:42:14 +09002449 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002450 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002451 if (!calledFromValidUser()) {
2452 return;
2453 }
satokb416a712010-11-25 20:42:14 +09002454 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002455 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2456 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002457 }
2458 }
2459
satok4fc87d62011-05-20 16:13:43 +09002460 @Override
satok735cf382010-11-11 20:40:09 +09002461 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002462 if (!calledFromValidUser()) {
2463 return false;
2464 }
satok735cf382010-11-11 20:40:09 +09002465 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002466 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002467 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002468 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002469 lastImi = mMethodMap.get(lastIme.first);
2470 } else {
2471 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002472 }
satok4fc87d62011-05-20 16:13:43 +09002473 String targetLastImiId = null;
2474 int subtypeId = NOT_A_SUBTYPE_ID;
2475 if (lastIme != null && lastImi != null) {
2476 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002477 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09002478 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2479 : mCurrentSubtype.hashCode();
2480 // If the last IME is the same as the current IME and the last subtype is not
2481 // defined, there is no need to switch to the last IME.
2482 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2483 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002484 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002485 }
2486 }
2487
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002488 if (TextUtils.isEmpty(targetLastImiId)
2489 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002490 // This is a safety net. If the currentSubtype can't be added to the history
2491 // and the framework couldn't find the last ime, we will make the last ime be
2492 // the most applicable enabled keyboard subtype of the system imes.
2493 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2494 if (enabled != null) {
2495 final int N = enabled.size();
2496 final String locale = mCurrentSubtype == null
2497 ? mRes.getConfiguration().locale.toString()
2498 : mCurrentSubtype.getLocale();
2499 for (int i = 0; i < N; ++i) {
2500 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002501 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002502 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002503 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2504 InputMethodUtils.getSubtypes(imi),
2505 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002506 if (keyboardSubtype != null) {
2507 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002508 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002509 imi, keyboardSubtype.hashCode());
2510 if(keyboardSubtype.getLocale().equals(locale)) {
2511 break;
2512 }
2513 }
2514 }
2515 }
2516 }
2517 }
2518
2519 if (!TextUtils.isEmpty(targetLastImiId)) {
2520 if (DEBUG) {
2521 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2522 + ", from: " + mCurMethodId + ", " + subtypeId);
2523 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002524 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002525 return true;
2526 } else {
2527 return false;
2528 }
satok735cf382010-11-11 20:40:09 +09002529 }
2530 }
2531
satoke7c6998e2011-06-03 17:57:59 +09002532 @Override
satok688bd472012-02-09 20:09:17 +09002533 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002534 if (!calledFromValidUser()) {
2535 return false;
2536 }
satok688bd472012-02-09 20:09:17 +09002537 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002538 if (!calledWithValidToken(token)) {
2539 final int uid = Binder.getCallingUid();
2540 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2541 + " token:" + token);
2542 return false;
2543 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002544 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002545 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2546 true /* forward */);
satok688bd472012-02-09 20:09:17 +09002547 if (nextSubtype == null) {
2548 return false;
2549 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002550 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2551 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002552 return true;
2553 }
2554 }
2555
2556 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002557 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2558 if (!calledFromValidUser()) {
2559 return false;
2560 }
2561 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002562 if (!calledWithValidToken(token)) {
2563 final int uid = Binder.getCallingUid();
2564 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2565 + "token. uid:" + uid + " token:" + token);
2566 return false;
2567 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002568 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002569 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype,
2570 true /* forward */);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002571 if (nextSubtype == null) {
2572 return false;
2573 }
2574 return true;
2575 }
2576 }
2577
2578 @Override
satok68f1b782011-04-11 14:26:04 +09002579 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002580 if (!calledFromValidUser()) {
2581 return null;
2582 }
satok68f1b782011-04-11 14:26:04 +09002583 synchronized (mMethodMap) {
2584 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2585 // TODO: Handle the case of the last IME with no subtypes
2586 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2587 || TextUtils.isEmpty(lastIme.second)) return null;
2588 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2589 if (lastImi == null) return null;
2590 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01002591 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002592 final int lastSubtypeId =
2593 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002594 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2595 return null;
2596 }
2597 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002598 } catch (NumberFormatException e) {
2599 return null;
2600 }
2601 }
2602 }
2603
satoke7c6998e2011-06-03 17:57:59 +09002604 @Override
satokee5e77c2011-09-02 18:50:15 +09002605 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002606 if (!calledFromValidUser()) {
2607 return;
2608 }
satok91e88122011-07-18 11:11:42 +09002609 // By this IPC call, only a process which shares the same uid with the IME can add
2610 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002611 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002612 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002613 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002614 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002615 final String[] packageInfos;
2616 try {
2617 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2618 } catch (RemoteException e) {
2619 Slog.e(TAG, "Failed to get package infos");
2620 return;
2621 }
satok91e88122011-07-18 11:11:42 +09002622 if (packageInfos != null) {
2623 final int packageNum = packageInfos.length;
2624 for (int i = 0; i < packageNum; ++i) {
2625 if (packageInfos[i].equals(imi.getPackageName())) {
2626 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002627 final long ident = Binder.clearCallingIdentity();
2628 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002629 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002630 } finally {
2631 Binder.restoreCallingIdentity(ident);
2632 }
satokee5e77c2011-09-02 18:50:15 +09002633 return;
satok91e88122011-07-18 11:11:42 +09002634 }
2635 }
2636 }
satoke7c6998e2011-06-03 17:57:59 +09002637 }
satokee5e77c2011-09-02 18:50:15 +09002638 return;
satoke7c6998e2011-06-03 17:57:59 +09002639 }
2640
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002641 @Override
2642 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002643 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002644 }
2645
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002646 @Override
Yohei Yukawa833bdce2016-05-15 20:05:56 -07002647 public void clearLastInputMethodWindowForTransition(IBinder token) {
2648 if (!calledFromValidUser()) {
2649 return;
2650 }
2651 final long ident = Binder.clearCallingIdentity();
2652 try {
2653 synchronized (mMethodMap) {
2654 if (!calledWithValidToken(token)) {
2655 final int uid = Binder.getCallingUid();
2656 Slog.e(TAG, "Ignoring clearLastInputMethodWindowForTransition due to an "
2657 + "invalid token. uid:" + uid + " token:" + token);
2658 return;
2659 }
2660 }
2661 mWindowManagerInternal.clearLastInputMethodWindowForTransition();
2662 } finally {
2663 Binder.restoreCallingIdentity(ident);
2664 }
2665 }
2666
2667 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002668 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002669 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002670 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002671 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002672 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002673 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2674 if (DEBUG) {
2675 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2676 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2677 + " actual: " + sequenceNumber);
2678 }
2679 return;
2680 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002681 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2682 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002683 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002684 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002685 }
2686 }
2687
satok28203512010-11-24 11:06:49 +09002688 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002690 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2691 }
2692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002694 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2695 if (token == null) {
2696 if (mContext.checkCallingOrSelfPermission(
2697 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2698 != PackageManager.PERMISSION_GRANTED) {
2699 throw new SecurityException(
2700 "Using null token requires permission "
2701 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002703 } else if (mCurToken != token) {
2704 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2705 + " token: " + token);
2706 return;
2707 }
2708
2709 final long ident = Binder.clearCallingIdentity();
2710 try {
2711 setInputMethodLocked(id, subtypeId);
2712 } finally {
2713 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 }
2715 }
2716
satok42c5a162011-05-26 16:46:14 +09002717 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002719 if (!calledFromValidUser()) {
2720 return;
2721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002723 if (!calledWithValidToken(token)) {
2724 final int uid = Binder.getCallingUid();
2725 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2726 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 return;
2728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 long ident = Binder.clearCallingIdentity();
2730 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002731 hideCurrentInputLocked(flags, null);
2732 } finally {
2733 Binder.restoreCallingIdentity(ident);
2734 }
2735 }
2736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002737
satok42c5a162011-05-26 16:46:14 +09002738 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002739 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002740 if (!calledFromValidUser()) {
2741 return;
2742 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002743 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002744 if (!calledWithValidToken(token)) {
2745 final int uid = Binder.getCallingUid();
2746 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2747 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002748 return;
2749 }
2750 long ident = Binder.clearCallingIdentity();
2751 try {
2752 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 } finally {
2754 Binder.restoreCallingIdentity(ident);
2755 }
2756 }
2757 }
2758
2759 void setEnabledSessionInMainThread(SessionState session) {
2760 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002761 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002763 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002764 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 } catch (RemoteException e) {
2766 }
2767 }
2768 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002769 if (mEnabledSession != null && mEnabledSession.session != null) {
2770 try {
2771 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2772 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2773 } catch (RemoteException e) {
2774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 }
2776 }
2777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002778
satok42c5a162011-05-26 16:46:14 +09002779 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002781 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002783 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002784 final boolean showAuxSubtypes;
2785 switch (msg.arg1) {
2786 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2787 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2788 // implemented so that auxiliary subtypes will be excluded when the soft
2789 // keyboard is invisible.
2790 showAuxSubtypes = mInputShown;
2791 break;
2792 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2793 showAuxSubtypes = true;
2794 break;
2795 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2796 showAuxSubtypes = false;
2797 break;
2798 default:
2799 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2800 return false;
2801 }
2802 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002803 return true;
2804
satok47a44912010-10-06 16:03:58 +09002805 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002806 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002807 return true;
2808
2809 case MSG_SHOW_IM_CONFIG:
2810 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002811 return true;
2812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 case MSG_UNBIND_INPUT:
2816 try {
2817 ((IInputMethod)msg.obj).unbindInput();
2818 } catch (RemoteException e) {
2819 // There is nothing interesting about the method dying.
2820 }
2821 return true;
2822 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002823 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 try {
2825 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2826 } catch (RemoteException e) {
2827 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002828 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 return true;
2830 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002831 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002833 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002834 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002835 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 } catch (RemoteException e) {
2837 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002838 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 return true;
2840 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002841 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002843 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002844 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002845 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 } catch (RemoteException e) {
2847 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002848 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07002850 case MSG_HIDE_CURRENT_INPUT_METHOD:
2851 synchronized (mMethodMap) {
2852 hideCurrentInputLocked(0, null);
2853 }
2854 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002856 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002858 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2860 } catch (RemoteException e) {
2861 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002862 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002864 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002865 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002866 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002867 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002869 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002871 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002872 // Dispose the channel if the input method is not local to this process
2873 // because the remote proxy will get its own copy when unparceled.
2874 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002875 channel.dispose();
2876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002878 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002882
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002883 case MSG_START_INPUT: {
2884 int missingMethods = msg.arg1;
2885 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002887 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002889 session.method.startInput((IInputContext) args.arg2, missingMethods,
2890 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 } catch (RemoteException e) {
2892 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002893 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002895 }
2896 case MSG_RESTART_INPUT: {
2897 int missingMethods = msg.arg1;
2898 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 try {
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002900 SessionState session = (SessionState) args.arg1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 setEnabledSessionInMainThread(session);
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002902 session.method.restartInput((IInputContext) args.arg2, missingMethods,
2903 (EditorInfo) args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 } catch (RemoteException e) {
2905 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002906 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002911
Yohei Yukawa33e81792015-11-17 21:14:42 -08002912 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002914 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 } catch (RemoteException e) {
2916 // There is nothing interesting about the last client dying.
2917 }
2918 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002919 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002920 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002921 IInputMethodClient client = (IInputMethodClient)args.arg1;
2922 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002924 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002926 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002927 } finally {
2928 // Dispose the channel if the input method is not local to this process
2929 // because the remote proxy will get its own copy when unparceled.
2930 if (res.channel != null && Binder.isProxy(client)) {
2931 res.channel.dispose();
2932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002934 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002936 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002937 case MSG_SET_ACTIVE:
2938 try {
2939 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2940 } catch (RemoteException e) {
2941 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2942 + ((ClientState)msg.obj).pid + " uid "
2943 + ((ClientState)msg.obj).uid);
2944 }
2945 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002946 case MSG_SET_INTERACTIVE:
2947 handleSetInteractive(msg.arg1 != 0);
2948 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002949 case MSG_SWITCH_IME:
2950 handleSwitchInputMethod(msg.arg1 != 0);
2951 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002952 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2953 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002954 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002955 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002956 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002957 } catch (RemoteException e) {
2958 Slog.w(TAG, "Got RemoteException sending "
2959 + "setUserActionNotificationSequenceNumber("
2960 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002961 + clientState.pid + " uid "
2962 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002963 }
2964 return true;
2965 }
satok01038492012-04-09 21:08:27 +09002966
2967 // --------------------------------------------------------------
2968 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002969 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002970 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 }
2972 return false;
2973 }
2974
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002975 private void handleSetInteractive(final boolean interactive) {
2976 synchronized (mMethodMap) {
2977 mIsInteractive = interactive;
2978 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2979
2980 // Inform the current client of the change in active status
2981 if (mCurClient != null && mCurClient.client != null) {
2982 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2983 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2984 }
2985 }
2986 }
2987
Yohei Yukawaae61f712015-12-09 13:00:10 -08002988 private void handleSwitchInputMethod(final boolean forwardDirection) {
2989 synchronized (mMethodMap) {
Yohei Yukawaae61f712015-12-09 13:00:10 -08002990 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawad39ae852016-04-10 20:28:40 -07002991 false, mMethodMap.get(mCurMethodId), mCurrentSubtype, forwardDirection);
Yohei Yukawaae61f712015-12-09 13:00:10 -08002992 if (nextSubtype == null) {
2993 return;
2994 }
2995 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07002996 final InputMethodInfo newInputMethodInfo = mMethodMap.get(mCurMethodId);
2997 if (newInputMethodInfo == null) {
2998 return;
2999 }
3000 final CharSequence toastText = InputMethodUtils.getImeAndSubtypeDisplayName(mContext,
3001 newInputMethodInfo, mCurrentSubtype);
3002 if (!TextUtils.isEmpty(toastText)) {
Yohei Yukawab2f901a2016-04-12 01:19:31 -07003003 if (mSubtypeSwitchedByShortCutToast == null) {
3004 mSubtypeSwitchedByShortCutToast = Toast.makeText(mContext, toastText,
3005 Toast.LENGTH_SHORT);
3006 } else {
3007 mSubtypeSwitchedByShortCutToast.setText(toastText);
3008 }
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003009 mSubtypeSwitchedByShortCutToast.show();
3010 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003011 }
3012 }
3013
satokdc9ddae2011-10-06 12:22:36 +09003014 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003015 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
3016 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09003017 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09003018 if (DEBUG) {
3019 Slog.d(TAG, "New default IME was selected: " + imi.getId());
3020 }
satok723a27e2010-11-11 14:58:11 +09003021 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003022 return true;
3023 }
3024
3025 return false;
3026 }
3027
Yohei Yukawa94e33302016-02-12 19:37:03 -08003028 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003029 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003030 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07003031 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003032 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08003033 mMethodList.clear();
3034 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003035
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003036 // Use for queryIntentServicesAsUser
3037 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038
Yohei Yukawaed4952a2016-02-17 07:57:25 -08003039 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
3040 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
3041 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003042 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003044 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
3045 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003046
satoke7c6998e2011-06-03 17:57:59 +09003047 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
3048 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 for (int i = 0; i < services.size(); ++i) {
3050 ResolveInfo ri = services.get(i);
3051 ServiceInfo si = ri.serviceInfo;
3052 ComponentName compName = new ComponentName(si.packageName, si.name);
3053 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
3054 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003055 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 + ": it does not require the permission "
3057 + android.Manifest.permission.BIND_INPUT_METHOD);
3058 continue;
3059 }
3060
Joe Onorato8a9b2202010-02-26 18:56:32 -08003061 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062
3063 try {
satoke7c6998e2011-06-03 17:57:59 +09003064 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08003065 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07003066 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08003067 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068
3069 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003070 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003072 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003073 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 }
3075 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003076
Yohei Yukawa859df052016-02-17 07:56:46 -08003077 // TODO: The following code should find better place to live.
3078 if (!resetDefaultEnabledIme) {
3079 boolean enabledImeFound = false;
3080 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
3081 final int N = enabledImes.size();
3082 for (int i = 0; i < N; ++i) {
3083 final InputMethodInfo imi = enabledImes.get(i);
3084 if (mMethodList.contains(imi)) {
3085 enabledImeFound = true;
3086 break;
3087 }
3088 }
3089 if (!enabledImeFound) {
3090 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3091 resetDefaultEnabledIme = true;
3092 resetSelectedInputMethodAndSubtypeLocked("");
3093 }
3094 }
3095
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003096 if (resetDefaultEnabledIme) {
3097 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003098 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003099 final int N = defaultEnabledIme.size();
3100 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003101 final InputMethodInfo imi = defaultEnabledIme.get(i);
3102 if (DEBUG) {
3103 Slog.d(TAG, "--- enable ime = " + imi);
3104 }
3105 setInputMethodEnabledLocked(imi.getId(), true);
3106 }
3107 }
3108
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003109 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003110 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003111 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003112 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3113 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003114 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003115 }
3116 } else {
3117 // Double check that the default IME is certainly enabled.
3118 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003119 }
3120 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003121 // Here is not the perfect place to reset the switching controller. Ideally
3122 // mSwitchingController and mSettings should be able to share the same state.
3123 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3124 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003125 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003129
satok217f5482010-12-15 05:19:19 +09003130 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003131 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003132 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003133 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3134 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003135 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003136 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003137 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003138 final int userId;
3139 synchronized (mMethodMap) {
3140 userId = mSettings.getCurrentUserId();
3141 }
3142 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003143 }
3144
3145 private void showConfigureInputMethods() {
3146 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3147 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3148 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3149 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003150 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003151 }
3152
satok2c93efc2012-04-02 19:33:47 +09003153 private boolean isScreenLocked() {
3154 return mKeyguardManager != null
3155 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3156 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003157
Seigo Nonaka14e13912015-05-06 21:04:13 -07003158 private void showInputMethodMenu(boolean showAuxSubtypes) {
3159 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003162 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003163
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003164 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003165 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003166 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003167
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003168 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003169 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003170 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3171 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003172 if (immis == null || immis.size() == 0) {
3173 return;
3174 }
3175
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003176 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177
satok688bd472012-02-09 20:09:17 +09003178 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003179 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003180 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003181
satokc3690562012-01-10 20:14:43 +09003182 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003183 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003184 if (currentSubtype != null) {
3185 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003186 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3187 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003188 }
3189 }
3190
Ken Wakasa761eb372011-03-04 19:06:18 +09003191 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003192 mIms = new InputMethodInfo[N];
3193 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003194 int checkedItem = 0;
3195 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003196 final ImeSubtypeListItem item = imList.get(i);
3197 mIms[i] = item.mImi;
3198 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003199 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003200 int subtypeId = mSubtypeIds[i];
3201 if ((subtypeId == NOT_A_SUBTYPE_ID)
3202 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3203 || (subtypeId == lastInputMethodSubtypeId)) {
3204 checkedItem = i;
3205 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003208
3209 final Context settingsContext = new ContextThemeWrapper(context,
3210 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3211
3212 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003213 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3214 @Override
3215 public void onCancel(DialogInterface dialog) {
3216 hideInputMethodMenu();
3217 }
3218 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003219
3220 final Context dialogContext = mDialogBuilder.getContext();
3221 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3222 com.android.internal.R.styleable.DialogPreference,
3223 com.android.internal.R.attr.alertDialogStyle, 0);
3224 final Drawable dialogIcon = a.getDrawable(
3225 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3226 a.recycle();
3227
3228 mDialogBuilder.setIcon(dialogIcon);
3229
Yohei Yukawad34e1482016-02-11 08:03:52 -08003230 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003231 final View tv = inflater.inflate(
3232 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3233 mDialogBuilder.setCustomTitle(tv);
3234
3235 // Setup layout for a toggle switch of the hardware keyboard
3236 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003237 mSwitchingDialogTitleView
3238 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003239 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003240 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003241 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003242 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003243 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003244 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3245 @Override
3246 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003247 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003248 // Ensure that the input method dialog is dismissed when changing
3249 // the hardware keyboard state.
3250 hideInputMethodMenu();
3251 }
3252 });
3253
Alan Viverette505e3ab2014-11-24 15:22:11 -08003254 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003255 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3256 final OnClickListener choiceListener = new OnClickListener() {
3257 @Override
3258 public void onClick(final DialogInterface dialog, final int which) {
3259 synchronized (mMethodMap) {
3260 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3261 || mSubtypeIds.length <= which) {
3262 return;
satok01038492012-04-09 21:08:27 +09003263 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003264 final InputMethodInfo im = mIms[which];
3265 int subtypeId = mSubtypeIds[which];
3266 adapter.mCheckedItem = which;
3267 adapter.notifyDataSetChanged();
3268 hideInputMethodMenu();
3269 if (im != null) {
3270 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3271 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003272 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003273 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003275 }
3276 }
3277 };
3278 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003281 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 mSwitchingDialog.getWindow().setType(
3283 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003284 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3285 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003286 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003287 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 mSwitchingDialog.show();
3289 }
3290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003291
Ken Wakasa05dbb652011-08-22 15:22:43 +09003292 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3293 private final LayoutInflater mInflater;
3294 private final int mTextViewResourceId;
3295 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003296 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003297 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3298 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3299 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003300
Ken Wakasa05dbb652011-08-22 15:22:43 +09003301 mTextViewResourceId = textViewResourceId;
3302 mItemsList = itemsList;
3303 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003304 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003305 }
3306
3307 @Override
3308 public View getView(int position, View convertView, ViewGroup parent) {
3309 final View view = convertView != null ? convertView
3310 : mInflater.inflate(mTextViewResourceId, null);
3311 if (position < 0 || position >= mItemsList.size()) return view;
3312 final ImeSubtypeListItem item = mItemsList.get(position);
3313 final CharSequence imeName = item.mImeName;
3314 final CharSequence subtypeName = item.mSubtypeName;
3315 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3316 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3317 if (TextUtils.isEmpty(subtypeName)) {
3318 firstTextView.setText(imeName);
3319 secondTextView.setVisibility(View.GONE);
3320 } else {
3321 firstTextView.setText(subtypeName);
3322 secondTextView.setText(imeName);
3323 secondTextView.setVisibility(View.VISIBLE);
3324 }
3325 final RadioButton radioButton =
3326 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3327 radioButton.setChecked(position == mCheckedItem);
3328 return view;
3329 }
3330 }
3331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003333 synchronized (mMethodMap) {
3334 hideInputMethodMenuLocked();
3335 }
3336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003337
The Android Open Source Project10592532009-03-18 17:39:46 -07003338 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003339 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340
The Android Open Source Project10592532009-03-18 17:39:46 -07003341 if (mSwitchingDialog != null) {
3342 mSwitchingDialog.dismiss();
3343 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003345
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003346 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003347 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003348 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003352
satok42c5a162011-05-26 16:46:14 +09003353 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003354 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003355 // TODO: Make this work even for non-current users?
3356 if (!calledFromValidUser()) {
3357 return false;
3358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 synchronized (mMethodMap) {
3360 if (mContext.checkCallingOrSelfPermission(
3361 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3362 != PackageManager.PERMISSION_GRANTED) {
3363 throw new SecurityException(
3364 "Requires permission "
3365 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3366 }
Anna Galusza9b278112016-01-04 11:37:37 -08003367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 long ident = Binder.clearCallingIdentity();
3369 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003370 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 } finally {
3372 Binder.restoreCallingIdentity(ident);
3373 }
3374 }
3375 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003376
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003377 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3378 // Make sure this is a valid input method.
3379 InputMethodInfo imm = mMethodMap.get(id);
3380 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003381 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003382 }
3383
satokd87c2592010-09-29 11:52:06 +09003384 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3385 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003386
satokd87c2592010-09-29 11:52:06 +09003387 if (enabled) {
3388 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3389 if (pair.first.equals(id)) {
3390 // We are enabling this input method, but it is already enabled.
3391 // Nothing to do. The previous state was enabled.
3392 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003393 }
3394 }
satokd87c2592010-09-29 11:52:06 +09003395 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3396 // Previous state was disabled.
3397 return false;
3398 } else {
3399 StringBuilder builder = new StringBuilder();
3400 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3401 builder, enabledInputMethodsList, id)) {
3402 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003403 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003404 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3405 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3406 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003407 }
3408 // Previous state was enabled.
3409 return true;
3410 } else {
3411 // We are disabling the input method but it is already disabled.
3412 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003413 return false;
3414 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003415 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003416 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003417
satok723a27e2010-11-11 14:58:11 +09003418 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3419 boolean setSubtypeOnly) {
3420 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003421 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003422
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003423 mCurUserActionNotificationSequenceNumber =
3424 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3425 if (DEBUG) {
3426 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3427 + mCurUserActionNotificationSequenceNumber);
3428 }
3429
3430 if (mCurClient != null && mCurClient.client != null) {
3431 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3432 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003433 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003434 }
3435
satok723a27e2010-11-11 14:58:11 +09003436 // Set Subtype here
3437 if (imi == null || subtypeId < 0) {
3438 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003439 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003440 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003441 if (subtypeId < imi.getSubtypeCount()) {
3442 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3443 mSettings.putSelectedSubtype(subtype.hashCode());
3444 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003445 } else {
3446 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003447 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003448 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003449 }
satokab751aa2010-09-14 19:17:36 +09003450 }
satok723a27e2010-11-11 14:58:11 +09003451
Yohei Yukawa68645a62016-02-17 07:54:20 -08003452 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003453 // Set InputMethod here
3454 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3455 }
3456 }
3457
3458 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3459 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3460 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3461 // newDefaultIme is empty when there is no candidate for the selected IME.
3462 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3463 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3464 if (subtypeHashCode != null) {
3465 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003466 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003467 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09003468 } catch (NumberFormatException e) {
3469 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3470 }
3471 }
3472 }
3473 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003474 }
3475
satok4e4569d2010-11-19 18:45:53 +09003476 // If there are no selected shortcuts, tries finding the most applicable ones.
3477 private Pair<InputMethodInfo, InputMethodSubtype>
3478 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3479 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3480 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003481 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003482 boolean foundInSystemIME = false;
3483
3484 // Search applicable subtype for each InputMethodInfo
3485 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003486 final String imiId = imi.getId();
3487 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3488 continue;
3489 }
satokcd7cd292010-11-20 15:46:23 +09003490 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003491 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003492 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003493 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003494 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003495 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003496 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003497 }
satokdf31ae62011-01-15 06:19:44 +09003498 // 2. Search by the system locale from enabledSubtypes.
3499 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003500 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003501 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003502 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003503 }
satoka86f5e42011-09-02 17:12:42 +09003504 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003505 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003506 final ArrayList<InputMethodSubtype> subtypesForSearch =
3507 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003508 ? InputMethodUtils.getSubtypes(imi)
3509 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003510 // 4. Search by the current subtype's locale from all subtypes.
3511 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003512 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003513 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003514 }
3515 // 5. Search by the system locale from all subtypes.
3516 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003517 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003518 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003519 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003520 }
satokcd7cd292010-11-20 15:46:23 +09003521 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003522 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003523 // The current input method is the most applicable IME.
3524 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003525 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003526 break;
satok7599a7f2010-12-22 13:45:23 +09003527 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003528 // The system input method is 2nd applicable IME.
3529 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003530 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003531 if ((imi.getServiceInfo().applicationInfo.flags
3532 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3533 foundInSystemIME = true;
3534 }
satok4e4569d2010-11-19 18:45:53 +09003535 }
3536 }
3537 }
3538 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003539 if (mostApplicableIMI != null) {
3540 Slog.w(TAG, "Most applicable shortcut input method was:"
3541 + mostApplicableIMI.getId());
3542 if (mostApplicableSubtype != null) {
3543 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3544 + "," + mostApplicableSubtype.getMode() + ","
3545 + mostApplicableSubtype.getLocale());
3546 }
3547 }
satok4e4569d2010-11-19 18:45:53 +09003548 }
satokcd7cd292010-11-20 15:46:23 +09003549 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003550 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003551 } else {
3552 return null;
3553 }
3554 }
3555
satokab751aa2010-09-14 19:17:36 +09003556 /**
3557 * @return Return the current subtype of this input method.
3558 */
satok42c5a162011-05-26 16:46:14 +09003559 @Override
satokab751aa2010-09-14 19:17:36 +09003560 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003561 // TODO: Make this work even for non-current users?
3562 if (!calledFromValidUser()) {
3563 return null;
3564 }
3565 synchronized (mMethodMap) {
3566 return getCurrentInputMethodSubtypeLocked();
3567 }
3568 }
3569
3570 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003571 if (mCurMethodId == null) {
3572 return null;
3573 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003574 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003575 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3576 if (imi == null || imi.getSubtypeCount() == 0) {
3577 return null;
satok4e4569d2010-11-19 18:45:53 +09003578 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003579 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003580 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3581 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003582 if (subtypeId == NOT_A_SUBTYPE_ID) {
3583 // If there are no selected subtypes, the framework will try to find
3584 // the most applicable subtype from explicitly or implicitly enabled
3585 // subtypes.
3586 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003587 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003588 // If there is only one explicitly or implicitly enabled subtype,
3589 // just returns it.
3590 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3591 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3592 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003593 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003594 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003595 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003596 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003597 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003598 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3599 true);
satok4e4569d2010-11-19 18:45:53 +09003600 }
satok3ef8b292010-11-23 06:06:29 +09003601 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003602 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003603 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003604 }
3605 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003606 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003607 }
3608
satok4e4569d2010-11-19 18:45:53 +09003609 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003610 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003611 @Override
satok4e4569d2010-11-19 18:45:53 +09003612 public List getShortcutInputMethodsAndSubtypes() {
3613 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003614 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003615 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003616 // If there are no selected shortcut subtypes, the framework will try to find
3617 // the most applicable subtype from all subtypes whose mode is
3618 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003619 Pair<InputMethodInfo, InputMethodSubtype> info =
3620 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003621 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003622 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003623 ret.add(info.first);
3624 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003625 }
satok3da92232011-01-11 22:46:30 +09003626 return ret;
satokf3db1af2010-11-23 13:34:33 +09003627 }
satokf3db1af2010-11-23 13:34:33 +09003628 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3629 ret.add(imi);
3630 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3631 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003632 }
3633 }
satokf3db1af2010-11-23 13:34:33 +09003634 return ret;
satok4e4569d2010-11-19 18:45:53 +09003635 }
3636 }
3637
satok42c5a162011-05-26 16:46:14 +09003638 @Override
satokb66d2872010-11-10 01:04:04 +09003639 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003640 // TODO: Make this work even for non-current users?
3641 if (!calledFromValidUser()) {
3642 return false;
3643 }
satokb66d2872010-11-10 01:04:04 +09003644 synchronized (mMethodMap) {
3645 if (subtype != null && mCurMethodId != null) {
3646 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003647 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003648 if (subtypeId != NOT_A_SUBTYPE_ID) {
3649 setInputMethodLocked(mCurMethodId, subtypeId);
3650 return true;
3651 }
3652 }
3653 return false;
3654 }
3655 }
3656
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003657 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003658 private static class InputMethodFileManager {
3659 private static final String SYSTEM_PATH = "system";
3660 private static final String INPUT_METHOD_PATH = "inputmethod";
3661 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3662 private static final String NODE_SUBTYPES = "subtypes";
3663 private static final String NODE_SUBTYPE = "subtype";
3664 private static final String NODE_IMI = "imi";
3665 private static final String ATTR_ID = "id";
3666 private static final String ATTR_LABEL = "label";
3667 private static final String ATTR_ICON = "icon";
Yohei Yukawa66baf692016-04-11 02:29:35 -07003668 private static final String ATTR_IME_SUBTYPE_ID = "subtypeId";
satoke7c6998e2011-06-03 17:57:59 +09003669 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003670 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003671 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3672 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3673 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003674 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003675 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3676 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003677 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003678 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003679 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003680 if (methodMap == null) {
3681 throw new NullPointerException("methodMap is null");
3682 }
3683 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003684 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003685 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3686 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003687 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003688 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003689 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3690 }
3691 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3692 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3693 if (!subtypeFile.exists()) {
3694 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003695 writeAdditionalInputMethodSubtypes(
3696 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003697 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003698 readAdditionalInputMethodSubtypes(
3699 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003700 }
3701 }
3702
3703 private void deleteAllInputMethodSubtypes(String imiId) {
3704 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003705 mAdditionalSubtypesMap.remove(imiId);
3706 writeAdditionalInputMethodSubtypes(
3707 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003708 }
3709 }
3710
3711 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003712 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003713 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003714 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003715 final int N = additionalSubtypes.length;
3716 for (int i = 0; i < N; ++i) {
3717 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003718 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003719 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003720 } else {
3721 Slog.w(TAG, "Duplicated subtype definition found: "
3722 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003723 }
3724 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003725 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3726 writeAdditionalInputMethodSubtypes(
3727 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003728 }
3729 }
3730
3731 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3732 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003733 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003734 }
3735 }
3736
3737 private static void writeAdditionalInputMethodSubtypes(
3738 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3739 HashMap<String, InputMethodInfo> methodMap) {
3740 // Safety net for the case that this function is called before methodMap is set.
3741 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3742 FileOutputStream fos = null;
3743 try {
3744 fos = subtypesFile.startWrite();
3745 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003746 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003747 out.startDocument(null, true);
3748 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3749 out.startTag(null, NODE_SUBTYPES);
3750 for (String imiId : allSubtypes.keySet()) {
3751 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3752 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3753 continue;
3754 }
3755 out.startTag(null, NODE_IMI);
3756 out.attribute(null, ATTR_ID, imiId);
3757 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3758 final int N = subtypesList.size();
3759 for (int i = 0; i < N; ++i) {
3760 final InputMethodSubtype subtype = subtypesList.get(i);
3761 out.startTag(null, NODE_SUBTYPE);
Yohei Yukawa66baf692016-04-11 02:29:35 -07003762 if (subtype.hasSubtypeId()) {
3763 out.attribute(null, ATTR_IME_SUBTYPE_ID,
3764 String.valueOf(subtype.getSubtypeId()));
3765 }
satoke7c6998e2011-06-03 17:57:59 +09003766 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3767 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3768 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003769 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3770 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003771 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3772 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3773 out.attribute(null, ATTR_IS_AUXILIARY,
3774 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003775 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3776 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003777 out.endTag(null, NODE_SUBTYPE);
3778 }
3779 out.endTag(null, NODE_IMI);
3780 }
3781 out.endTag(null, NODE_SUBTYPES);
3782 out.endDocument();
3783 subtypesFile.finishWrite(fos);
3784 } catch (java.io.IOException e) {
3785 Slog.w(TAG, "Error writing subtypes", e);
3786 if (fos != null) {
3787 subtypesFile.failWrite(fos);
3788 }
3789 }
3790 }
3791
3792 private static void readAdditionalInputMethodSubtypes(
3793 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3794 if (allSubtypes == null || subtypesFile == null) return;
3795 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003796 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003797 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003798 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003799 int type = parser.getEventType();
3800 // Skip parsing until START_TAG
3801 while ((type = parser.next()) != XmlPullParser.START_TAG
3802 && type != XmlPullParser.END_DOCUMENT) {}
3803 String firstNodeName = parser.getName();
3804 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3805 throw new XmlPullParserException("Xml doesn't start with subtypes");
3806 }
3807 final int depth =parser.getDepth();
3808 String currentImiId = null;
3809 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3810 while (((type = parser.next()) != XmlPullParser.END_TAG
3811 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3812 if (type != XmlPullParser.START_TAG)
3813 continue;
3814 final String nodeName = parser.getName();
3815 if (NODE_IMI.equals(nodeName)) {
3816 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3817 if (TextUtils.isEmpty(currentImiId)) {
3818 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3819 continue;
3820 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003821 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003822 allSubtypes.put(currentImiId, tempSubtypesArray);
3823 } else if (NODE_SUBTYPE.equals(nodeName)) {
3824 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3825 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3826 continue;
3827 }
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003828 final int icon = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003829 parser.getAttributeValue(null, ATTR_ICON));
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003830 final int label = Integer.parseInt(
satoke7c6998e2011-06-03 17:57:59 +09003831 parser.getAttributeValue(null, ATTR_LABEL));
3832 final String imeSubtypeLocale =
3833 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003834 final String languageTag =
3835 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003836 final String imeSubtypeMode =
3837 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3838 final String imeSubtypeExtraValue =
3839 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003840 final boolean isAuxiliary = "1".equals(String.valueOf(
3841 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003842 final boolean isAsciiCapable = "1".equals(String.valueOf(
3843 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003844 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder()
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003845 .setSubtypeNameResId(label)
3846 .setSubtypeIconResId(icon)
3847 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003848 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003849 .setSubtypeMode(imeSubtypeMode)
3850 .setSubtypeExtraValue(imeSubtypeExtraValue)
3851 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa66baf692016-04-11 02:29:35 -07003852 .setIsAsciiCapable(isAsciiCapable);
3853 final String subtypeIdString =
3854 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_ID);
3855 if (subtypeIdString != null) {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003856 builder.setSubtypeId(Integer.parseInt(subtypeIdString));
Yohei Yukawa66baf692016-04-11 02:29:35 -07003857 }
3858 tempSubtypesArray.add(builder.build());
satoke7c6998e2011-06-03 17:57:59 +09003859 }
3860 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003861 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3862 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003863 return;
satoke7c6998e2011-06-03 17:57:59 +09003864 }
3865 }
3866 }
3867
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003868 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3869 @NonNull
3870 private final Handler mHandler;
3871
3872 LocalServiceImpl(@NonNull final Handler handler) {
3873 mHandler = handler;
3874 }
3875
3876 @Override
3877 public void setInteractive(boolean interactive) {
3878 // Do everything in handler so as not to block the caller.
3879 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3880 interactive ? 1 : 0, 0));
3881 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003882
3883 @Override
3884 public void switchInputMethod(boolean forwardDirection) {
3885 // Do everything in handler so as not to block the caller.
3886 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3887 forwardDirection ? 1 : 0, 0));
3888 }
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07003889
3890 @Override
3891 public void hideCurrentInputMethod() {
3892 mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
3893 mHandler.sendEmptyMessage(MSG_HIDE_CURRENT_INPUT_METHOD);
3894 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003895 }
3896
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003897 private static String imeWindowStatusToString(final int imeWindowVis) {
3898 final StringBuilder sb = new StringBuilder();
3899 boolean first = true;
3900 if ((imeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
3901 sb.append("Active");
3902 first = false;
3903 }
3904 if ((imeWindowVis & InputMethodService.IME_VISIBLE) != 0) {
3905 if (!first) {
3906 sb.append("|");
3907 }
3908 sb.append("Visible");
3909 }
3910 return sb.toString();
3911 }
3912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 @Override
3914 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3915 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3916 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3919 + Binder.getCallingPid()
3920 + ", uid=" + Binder.getCallingUid());
3921 return;
3922 }
3923
3924 IInputMethod method;
3925 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003926 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 synchronized (mMethodMap) {
3931 p.println("Current Input Method Manager state:");
3932 int N = mMethodList.size();
3933 p.println(" Input Methods:");
3934 for (int i=0; i<N; i++) {
3935 InputMethodInfo info = mMethodList.get(i);
3936 p.println(" InputMethod #" + i + ":");
3937 info.dump(p, " ");
3938 }
3939 p.println(" Clients:");
3940 for (ClientState ci : mClients.values()) {
3941 p.println(" Client " + ci + ":");
3942 p.println(" client=" + ci.client);
3943 p.println(" inputContext=" + ci.inputContext);
3944 p.println(" sessionRequested=" + ci.sessionRequested);
3945 p.println(" curSession=" + ci.curSession);
3946 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003947 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003949 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3950 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003951 focusedWindowClient = mCurFocusedWindowClient;
3952 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3954 + " mBoundToMethod=" + mBoundToMethod);
3955 p.println(" mCurToken=" + mCurToken);
3956 p.println(" mCurIntent=" + mCurIntent);
3957 method = mCurMethod;
3958 p.println(" mCurMethod=" + mCurMethod);
3959 p.println(" mEnabledSession=" + mEnabledSession);
Yohei Yukawaebda7d72016-04-02 17:39:23 -07003960 p.println(" mImeWindowVis=" + imeWindowStatusToString(mImeWindowVis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 p.println(" mShowRequested=" + mShowRequested
3962 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3963 + " mShowForced=" + mShowForced
3964 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003965 p.println(" mCurUserActionNotificationSequenceNumber="
3966 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003967 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003968 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003969 p.println(" mSwitchingController:");
3970 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003971 p.println(" mSettings:");
3972 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003974
Jeff Brownb88102f2010-09-08 11:49:43 -07003975 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003977 pw.flush();
3978 try {
3979 client.client.asBinder().dump(fd, args);
3980 } catch (RemoteException e) {
3981 p.println("Input method client dead: " + e);
3982 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003983 } else {
3984 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003986
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003987 if (focusedWindowClient != null && client != focusedWindowClient) {
3988 p.println(" ");
3989 p.println("Warning: Current input method client doesn't match the last focused. "
3990 + "window.");
3991 p.println("Dumping input method client in the last focused window just in case.");
3992 p.println(" ");
3993 pw.flush();
3994 try {
3995 focusedWindowClient.client.asBinder().dump(fd, args);
3996 } catch (RemoteException e) {
3997 p.println("Input method client in focused window dead: " + e);
3998 }
3999 }
4000
Jeff Brownb88102f2010-09-08 11:49:43 -07004001 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 pw.flush();
4004 try {
4005 method.asBinder().dump(fd, args);
4006 } catch (RemoteException e) {
4007 p.println("Input method service dead: " + e);
4008 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004009 } else {
4010 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 }
4012 }
4013}